C語言實現(xiàn)學(xué)生消費(fèi)管理系統(tǒng)
本文實例為大家分享了C語言實現(xiàn)學(xué)生消費(fèi)管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
代碼可以實現(xiàn)的功能:
(1)直接從終端鍵盤輸入信息
(2)從磁盤文件錄入學(xué)生信息
(3)可以查詢學(xué)生消費(fèi)信息
(4)可以刪除學(xué)生信息
(5)可以添加學(xué)生的消費(fèi)信息,并顯示添加后學(xué)生的人數(shù)
(6)顯示錄入的學(xué)生信息
(7) 把錄入的學(xué)生信息保存到指定的文件中
代碼:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define LEN sizeof(struct scorenode) #define DEBUG struct scorenode ? ? ? ? ? ? ? ?//定義結(jié)構(gòu)體 {int number;/*學(xué)號*/ char name[10];/*姓名*/ int xiaofei;/*消費(fèi)情況*/ struct scorenode *next; }; typedef struct scorenode score; int n,k;/*n,k為全局變量,本程序中的函數(shù)均可*p3以使用它*/ void menu(); score *creat(void); score *load(score *head); score *search(score *head); score *del(score *head); score *add(score *head,score *stu); void print(score *head); save(score *p1); /*==============================================================================================*/ /*=========================創(chuàng)建鏈表,此函數(shù)帶回一個指向鏈表頭的指針=============================*/ score *creat(void) { ?? ?score *head; ? ? score *p1,*p2,*p3,*max; ?? ?int i,j; ? ? char t[10]; ?? ?n=0; ? ? p1=p2=p3=(score *)malloc(LEN);/*head=p3; 開辟一個新單元*/ ? ? ? printf("請輸入學(xué)生信息!(以0結(jié)束)\n"); repeat1: printf("請輸入學(xué)生編號(編號>0):");/*輸入學(xué)號,學(xué)號應(yīng)大于0*/ ? ? ? ?scanf("%d",&p1->number); ? ? ?while(p1->number<0) ?? ? { ?? ??? ? getchar(); ?? ??? ? printf("錯誤,請重新輸入號碼:"); ? ? ? ? ?scanf("%d",&p1->number); ?? ? } /*輸入學(xué)號為字符或小于0時,程序報錯,提示重新輸入學(xué)號*/ ?? ?if(p1->number==0) ?? ??? ?goto end;/*當(dāng)輸入的學(xué)號為0時,轉(zhuǎn)到末尾,結(jié)束創(chuàng)建鏈表*/ ?? ?else ?? ?{ ?? ??? ?p3=head; ?? ??? ?if(n>0) ?? ??? ?{ ?? ??? ??? ?for(i=0;i<n;i++) ?? ??? ??? ?{ ?? ??? ??? ??? ?if(p1->number!=p3->number) ?? ??? ??? ??? ??? ?p3=p3->next; ?? ??? ??? ??? ?else ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?printf("number repeate,please input again!\n"); ?? ??? ??? ??? ??? ?goto repeat1; ? ? ?/*當(dāng)輸入的學(xué)號已經(jīng)存在,程序報錯,返回前面重新輸入*/ ?? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ? } ? ? ? ?printf("請輸入學(xué)生姓名"); ? ? ? ?scanf("%s",&p1->name);/*輸入學(xué)生姓名*/ ? ? ? ?printf("請輸入消費(fèi)情況");/*輸入消費(fèi)情況;*/ ?? ? scanf("%d",&p1->xiaofei); ?while(p1->number!=0) ? { ? ? n=n+1; ?if(n==1) ? head=p1; ?else ? ? p2->next=p1; ? ? p2=p1; ? ? p1=(score *)malloc(LEN); ? ? printf("請輸入學(xué)生信息(以0結(jié)束)\n"); repeat2:printf("請輸入學(xué)號(學(xué)號應(yīng)大于0):"); ? ? ? scanf("%d",&p1->number);/*輸入學(xué)號,學(xué)號應(yīng)大于0*/ ? ?while(p1->number<0) ? ?{getchar(); ? ? printf("請重新輸入學(xué)號:"); ? ? ? ?scanf("%d",&p1->number);} /*輸入學(xué)號為字符或小于0時,程序報錯,提示重新輸入學(xué)號*/ ? if(p1->number==0) ? ?goto end;/*當(dāng)輸入的學(xué)號為0時,轉(zhuǎn)到末尾,結(jié)束創(chuàng)建鏈表*/ ? else ? { ? p3=head; ? if(n>0) ? ?{for(i=0;i<n;i++) ? ? ? {if(p1->number!=p3->number) ? ? p3=p3->next; ? ? else ? ? {printf("number repeate,please input again!\n"); ? ? ?goto repeat2; ? ? ?/*當(dāng)輸入的學(xué)號已經(jīng)存在,程序報錯,返回前面重新輸入*/ ? ? } ? ? } ? ?} ? } ? printf("請輸入學(xué)生姓名:"); ? ? ? ?scanf("%s",&p1->name);/*輸入學(xué)生姓名*/ ? ? ?printf("請輸入學(xué)生消費(fèi)形況:"); ? ? ? ?scanf("%d",&p1->xiaofei);/*輸入消費(fèi)情況; */ } end: p1=head; ? ? ?p3=p1; ? for(i=1;i<n;i++) ? ? ? ? ? ? ? ?{ ? ? ? ? ? ? ?for(j=i+1;j<=n;j++) ? ? ? ? ? ? ? ? ?{ ? ? ? ?max=p1; ? ? ? ?p1=p1->next; ? ? ? ?if(max->number>p1->number) ? ? ? ? { ? ? ? ? k=max->number; ? ? ? ? max->number=p1->number; ? ? ? ? p1->number=k; ? ? ? ? ?/*交換前后結(jié)點中的學(xué)號值,使得學(xué)號大者移到后面的結(jié)點中*/ ? ? ? ? strcpy(t,max->name); ? ? ? ? strcpy(max->name,p1->name); ? ? ? ? strcpy(p1->name,t); ? ? ?/*交換前后結(jié)點中的姓名,使之與學(xué)號相匹配*/ ? ? ? ? ? ? ? ? ? ? ? ?/*交換前后結(jié)點中的消費(fèi)情況,使之與學(xué)號相匹配*/ ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ?} ? ? max=head;p1=head;/*重新使max,p指向鏈表頭*/ ? } p2->next=NULL;/*鏈表結(jié)尾*/ ? ? ? ?printf("input student's num:%d ge!\n",n); ?? ? ? getch(); ? ? ? ?return(head); } /*==============================================================================================*/ /*===========================從文件讀入學(xué)生記錄=================================================*/ score *load(score *head) { ?? ??? ?score *p1,*p2; ? ? ? ? int m=0; ? ? ? ? char filepn[10]; ?? ??? ?FILE *fp; ?? ??? ?printf("請輸入路徑及文件名:"); ?? ??? ?scanf("%s",filepn);/*輸入文件路徑及名稱*/ ?? ??? ?if((fp=fopen(filepn,"r+"))==NULL) ?? ??? ?{ ?? ??? ??? ?printf("不能打開此文件\n"); ?? ??? ??? ?getch(); ?? ??? ??? ?return 0; ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?p1=(score *)malloc(LEN); /*開辟一個新單元*/ ?? ??? ??? ?fscanf(fp,"%d %s %d\n",&p1->number,p1->name,&p1->xiaofei); ?? ??? ??? ?printf("|%d\t|%s\t|%d\t\n",p1->number,p1->name,p1->xiaofei); ?? ??? ??? ?/*文件讀入與顯示*/ ?? ??? ??? ?head=NULL; ?? ??? ??? ?do ?? ??? ??? ?{ ?? ??? ??? ??? ?n=n+1; ?? ??? ??? ??? ?if(n==1) ?? ??? ??? ??? ??? ?head=p1; ?? ??? ??? ??? ?else ?? ??? ??? ??? ??? ?p2->next=p1; ?? ??? ??? ??? ?p2=p1; ?? ??? ??? ??? ?p1=(score *)malloc(LEN); ?/*開辟一個新單元*/ ?? ??? ??? ??? ?fscanf(fp,"%d %s %d\n",&p1->number,p1->name,&p1->xiaofei); ?? ??? ??? ??? ?printf("|%d\t|%s\t|%d\t\n",p1->number,p1->name,p1->xiaofei); ?? ??? ??? ??? ?/*文件讀入與顯示*/ ?? ??? ??? ?}while(!feof(fp)); ?? ??? ??? ?p2->next=p1; ?? ??? ??? ?p1->next=NULL; ?? ??? ??? ?n=n+1; ?? ??? ?} ?? ??? ?printf("-----------------------------------------\n");/*表格下線*/ ?? ??? ?getch(); ?? ??? ?fclose(fp);/*結(jié)束讀入,關(guān)閉文件*/ ?? ??? ?return (head); } /*==============================================================================================*/ /*=====================查詢學(xué)生消費(fèi)=====================================================*/ score *search(score *head) { ?? ?int number; ?? ?score *p1,*p2; ?? ?printf("input the student's number of searching:"); ?? ?scanf("%d",&number); ?? ?getchar(); ?? ?while(number!=0) ?? ?{ ?? ??? ?if(head==NULL) ?? ??? ?{ ?? ??? ??? ?printf("\n nobody information!\n"); ?? ??? ??? ?return(head); ?? ??? ?} ?? ??? ?printf("-----------------------------------------\n"); ?? ??? ?printf("|number\t|name\t|consume\t \n"); ?? ??? ?printf("-----------------------------------------\n");/*打印表格域*/ ?? ??? ?p1=head; ?? ??? ?while(number!=p1->number&&p1->next!=NULL) ?? ??? ?{ ?? ??? ??? ?p2=p1; ?? ??? ??? ?p1=p1->next; ?? ??? ?} ? ? ? ? if(number==p1->number) ?? ??? ?{ ?? ??? ??? ? printf("|%d\t|%s\t|%d\t\n",p1->number,p1->name,p1->xiaofei); ?? ??? ??? ? printf("-----------------------------------------\n"); ?? ??? ?}/*打印表格域*/ ? ? ? ? else ?? ??? ??? ?printf("%dthis student not exist!\n",number); ?? ??? ?printf("input the student's number of searching:"); ?? ??? ?scanf("%d",&number); ?? ??? ?getchar(); ?? ?} ?? ??? ?printf("already exit!\n"); ?? ??? ?getchar(); ?? ??? ?return(head); } /*==============================================================================================*/ /*=======================刪除學(xué)生資料================================================*/ score *del(score *head) { ?? ?score *p1,*p2; ?? ?int number; ?? ?printf("input the student's number of deleting(input 0 exit):"); ?? ?scanf("%d",&number); ?? ?getchar(); ?? ?while(number!=0)/*輸入學(xué)號為0時退出*/ ?? ?{ ?? ??? ?if(head==NULL) ?? ??? ?{ ?? ??? ?printf("\nnobody information!\n"); ?? ??? ?return(head); ?? ??? ?} ?? ??? ?p1=head; ?? ??? ?while(number!=p1->number&&p1->next!=NULL) ? ?/*p1指向的不是所要找的首結(jié)點,并且后面還有結(jié)點*/ ?? ??? ?{ ?? ??? ?p2=p1; ?? ??? ?p1=p1->next; ?? ??? ?} /*p1后移一個結(jié)點*/ ?? ??? ?if(number==p1->number) ? ?/*找到了*/ ?? ??? ?{ ?? ??? ?if(p1==head) ?? ??? ?head=p1->next; ? ?/*若p1指向的是首結(jié)點,把地二個結(jié)點地址賦予head*/ ?? ??? ?else ?? ??? ?p2->next=p1->next; ? ?/*否則將下一個結(jié)點地址 賦給前一結(jié)點地址*/ ?? ??? ?printf("delete number:%d\n",number); ?? ??? ?n=n-1; ?? ??? ?} ?? ??? ?else ?? ??? ?printf("%d student not exist!\n",number); ? /*找不到該結(jié)點*/ ?? ?printf("input the student's number of deleting:"); ?? ?scanf("%d",&number); ?? ?getchar(); ?? ?} #ifdef DEBUG ?printf("already exit\n"); #endif ?printf("now how many students:%d ge!\n",n); ?getch(); ?return(head); } /*==============================================================================================*/ /*==============================================================================================*/ score *add(score *head,score *stu) { ?? ?score *p0,*p1,*p2,*p3,*max; ? ? int i,j; ? ? char t[10]; ?? ?p3=stu=(score *)malloc(LEN);/*開辟一個新單元*/ ?? ?printf("\ninput the student's information of adding!"); ?? ?repeat4: printf("please input the student's number(number>0):"); ?? ?scanf("%d",&stu->number); ? /*輸入學(xué)號,學(xué)號應(yīng)大于0*/ ? ? while(stu->number<0) ?? ?{ ?? ??? ?getchar(); ?? ??? ?printf("error,please input number again:"); ?? ??? ?scanf("%d",&stu->number); ?? ?}/*輸入錯誤,重新輸入學(xué)號*/ ? ? ?/******************************************************/ ? ? if(stu->number==0) ?? ?goto end2;/*當(dāng)輸入的學(xué)號為0時,轉(zhuǎn)到末尾,結(jié)束追加*/ ?? ?else ?? ?{ ?? ??? ?p3=head; ?? ??? ?if(n>0) ?? ??? ?{ ?? ??? ??? ?for(i=0;i<n;i++) ?? ??? ??? ?{ ?? ??? ??? ??? ?if(stu->number!=p3->number) ?? ??? ??? ??? ??? ?p3=p3->next; ?? ??? ??? ??? ?else ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?printf("number repeat,please input again!\n"); ?? ??? ??? ??? ??? ?goto repeat4; ? ? ?/*當(dāng)輸入的學(xué)號已經(jīng)存在,程序報錯,返回前面重新輸入*/ ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?} ? /******************************************************/ ? ?printf("input the student's name:"); ? ?scanf("%s",stu->name); ? ? ? ? ? ?/*輸入學(xué)生姓名*/ ? ?printf("please input the consuming:"); ? ?scanf("%d",&stu->xiaofei); ? ?p1=head; ? ?p0=stu; ?? ?if(head==NULL) ?? ?{ ?? ? ? head=p0; ?? ? ? p0->next=NULL; ?? ?}/*當(dāng)原來鏈表為空時,從首結(jié)點開始存放資料*/ ?? ?else/*原來鏈表不為空*/ ?? ?{ ?? ??? ?if(p1->next==NULL)/*找到原來鏈表的末尾*/ ?? ??? ?{ ?? ??? ??? ?p1->next=p0; ?? ??? ??? ?p0->next=NULL;/*將它與新開單元相連接*/ ?? ??? ?} ?? ??? ?else ?? ??? ?{ ?? ??? ??? ?while(p1->next!=NULL)/*還沒找到末尾,繼續(xù)找*/ ?? ??? ??? ?{ ?? ??? ??? ??? ?p2=p1; ?? ??? ??? ??? ?p1=p1->next; ?? ??? ??? ?} ?? ??? ??? ?p1->next=p0; ?? ??? ??? ?p0->next=NULL; ?? ??? ?} ?? ?} ?? ?n=n+1; ?? ?p1=head; ?? ?p0=stu; ?? ?for(i=1;i<n;i++) ?? ?{ ?? ??? ?for(j=i+1;j<=n;j++) ? ? ? ? { ?? ??? ??? ?max=p1; ?? ??? ??? ?p1=p1->next; ?? ??? ??? ?if(max->number>p1->number) ?? ??? ??? ?{ ?? ??? ??? ?k=max->number; ?? ??? ??? ?max->number=p1->number; ?? ??? ??? ?p1->number=k; ? ? ? ? ?/*交換前后結(jié)點中的學(xué)號值,使得學(xué)號大者移到后面的結(jié)點中*/ ?? ??? ??? ?strcpy(t,max->name); ?? ??? ??? ?strcpy(max->name,p1->name); ?? ??? ??? ?strcpy(p1->name,t); ? ? ?/*交換前后結(jié)點中的姓名,使之與學(xué)號相匹配*/ ? ? ? ? ? ? ? ? ? /*交換前后結(jié)點中的消費(fèi)情況,使之與學(xué)號相匹配*/ ? ? ? ? ? ? } ?? ??? ??? ?max=head; ?? ??? ??? ?p1=head;/*重新使max,p指向鏈表頭*/ ?? ??? ?} ?? ?} ?? ?end2: ?? ??? ?printf("now how many students are they:%d ge!\n",n); ?? ??? ?getch(); ?? ??? ?return(head); } ?/*==============================================================================================*/ ?/*==============================================================================================*/ void print(score *head) { ?score *p; ?if(head==NULL) ? ? ? ? ?printf("\nnobody information!\n"); ?else ?{ ?? ? printf("%d\n",n); ?printf("-----------------------------------------\n"); ?printf("|number\t|name\t|consume\t |\n"); ?printf("-----------------------------------------\n");/*打印表格域*/ ?p=head; ? ? ?do ? ? {printf("|%d\t|%s\t|%d\t\n",p->number,p->name,p->xiaofei); ? ? ? ? printf("-----------------------------------------\n");/*打印表格域*/ ? ? ? ?p=p->next; ?? ?}while (p!=NULL);/*打印完成了*/ ?? ?getch(); ?} } /*==============================================================================================*/ /*========================(保存)把錄入的學(xué)生信息保存到指定的文件中============================*/ save(score *p1) ? ? ? ? ? ? ? ? //保存 { ?FILE *fp; ?if((fp=fopen("d:\\text","wb+"))==NULL) ? ? ? ? ? ? ? ? ? //文件保存在d盤下,文件名為text ?{ ? printf("can't open this file!\n"); ? ? ? ? ? ? ? ? ? ? ? ?//不能打開這個文件 ? return 0; ?} ?else ?{ ?? ?while(p1!=NULL) ?? ?{ ?? ?fprintf(fp,"%d %s %d\t\t\t",p1->number,p1->name,p1->xiaofei);//將學(xué)生信息寫入到文件中 ? ? p1=p1->next; ?? ?} ?? ?printf("file save complete!please enter return!\n"); ? ? ? ? //文件保存完成!請輸入空車鍵返回 ?? ?getch(); ?} ?? ? fclose(fp); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //關(guān)閉文件 } /*==============================================================================================*/ /*==================================主菜單===================================================*/ void menu() { ?? ?system("cls"); ?? ?printf("\n\n\n"); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?printf("\t\t-------------STUDENT CONSUME-------------\n"); ?? ?printf("\t\t\t0 ?退出 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\n"); ? ? ? ? ?//錯誤 ?? ?printf("\t\t\t1 ?直接從終端鍵盤輸入信息 ? ? ? ? ? ? ?\n"); ? ? ? ? ?//直接從終端鍵盤輸入信息 ?? ?printf("\t\t\t2 ?從磁盤文件錄入學(xué)生信息 ? ? ? ? ? ? ?\n"); ? ? ? ? ?//(加載)從磁盤文件錄入學(xué)生信息 ?? ?printf("\t\t\t3 ?可以查詢學(xué)生消費(fèi)信息 ? ? ? ? ? ? ? ?\n"); ? ? ? ? ?//(搜索)可以查詢學(xué)生消費(fèi)信息 ?? ?printf("\t\t\t4 ?可以刪除學(xué)生信息 ? ? ? ? ? ? ? ? ? ?\n"); ? ? ? ? ?//(刪除)可以刪除學(xué)生信息 ?? ?printf("\t\t\t5 ?可以添加學(xué)生的消費(fèi)信息 ? ? ? ? ? ? ?\n"); ? ? ? ? ?//(添加)可以添加學(xué)生的消費(fèi)信息,并顯示添加后學(xué)生的人數(shù) ?? ?printf("\t\t\t6 ?顯示錄入的學(xué)生信息 ? ? ? ? ? ? ? ? ?\n"); ? ? ? ? ?//(顯示)顯示錄入的學(xué)生信息 ?? ?printf("\t\t\t7 ?把錄入的學(xué)生信息保存到指定的文件中 ?\n"); ? ? ? ? ?//(保存)把錄入的學(xué)生信息保存到指定的文件中 ?? ?printf("\t\t-----------------------------------------\n\n"); ? ? ? ?//? ?? ?printf("\t\tchoose(0-7):"); } /*===============================主函數(shù)================================================*/ main() { ?? ?int num; ?? ?score *head=0,*stu=0; ?? ?menu(); ?? ?scanf("%d",&num); ?? ?while(1) ?? ?{ ?? ??? ?switch(num) ?? ??? ?{ ?? ??? ?case 1: head=creat();break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸入 ?? ??? ?case 2: head=load(head);break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //加載 ?? ??? ?case 3: head=search(head);break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //搜索 ?? ??? ?case 4: head=del(head);break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//刪除 ?? ??? ?case 5: head=add(head,stu);break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//添加 ?? ??? ?case 6: print(head);break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //顯示 ?? ??? ?case 7: save(head);break; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//保存 ?? ??? ?case 0: exit(0); ?? ??? ?default:printf("Input error,please again!"); ?? ??? ?} ?? ??? ?menu(); ?? ??? ?scanf("%d",&num); ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++ std::make_unique和std::make_shared用法小結(jié)
本文主要介紹了C++ std::make_unique和std::make_shared用法,使用std::make_unique和std::make_shared能夠簡化動態(tài)分配內(nèi)存和構(gòu)造對象的過程,提高代碼的安全性和可讀性,感興趣的可以了解一下2023-11-11C語言實現(xiàn)選擇題標(biāo)準(zhǔn)化考試系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)選擇題標(biāo)準(zhǔn)化考試系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06