看板 ESOE-90 關於我們 聯絡資訊
#include<stdio.h> #include<stdlib.h> #include<string.h> #define NAME_LEN 80 #define MAX_UNITS 10 #define FILESRC "./stdScore.data" typedef struct { int id; char name[NAME_LEN]; int calculus; int computer; int total; int rank; } student_t; int fun_1(student_t*); int fun_2(const student_t*); int fun_3(const student_t*); int bsort(student_t*); int saveFile(const student_t*,char *); int loadFile(student_t*,char *); int sByName(const student_t*); int delFile(student_t*,char *); int main() { student_t students[MAX_UNITS]; int choies = 1; int nMaxUnits = MAX_UNITS; while(--nMaxUnits + 1) //清空學生資料 students[nMaxUnits].name[0] = '\0'; loadFile(students,FILESRC); //讀入學生資料 while(choies) { printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "Input what you want?", "1)Enter score", "2)Seek student by ID", "3)Seek student by NAME", "4)Student Table", "5)Save the Data", "6)Delete the Data", "0)Exit the program"); scanf("%d",&choies); switch(choies) { case 1:fun_1(students);break; case 2:fun_2(students);break; case 3:sByName(students);break; case 4:fun_3(students);break; case 5:saveFile(students,FILESRC);break; case 6:delFile(students,FILESRC);break; } } return 0; } int cmp(const void *student1,const void *student2) { return (*(const student_t **)student2) -> total - (*(const student_t **)student1) -> total; } int bsort(student_t *students) { student_t **tmp; int num = 0; int total = 0; while(students[num].name[0] != '\0') { students[num].total = students[num].calculus + students[num].computer; tmp[num] = &students[num]; num++; } total = num; qsort(tmp,total,sizeof(student_t *),cmp); for(num=0;num<total;num++) tmp[num] -> rank = num + 1; return 0; } int fun_1(student_t *students) { int num=0; while(1) { while(students[num].name[0] != '\0') num++; printf("\nPlease input student NAME: or 0 to MENU"); scanf("%s",students[num].name); if(students[num].name[0] == '0') { students[num].name[0] = '\0'; break; } students[num].id = num + 1; printf("\nPlease input %s\'s CACULUS SCORE:",students[num].name); scanf("%d",&students[num].calculus); printf("\nPlease input %s\'s COMPUTER SCORE:",students[num].name); scanf("%d",&students[num].computer); } bsort(students); return 0; } int fun_2(const student_t *students) { int num = 0; while(1) { printf("\nPlease enter student ID: or 0 to MENU"); scanf("%d",&num); if(!num)break; num--; printf("ID\tNAME\tCACU\tCOMP\tTOTAL\tRANK\n"); printf("%d\t%s\t%d\t%d\t%d\t%d\n", students[num].id, students[num].name, students[num].calculus, students[num].computer, students[num].total, students[num].rank); } return 0; } int fun_3(const student_t *students) { int num = 0; printf("ID\tNAME\tCACU\tCOMP\tTOTAL\tRANK\n"); while(students[num].name[0] != '\0') { printf("%d\t%s\t%d\t%d\t%d\t%d\n", students[num].id, students[num].name, students[num].calculus, students[num].computer, students[num].total, students[num].rank); num++; } return 0; } int saveFile(const student_t *students,char *src) { FILE *ptrFile; int count = 0; if(!(ptrFile = fopen(src,"wb"))) { printf("cannot open this file (wb):%s",src); return 0; } while(students[count].name[0] != '\0') fwrite(&students[count++],sizeof(student_t),1,ptrFile); //寫入學生資料 fclose(ptrFile); return 0; } int loadFile(student_t *students,char *src) { FILE *ptrFile; int count = 0; if(!(ptrFile = fopen(src,"rb"))) { printf("cannot open this file (rb):%s",src); return 0; } while(!feof(ptrFile)) //讀入原有學生資料 { fread(&students[count],sizeof(student_t),1,ptrFile); count++; } fclose(ptrFile); return 0; } int sByName(const student_t *students) { int count; char name[NAME_LEN]; int flag = 1; printf("\nPlaese input the NAME:"); scanf("%s",name); while(name[0] != '0') { count = 0; while(students[count].name[0] != '\0') { if(!strcmp(students[count].name,name)) { printf("ID\tNAME\tCACU\tCOMP\tTOTAL\tRANK\n"); printf("%d\t%s\t%d\t%d\t%d\t%d\n", students[count].id, students[count].name, students[count].calculus, students[count].computer, students[count].total, students[count].rank); flag = 0; } count++; } if(flag) printf("\ncannot find student %s",name); flag = 1; printf("\nPlaese input the NAME or 0 to EXIT:"); scanf("%s",name); } return 0; } int delFile(student_t *students,char *src) { FILE *ptrFile; int nMaxUnits = MAX_UNITS; if(!(ptrFile = fopen(src,"wb"))) { printf("cannot open this file (wb):%s",src); return 0; } fclose(ptrFile); while(--nMaxUnits + 1) //清空學生資料 students[nMaxUnits].name[0] = '\0'; return 0; } -- 真的有人想看嗎@@? 應該我自己都不想~><~... 大叔~~~教教我妳的格調程式法啦~~~ -- 這個世界的成分 不是原子 而是故事 因為原子的本身 也是故事 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.216.12.169