C語言學生管理系統
阿新 • • 發佈:2020-11-22
#include <stdio.h> #include<string.h> #include<stdlib.h> #define PRINT_ERROR_FILE(e)\ do\ {\ if(e==NULL)\ {\ printf("開啟檔案失敗!\n");\ exit(-1);\ }\ }while(0) typedef struct _Grade { int id; //課程編號 char name[20]; //課程名稱 double score;//學分 }Grade; typedefstruct _Stu { Grade gb; int xuehao; char name[20]; char sex[5]; int Su_se; char dian_hua[12]; double normal_score; double lab_score; double shijuan_score; double sum_score; double result_xuefen; }Stu; typedef struct _Node { Stu s; struct _Node* next; }Node; Node* head=NULL; void Input_Message() { Node* cur=head; while(cur->next) { cur=cur->next; } if(head->next==NULL) { head->next=(Node*)malloc(sizeof(Node)); head->next->next=NULL; cur=head->next; } else { Node* temp=(Node*)malloc(sizeof(Node)); temp->next=NULL; cur->next=temp; cur=temp; } printf("請輸入課程編號:"); scanf("%d",&cur->s.gb.id); printf("請輸入課程名稱:"); scanf("%s",cur->s.gb.name); printf("請輸入課程學分:"); scanf("%lf",&cur->s.gb.score); printf("請輸入學生姓名:"); scanf("%s",cur->s.name); printf("請輸入學生學號:"); scanf("%d",&cur->s.xuehao); printf("請輸入學生性別:"); scanf("%s",cur->s.sex); printf("請輸入學生宿舍號碼:"); scanf("%d",&cur->s.Su_se); printf("請輸入學生電話:"); scanf("%s",cur->s.dian_hua); printf("請輸入學生平時成績:"); scanf("%lf",&cur->s.normal_score); printf("請輸入學生實驗成績:"); scanf("%lf",&cur->s.lab_score); printf("請輸入學生卷面成績:"); scanf("%lf",&cur->s.shijuan_score); if(cur->s.lab_score==-1) { cur->s.sum_score=(cur->s.normal_score)*0.30+(cur->s.shijuan_score)*0.70; } else { cur->s.sum_score=(cur->s.normal_score)*0.15+(cur->s.lab_score)*0.15+(cur->s.shijuan_score)*0.7; } double t=cur->s.sum_score; if((t>=90)&&(t<=100)) { cur->s.result_xuefen=t; } else if ((t>=80)&&(t<90)) { cur->s.result_xuefen=t*0.8; } else if ((t>=70)&&(t<80)) { cur->s.result_xuefen=t*0.7; } else if ((t>=60)&&(t<70)) { cur->s.result_xuefen=t*0.6; } else if (t<60) { cur->s.result_xuefen=0.0; } } void Print_Info() { printf("課程編號 課程名稱 課程學分 學號\t姓名\t性別\t宿舍\t電話\t\t平時成績 實驗成績 卷面成績 綜合成績 最後學分\n"); Node* cur=head->next; while(cur) { printf("%-8d %-8s %-8lf %-4d\t%-4s\t%-4s\t%-4d\t%-4s\t\t%-8.1lf %-8.1lf %-8.1lf %-8.1lf %-8.1lf\n",cur->s.gb.id,cur->s.gb.name, cur->s.gb.score, cur->s.xuehao,cur->s.name,cur->s.sex,cur->s.Su_se, cur->s.dian_hua,cur->s.normal_score,cur->s.lab_score,cur->s.shijuan_score, cur->s.sum_score,cur->s.sum_score); cur=cur->next; } } void Print_Single(Node* cur) { printf("課程編號 課程名稱 課程學分 學號\t姓名\t性別\t宿舍\t電話\t\t平時成績 實驗成績 卷面成績 綜合成績 最後學分\n"); printf("%-8d %-8s %-8lf %-4d\t%-4s\t%-4s\t%-4d\t%-4s\t\t%-8.1lf %-8.1lf %-8.1lf %-8.1lf %-8.1lf\n",cur->s.gb.id,cur->s.gb.name, cur->s.gb.score, cur->s.xuehao,cur->s.name,cur->s.sex,cur->s.Su_se, cur->s.dian_hua,cur->s.normal_score,cur->s.lab_score,cur->s.shijuan_score, cur->s.sum_score,cur->s.sum_score); } void Find_Info() { Node* cur=head->next; printf("請輸入要查詢學生的姓名或者學號\n首先請選擇查詢方式:1.姓名. 2.學號\n"); int x; scanf("%d",&x); if(x==1) { printf("請輸入要查詢的學生姓名:"); char name[20]; scanf("%s",name); while(cur) { if(strcmp(name,cur->s.name)==0) { printf("找到了該學生,接下來列印該學生資訊\n"); Print_Single(cur); return; } cur=cur->next; } printf("沒有找到該學生資訊\n"); return; } else { printf("請輸入要查詢的學生學號:"); int id; scanf("%d",&id); while(cur) { if(id==cur->s.xuehao) { printf("找到了該學生,接下來列印該學生資訊\n"); Print_Single(cur); return; } cur=cur->next; } printf("沒有找到該學生資訊\n"); return; } } Node* Find_By_Number(int id) { Node* cur=head->next; while(cur) { if(cur->s.xuehao==id) { //Print_Single(cur); return cur; } cur=cur->next; } printf("找不到這個學號對應的學生資訊\n"); return NULL; } Node* Find_By_Name(char * s) { Node* cur=head->next; while(cur) { if(strcmp(s,cur->s.name)==0) { //Print_Single(cur); return cur; } cur=cur->next; } printf("沒有這位學生\n"); return NULL; } void Del_Stu(int id) { //刪除的前提是你得必須先找到該學生. Node* cur=Find_By_Number(id); if(!cur)return; if(cur->next==NULL) { free(cur); printf("刪除成功\n"); return; } //找到所查詢節點的前一個節點 Node* temp=head; while(temp->next) { if(temp->next->s.xuehao==id) { temp->next=cur->next; cur->next=NULL; free(cur); printf("刪除aaa成功\n"); return; } temp=temp->next; } } void Result(Node* head,int n) { Node* pre,*q,*p; for (int i=0;i<n-1;i++) { pre=head; p=head->next; q=p->next; for(int j=0;j<n-(i+1);j++) { if(p->s.result_xuefen>q->s.result_xuefen) { pre->next=q; p->next=q->next; q->next=p; pre=q; q=p->next; continue; } pre=pre->next; p=p->next; q=q->next; } } } int GetLength(Node* head) { Node* cur=head->next; int count=0; while(cur) { count++; cur=cur->next; } return count; } void Write_File(char * filename) { Node* cur=head->next; FILE* fs=fopen(filename,"w+"); PRINT_ERROR_FILE(fs); while(cur) { fwrite((void*)&cur->s,sizeof(Stu),1,fs); cur=cur->next; } printf("寫入成功\n"); fclose(fs); } Node* Read_File(char* filename) { head=(Node*)malloc(sizeof(Node)); head->next=NULL; FILE* fs=fopen(filename,"r+"); PRINT_ERROR_FILE(fs); Node* cur=(Node*)malloc(sizeof(Node)); while(fread( ((void*)&cur->s),sizeof(Stu),1,fs ) ) { cur->next=head->next; head->next=cur; cur=(Node*)malloc(sizeof(Node)); } free(cur); return head; } int main() { int a; head=(Node*)malloc(sizeof(Node)); head->next=NULL; char filename[20]; while(1) { printf("\t\t\t\t\t\t學生單科成績查詢系統\n"); printf("請先選擇功能:\n"); printf("1.錄入學生資訊\n"); printf("2.列印學生資訊\n"); printf("3.查詢學生資訊\n"); printf("4.刪除學生\n"); printf("5.綜合排序\n"); printf("6.讀資料到檔案\n"); printf("7.讀檔案到記憶體\n"); char ch=getch(); switch(ch) { case '1': Input_Message(); break; case '2': Print_Info(); break; case '3': printf("請選擇查詢方式:1.通過學號查詢.2.通過姓名查詢\n"); int x; scanf("%x",&x); if(x==1) { int temp; printf("請輸入學號:"); scanf("%d",&temp); Node*t= Find_By_Number(temp); if(t==NULL)break; printf("該學生資訊情況如下\n"); Print_Single(t); } else { char name[20]; scanf("%s",name); Node*t= Find_By_Name(name); if(t==NULL)break; Print_Single(t); } break; case '4': printf("請輸入要刪除的學生學號:"); int id; scanf("%d",&id); Node *cur=Find_By_Number(id); if(cur==NULL)break; Del_Stu(id); break; case '5': a=GetLength(head); Result(head,a); break; case '6': printf("請輸入檔案的名字:"); scanf("%s",filename); Write_File(filename); break; case '7': Read_File(filename); Print_Info(); break; } } return 0; }