您好,登錄后才能下訂單哦!
本篇內容主要講解“怎么用C語言實現食堂就餐管理系統”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么用C語言實現食堂就餐管理系統”吧!
代碼如下:
#include<stdio.h>#include<string.h>#include<stdlib.h>#define N 20#define M 20struct student{char num[20]; char name[10]; float dietfee; };struct diet{char date[20];char breakfast[100];char lunch[100];char supper[100];};void menu();void reads();void readss();void addstudent();void adddiet();void addfee();void query();void show();void dele();void midefy();void main(){ int n; while(1) { menu(); printf("\n 請輸入您要選擇的操作序號:"); scanf("%d",&n); switch(n) {case 1: addstudent();break; case 2: show();break; case 3: adddiet();break; case 4: addfee();break; case 5: query();break; case 6: dele();break; case 7: midefy();break; case 8: exit(0); default: printf("輸入錯誤!\n "); } }}void menu(){printf(" **************************** 食堂就餐餐費管理系統************************ "); printf("\n 1 添加學生信息"); printf("\n 2 顯示學生信息"); printf("\n 3 添加食譜信息"); printf("\n 4 按學號添加餐費"); printf("\n 5 查詢某日食譜"); printf("\n 6 刪除學生信息"); printf("\n 7 修改學生信息"); printf("\n 8 退出");} void reads(struct student stu[N],int *n) //讀取學生信息文件{ FILE *fp; int i=0; if((fp=fopen("studenttt.txt","rb"))==NULL) { printf("文件打開失敗!\n"); return; } else { *n=getw(fp); for(i=0;i<*n;i++) fread(&stu[i],sizeof(struct student),1,fp); } fclose(fp);}void addstudent() //添加學生信息{FILE *fp; int n,i,j; struct student stu; if((fp=fopen("studenttt.txt","rb+"))!=NULL) //讀寫 {j=getw(fp); rewind(fp); printf("請輸入要添加的學生數量:"); scanf("%d",&n); putw(j+n,fp); fseek(fp,0L,2); //移動位置指針 for(i=1;i<=n;i++) { printf("\n請輸入第%d個學生的學號和姓名:\n",i); scanf("%s%s",stu.num,stu.name); printf("\n請輸入第%d個學生的總餐費:\n",i); scanf("%f",&stu.dietfee); fwrite(&stu,sizeof(struct student),1,fp); //向文件寫一組數據 } } else //文件不存在時,新建一個文件,并將數據寫入該文件 { if((fp=fopen("studenttt.txt","wb"))!=NULL) //只寫 { printf("請輸入要添加的學生數量:"); scanf("%d",&n); putw(n,fp); for(i=1;i<=n;i++) { printf("\n請輸入第%d個學生的學號和姓名:\n",i); scanf("%s%s",stu.num,stu.name); printf("\n請輸入第%d個學生的總餐費:\n",i); scanf("%f",&stu.dietfee); fwrite(&stu,sizeof(struct student),1,fp); } } } fclose(fp);}void readss(struct diet die[M],int *t) //讀取食譜文件{ FILE *fp2; int i=0; if((fp2=fopen("diet.txt","rb"))==NULL) { printf("文件打開失敗!\n"); return; } else { *t=getw(fp2); for(i=0;i<*t;i++) fread(&die[i],sizeof(struct diet),1,fp2); } fclose(fp2);}void adddiet() //添加食譜信息{FILE *fp2; int n,i,j; struct diet die; if((fp2=fopen("diet.txt","rb+"))!=NULL) {j=getw(fp2); rewind(fp2); printf("請輸入要添加的食譜數量:"); scanf("%d",&n); putw(j+n,fp2); fseek(fp2,0L,2); for(i=1;i<=n;i++) { printf("\n請輸入第%d個食譜的日期:\n",i); scanf("%s",die.date); printf("\n請輸入第%d個食譜的早餐和費用,午餐和費用,晚餐和費用:\n",i); scanf("%s%s%s",&die.breakfast,&die.lunch,&die.supper); fwrite(&die,sizeof(struct diet),1,fp2); } } else //文件不存在時,新建一個文件,并將數據寫入該文件 { if((fp2=fopen("diet.txt","wb"))!=NULL) { printf("請輸入要添加的食譜數量:"); scanf("%d",&n); putw(n,fp2); for(i=1;i<=n;i++) { printf("\n請輸入第%d個食譜的日期:\n",i); scanf("%s",die.date); printf("\n請輸入第%d個食譜的早餐和費用,午餐和費用,晚餐和費用:\n",i); scanf("%s%s%s",&die.breakfast,&die.lunch,&die.supper); fwrite(&die,sizeof(struct diet),1,fp2); } } } fclose(fp2);}void show() //顯示學生信息{struct student stu[N]; int i,n; reads(stu,&n); if(n==0) return; for(i=0;i<n;i++) printf("\n 學號: %s姓名: %s餐費:%f\n",stu[i].num,stu[i].name,stu[i].dietfee);}void addfee() //添加學生餐費{struct student stu[N],temp; int n,i; float g; FILE *fp; printf("\n請輸入要增加餐費學生學號:"); scanf("%s",temp.num); reads(stu,&n); for(i=0;i<n;i++) if(strcmp(temp.num,stu[i].num)==0) break; if(i>=n) {printf("無此學生信息!\n"); return; } printf("\n請輸入增加該學生的餐費數:\n"); scanf("%f",&g); stu[i].dietfee+=g; /*strcpy(stu[i].dietfee,temp.dietfee);*/ if((fp=fopen("studenttt.txt","wb"))==NULL) {printf("打開文件失敗!"); return; } putw(n,fp); for(i=0;i<n;i++) fwrite(&stu[i],sizeof(struct student),1,fp); fclose(fp);}void query() //查詢某日食譜信息 { struct diet die[M]; char date[20]; int i,n; readss(die,&n); printf("\n請輸入待查詢食譜日期:\n"); scanf("%s",date); for(i=0;i<n;i++) if(strcmp(date,die[i].date)==0) break; if(i>=n) {printf("沒有找到該日食譜信息!\n"); return; } else printf("\n 日期:%s早餐%s:午餐:%s晚餐:%s\n",die[i].date,die[i].breakfast,die[i].lunch,die[i].supper); } void dele() //刪除學生信息{ struct student stu[N]; struct student stu2[N]; int i,n,m,s; FILE *fp; int j; char num2[20]; printf("請輸入要刪除的學生學號:"); scanf("%s",&num2); reads(stu,&n); if(n==0) { printf("沒有學生可以刪除!"); return; } for(m=0,i=0;i<n;i++){ if(strcmp(stu[i].num,num2)!=0) { strcpy(stu2[m].num,stu[i].num); strcpy(stu2[m].name,stu[i].name); stu2[m].dietfee =stu[i].dietfee; s=1; m++; }else{ continue; } } if(s==0){ printf("該學生不存在!"); return; } else{ if((fp=fopen("studenttt.txt","wb"))!=NULL) //只寫 { putw(m,fp); for(j=0;j<m;j++) { fwrite(&stu2[j],sizeof(struct student),1,fp); } } fclose(fp); printf("刪除成功!\n"); }}void midefy(){ //按照學號修改 struct student stu[N]; struct student stu2[N]; int i,n,m,s; FILE *fp; int j; char num2[20]; printf("請輸入要修改的學生學號:"); scanf("%s",&num2); reads(stu,&n); if(n==0) { printf("沒有學生可以修改!\n"); return; } for(m=0,i=0;i<n;i++,m++){ if(strcmp(stu[i].num,num2)!=0) { strcpy(stu2[m].num,stu[i].num); strcpy(stu2[m].name,stu[i].name); stu2[m].dietfee =stu[i].dietfee; }else{ s=1; strcpy(stu2[m].num,stu[i].num); printf("\n你要修改學生的學號為%s:\n",stu[i].num); printf("\n請輸入該學生的姓名:"); scanf("%s",stu2[m].name); printf("\n請輸入該學生的總餐費:\n",i); scanf("%f",&stu2[m].dietfee); continue; } } if(s==0){ printf("該學生不存在!\n"); return; } else{ if((fp=fopen("studenttt.txt","wb"))!=NULL) //只寫 { putw(m,fp); for(j=0;j<m;j++) { fwrite(&stu2[j],sizeof(struct student),1,fp); } } fclose(fp); printf("修改成功!\n"); }}
到此,相信大家對“怎么用C語言實現食堂就餐管理系統”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。