C語言實現(xiàn)學生個人消費管理系統(tǒng)
本文實例為大家分享了C語言實現(xiàn)學生個人消費管理系統(tǒng)的具體代碼,供大家參考,具體內(nèi)容如下
程序介紹
運行程序時,首先進入到菜單部分,菜單部分提供了菜單顯示和輸入功能部分。其運行效果如圖所示。在主界面上輸入數(shù)字0——7,實現(xiàn)相應的功能。
此系統(tǒng)有兩種錄入方式,一種是輸入1時,直接從終端鍵盤輸入信息,使用方法如下:
主界面輸入“1”,進入創(chuàng)建界面,開始創(chuàng)建學生個人消費信息。學生個人消費信息創(chuàng)建界面運行效果如圖所示。
另一種是輸入2時,從磁盤文件錄入學生信息。使用方法如下:
首先在自己創(chuàng)建的文件f:\cff.txt記事本中輸入學生個人消費信息,如圖所示。
然后,在主界面中輸入“2”,開始加載學生消費信息文件,根據(jù)提示輸入存有學生信息的文件路徑和名稱,即可彈出文件中的信息。運行效果如圖所示。
通過以上兩種錄入方式,分別對學生信息進行3-7的操作,當輸入1,創(chuàng)建完信息后,可以直接操作3-7的功能,同樣在輸入2后,也可以直接操作3-7的功能。但是需要注意,每次3-7的功能操作都是針對前一步的錄入方式。與之前操作過的錄入方式無關(guān)。
輸入“3”,可以查詢學生消費信息,根據(jù)提示輸入查詢的學生學號,即可調(diào)出該學生的信息,運行效果如圖所示。
輸入“5”,可以添加學生的消費信息,并顯示添加后學生的人數(shù)。運行效果如圖所示。
輸入“6”,可以顯示錄入的學生信息,運行效果如圖所示。
輸入“7”,可以把錄入的學生信息保存到指定的文件中,運行效果如圖所示。
本程序指定的保存路徑為F:\CONSUME,保存成功后在F盤中會自動生成CONSUME文件,效果如圖所示。
由于未指定文件的打開方式,故打開文件會彈出打開方式對話框,如圖所示。
以記事本的方式打開文件,效果如圖所示。
代碼
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #define LEN sizeof(struct scorenode) #define DEBUG struct scorenode { ?? ?int number;/*學號*/ ?? ?char name[10];/*姓名*/ ?? ?int xiaofei;/*消費情況*/ ?? ?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); int 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("please input student's information,input 0 exit!\n"); ?? ?repeat1: printf("please input student's number(number>0):");/*輸入學號,學號應大于0*/ ? ? ? ?scanf(" ? %d",&p1->number); ? ? ?while(p1->number<0) ?? ? { ?? ??? ? getch(); ?? ??? ? printf("error,please input number again:"); ? ? ? ? ?scanf("%d",&p1->number); ?? ? } ? ? /*輸入學號為字符或小于0時,程序報錯,提示重新輸入學號*/ ?? ?if(p1->number==0) ?? ??? ?goto end;/*當輸入的學號為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; ? ? ?/*當輸入的學號已經(jīng)存在,程序報錯,返回前面重新輸入*/ ?? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ? } ? ? ? ?printf("please input student's name:"); ? ? ? ?scanf("%s",&p1->name);/*輸入學生姓名*/ ? ? ? ?printf("please input student's consume money:");/*輸入消費情況;*/ ?? ??? ? 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("please input student's information,input 0 exit!\n"); ?? ??? ?repeat2:printf("please input student's number(number>0):"); ? ? ? ?? ?scanf("%d",&p1->number);/*輸入學號,學號應大于0*/ ?? ? ? while(p1->number<0) ?? ? ? { ?? ??? ? ? ?? ?getch(); ?? ??? ??? ?printf("error,please input number again:"); ?? ??? ? ? ?scanf("%d",&p1->number); ?? ??? ?} ?? ??? ? ? /*輸入學號為字符或小于0時,程序報錯,提示重新輸入學號*/ ?? ??? ? ?if(p1->number==0) ?? ??? ? ? ?? ?goto end;/*當輸入的學號為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; ?? ??? ??? ??? ??? ??? ? ? ? /*當輸入的學號已經(jīng)存在,程序報錯,返回前面重新輸入*/ ?? ??? ??? ??? ??? ? ? ?} ?? ??? ??? ??? ? ? ?} ?? ??? ??? ? ? } ?? ??? ? ?} ? ?? ??? ? ?? ?} ?? ?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é)點中的學號值,使得學號大者移到后面的結(jié)點中*/ ?? ??? ? ?? ??? ? ? ? ? ?strcpy(t,max->name); ?? ??? ? ? ? ? ?strcpy(max->name,p1->name); ?? ??? ? ? ? ? ?strcpy(p1->name,t); ? ? ?? ??? ??? ? /*交換前后結(jié)點中的姓名,使之與學號相匹配*/ ? ? ? ? ? ? ? ? ?/*交換前后結(jié)點中的消費情況,使之與學號相匹配*/ ? ? ? ? ? ??? ?} ? ? ? ? ?} ? ? ?? ?max=head;p1=head;/*重新使max,p指向鏈表頭*/ ? ?? ?} ?? ?p2->next=NULL;/*鏈表結(jié)尾*/ ? ?printf("input student's num:%d ge!\n",n); ? ?getch(); ? ?return(head); } /*==============================================================================================*/ /*===========================從文件讀入學生記錄=================================================*/ score *load(score *head) { ?? ?score *p1,*p2; ? ? int m=0; ? ? char filepn[10]; ?? ?FILE *fp; ?? ?printf("please input file's postion and its name:"); ?? ?scanf("%s",filepn);/*輸入文件路徑及名稱*/ ?? ?if((fp=fopen(filepn,"r+"))==NULL) ?? ?{ ?? ??? ?printf("can't open this file!\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); } /*==============================================================================================*/ /*=====================查詢學生消費=====================================================*/ score *search(score *head) { ?? ?int number; ?? ?score *p1,*p2; ?? ?printf("input the student's number of searching:"); ?? ?scanf("%d",&number); ?? ?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); ?? ??? ?getch(); ?? ?} ?? ??? ?printf("already exit!\n"); ?? ??? ?getch(); ?? ??? ?return(head); } /*==============================================================================================*/ /*=======================刪除學生資料================================================*/ score *del(score *head) { ?? ?score *p1,*p2; ?? ?int number; ?? ?printf("input the student's number of deleting(input 0 exit):"); ?? ?scanf("%d",&number); ?? ?while(number!=0)/*輸入學號為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); ?? ?} ?? ?#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); ??? ? /*輸入學號,學號應大于0*/ ? ? while(stu->number<0) ?? ?{ ?? ??? ?getch(); ?? ??? ?printf("error,please input number again:"); ?? ??? ?scanf("%d",&stu->number); ?? ?}/*輸入錯誤,重新輸入學號*/ ? ? ?/******************************************************/ ? ? if(stu->number==0) ?? ?goto end2;/*當輸入的學號為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; ? ? ?? ??? ??? ??? ? /*當輸入的學號已經(jīng)存在,程序報錯,返回前面重新輸入*/ ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?} ? /******************************************************/ ? ?printf("input the student's name:"); ? ?scanf("%s",stu->name); ? ? ? ? ? ?/*輸入學生姓名*/ ? ?printf("please input the consuming:"); ? ?scanf("%d",&stu->xiaofei); ? ? ? ?p1=head; ? ?p0=stu; ?? ?if(head==NULL) ?? ?{ ?? ? ? head=p0; ?? ? ? p0->next=NULL; ?? ?}/*當原來鏈表為空時,從首結(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é)點中的學號值,使得學號大者移到后面的結(jié)點中*/ ?? ??? ??? ?strcpy(t,max->name); ?? ??? ??? ?strcpy(max->name,p1->name); ?? ??? ??? ?strcpy(p1->name,t); ? ? ??? ??? ?/*交換前后結(jié)點中的姓名,使之與學號相匹配*/ ? ? ? ? ? ? /*交換前后結(jié)點中的消費情況,使之與學號相匹配*/ ? ? ? ? ? ? } ?? ??? ??? ?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(); ?? ? } } /*==============================================================================================*/ /*==============================================================================================*/ int save(score *p1) { ?? ? FILE *fp; ?? ? if((fp=fopen("f:\\consume","wb"))==NULL) ?? ? { ?? ??? ? ?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); ?? ??? ??? ?/*?? ?printf("file write error\n");*/ ?? ??? ??? ?p1=p1->next; ?? ??? ?} ?? ??? ?printf("file save complete!please enter return!\n"); ?? ??? ?getch(); ?? ? } ?? ?fclose(fp); } /*==============================================================================================*/ /*==================================主菜單===================================================*/ void menu() { ?? ?system("cls"); ?? ?printf("\n\n\n"); ?? ?printf("\t\t-------------STUDENT CONSUME-------------\n"); ?? ?printf("\t\t\t0 ?exit ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\n"); ?? ?printf("\t\t\t1 ?creat ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? \n"); ?? ?printf("\t\t\t2 ?load ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\n"); ?? ?printf("\t\t\t3 ?search ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\n"); ?? ?printf("\t\t\t4 ?delete ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\n"); ?? ?printf("\t\t\t5 ?add ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? \n"); ?? ?printf("\t\t\t6 ?show ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\n"); ?? ?printf("\t\t\t7 ?save ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?\n"); ?? ?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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實現(xiàn)十進制數(shù)轉(zhuǎn)換為二進制數(shù)的數(shù)學算法
這篇文章和大家分享一下我個人對十進制數(shù)轉(zhuǎn)換為二進制數(shù)的想法,目前暫時更新只整數(shù)十進制的轉(zhuǎn)換,后續(xù)會更新帶有小數(shù)的進制轉(zhuǎn)換,代碼使用c++實現(xiàn)2021-09-09Qt基礎(chǔ)開發(fā)之Qt文件操作類QFile讀寫文件的詳細方法與實例及QDataStream的使用方法
這篇文章主要介紹了Qt基礎(chǔ)開發(fā)之Qt文件操作類QFile讀寫文件的詳細方法與實例,需要的朋友可以參考下2020-03-03