程式題目
Supposedly, there is a file “d:/cs/score.dat” storing 4 student records listed below. The first column is “Student ID”; the second column is “Name” the third column is “Score”. Write a C program to find the highest score and print out his/her record. (from ccu Bo)
101 John
80
102 Bo 98
103 Mary 76
104 Bruce 87
|
程式範例1
//Parallel array #include <stdio.h> #include <stdlib.h> int main() { FILE *f0; int sid[4]; //The parallel array skill is directly setting a set of array variables which have equal status each other char *name[4]; //see ibid int score[4]; //see ibid int i = 0; int max_index; int max_score; f0 = fopen ("d:/cs/score.dat", "r"); for (i=0; i<4; i++) { name[i]=(char *)calloc(10,sizeof(char)); fscanf(f0, "%d %s %d", &sid[i], name[i], &score[i]); } for (i=0; i<4; i++) { printf("%8d %8s %8d \n", sid[i], name[i], score[i]); } fclose(f0); max_score = score[0]; max_index = 0; for (i=1; i<4; i++) { if (max_score < score[i]) { max_index = i; max_score = score[i]; } } printf("\n\nthe highest score is : "); printf("%8d %8s %8d \n", sid[max_index], name[max_index], score[max_index]); return 0; }
程式範例2
//struct of array #include <stdio.h> #include <stdlib.h> typedef struct record //using "structure" skill to write data storage room { int sid; char *name; int score; } R; int main() { FILE *f0; int i = 0; int max_index; int max_score; R a[4]; //data type is structure written by myself and setting the variable is "array" f0 = fopen ("d:/cs/score.dat", "r"); for (i=0; i<4; i++) { a[i].name=(char *)calloc(10,sizeof(char)); fscanf(f0, "%d %s %d", &a[i].sid, a[i].name, &a[i].score); } for (i=0; i<4; i++) { printf("%8d %8s %8d \n", a[i].sid, a[i].name, a[i].score); } fclose(f0); max_score = a[0].score; max_index = 0; for (i=1; i<4; i++) { if (max_score < a[i].score) { max_index = i; max_score = a[i].score; } } printf("\n\nthe highest score is : "); printf("%8d %8s %8d \n", a[max_index].sid, a[max_index].name, a[max_index].score); return 0; }
程式範例3
//Use functions defined by myself #include <stdio.h> #include <stdlib.h> typedef struct record //using "structure" skill to write data storage room { int sid; char *name; int score; } R; R a[4]; //use to record four students' data void readfile (FILE *f) { int i; for (i=0; i<4; i++) { a[i].name=(char *)calloc(10,sizeof(char)); fscanf(f, "%d %s %d", &a[i].sid, a[i].name, &a[i].score); //record four students' data } } void printdata() { int i; for (i=0; i<4; i++) { printf("%8d %8s %8d \n", a[i].sid, a[i].name, a[i].score); //print four students' data } } R getmax() //subfunction returns its output outcome by R data type { int i; int max_index; int max_score; max_score = a[0].score; max_index = 0; for (i=1; i<4; i++) { if (max_score < a[i].score) { max_index = i; max_score = a[i].score; } } return (a[max_index]); } int main() { FILE *f0; f0 = fopen ("d:/cs/score.dat", "r"); readfile(f0); fclose(f0); printdata(); R max; //storing the subfunction getmax() output value is used by declaring R data type's variable max = getmax(); //using the left value to store the right value's return value printf("\n\nthe highest score is : "); printf("%8d %8s %8d \n",max.sid, max.name,max.score); return 0; }
程式範例4
使用ADT(抽象資料型別, Abstract Data Type)的技巧來撰寫本程式
<寫法一>
/*the filename: main *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "Re.h" int main() { R b; ReadStudentRecord(); PrintStudentRecord(); b = GetHighestRecord(); printf("\n\n the highest score is : \n"); printf("%8d %8s %8d \n", b.id, b.name, b.score); return 0; }
/*the filename: Re *the filename extension: .h */ #ifndef RE_H_INCLUDED #define RE_H_INCLUDED typedef struct record { int id; char *name; int score; }R; R a[4]; FILE *f0; void ReadStudentRecord(){ int i; f0 = fopen ("d:/cs/score.dat", "r"); for (i=0; i<4; i++){ a[i].name = (char *) calloc(10, sizeof(char)); fscanf(f0, "%d %s %d", &a[i].id, a[i].name, &a[i].score); } fclose(f0); } void PrintStudentRecord(){ int i; for (i=0; i<4; i++){ printf("%8d %8s %8d \n", a[i].id, a[i].name, a[i].score); } } R GetHighestRecord(){ int max_index; int max_score; int i; max_score = a[0].score; max_index = 0; for (i=1; i<4; i++){ if (max_score < a[i].score){ max_index = i; max_score = a[i].score; } } return (a[max_index]); } #endif // RE_H_INCLUDED
<寫法二>
/*the filename: main *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "Re.h" int main() { R b; ReadStudentRecord(); PrintStudentRecord(); b = GetHighestRecord(); printf("\n\n the highest score is : \n"); printf("%8d %8s %8d \n", b.id, b.name, b.score); return 0; }
/*the filename: Re *the filename extension: .h */ #ifndef RE_H_INCLUDED #define RE_H_INCLUDED typedef struct record { int id; char *name; int score; }R; void ReadStudentRecord(); void PrintStudentRecord(); R GetHighestRecord(); #endif // RE_H_INCLUDED
/*the filename: define_and_read_functions *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "Re.h" R a[4]; FILE *f0; int i; void ReadStudentRecord(){ f0 = fopen ("d:/cs/score.dat", "r"); for (i=0; i<4; i++){ a[i].name = (char *) calloc(10, sizeof(char)); fscanf(f0, "%d %s %d", &a[i].id, a[i].name, &a[i].score); } fclose(f0); } void PrintStudentRecord(){ for (i=0; i<4; i++){ printf("%8d %8s %8d \n", a[i].id, a[i].name, a[i].score); } } R GetHighestRecord(){ int max_index; int max_score; int i; max_score = a[0].score; max_index = 0; for (i=1; i<4; i++){ if (max_score < a[i].score){ max_index = i; max_score = a[i].score; } } return (a[max_index]); }
<寫法三>
/*the filename: main *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "Re.h" R a[4]; //因為在Re.h檔中,宣告了extern,所以在main.c檔中,需要再做一次宣告 FILE *f0; //因為在Re.h檔中,宣告了extern,所以在main.c檔中,需要再做一次宣告 int main() { R b; ReadStudentRecord(); PrintStudentRecord(); b = GetHighestRecord(); printf("\n\n the highest score is : \n"); printf("%8d %8s %8d \n", b.id, b.name, b.score); return 0; }
/*the filename: Re *the filename extension: .h */ #ifndef RE_H_INCLUDED #define RE_H_INCLUDED typedef struct record { int id; char *name; int score; }R; extern R a[4]; extern FILE *f0; void ReadStudentRecord(); void PrintStudentRecord(); R GetHighestRecord(); #endif // RE_H_INCLUDED
/*the filename: define_and_read_functions *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "Re.h" void ReadStudentRecord(){ int i; f0 = fopen ("d:/cs/score.dat", "r"); for (i=0; i<4; i++){ a[i].name = (char *) calloc(10, sizeof(char)); fscanf(f0, "%d %s %d", &a[i].id, a[i].name, &a[i].score); } fclose(f0); } void PrintStudentRecord(){ int i; for (i=0; i<4; i++){ printf("%8d %8s %8d \n", a[i].id, a[i].name, a[i].score); } } R GetHighestRecord(){ int max_index; int max_score; int i; max_score = a[0].score; max_index = 0; for (i=1; i<4; i++){ if (max_score < a[i].score){ max_index = i; max_score = a[i].score; } } return (a[max_index]); }
程式範例5
使用linked list(連結串列)的技巧來撰寫本程式
<寫法一>
/*the filename: main *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "list.h" LIST *myList; void readStudent(){ FILE *f0; int sid; char name[10]; int score; f0 = fopen("d:/cs/score.dat", "r"); while (!feof(f0)){ fscanf(f0, "%d %s %d", &sid, name, &score); insertFirstList(myList, sid, name, score); } fclose(f0); } void printStudent(LIST *listHead){ printList(listHead); } NODE *getHscoreStudent(LIST *listHead){ int hscore; NODE *hStudent; NODE *curr; if (listHead->count == 0){ return NULL; }else if (listHead->count == 1){ return listHead->head; }else { hStudent = listHead->head; for (curr = hStudent->link; curr != NULL; curr = curr->link) { if (curr->score > hStudent->score) { hStudent = curr; } } return hStudent; } } int main(){ NODE *x; myList = (LIST *) malloc(sizeof(LIST)); creatList(myList); // insertFirstList(myList, 101, "john", 80); // insertFirstList(myList, 102, "Andy", 100); readStudent(); printStudent(myList); printf("******************\n"); x = getHscoreStudent(myList); printNode(x); }
/*the filename: list *the filename extension: .h */ #ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED typedef struct node { int sid; char name[10]; int score; struct node *link; } NODE; typedef struct list { int count; struct node *head; } LIST; void creatList(LIST *listHead); void insertFirstList(LIST *listHead, int sid, char *name, int score); void printList(LIST *listHead); void printNode(NODE *input); void deleteFirstList(LIST *listHead); NODE *getFirstList(LIST *listHead); #endif // LIST_H_INCLUDED
/*the filename: list_implement *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h" void creatList(LIST *listHead) { listHead->count = 0; listHead->head = NULL; } void insertFirstList(LIST *listHead, int sid, char *name, int score){ NODE *pNew; pNew = (NODE *) malloc(sizeof(struct node)); pNew->sid = sid; strcpy (pNew->name, name); pNew->score = score; if (listHead->head == NULL){ // case 1: empty list pNew->link = NULL; listHead->head = pNew; } else { // case 2: begining list pNew->link = listHead->head; listHead->head = pNew; } listHead->count++; } void deleteFirstList(LIST *listHead){ NODE *pLoc; if (listHead->count == 0){ printf("Do nothing \n"); }else if (listHead->count == 1){ pLoc = listHead->head; listHead->head = NULL; listHead->count--; free(pLoc); } else { pLoc = listHead->head; listHead->head = pLoc->link; listHead->count--; free(pLoc); } } NODE *getFirstList(LIST *listHead){ NODE *pLoc; if (listHead->count == 0){ printf("Do nothing \n"); }else { pLoc = listHead->head; return pLoc; } } void printNode(NODE *input){ if (input != NULL){ printf("the record: %d \t %s \t %d\n", input->sid, input->name, input->score); } } void printList(LIST *listHead){ NODE *curr; if (listHead->head != NULL){ printf("the total count = %d\n", listHead->count); for (curr = listHead->head; curr != NULL; curr = curr->link){ printNode(curr); } } else{ printf("the list is empty\n"); } }
<寫法二>
/*the filename: main *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include "list.h" LIST *myList; void readStudent(){ FILE *f0; int id; char name[10]; int score; f0 = fopen("d:/cs/score.dat", "r"); while (fscanf(f0, "%d %s %d", &id, name, &score)!=EOF){ insertFirstList(myList, id, name, score); } fclose(f0); } void printStudent(LIST *listHead){ printList(listHead); } NODE *getHscoreStudent(LIST *listHead){ NODE *hStudent; NODE *curr; if (listHead->count == 0){ return listHead->head; }else { hStudent = listHead->head; for (curr = hStudent->link; curr != NULL; curr = curr->link) { if (curr->score > hStudent->score) { hStudent = curr; } } return hStudent; } } int main(){ NODE *x; myList = (LIST *) malloc(sizeof(LIST)); creatList(myList); readStudent(); printStudent(myList); printf("******************\n"); x = getHscoreStudent(myList); printNode(x); printf("\n\nfollow demo delete function:\n"); for(i=0; i<7; i++) { deleteFirstList(myList); x = getFirstList(myList); dumpNode(x); } return 0; }
/*the filename: list *the filename extension: .h */ #ifndef LIST_H_INCLUDED #define LIST_H_INCLUDED typedef struct node { int sid; char name[10]; int score; struct node *link; } NODE; typedef struct list { int count; NODE *head; } LIST; void creatList(LIST *listHead); void insertFirstList(LIST *listHead, int sid, char *name, int score); void printList(LIST *listHead); void printNode(NODE *input); void deleteFirstList(LIST *listHead); NODE *getFirstList(LIST *listHead); #endif // LIST_H_INCLUDED
/*the filename: list_implement *the filename extension: .c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h" void creatList(LIST *listHead) { listHead->count = 0; listHead->head = NULL; } void insertFirstList(LIST *listHead, int sid, char *name, int score){ NODE *pNew; pNew = (NODE *) malloc(sizeof(struct node)); pNew->sid = sid; strcpy (pNew->name, name); pNew->score = score; pNew->link = listHead->head; listHead->head = pNew; listHead->count++; } void printList(LIST *listHead){ NODE *curr; if (listHead->head != NULL){ printf("the total count = %d\n", listHead->count); for (curr = listHead->head; curr != NULL; curr = curr->link){ printNode(curr); } } else{ printf("the list is empty\n"); } } void printNode(NODE *input){ if (input != NULL){ printf("the record: %d \t %s \t %d\n", input->sid, input->name, input->score); } } void deleteFirstList(LIST *listHead){ NODE *pLoc; if (listHead->count == 0){ printf("Do nothing \n"); }else if (listHead->count == 1){ pLoc = listHead->head; listHead->head = NULL; listHead->count--; free(pLoc); } else { pLoc = listHead->head; listHead->head = pLoc->link; listHead->count--; free(pLoc); } } NODE *getFirstList(LIST *listHead){ NODE *pLoc; if (listHead->count == 0){ printf("Do nothing \n"); }else { pLoc = listHead->head; return pLoc; } }
http://www.geeksforgeeks.org/linked-list-vs-array/
回覆刪除Array&Linked list各寫2個好處
刪除