C Language to Realize canteen Dining Management System (with linked list)
- 2020-06-19 11:30:09
- OfStack
This article shares the specific code of C language dining management system for your reference. The specific content is as follows
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 20
#define M 20
struct 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 Please enter the operation number you want to select: ");
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(" Typo! \n ");
}
}
}
void menu()
{printf(" **************************** Canteen dining fee management system ************************ ");
printf("\n 1 Add Student information ");
printf("\n 2 Display student information ");
printf("\n 3 Add recipe information ");
printf("\n 4 Add the meal fee according to the student number ");
printf("\n 5 Look up recipes for a particular day ");
printf("\n 6 Delete student information ");
printf("\n 7 Modify student information ");
printf("\n 8 exit ");
}
void reads(struct student stu[N],int *n) // Read the student information file
{
FILE *fp;
int i=0;
if((fp=fopen("studenttt.txt","rb"))==NULL)
{ printf(" File opening failed! \n");
return;
}
else
{
*n=getw(fp);
for(i=0;i<*n;i++)
fread(&stu[i],sizeof(struct student),1,fp);
}
fclose(fp);
}
void addstudent() // Add Student information
{FILE *fp;
int n,i,j;
struct student stu;
if((fp=fopen("studenttt.txt","rb+"))!=NULL) // Read and write
{j=getw(fp);
rewind(fp);
printf(" Please enter the number of students to add: ");
scanf("%d",&n);
putw(j+n,fp);
fseek(fp,0L,2); // Move position pointer
for(i=1;i<=n;i++)
{ printf("\n Please enter the first %d Student id and name of each student: \n",i);
scanf("%s%s",stu.num,stu.name);
printf("\n Please enter the first %d Total meal cost for each student: \n",i);
scanf("%f",&stu.dietfee);
fwrite(&stu,sizeof(struct student),1,fp); // To a file to write 1 Set of data
}
}
else // If the file does not exist, create it 1 , and writes the data to that file
{
if((fp=fopen("studenttt.txt","wb"))!=NULL) // Just write
{
printf(" Please enter the number of students to add: ");
scanf("%d",&n);
putw(n,fp);
for(i=1;i<=n;i++)
{ printf("\n Please enter the first %d Student id and name of each student: \n",i);
scanf("%s%s",stu.num,stu.name);
printf("\n Please enter the first %d Total meal cost for each student: \n",i);
scanf("%f",&stu.dietfee);
fwrite(&stu,sizeof(struct student),1,fp);
}
}
}
fclose(fp);
}
void readss(struct diet die[M],int *t) // Read the recipe file
{
FILE *fp2;
int i=0;
if((fp2=fopen("diet.txt","rb"))==NULL)
{ printf(" File opening failed! \n");
return;
}
else
{
*t=getw(fp2);
for(i=0;i<*t;i++)
fread(&die[i],sizeof(struct diet),1,fp2);
}
fclose(fp2);
}
void adddiet() // Add recipe information
{FILE *fp2;
int n,i,j;
struct diet die;
if((fp2=fopen("diet.txt","rb+"))!=NULL)
{j=getw(fp2);
rewind(fp2);
printf(" Please enter the number of recipes to add: ");
scanf("%d",&n);
putw(j+n,fp2);
fseek(fp2,0L,2);
for(i=1;i<=n;i++)
{ printf("\n Please enter the first %d Date of recipe: \n",i);
scanf("%s",die.date);
printf("\n Please enter the first %d Three recipes for breakfast and fare, lunch and fare, dinner and fare: \n",i);
scanf("%s%s%s",&die.breakfast,&die.lunch,&die.supper);
fwrite(&die,sizeof(struct diet),1,fp2);
}
}
else // If the file does not exist, create it 1 , and writes the data to that file
{
if((fp2=fopen("diet.txt","wb"))!=NULL)
{
printf(" Please enter the number of recipes to add: ");
scanf("%d",&n);
putw(n,fp2);
for(i=1;i<=n;i++)
{ printf("\n Please enter the first %d Date of recipe: \n",i);
scanf("%s",die.date);
printf("\n Please enter the first %d Three recipes for breakfast and fare, lunch and fare, dinner and fare: \n",i);
scanf("%s%s%s",&die.breakfast,&die.lunch,&die.supper);
fwrite(&die,sizeof(struct diet),1,fp2);
}
}
}
fclose(fp2);
}
void show() // Display student information
{struct student stu[N];
int i,n;
reads(stu,&n);
if(n==0)
return;
for(i=0;i<n;i++)
printf("\n Student number: %s The name : %s Bill: %f\n",stu[i].num,stu[i].name,stu[i].dietfee);
}
void addfee() // Add the student meal fee
{struct student stu[N],temp;
int n,i;
float g;
FILE *fp;
printf("\n Please enter the student number to increase the meal fee: ");
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(" No student information is available !\n");
return;
}
printf("\n Please enter the amount to increase the student's meal fee: \n");
scanf("%f",&g);
stu[i].dietfee+=g;
/*strcpy(stu[i].dietfee,temp.dietfee);*/
if((fp=fopen("studenttt.txt","wb"))==NULL)
{printf(" Failed to open file! ");
return;
}
putw(n,fp);
for(i=0;i<n;i++)
fwrite(&stu[i],sizeof(struct student),1,fp);
fclose(fp);
}
void query() // Search for recipe information for a particular day
{
struct diet die[M];
char date[20];
int i,n;
readss(die,&n);
printf("\n Please enter the date of the recipe to be inquired: \n");
scanf("%s",date);
for(i=0;i<n;i++)
if(strcmp(date,die[i].date)==0) break;
if(i>=n)
{printf(" No recipe information for that day found! \n");
return;
}
else
printf("\n Date: %s breakfast %s Lunch: %s Dinner: %s\n",die[i].date,die[i].breakfast,die[i].lunch,die[i].supper);
}
void dele() // Delete student information
{
struct student stu[N];
struct student stu2[N];
int i,n,m,s;
FILE *fp;
int j;
char num2[20];
printf(" Please enter the student id you want to delete: ");
scanf("%s",&num2);
reads(stu,&n);
if(n==0)
{
printf(" No students can delete it! ");
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(" The student does not exist! ");
return;
} else{
if((fp=fopen("studenttt.txt","wb"))!=NULL) // Just write
{
putw(m,fp);
for(j=0;j<m;j++)
{
fwrite(&stu2[j],sizeof(struct student),1,fp);
}
}
fclose(fp);
printf(" Delete successful! \n");
}
}
void midefy(){ // Revise according to student number
struct student stu[N];
struct student stu2[N];
int i,n,m,s;
FILE *fp;
int j;
char num2[20];
printf(" Please enter the student id you want to modify: ");
scanf("%s",&num2);
reads(stu,&n);
if(n==0)
{
printf(" No student can change it! \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 You need to change the student's student number to %s : \n",stu[i].num);
printf("\n Please enter the student's name :");
scanf("%s",stu2[m].name);
printf("\n Please enter the student's total meal fee: \n",i);
scanf("%f",&stu2[m].dietfee);
continue;
}
}
if(s==0){
printf(" The student does not exist! \n");
return;
} else{
if((fp=fopen("studenttt.txt","wb"))!=NULL) // Just write
{
putw(m,fp);
for(j=0;j<m;j++)
{
fwrite(&stu2[j],sizeof(struct student),1,fp);
}
}
fclose(fp);
printf(" Modified successfully! \n");
}
}
Source code download: C language meal management system
For more information, please pay attention to the topic management System Development.