首页 > 网站 > 帮助中心 > 正文

C语言实现食堂就餐管理系统(带链表)

2024-07-09 22:41:57
字体:
来源:转载
供稿:网友

本文实例为大家分享了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");  }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表