C语言期末作业15个(上)
- 1、C语言学生成绩管理系统
- 2、C语言图书馆管理系统(复杂版)
- 3、C语言图书馆管理系统(简洁版)
- 4、C语言停车管理系统(两个版本)
- 5、C语言学生考勤系统
- 6、C语言班级学生管理系统
- 7、C语言超市收款系统
- 8、C语言超市信息管理系统
- 9、C语言货物管理系统
- 10、C语言歌曲信息管理系统
- 11、C语言职工信息管理系统源码
- 12、C语言学生打卡系统
- 13、C语言小学生计算机辅助教学系统
- 14、C语言门禁系统
- 15、C语言银行管理系统(文件操作)
1、C语言学生成绩管理系统
建议点赞收藏
#include #include #include struct student{int num;char name[20];float pingshi;float shiyan;float kaoshi;double zongping;}stu[4]; void main() {void a();void b();void c();void d();void e();int n;while(n!=6){ printf("\t大学计算机基础成绩管理系统\n");printf("1:输入一个班学生大学计算机基础成绩记录\n");printf("2:显示所有成绩记录\n");printf("3:计算并输出平均成绩,均方差\n");printf("4:输出成绩与排名\n");printf("5:结果存入文件chengji.txt\n");printf("6:退出系统\n");printf("输入选项代码:");scanf("%d",&n);switch(n){case 1:a();break;case 2:b();break;case 3:c();break;case 4:d();break;case 5:e();break;case 6:printf("\n*******************谢谢使用!*******************\n");break;break;}} getchar(); } /* 子 函 数*/ void a() /* 输入一个班的学生记录*/{int i; for(i=0;i<4;i++) { printf("请输入学号 姓名 平时成绩 实验成绩 考试成绩:"); scanf("%d%s%f%f%f",&stu[i].num,stu[i].name,&stu[i].pingshi,&stu[i].shiyan,&stu[i].kaoshi); }for(i=0;i<4;i++) stu[i].zongping=0.1*stu[i].pingshi+0.3*stu[i].shiyan+0.6*stu[i].kaoshi; } void b()/* 显示所有记录*/{int i; printf("学号 姓名 平时成绩 实验成绩 考试成绩 总评成绩\n"); for(i=0;i<4;i++) printf("%d%14.2s%14.2f%14.2f%14.2f%14.2f\n",stu[i].num,stu[i].name,stu[i].pingshi,stu[i].shiyan,stu[i].kaoshi,stu[i].zongping);}void c()/* 求出全班平均成绩,显示均方差*/ {int a[4]={0,1,2,3};int i,j;double total=0,pfc=0,bzc=0;double ave;for(i=0;i<4;i++){total=total+stu[i].zongping;}ave=total/4.0;printf("总评平均成绩是%f\n",ave);for(i=0;i<4;i++){pfc=pow((stu[i].zongping-ave),2)/4;}bzc=sqrt(pfc);printf("\n平方差是%f\n",pfc);printf("\n标准差是%f\n",bzc); } void d(){int a[4]={0,1,2,3};int i,j,temp;for(j=0;j<3;j++){for(i=0;i<3-j;i++)if(stu[a[i]].zongping>stu[a[i+1]].zongping){temp=a[i];a[i]=a[i+1];a[i+1]=temp; }} printf("顺序为:\n"); printf("学号 姓名 总评成绩\n"); for(i=0;i<4;i++) printf("%d%10.2s%15.2f\n",stu[a[i]].num,stu[a[i]].name,stu[a[i]].zongping); printf("\n");}void e() {int i; FILE *fp; fp=fopen("chengji.txt","w"); fprintf(fp,"学号 姓名 平时成绩 实验成绩 考试成绩 总评成绩\n"); for(i=0;i<4;i++) fprintf(fp,"%d%14.2s%14.2f%14.2f%14.2f%14.2f\n",stu[i].num,stu[i].name,stu[i].pingshi,stu[i].shiyan,stu[i].kaoshi,stu[i].zongping);printf("\n\n*******************恭喜,保存完成!*******************\n\n"); }
2、C语言图书馆管理系统(复杂版)
流程图
#include#include int bookNumber = 1;//全局变量 表示图书的序号int usersNumber = 1;//表示读者编号char enter;//接收换行符//添加图书的函数void addBook(FILE *Book){FILE *Book1;//当文件和书本都存在时需要新建文件来更改信息int judgeName = 0;//判断此书是否已经被存放int num = 1, readNum, readNumber;//书本的数量存放读到的书本数量读到的书本序号char name[20] = {0}, readName[20] = {0};//存放图书名字暂时存放读到的字符串名printf("请输入要添加的图书名:");for(int i = 0; i<20; i++)//从输入流取得需要添加的书本名字{if(i==0)scanf("%c%c", &enter,&name[i]);elsescanf("%c",&name[i]);if(name[i]==10)//将最后的换行符改为空{name[i]=0;break;}}if(access("d:/Book.txt",F_OK)==0)//access函数判断文件是否存在,存在返回值为0 使用的头文件为io{judgeName = 1;FILE *judge = fopen("d:/Book.txt", "r");//读取数据文件,查找是否已经存在此书while(!feof(judge)){fscanf(judge, "%d %s %d", &readNumber, readName, &readNum); //由于readName已经表示地址,不需要取地址符fscanf(judge,"\n");//读入换行符,准备下一行的读取if(strcmp(name, readName)==0)//发现已经存在此书后跳出循环{judgeName = 2;//为2时表示已经存在此书,需要新建一个文件来更改信息break;}}fclose(judge);//关闭用于判断而打开的文件bookNumber = readNumber+1;if(judgeName==1)Book = fopen("d:/Book.txt", "a");//文件存在 且不存在相同书名时向后追加内容else if(judgeName==2){Book1 = fopen("d:/Book1.txt", "w");//新建一个文件暂存Book中的信息,最后再返回源文件中Book = fopen("d:/Book.txt","r");//只读打开原本的文件}}elseBook = fopen("d:/Book.txt","w");//文件不存在时新建一个文件if (judgeName<2){fprintf(Book, "%d %s %d\n", bookNumber, name, num);//写入序号 书名 数量和换行符bookNumber++; fclose(Book);}else{while(!feof(Book)){fscanf(Book, "%d %s %d\n", &readNumber, readName, &readNum);if(strcmp(name, readName)==0)fprintf(Book1, "%d %s %d\n", readNumber, readName, readNum+1);//写入序号 书名 数量+1和换行符elsefprintf(Book1, "%d %s %d\n", readNumber, readName, readNum);//写入序号 书名 数量和换行符}bookNumber++;fclose(Book);//关闭文件fclose(Book1);Book1 = fopen("d:/Book1.txt", "r");Book = fopen("d:/Book.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Book1)){fscanf(Book1, "%d %s %d\n", &readNumber, readName, &readNum);fprintf(Book, "%d %s %d\n", readNumber, readName, readNum);}fclose(Book);fclose(Book1);remove("d:/Book1.txt");//删除原文件//rename("d:/Book1.txt", "d:/Book.txt");//将更改后的文件改名为原数据文件}printf("添加成功!\n");}//修改图书信息函数void changeBook(FILE *Book){while(1){char name[20] = {0}, readName[20] = {0};printf(" Edit Book Information\n");printf("[------------------------------------------]\n");printf("\t请选择需要修改的信息:\n1.修改图书名称\n2.修改图书数量\n3.返回主菜单\n");printf("[------------------------------------------]\n");int function, readNumber, readNum, find = 0;//功能选择 读取到的序号 读取到的数量 是否找到此书//判断修改后的书名是否和原有的重复Book = fopen("d:/Book.txt","r");//只读打开原文件int ifNumber, ifNum;char ifName[20] = {0};FILE *ifUserExisted = fopen("d:/Users1.txt","w");while(!feof(Book)){fscanf(Book, "%d %s %d\n", &ifNumber, ifName, &ifNum);fprintf(ifUserExisted, "%d %s %d\n", &ifNumber, ifName, ifNum);}fclose(ifUserExisted);fclose(Book); scanf("%d",&function);if(function == 3){remove("d:/Book1.txt");//有时会多余创造了文件,需要删除 return;}Book = fopen("d:/Book.txt","r");//只读打开原文件FILE *Book1 = fopen("d:/Book1.txt", "w");//先新建一个文件存储更改后的信息printf("请输入需要更改的图书名:");for(int i = 0; i<20; i++)//从输入流取得需要添加的书本名字{if(i==0)scanf("%c%c", &enter,&name[i]);elsescanf("%c",&name[i]);if(name[i]==10)//将最后的换行符改为空{name[i]=0;break;}}if(function==1){while(!feof(Book)){fscanf(Book, "%d %s %d\n", &readNumber, readName, &readNum);if(strcmp(name, readName)==0){find = 1;//找到了此书printf("您要将书名%s更改为:", name);char changeName[20] = {0};for(int i = 0; i<20; i++)//从输入流取得需要更改的书本名字{if(i==0)scanf("%c",&changeName[i]);elsescanf("%c",&changeName[i]);if(changeName[i]==10)//将最后的换行符改为空{changeName[i]=0;break;}}ifUserExisted = fopen("d:/Users1.txt","r"); //判断更改后的名字是否会和已存在的书名冲突while(!feof(ifUserExisted)){fscanf(ifUserExisted, "%d %s %d\n", &ifNumber, ifName, &ifNum);if (strcmp(changeName, ifName)==0){printf("更改失败:已存在名称为%s的图书\n", changeName);fclose(ifUserExisted);fclose(Book);//关闭文件fclose(Book1);return;}}fprintf(Book1, "%d %s %d\n", readNumber, changeName, readNum);//写入序号 更改后的书名 数量和换行符}elsefprintf(Book1, "%d %s %d\n", readNumber, readName, readNum);//写入序号 书名 数量和换行符}if(find==0)//若没找到此书{printf("没有找到此书!请重新输入\n");break;}fclose(Book);//关闭文件fclose(Book1);Book1 = fopen("d:/Book1.txt", "r");Book = fopen("d:/Book.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Book1)){fscanf(Book1, "%d %s %d\n", &readNumber, readName, &readNum);fprintf(Book, "%d %s %d\n", readNumber, readName, readNum);}fclose(Book);fclose(Book1);remove("d:/Book1.txt");//删除原文件//rename("d:/Book1.txt", "d:/Book.txt");//将更改后的文件改名为原数据文件此方法成功率低!!!printf("更改成功!\n");}else if(function == 2){while(!feof(Book)){fscanf(Book, "%d %s %d\n", &readNumber, readName, &readNum);if(strcmp(name, readName)==0){find = 1;//找到了此书printf("您要将数量%d更改为:", readNum);int newNum = 0;scanf("%d",&newNum);fprintf(Book1, "%d %s %d\n", readNumber, readName, newNum);//写入序号 书名 更改后的数量和换行符}elsefprintf(Book1, "%d %s %d\n", readNumber, readName, readNum);//写入序号 书名 数量和换行符}if(find==0)//若没找到此书{printf("没有找到此书!请重新输入");break;}fclose(Book);//关闭文件fclose(Book1);Book1 = fopen("d:/Book1.txt", "r");Book = fopen("d:/Book.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Book1)){fscanf(Book1, "%d %s %d\n", &readNumber, readName, &readNum);fprintf(Book, "%d %s %d\n", readNumber, readName, readNum);}fclose(Book);fclose(Book1);remove("d:/Book1.txt");//删除原文件//rename("d:/Book1.txt", "d:/Book.txt");//将更改后的文件改名为原数据文件此方法易出现问题!!!printf("更改成功!\n");}else{printf("输入格式有误,请重新输入!");break;}}}//删除书籍的函数void delBook(FILE *Book){while(1){printf("请输入您想删除的图书名:(输入\"-r\"返回上一级)");char delName[20] = {0};for(int i = 0; i<20; i++)//从输入流取得需要删除的书本名字{//if(i==0)//scanf("%c%c", &enter,&delName[i]);//elsescanf("%c",&delName[i]);if(delName[i]==10)//将最后的换行符改为空{delName[i]=0;break;}}if(strcmp(delName, "-r")==0)//输入返回上一级的指示后返回return;int readNumber, readNum, find = 0;char readName[20] = {0};Book = fopen("d:/Book.txt","r");FILE *Book1 = fopen("d:/Book1.txt","w");while (!feof(Book)){fscanf(Book, "%d %s %d\n", &readNumber, readName, &readNum);if(strcmp(delName, readName)==0){find = 1;//找到了此书//因为要删除此条信息,所以不再向新文件中输出它}else{if(find==1)fprintf(Book1, "%d %s %d\n", readNumber-1, readName, readNum);//排在删除后的写入-1的序号 书名 数量和换行符elsefprintf(Book1, "%d %s %d\n", readNumber, readName, readNum);//删除前的写入的序号 书名 数量和换行符}}if(find==0)//若没找到此书{printf("没有找到此书!请重新输入\n");continue;}fclose(Book);fclose(Book1);Book1 = fopen("d:/Book1.txt", "r");Book = fopen("d:/Book.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Book1)){fscanf(Book1, "%d %s %d\n", &readNumber, readName, &readNum);fprintf(Book, "%d %s %d\n", readNumber, readName, readNum);}fclose(Book);fclose(Book1);remove("d:/Book1.txt");//删除原文件printf("删除成功!\n");}}//展示全部图书的函数void showBook(FILE *Book){printf("Library \n");printf("[------------------------------------------]\n");printf("\t序号\t书名\t\t数量\n");char readName[20] = {0};int readNumber, readNum;Book = fopen("d:/Book.txt", "r");while(!feof(Book)){fscanf(Book, "%d %s %d\n", &readNumber, readName, &readNum);printf("\t%d\t%s\t\t%d\n",readNumber, readName, readNum); }fclose(Book);printf("[------------------------------------------]\n");}//向管理员展示全部用户的函数void showUsers(FILE *Users){printf(" Users\n");printf("[------------------------------------------]\n");printf("\t序号\t姓名\t所借阅图书\n");char readName[20] = {0}, borrowBook[20] = {0};int readNumber;Users = fopen("d:/Users.txt", "r");while(!feof(Users)){fscanf(Users, "%d %s %s\n", &readNumber, readName, borrowBook);printf("\t%d\t%s\t%s\n",readNumber, readName, borrowBook); }fclose(Users);printf("[------------------------------------------]\n");}//添加用户函数 void addUsers(FILE *Users){FILE *Users1;//当文件和用户都存在时需要新建文件来更改信息int judgeName = 0;//判断此用户是否已经存在int num = 1, readNumber;//存放读到的书本数量读到的用户序号char name[20] = {0}, readName[20] = {0}, borrowBook[20] = {"None"},readBorrowBook[20] = {"None"};//存放用户名字暂时存放读到的字符串名printf("请输入要添加的用户名:");for(int i = 0; i<20; i++)//从输入流取得需要添加的用户名{if(i==0)scanf("%c%c", &enter,&name[i]);elsescanf("%c",&name[i]);if(name[i]==10)//将最后的换行符改为空{name[i]=0;break;}}if(access("d:/Users.txt",F_OK)==0)//access函数判断文件是否存在,存在返回值为0 使用的头文件为io{ judgeName = 1;FILE *judge = fopen("d:/Users.txt", "r");//读取数据文件,查找是否已经存在此用户while(!feof(judge)){fscanf(judge, "%d %s %s", &readNumber, readName, readBorrowBook); //由于readName已经表示地址,不需要取地址符fscanf(judge,"\n");//读入换行符,准备下一行的读取if(strcmp(name, readName)==0)//发现已经存在此用户后跳出循环{printf("已经存在此用户,无法添加。");//已经存在此用户,返回return;}}fclose(judge);//关闭用于判断而打开的文件usersNumber = readNumber+1;if(judgeName==1)Users = fopen("d:/Users.txt", "a");//文件存在 且不存在相同用户时向后追加内容}elseUsers = fopen("d:/Users.txt","w");//文件不存在时新建一个文件fprintf(Users, "%d %s %s\n", usersNumber, name, borrowBook);//写入序号 用户名 所借书和换行符usersNumber++; fclose(Users);printf("添加成功!\n");}//修改读者信息函数void changeUsers(FILE *Users){while(1){char name[20] = {0}, readName[20] = {0}, borrowBook[20] = {"None"};printf("Edit Reader Name\n");printf("[------------------------------------------]\n");printf("\t请选择需要修改的信息:\n1.修改读者名称\n2.返回主菜单\n");printf("[------------------------------------------]\n");int function, readNumber, find = 0;//功能选择 读取到的序号是否找到此书Users = fopen("d:/Users.txt","r");//只读打开原文件//此处创建ifUserExisted,一会用于查找是否存在相同的名字int ifNumber;char ifName[20] = {0}, ifBorrow[20] = {0};FILE *ifUserExisted = fopen("d:/Book1.txt","w");while(!feof(Users)){fscanf(Users, "%d %s %s\n", &ifNumber, ifName, ifBorrow);fprintf(ifUserExisted, "%d %s %s\n", &ifNumber, ifName, ifBorrow);}fclose(ifUserExisted);fclose(Users); Users = fopen("d:/Users.txt","r");//只读打开原文件scanf("%d",&function);if(function == 2){remove("d:/Users1.txt");//有时会多余创造了文件,需要删除 return;}FILE *Users1 = fopen("d:/Users1.txt", "w");//先新建一个文件存储更改后的信息printf("请输入需要更改的用户名:");for(int i = 0; i<20; i++)//从输入流取得需要更改的读者名字{if(i==0)scanf("%c%c", &enter,&name[i]);elsescanf("%c",&name[i]);if(name[i]==10)//将最后的换行符改为空{name[i]=0;break;}}if(function==1){while(!feof(Users)){fscanf(Users, "%d %s %s\n", &readNumber, readName, borrowBook);if(strcmp(name, readName)==0){find = 1;//找到了此读者printf("您要将读者名\"%s\"更改为:", name);char changeName[20] = {0};for(int i = 0; i<20; i++)//从输入流取得需要更改的用户名字{if(i==0)scanf("%c",&changeName[i]);elsescanf("%c",&changeName[i]);if(changeName[i]==10)//将最后的换行符改为空{changeName[i]=0;break;}}ifUserExisted = fopen("d:/Book1.txt","r"); //判断更改后的名字是否会和存在的用户名冲突while(!feof(ifUserExisted)){fscanf(ifUserExisted, "%d %s %s\n", &ifNumber, ifName, ifBorrow);if (strcmp(changeName, ifName)==0){printf("更改失败:已存在名称为%s的读者\n", changeName);fclose(ifUserExisted);fclose(Users);//关闭文件fclose(Users1);return;}}fprintf(Users1, "%d %s %s\n", readNumber, changeName, borrowBook);//写入序号 更改后的读者名 所借书籍和换行符}elsefprintf(Users1, "%d %s %s\n", readNumber, readName, borrowBook);//写入序号 书名 数量和换行符}if(find==0)//若没找到此读者{printf("没有找到此读者!请重新输入\n");break;}fclose(Users);//关闭文件fclose(Users1);Users1 = fopen("d:/Users1.txt", "r");Users = fopen("d:/Users.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Users1)){fscanf(Users1, "%d %s %s\n", &readNumber, readName, borrowBook);fprintf(Users, "%d %s %s\n", readNumber, readName, borrowBook);}fclose(Users);fclose(Users1);remove("d:/Users1.txt");//删除原文件//rename("d:/Book1.txt", "d:/Book.txt");//将更改后的文件改名为原数据文件此方法成功率低!!!printf("更改成功!\n");}else{printf("输入格式有误,请重新输入!\n");break;}}}//删除用户的函数void delUsers(FILE *Users){while(1){printf("请输入您想删除的读者名:(输入\"-r\"返回上一级)");char delName[20] = {0};for(int i = 0; i<20; i++)//从输入流取得需要删除的用户名字{//if(i==0)//scanf("%c%c", &enter,&delName[i]);//elsescanf("%c",&delName[i]);if(delName[i]==10)//将最后的换行符改为空{delName[i]=0;break;}}if(strcmp(delName, "-r")==0)//输入返回上一级的指示后返回return;int readNumber, find = 0;char readName[20] = {0}, borrowBook[20] = {"None"};Users = fopen("d:/Users.txt","r");FILE *Users1 = fopen("d:/Users1.txt","w");while (!feof(Users)){fscanf(Users, "%d %s %s\n", &readNumber, readName, borrowBook);if(strcmp(delName, readName)==0){find = 1;//找到了此读者//因为要删除此条信息,所以不再向新文件中输出它}else{if(find==1)fprintf(Users1, "%d %s %s\n", readNumber-1, readName, borrowBook);//排在删除后的写入-1的序号 读者名 所借书和换行符elsefprintf(Users1, "%d %s %s\n", readNumber, readName, borrowBook);//删除前的写入的序号 读者名 所借书和换行符}}if(find==0)//若没找到此读者{printf("没有找到此读者!请重新输入\n");continue;}fclose(Users1);fclose(Users);Users1 = fopen("d:/Users1.txt", "r");Users = fopen("d:/Users.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Users1)){fscanf(Users1, "%d %s %s\n", &readNumber, readName, borrowBook);fprintf(Users, "%d %s %s\n", readNumber, readName, borrowBook);}fclose(Users1);fclose(Users);remove("d:/Users1.txt");//删除原文件printf("删除成功!\n");}}//借阅函数void borrowBooking(char readerName[]){printf("请输入您想借阅的图书名:");char name[20] = {0};for(int i = 0; i<20; i++)//从输入流取得图书名{if(i==0)scanf("%c%c", &enter,&name[i]);elsescanf("%c",&name[i]);if(name[i]==10)//将最后的换行符改为空{name[i]=0;break;}}char readName[20] = {0};int readNumber, readNum, find = 0;//更改被借阅的图书的信息FILE *Book = fopen("d:/Book.txt", "r");FILE *Book1 = fopen("d:/Book1.txt", "w");while (!feof(Book)){fscanf(Book, "%d %s %d\n", &readNumber, readName, &readNum);if(strcmp(name, readName)==0){find = 1;if(readNum<1){printf("借阅失败:无剩余可借阅的%s!\n", readName);return;}fprintf(Book1, "%d %s %d\n", readNumber, readName, readNum-1); //若找到需要借阅的图书,则在此书数量上减1 } elsefprintf(Book1, "%d %s %d\n", readNumber, readName, readNum);//删除前的写入的序号 读者名 所借书和换行符 }if(find==0)//若没找到此书{printf("借阅失败:没有找到此书!\n");return;}fclose(Book);fclose(Book1);Book1 = fopen("d:/Book1.txt", "r");Book = fopen("d:/Book.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Book1)){fscanf(Book1, "%d %s %d\n", &readNumber, readName, &readNum);fprintf(Book, "%d %s %d\n", readNumber, readName, readNum);}fclose(Book1);fclose(Book);remove("d:/Book1.txt");//删除原文件 //更改读者的信息FILE *Users = fopen("d:/Users.txt", "r");FILE *Users1 = fopen("d:/Users1.txt", "w");char borrowBook[20] = {"None"};while (!feof(Users)){fscanf(Users, "%d %s %s\n", &readNumber, readName, borrowBook);if(strcmp(readerName, readName)==0)fprintf(Users1, "%d %s %s\n", readNumber, readName, name); //在读者的借阅图书上写上所借阅的图书名 elsefprintf(Users1, "%d %s %s\n", readNumber, readName, borrowBook); }fclose(Users1);fclose(Users);Users1 = fopen("d:/Users1.txt", "r");Users = fopen("d:/Users.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Users1)){fscanf(Users1, "%d %s %s\n", &readNumber, readName, borrowBook);fprintf(Users, "%d %s %s\n", readNumber, readName, borrowBook);}fclose(Users1);fclose(Users);remove("d:/Users1.txt");//删除原文件有时候还是删不掉printf("借阅成功!\n"); }//还书函数void returnBooking(char readerName[], char borrowBook[]){char readName[20] = {0};int readNumber, readNum, find = 0;//更改被还回的图书的信息FILE *Book = fopen("d:/Book.txt", "r");FILE *Book1 = fopen("d:/Book1.txt", "w");while (!feof(Book)){fscanf(Book, "%d %s %d\n", &readNumber, readName, &readNum);if(strcmp(borrowBook, readName)==0){fprintf(Book1, "%d %s %d\n", readNumber, readName, readNum+1); //若找到被还的图书,则在此书数量上加1find = 1;}elsefprintf(Book1, "%d %s %d\n", readNumber, readName, readNum); //不是还回的书 }fclose(Book);fclose(Book1);Book1 = fopen("d:/Book1.txt", "r");Book = fopen("d:/Book.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Book1)){fscanf(Book1, "%d %s %d\n", &readNumber, readName, &readNum);fprintf(Book, "%d %s %d\n", readNumber, readName, readNum);}bookNumber = readNumber+1;fclose(Book1);fclose(Book);remove("d:/Book1.txt");//删除原文件if(find==0)//还书时没有书城中没有此书,直接添加一本{FILE *Book;Book = fopen("d:/Book.txt", "a");fprintf(Book, "%d %s 1\n", bookNumber, borrowBook);//写入序号 书名 数量和换行符bookNumber++; fclose(Book);}//更改读者的信息FILE *Users = fopen("d:/Users.txt", "r");FILE *Users1 = fopen("d:/Users1.txt", "w");char rborrowBook[20] = {"None"};while (!feof(Users)){fscanf(Users, "%d %s %s\n", &readNumber, readName, rborrowBook);if(strcmp(readerName, readName)==0)fprintf(Users1, "%d %s %s\n", readNumber, readName, "None"); //还回书后所借阅图书变为"None" elsefprintf(Users1, "%d %s %s\n", readNumber, readName, rborrowBook); }fclose(Users1);fclose(Users);Users1 = fopen("d:/Users1.txt", "r");Users = fopen("d:/Users.txt", "w+"); //刷新原文件,再次写入正确数据while(!feof(Users1)){fscanf(Users1, "%d %s %s\n", &readNumber, readName, rborrowBook);fprintf(Users, "%d %s %s\n", readNumber, readName, rborrowBook);}fclose(Users1);fclose(Users);remove("d:/Users1.txt");//删除原文件有时候还是删不掉printf("成功还回%s\n", borrowBook);}//单独查询功能,根据编号或书名查询书被哪些人借走void searchBook(){while (1){int function = 0;FILE *Book = fopen("d:/Book.txt", "r");FILE *Users = fopen("d:/Users.txt", "r");char readerName[20] = {0}, bookName[20] = {0}, borrowBook[20] = {0}, name[20] = {0};int bookNumber, bookNum, find = 0, readerNumber;printf(" Search \n");printf("[------------------------------------------]\n");printf("\t\t请选择查询功能:\n\t1.根据编号查询\n\t2.根据书名查询\n\t3.返回主界面\n");printf("[------------------------------------------]\n");scanf("%d",&function); // 输入需要的功能switch (function){case 1:printf("您要查找的书序号是:");scanf("%d",&function); // 输入需要查找的图书编号while(!feof(Book)){fscanf(Book, "%d %s %d\n", &bookNumber, bookName, &bookNum); //由于bookName已经表示地址,不需要取地址符if(bookNumber==function)//发现查找的书后跳出循环{find = 1;//找到书break;}}if(find==0){printf("没有找到编号为%d的图书。\n",function);break;}printf(" Search Result\n");printf("[------------------------------------------]\n");find = 0;//需要在查找谁借阅时再次使用printf("\t编号:\t\t%d\n\t书名:\t\t%s\n\t剩余数量:\t%d\n\t",bookNumber, bookName, bookNum);while (!feof(Users)){fscanf(Users, "%d %s %s\n", &readerNumber, readerName, borrowBook);//遍历读者查找谁借阅了这本书 if (strcmp(borrowBook, bookName)==0){if(find==0){find = 1;printf("借阅此书的读者:");printf("%s",readerName);}elseprintf(" ,%s",readerName);}}if(find==0)printf("还没有读者借阅此书。\n");elseprintf("\n");printf("[------------------------------------------]\n");printf("输入回车返回...\n");enter = getchar();enter = getchar();break;case 2:printf("您要查找的书名是:");scanf("%s",name); // 输入需要查找的图书名while(!feof(Book)){fscanf(Book, "%d %s %d\n", &bookNumber, bookName, &bookNum); //由于bookName已经表示地址,不需要取地址符if(strcmp(bookName, name)==0)//发现查找的书后跳出循环{find = 1;//找到书break;}}if(find==0){printf("没有找到名称为%s的图书。\n",name);break;}printf(" Search Result\n");printf("[------------------------------------------]\n");find = 0;//需要在查找谁借阅时再次使用printf("\t编号:\t\t%d\n\t书名:\t\t%s\n\t剩余数量:\t%d\n\t",bookNumber, bookName, bookNum);while (!feof(Users)){fscanf(Users, "%d %s %s\n", &readerNumber, readerName, borrowBook);//遍历读者查找谁借阅了这本书 if (strcmp(borrowBook, bookName)==0){if(find==0){find = 1;printf("借阅此书的读者:");printf("%s",readerName);}elseprintf(" ,%s",readerName);}}if(find==0)printf("还没有读者借阅此书。\n");elseprintf("\n");printf("[------------------------------------------]\n");printf("输入回车返回...\n");enter = getchar();enter = getchar();break;case 3:return;default:printf("您输入的格式有误,请重新输入\n");break;}fclose(Book);fclose(Users);}}int main(int argc, char *argv[])//需要从命令行读入参数{char identity1[] = "-u";//读者身份char identity2[] = "-a";//管理员身份FILE *Book, *Users;//两个文件分别存储图书信息和读者信息。int function1, function2; //功能选项/*调试用char readerName[20] = {0};int test = 0;scanf("%d %s",&test, readerName);if(test==1)*/if(strcmp(argv[1], identity2)==0)//管理员操作界面{char Administrator[20] = "Zhang";if(strcmp(Administrator, argv[2])==0){while(1){ printf("menu\n");printf("[------------------------------------------]\n");printf(" 管理员您好!请选择您要使用的功能:\n1.新增或修改图书信息\n2.新增或修改读者信息\n3.查看全部馆藏书籍\n4.查看全部读者及其借阅信息\n5.图书详情查询\n6.结束程序\n");printf("[------------------------------------------]\n");scanf("%d",&function1);switch (function1){case 1://更改图书信息printf("Books \n");printf("[------------------------------------------]\n");printf("\t请选择要对图书进行的操作:\n1.添加图书信息\n2.修改图书信息\n3.删除图书信息\n4.返回主菜单\n");printf("[------------------------------------------]\n");scanf("%d",&function2);//输入第二级操作指令switch (function2){case 1:addBook(Book);//调用添加图书函数break;case 2:changeBook(Book);//修改函数break;case 3:scanf("%c",&enter);delBook(Book);//删除函数break;case 4:break;default:printf("输入的值令有误,请重新输入");break;}break;case 2://更改读者信息printf("Readers \n");printf("[------------------------------------------]\n");printf("\t请选择要对用户进行的操作:\n1.添加用户信息\n2.修改用户信息\n3.删除用户信息\n");printf("[------------------------------------------]\n");scanf("%d",&function2);//输入第二级操作指令switch (function2){case 1:addUsers(Users);//添加用户信息break;case 2:changeUsers(Users);//修改用户信息break;case 3:scanf("%c",&enter);delUsers(Users);//删除用户信息break;default:printf("输入的值令有误,请重新输入");break;}break;case 3:showBook(Book);printf("输入回车返回...\n");enter = getchar();enter = getchar();break;case 4:showUsers(Users);printf("输入回车返回...\n");enter = getchar();enter = getchar();break;case 5:searchBook();break;case 6:printf("感谢您的使用,再见!");return 0;default:printf("输入格式有误,请重新输入!");break;}}}elseprintf("管理员名有误,请重新输入!");}/*else if(test == 2)*/ else if (strcmp(argv[1], identity1)==0)//读者的操作界面{char readName[20] = {0}, borrowBook[20] = {"None"};int readNumber,find = 0;FILE *Users = fopen("d:/Users.txt","r");//需要判断是否存在此用户while (!feof(Users)){fscanf(Users, "%d %s %s\n", &readNumber, readName, borrowBook);if(strcmp(argv[2], readName)==0){ find = 1;break;}}fclose(Users);if(find==0){printf("不存在用户名为%s的读者!", argv[2]);return 0;}while (1){FILE *Users = fopen("d:/Users.txt","r");//刷新用户的借阅信息while (!feof(Users)){fscanf(Users, "%d %s %s\n", &readNumber, readName, borrowBook);if(strcmp(argv[2], readName)==0)break;}fclose(Users);printf("menu\n");printf("[------------------------------------------]\n");printf("\t读者%s您好!请选择您要使用的功能:\n1.借阅图书\n2.还回图书\n3.查看全部馆藏书籍\n4.结束程序\n", argv[2]);printf("[------------------------------------------]\n");scanf("%d",&function1);switch (function1){case 1://借阅图书if(strcmp(borrowBook, "None")==0)borrowBooking(argv[2]);elseprintf("您已经借阅图书,请先还书再进行借阅操作。\n");printf("输入回车返回...\n");enter = getchar();enter = getchar();break;case 2://还书if(strcmp(borrowBook, "None")!=0)returnBooking(argv[2], borrowBook);elseprintf("您当前还没有借阅图书。\n");printf("输入回车返回...\n");enter = getchar();enter = getchar();break;case 3:showBook(Book);printf("输入回车返回...\n");enter = getchar();enter = getchar();break;case 4:printf("感谢您的使用,再见!");return 0;default:printf("输入格式有误,请重新输入!");break;}}}elseprintf("您输入的格式有误,请重新输入!");return 0;}
3、C语言图书馆管理系统(简洁版)
#include #include #include #include struct books_list { char author[20]; /*作者名*/char bookname[20]; /*书名*/char publisher[20]; /*出版单位*/char pbtime[15]; /*出版时间*/char loginnum[10]; /*登陆号*/float price;/*价格*/char classfy[10]; /*分类号*/struct books_list * next; /*链表的指针域*/ };struct books_list * Create_Books_Doc(); /*新建链表*/ void InsertDoc(struct books_list * head); /*插入*/ void DeleteDoc(struct books_list * head , int num);/*删除*/ void Print_Book_Doc(struct books_list * head);/*浏览*/ void search_book(struct books_list * head); /*查询*/ void save(struct books_list * head);/*保存数据至文件*//*新建链表头节点*/ struct books_list * Create_Books_Doc() {struct books_list * head;head=(struct books_list *)malloc(sizeof(struct books_list)); /*分配头节点空间*/head->next=NULL; /*头节点指针域初始化,定为空*/return head; }/*保存数据至文件*/ void save(struct books_list * head) {struct books_list *p;FILE *fp;p=head;fp=fopen("data.txt","w+"); /*以写方式新建并打开 data.txt文件*/fprintf(fp,"┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n"); /*向文件输出表格*/fprintf(fp,"┃登录号┃ 书 名 ┃ 作 者┃ 出版单位 ┃ 出版时间 ┃分类号┃ 价格 ┃\n");fprintf(fp,"┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n");/*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/while(p->next!= NULL){p=p->next;fprintf(fp,"┃%-6.6s┃%-10.10s┃%-10.10s┃%-10.10s┃%-12.12s┃%-6.6s┃%.2f ┃\n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p->price);}fprintf(fp,"┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n");fclose(fp);printf("已将图书数据保存到 data.txt 文件\n"); }/*插入*/ void InsertDoc(struct books_list *head) {/*定义结构体指针变量 s指向开辟的新结点首地址 p为中间变量*/struct books_list *s, *p;char flag='Y'; /*定义flag,方便用户选择重复输入*/p=head;/*遍历到尾结点,p指向尾结点*/while(p->next!= NULL){p=p->next;}/*开辟新空间,存入数据,添加进链表*/while(flag=='Y'||flag=='y'){s=(struct books_list *)malloc(sizeof(struct books_list));printf("\n请输入图书登陆号:");fflush(stdin);scanf("%s",s->loginnum);printf("\n请输入图书书名:");fflush(stdin);scanf("%s",s->bookname);printf("\n请输入图书作者名:");fflush(stdin);scanf("%s",s->author);printf("\n请输入图书出版社:");fflush(stdin);scanf("%s",s->publisher);printf("\n请输入图书出版时间:");fflush(stdin);scanf("%s",s->pbtime);printf("\n请输入图书分类号:");fflush(stdin);scanf("%s",s->classfy);printf("\n请输入图书价格:");fflush(stdin);scanf("%f",&s->price);printf("\n");p->next=s; /*将新增加的节点添加进链表*/p=s; /*p指向尾节点,向后移*/s->next=NULL;printf("━━━━ 添加成功!━━━━");printf("\n继续添加?(Y/N):");fflush(stdin);scanf("%c",&flag);printf("\n");if(flag=='N'||flag=='n'){break;}else if(flag=='Y'||flag=='y'){continue;}}save(head); /*保存数据至文件*/return; }/*查询操作*/void search_book(struct books_list *head) {struct books_list * p;char temp[20];p=head;if(head==NULL || head->next==NULL) /*判断数据库是否为空*/{printf(" ━━━━ 图书库为空!━━━━\n");}else{printf("请输入您要查找的书名: ");fflush(stdin);scanf("%s",temp);/*指针从头节点开始移动,遍历至尾结点,查找书目信息*/while(p->next!= NULL){p=p->next;if(strcmp(p->bookname,temp)==0){printf("\n图书已找到!\n");printf("\n");printf("登录号: %s\t\n",p->loginnum);printf("书名: %s\t\n",p->bookname);printf("作者名: %s\t\n",p->author);printf("出版单位: %s\t\n",p->publisher);printf("出版时间: %s\t\n",p->pbtime);printf("分类号: %s\t\n",p->classfy);printf("价格: %.2f\t\n",p->price);}if(p->next==NULL){printf("\n查询完毕!\n");}}}return; } /*浏览操作*/void Print_Book_Doc(struct books_list * head) {struct books_list * p;if(head==NULL || head->next==NULL) /*判断数据库是否为空*/{printf("\n━━━━ 没有图书记录! ━━━━\n\n");return;}p=head;printf("┏━━━┳━━━━━┳━━━━━┳━━━━━┳━━━━━━┳━━━┳━━━━┓\n");printf("┃登录号┃ 书 名 ┃ 作 者┃ 出版单位 ┃ 出版时间 ┃分类号┃ 价格 ┃\n");printf("┣━━━╋━━━━━╋━━━━━╋━━━━━╋━━━━━━╋━━━╋━━━━┫\n");/*指针从头节点开始移动,遍历至尾结点,依次输出图书信息*/while(p->next!= NULL){p=p->next;printf("┃%-6.6s┃%-10.10s┃%-10.10s┃%-10.10s┃%-12.12s┃%-6.6s┃%.2f ┃\n",p->loginnum,p->bookname,p->author,p->publisher,p->pbtime,p->classfy,p->price); /*循环输出表格*/}printf("┗━━━┻━━━━━┻━━━━━┻━━━━━┻━━━━━━┻━━━┻━━━━┛\n");printf("\n"); }/*删除操作*/ void DeleteDoc(struct books_list * head) {struct books_list *s,*p; /*s为中间变量,p为遍历时使用的指针*/char temp[20];int panduan; /*此变量用于判断是否找到了书目*/panduan=0;p=s=head;printf("[请输入您要删除的书名]:");scanf("%s",temp);/*遍历到尾结点*/while(p!= NULL){if(strcmp(p->bookname,temp)==0){panduan++;break;}p=p->next;}if(panduan==1){for(;s->next!=p;) /*找到所需删除卡号结点的上一个结点*/{s=s->next;}s->next=p->next; /*将后一节点地址赋值给前一节点的指针域*/free(p);printf("\n━━━━ 删除成功! ━━━━\n");}else /*未找到相应书目*/{printf("您输入的书目不存在,请确认后输入!\n");}return; }int main() {struct books_list * head;char choice;head=NULL;for(;;) /*实现反复输入选择*/{printf(" ┏━┓━━━━━━━━━━━━━━━━━━━┏━┓\n");printf(" ┃ ┃ tony 图书馆管理系统 ┃ ┃\n");printf(" ┃ ┗━━━━━━━━━━━━━━━━━━━┛ ┃\n");printf(" ┃ [1]图书信息录入 ┃\n");printf(" ┃ ┃\n");printf(" ┃ [2]图书信息浏览 ┃\n");printf(" ┃ ┃\n");printf(" ┃ [3]图书信息查询 ┃\n");printf(" ┃ ┃\n");printf(" ┃ [4]图书信息删除 ┃\n");printf(" ┃ ┃\n");printf(" ┃ [5]退出系统┃\n");printf(" ┃ ┃\n");printf(" ┃ ┃\n");printf(" ┗━━━━━━━━━━━━━━━━━━━━━━━┛\n");printf("请选择:");fflush(stdin);scanf("%c",&choice);if(choice=='1'){if(head==NULL){head=Create_Books_Doc();}InsertDoc(head); }else if(choice=='2'){Print_Book_Doc(head);}else if(choice=='3'){search_book(head);}else if(choice=='4'){DeleteDoc(head);} else if(choice=='5'){printf("\n");printf(" ━━━━━━━━ 感谢使用图书管理系统 ━━━━━━━━\n");break;}else{printf("━━━━ 输入错误,请重新输入!━━━━\n"); }} return 0; }
4、C语言停车管理系统(两个版本)
#include "stdio.h" #include "stdlib.h" #include "string.h" #define MAX 2//车库容量#define price 0.05//每车每分钟费用 typedef struct time //时间结点 {int hour;int min;}Time; typedef struct node //车辆信息结点{char num[10];Time reach;Time leave;}CarNode; typedef struct NODE//模拟车站 {CarNode *stack[MAX+1];int top;}SeqStackCar; typedef struct car {CarNode *data;struct car *next;}QueueNode;typedef struct Node //模拟通道 {QueueNode *head;QueueNode *rear;}LinkQueueCar;void InitStack(SeqStackCar *); //初始化栈 int InitQueue(LinkQueueCar *); //初始化便道 int arrival(SeqStackCar *,LinkQueueCar *); //车辆到达 void leave(SeqStackCar *,SeqStackCar *,LinkQueueCar *); //车辆离开void list(SeqStackCar,LinkQueueCar); //显示存车信息 int main(void){SeqStackCar Enter,Temp;LinkQueueCar Wait;int ch;InitStack(&Enter);//初始化车站InitStack(&Temp); //初始化让路的临时栈InitQueue(&Wait); //初始化通道while(1){printf("\n1. The car arrive\n");printf("2. The car leave\n");printf("3. The schedule\n");printf("4. Exit\n");while(1){scanf("%d",&ch);if(ch>=1 && ch<=4) break;else printf("\nPlease choose: 1|2|3|4.");}switch(ch){case 1: arrival(&Enter,&Wait);//车辆到达 break; case 2: leave(&Enter,&Temp,&Wait);//车辆离开break; case 3: list(Enter,Wait);break;//列表打印信息 case 4: exit(0);//退出主程序default: break;}}} void InitStack(SeqStackCar *s) //初始化栈 {int i;s->top=0;for(i=0;i<=MAX;i++)s->stack[s->top]=NULL;}int InitQueue(LinkQueueCar *Q) //初始化便道 {Q->head=(QueueNode *)malloc(sizeof(QueueNode));if(Q->head!=NULL){Q->head->next=NULL;Q->rear=Q->head;return 1;}else return -1;}void print(CarNode *p,int room) //打印出站车的信息 {int A1,A2,B1,B2;printf("\nplease input thedepart time:/**:**/");scanf("%d:%d",&(p->leave.hour),&(p->leave.min));printf("\nthe number of the car:");puts(p->num);printf("\nthe time the car arrive: %d:%d",p->reach.hour,p->reach.min);printf("the depart time: %d:%d",p->leave.hour,p->leave.min);A1=p->reach.hour;A2=p->reach.min;B1=p->leave.hour;B2=p->leave.min;printf("\nthe fee: %2.1f元",((B1-A1)*60+(B2-A2))*price);free(p);}int arrival(SeqStackCar *Enter,LinkQueueCar *W) //车辆到达 {CarNode *p;QueueNode *t;p=(CarNode *)malloc(sizeof(CarNode));flushall();printf("\ninput the number of the car(例:陕A1234):");gets(p->num);if(Enter->top<MAX) //车场未满,车进车场 {Enter->top++;printf("\nthe place of the car.",Enter->top);printf("\nthe time thecar arrive:/**:**/");scanf("%d:%d",&(p->reach.hour),&(p->reach.min));Enter->stack[Enter->top]=p;return 1;}else //车场已满,车进便道 {printf("\n该车须在便道等待!");t=(QueueNode *)malloc(sizeof(QueueNode));t->data=p;t->next=NULL;W->rear->next=t;W->rear=t;return 1;}}void leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W)//车辆离开 { int i, room;CarNode *p,*t;QueueNode *q;//判断车场内是否有车 if(Enter->top>0) //有车 {while(1) //输入离开车辆的信息{printf("\n请输入车在车场的位置/1--%d/:",Enter->top);scanf("%d",&room);if(room>=1&&room<=Enter->top) break;}while(Enter->top>room) //车辆离开{Temp->top++;Temp->stack[Temp->top]=Enter->stack[Enter->top];Enter->stack[Enter->top]=NULL;Enter->top--;}p=Enter->stack[Enter->top];Enter->stack[Enter->top]=NULL;Enter->top--;while(Temp->top>=1){Enter->top++;Enter->stack[Enter->top]=Temp->stack[Temp->top];Temp->stack[Temp->top]=NULL;Temp->top--;}print(p,room);//判断通道上是否有车及车站是否已满 if((W->head!=W->rear)&&Enter->top<MAX) //便道的车辆进入车场 {q=W->head->next;t=q->data;Enter->top++;printf("\n便道的%s号车进入车场第%d位置.",t->num,Enter->top);printf("\n请输入现在的时间/**:**/:");scanf("%d:%d",&(t->reach.hour),&(t->reach.min));W->head->next=q->next;if(q==W->rear) W->rear=W->head;Enter->stack[Enter->top]=t;free(q);}else printf("\n便道里没有车.\n");}else printf("\n车场里没有车."); //没车 }void list1(SeqStackCar *S) //列表显示车场信息 {int i;if(S->top>0)//判断车站内是否有车 {printf("\n车场:");printf("\n 位置 到达时间 车牌号\n");for(i=1;i<=S->top;i++){printf(" %d ",i);printf("%d:%d ",S->stack[i]->reach.hour,S->stack[i]->reach.min);puts(S->stack[i]->num);}}else printf("\n车场里没有车");}void list2(LinkQueueCar *W)//列表显示便道信息 {QueueNode *p;p=W->head->next;if(W->head!=W->rear)//判断通道上是否有车{printf("\n等待车辆的号码为:");while(p!=NULL){puts(p->data->num);p=p->next;}}else printf("\n便道里没有车.");}void list(SeqStackCar S,LinkQueueCar W){int flag,tag;flag=1;while(flag){printf("\n请选择 1|2|3:");printf("\n1.车场\n2.便道\n3.返回\n");while(1){scanf("%d",&tag);if(tag>=1 || tag<=3) break;else printf("\n请选择 1|2|3:");}switch(tag){ case 1: list1(&S); break;//列表显示车场信息 case 2: list2(&W); break;//列表显示便道信息 case 3: flag=0; break; default: break; }}}
第二种版本
#include "stdio.h" #include "stdlib.h" #define SIZE 10 typedef struct{ int hour; int min;}time; //车的时间结构体 typedef struct{ int num; int position; time t; float money;}Car; //车的信息 typedef struct{ Car elem[SIZE+1];int top;//指向便道中的第一个空位 } Stack;//创建堆栈 typedef struct Node{ Car data;struct Node *next;}CQueueNode;//建立过道的程序: typedef struct{ CQueueNode *front; CQueueNode *rear;}LinkQueue;//设置的便道 //便道初始化程序 void InitQueue(LinkQueue *Q){ Q->front=(CQueueNode*)malloc(sizeof(CQueueNode)); //使mallo返回的指针转换为指向CQueueNode类型数据的指针 if(Q->front!=NULL){ Q->rear=Q->front;Q->front->next=NULL; }}int EnterQueue(LinkQueue *Q,Car *t){ CQueueNode *NewNode;NewNode=(CQueueNode*)malloc(sizeof(CQueueNode));//给便道申请空间 if(NewNode!=NULL){ NewNode->data.num=t->num;NewNode->data.t.hour=t->t.hour;NewNode->data.t.min=t->t.min;NewNode->next=NULL;Q->rear->next=NewNode;Q->rear=NewNode;return 1;}else return 0;}void InitStack(Stack *S){ S->top=0; }//确保堆栈为空 void Push(Stack *S,Car *r)//便道中的车入库 { S->top++;S->elem[S->top].num=r->num;r->position=S->elem[S->top].position=S->top;S->elem[S->top].t.hour=r->t.hour;S->elem[S->top].t.min=r->t.min;} int IsEmpty(Stack* S)//判断车库是否为空 { return(S->top==0" />1:0); }int IsFull(Stack *S)//判断车库是否为满 { return(S->top==SIZE?1:0); }int GetTop(Stack *S,Car *n)//车离开车库 { n->num=S->elem[S->top].num;n->position=S->elem[S->top].position;n->t.hour=S->elem[S->top].t.hour;n->t.min=S->elem[S->top].t.min;return 1; } int DeleteQueue(LinkQueue *Q,Car *x){ CQueueNode *p;if(Q->front==Q->rear) return 0; //判断便道为空 p=Q->front->next; //将便道中的车放入车库 Q->front->next=p->next;if(Q->rear==p)Q->rear=Q->front;x->num=p->data.num;x->t.hour=p->data.t.hour;x->t.min=p->data.t.min;free(p); //释放临时指针 return 1;}void In(Stack *S,LinkQueue *Q,Car*r){ if(IsFull(S)){ printf("车库已满,请等待!");EnterQueue(Q,r); //车进入便道} else{ Push(S,r);printf("\n您现在所在位置 %d",r->position);//打印车的位置 }} void TaM(Car *r,int h,int m){ if(m>r->t.min) { r->t.min+=60; r->t.hour-=1; } h=r->t.hour-h; m=r->t.min-m; printf("\n停车 %d小时 %d 分钟\n",h,m); printf("每小时收费30元\n"); h=h*60;m=h+m;r->money=0.5*m;printf("请支付金额%.2f元\n",r->money); //输出车主应付金额 } void Out(Stack *S,Stack *S0,Car *r,LinkQueue *Q){ int tag=S->top;Car x;if(IsEmpty(S)) printf("没有此车!");else{ for(;r->num!=S->elem[tag].num && tag>0;tag--){ Push(S0,&S->elem[tag]);S->top--;}if(r->num==S->elem[tag].num){ TaM(r,S->elem[tag].t.hour,S->elem[tag].t.min);S->top--;for(;S0->top>0;S0->top--)Push(S,&S0->elem[S0->top]);if(S->top<SIZE && Q->front!=Q->rear)//判断车库是否有此车,有就找到此车,然后退出 { DeleteQueue(Q,&x); Push(S,&x);}} else if(tag==0)//过道中的车无需收车费 { printf("未进入停车场应支付金额 0元!");for(;S0->top>0;S0->top--) Push(S,&S0->elem[S0->top]); }}}void print1(Stack *S){ int tag;Car x;printf("停车场停车情况:\n");if(IsEmpty(S)) printf("无车!");for(tag=S->top;S->top>0;S->top--)if(GetTop(S,&x)) //显示车库中个车的信息及到达时间 printf("车牌号 %d,所在位置 %d,到达/离开时间 %d:%d\n",x.num,x.position,x.t.hour,x.t.min);S->top=tag;}void print2(LinkQueue *Q){ CQueueNode *p;p=Q->front->next;for(;p!=NULL;p=p->next)//显示过道上车的信息及到达时间 printf("等待车牌号 %d, 到达/离开时间 %d:%d",p->data.num,p->data.t.hour,p->data.t.min);}void print(){ printf("\n***********************************欢迎光临*************************************\n");printf("\n 请选择:\n");printf("\n 1 :到达");printf("\n 2 :离开");printf("\n 3 :搜索");printf("\n 4 :退出\n");printf("\n"); }int main(void){ int n,m,i=1,j,flag=0; Car c[10]; Stack S,S0;//设定堆栈S,SO LinkQueue Q; //便道 InitStack(&S);//堆栈S InitStack(&S0);//临时堆栈S0 InitQueue(&Q); while(1){ print();scanf("%d",&m);switch(m) { case 1: printf("\n请输入车牌号:");scanf("%d",&c[i].num);printf("\n请输入到达/离开时间:");scanf("%d:%d",&c[i].t.hour,&c[i].t.min);In(&S,&Q,&c[i]);i++; //车辆的情况 break; case 2: printf("\n请输入车牌号:");scanf("%d",&n);for(j=0;j<10;j++)if(n==c[j].num) break;printf("\n请输入到达/离开时间:");scanf("%d:%d",&c[j].t.hour,&c[j].t.min);Out(&S,&S0,&c[j],&Q);//车辆的情况 break; case 3: print1(&S); //输出车库中车的信息 print2(&Q); //输出过道上车的信息 break;//终止 case 4: flag=1; break;default: printf("\n输入错误,请输入 1,2,3 或4");}if(flag) break; //结束程序 } return 0;}
5、C语言学生考勤系统
#include#include#includeconst int N = 1010;const int LEN = 20;const int CLAstunumber = 10;//每个班CLAstunumber人 typedef struct{char attdate[LEN];//日期int atts;//出勤情况 0缺勤 1出勤}attdent;typedef struct{char stname[LEN];char sex[LEN];int stnumber;int core;attdent stattdent[N];//最多纪录N个日期的考勤情况}studentinf;int cnt;//记录日期数组用到哪里 studentinf students[CLAstunumber];void menu(){printf("========================================================\n");printf("0.学生信息录入\n");printf("1.开始考勤\n");printf("2.修改学生考勤纪录\n");printf("3.生成并查看学生考勤分数\n");printf("4.读取数据\n");printf("5.保存数据\n");printf("6.退出\n");printf("========================================================\n");}void studentinput(){//录入操作 for(int i = 0 ; i < CLAstunumber; i ++){printf("----------------------------------------\n");printf("请输入学生姓名: |\n");studentinf student1;scanf("%s",student1.stname);printf("请输入学生性别: |\n");scanf("%s",student1.sex);printf("请输入学生学号: |\n");scanf("%d",&student1.stnumber);student1.core = 0;//(student1.stattdent[N]).attdate[LEN] = '0';students[i] = student1;printf("----------------------------------------\n");}}void attcheck(){//考勤操作 for(int i = 0;i < CLAstunumber ; i ++){students[i];printf("------------------------------\n");printf("姓名:%s\n",students[i].stname);printf("性别:%s\n",students[i].sex);printf("学号:%d\n",students[i].stnumber);printf("请输入时间:XXXX/XX/XX\n");scanf("%s",(students[i].stattdent[cnt]).attdate);printf("请输入该学生出勤情况:(0表示缺勤,1表示出勤)\n");scanf("%d",&(students[i].stattdent[cnt]).atts);cnt ++;printf("------------------------------\n");}}void changeinf(){//遍历修改char fname[LEN];char fdate[LEN];printf("----------------------------------\n");printf("请输入学生姓名:\n");scanf("%s",fname);int stand1;//退出第一层for标准 for(int i = 0;i < CLAstunumber; i ++) {if(!strcmp(fname,students[i].stname)){printf("学生姓名:%s\n",students[i].stname);printf("请输入时间:(XXXX/XX/XX)\n");scanf("%s",fdate);for(int j = 0;j < cnt ; j ++){if(!strcmp(fdate,(students[i].stattdent[j]).attdate))printf("学生:%s",fname);printf("考勤时间:%s\n",fdate);printf("出勤情况:%d\n",(students[i].stattdent[j]).atts);printf("请输入修改后的出勤情况:\n");scanf("%d",&(students[i].stattdent[j]).atts);printf("修改成功!\n");stand1 = 1;break;}if(stand1 == 1) break;}}printf("----------------------------------\n");}void checkcore(){//生成考勤分数 = 出勤次数即分数 for(int i = 0;i < CLAstunumber; i++){printf("----------------------------\n");printf("学生姓名:%s\n",students[i].stname);printf("学生性别:%s\n",students[i].sex);printf("学生学号:%d\n",students[i].stnumber);//计算分数students[i].core = 0;for(int j = 0;j < cnt ; j ++){students[i].core += (students[i].stattdent[j]).atts;} printf("考勤分数:%d\n",students[i].core);printf("----------------------------\n");}}void readdata(){FILE *fp = NULL;fp = fopen("data.txt","rb");//读出 int i = 0;while(!feof(fp)){fread(&students[i],sizeof(studentinf),1,fp);i ++;}i --;cnt = i;fclose(fp);printf("读取成功!\n");}void savedata(){FILE *fp = NULL;fp = fopen("data.txt","wb");//写入for(int i = 0;i < CLAstunumber; i ++){fwrite(&students[i],sizeof(studentinf),1,fp);}fclose(fp);printf("保存成功!\n");}int main(){menu();int op;while(1){scanf("%d",&op);if(op == 0){system("cls");studentinput();menu();}else if(op == 1){system("cls");attcheck();menu();} else if(op == 2){changeinf();menu();}else if(op == 3){system("cls");checkcore();menu();}else if(op == 4){system("cls");readdata();menu();}else if(op == 5){system("cls");savedata();menu();}else if(op == 6){break;}else printf("input error!\n");} }
6、C语言班级学生管理系统
#include#include#include#includeint count1=0;int count2=0;int count3=0;void class_print();void men1_student();void men2_student(); void men3_student();void men4_student(); void input_student();void input_grade(); void input_reward();void input_finance();void report_finance(); void seek_student();void seek_grade();void seek_reward();void change_student();void change_grade();void change_reward();int now1_student();int now2_student();int now3_student();struct date{ int year; int month; int day;};struct student{ char number[2]; char name[10]; char sex[4]; char tel[15]; char id[20]; char study[10];};struct grade{ char number[2]; char name[10]; char math[4]; char English[4]; char Cprogram[4]; char sumtest[4];};struct reward{ char number[2]; char name[10]; struct date time; char rewarding[80];};struct finance{ char thing[20]; char type[8]; struct date time2; int i;};struct student m_stu[50];struct grade g_stu[50];struct reward r_stu[50];struct finance f_class;struct grade t_change;main(){int choice=-1; //一级菜单 while(choice!=0) { printf("\n"); printf("\t\t\t\t\t\t**");class_print();printf("**\n"); printf("\t\t\t\t\t\t****************************\n"); printf("\t\t\t\t\t\t**班级学生学籍管理系统**\n");printf("\t\t\t\t\t\t** 1.户籍管理系统 **\n"); printf("\t\t\t\t\t\t** 2.成绩管理系统 **\n"); printf("\t\t\t\t\t\t** 3.奖惩管理系统 **\n"); printf("\t\t\t\t\t\t** 4.财务管理系统 **\n"); printf("\t\t\t\t\t\t** 0.退出 **\n"); printf("\t\t\t\t\t\t请选择"); scanf("%d",&choice); switch(choice) {case 1:men1_student();break; case 2: men2_student();break; case 3:men3_student(); break; case 4:men4_student(); break;case 0: printf("返回上一级");break; default :break; }}} void men1_student() //二级菜单(学生户籍管理){int choice=-1;while(choice!=0){printf("\n");printf("\t\t\t\t\t\t****************************\n");printf("\t\t\t\t\t\t**");class_print();printf("**\n"); printf("\t\t\t\t\t\t**班级学生户籍信息管理**\n"); printf("\t\t\t\t\t\t**户籍信息已录入人数:");now1_student();printf("**\n");printf("\t\t\t\t\t\t** 1.户籍录入系统 **\n"); printf("\t\t\t\t\t\t** 2.户籍修改系统 **\n"); printf("\t\t\t\t\t\t** 3.户籍查询系统 **\n"); printf("\t\t\t\t\t\t** 0.返回 **\n"); printf("\t\t\t\t\t\t请选择");scanf("%d",&choice);switch(choice){ case 1:input_student();break;case 2:change_student();break;case 3:seek_student();break;case 0:printf("返回上一级");break;default :break; } getch(); }}void input_student() // 户籍录入函数录入信息保存至D盘根目录下information1.txt文件下{ int j; printf("学生户籍信息录入"); printf("\n班内序号: "); scanf("%s",&m_stu[count1].number); printf("\n姓名: "); gets(m_stu[count1].name); gets(m_stu[count1].name); printf("\n性别: "); scanf("%s",&m_stu[count1].sex); printf("\n电话号码: "); scanf("%s",&m_stu[count1].tel); printf("\n身份证号: "); scanf("%s",&m_stu[count1].id); printf("\n学号: "); scanf("%s",&m_stu[count1].study); printf("是否保存该学生信息?1-保存 2-放弃"); scanf("%d",&j); if(j==1) {FILE *info; //保存至本地文件 info=fopen("d:\\information1.txt","at+");fwrite(&m_stu[count1],sizeof(struct student),1,info);fclose(info);printf("信息已保存至D:\\information1.tex 文件下,按任意键继续");count1++;} else printf("放弃保存,按任意键继续"); }void seek_student()//查询户籍信息函数{ FILE *info;info=fopen("d:\\information1.txt","rt"); int i;printf("请输入该学生班内序号");scanf("%d",&i);if(i<1||i>50) printf("抱歉,该学生信息不存在");else { rewind(info); fseek(info,sizeof(struct student)*(i-1),0); fread(&m_stu[i-1],sizeof(struct student),1,info); printf("\n\n序号 姓名性别 电话号码\t 身份证号\t\t学号\t"); printf("\n\n%d %s %s %s %s%s",i,m_stu[i-1].name,m_stu[i-1].sex,m_stu[i-1].tel,m_stu[i-1].id,m_stu[i-1].study);}}void change_student()//修改户籍信息{ int i=0,j,k,f;char h[100000]; FILE *info;info=fopen("d:\\information1.txt","rt"); printf("\n\t请输入要修改户籍信息的学生班内序号");scanf("%d",&j);if(j<1||j>50) printf("抱歉,该学生信息不存在");else { rewind(info); while(fgets(h,sizeof(struct student),info)!=NULL) { fseek(info,sizeof(struct student)*i,0); fread(&m_stu[i],sizeof(struct student),1,info); i++; }}fclose(info); printf("\n该学生目前户籍信息为\n:"); printf("\n\n序号 姓名性别 电话号码\t 身份证号\t\t学号\t"); printf("\n\n%d %s %s %s %s%s",j,m_stu[j-1].name,m_stu[j-1].sex,m_stu[j-1].tel,m_stu[j-1].id,m_stu[j-1].study);printf("确定修改此学生信息?1- 修改 2- 返回");scanf("%d",&k); if(k==1) { printf("\n姓名: "); gets(m_stu[j-1].name); gets(m_stu[j-1].name); printf("\n性别: "); scanf("%s",&m_stu[j-1].sex); printf("\n电话号码: "); scanf("%s",&m_stu[j-1].tel); printf("\n身份证号: "); scanf("%s",&m_stu[j-1].id); printf("\n学号: "); scanf("%s",&m_stu[j-1].study); }else return; info=fopen("d:\\information1.txt","wt"); for(f=0;f<i;f++) {fseek(info,sizeof(struct student)*f,0);fwrite(&m_stu[f],sizeof(struct student),1,info); } fclose(info); printf("修改成功!"); }void men2_student() //二级菜单(学生成绩管理){ int choice=-1;while(choice!=0) { printf("\n\t\t\t\t\t\t****************************\n"); printf("\t\t\t\t\t\t**");class_print();printf("**\n"); printf("\t\t\t\t\t\t**班级学生成绩信息管理**\n"); printf("\t\t\t\t\t\t**成绩信息已录入人数:");now2_student();printf("**\n");printf("\t\t\t\t\t\t** 1.成绩录入系统 **\n"); printf("\t\t\t\t\t\t** 2.成绩修改系统 **\n"); printf("\t\t\t\t\t\t** 3.成绩查询系统 **\n"); printf("\t\t\t\t\t\t** 4.综测排名系统 **\n"); printf("\t\t\t\t\t\t** 0.返回 **\n"); printf("\t\t\t\t\t\t请选择");scanf("%d",&choice);switch(choice){case 1:input_grade();break;case 2:change_grade(); break;case 3:seek_grade(); break;case 4:printf("功能正在研发中,敬请期待...");break;default: break;}getch();}}void input_grade() // 成绩录入函数 ,录入信息保存至 D盘根目录下information2.txt文件下{ int j; printf("学生成绩信息录入"); printf("\n班内序号"); scanf("%s",&g_stu[count2].number); printf("\n姓名: "); gets(g_stu[count2].name); gets(g_stu[count2].name); printf("\n高数: "); scanf("%s",&g_stu[count2].math); printf("\n英语:"); scanf("%s",&g_stu[count2].English); printf("\nC语言:"); scanf("%s",&g_stu[count2].Cprogram); printf("\n综测:"); scanf("%s",&g_stu[count2].sumtest); printf("是否保存该学生信息?1-保存 2-放弃"); scanf("%d",&j); if(j==1) {FILE *info2; //保存至本地文件 info2=fopen("d:\\information2.txt","at+");fwrite(&g_stu[count2],sizeof(struct grade),1,info2);fclose(info2);printf("信息已保存至D:\\information.tex2 文件下,按任意键继续");count2++;} else printf("放弃保存,按任意键继续"); }void seek_grade() //查询成绩信息函数{ FILE *info2;info2=fopen("d:\\information2.txt","rt"); int i;printf("\n\t请输入该学生班内序号");scanf("%d",&i);if(i<1||i>50) printf("\n抱歉,该学生信息不存在");else { rewind(info2); fseek(info2,sizeof(struct grade)*(i-1),0); fread(&g_stu[i-1],sizeof(struct grade),1,info2); printf("\n\n序号 姓名高数英语C语言综测"); printf("\n\n%d %s %s %s %s%s",i,g_stu[i-1].name,g_stu[i-1].math,g_stu[i-1].English,g_stu[i-1].Cprogram,g_stu[i-1].sumtest);}}void change_grade() //修改成绩信息 { int i=0,j,k,f;char h[100000]; FILE *info;info=fopen("d:\\information2.txt","rt"); printf("\n\t请输入要修改成绩信息的学生班内序号");scanf("%d",&j);if(j<1||j>50) printf("\n抱歉,该学生信息不存在");else { rewind(info); while(fgets(h,sizeof(struct grade),info)!=NULL) { fseek(info,sizeof(struct grade)*i,0); fread(&g_stu[i],sizeof(struct grade),1,info); i++; }}fclose(info); printf("\n该学生目前成绩信息为\n:"); printf("\n\n序号 姓名高数 英语C语言综测"); printf("\n\n%d %s %s %s %s%s",j,g_stu[j-1].name,g_stu[j-1].math,g_stu[j-1].English,g_stu[j-1].Cprogram,g_stu[j-1].sumtest);printf("\n\t确定修改此学生信息?1- 修改 2- 返回");scanf("%d",&k); if(k==1) { printf("\n姓名: "); gets(g_stu[j-1].name); gets(g_stu[j-1].name); printf("\n高数: "); scanf("%s",&g_stu[j-1].math); printf("\n英语: "); scanf("%s",&g_stu[j-1].English); printf("\nC语言: "); scanf("%s",&g_stu[j-1].Cprogram); printf("\n综测: "); scanf("%s",&g_stu[j-1].sumtest); }else return; info=fopen("d:\\information2.txt","wt");if(info==NULL){ printf("不能打开此文件,按任意键继续"); getch(); } for(f=0;f<i;f++) {fseek(info,sizeof(struct grade)*f,0);fwrite(&g_stu[f],sizeof(struct grade),1,info); } fclose(info); printf("修改成功!"); }void men3_student()//二级菜单(学生奖惩管理){int choice=-1;while(choice!=0){printf("\n");printf("\t\t\t\t\t\t****************************\n");printf("\t\t\t\t\t\t**");class_print();printf("**\n"); printf("\t\t\t\t\t\t**班级学生奖惩信息管理**\n"); printf("\t\t\t\t\t\t**奖惩信息已录入人数:");now3_student();printf("**\n");printf("\t\t\t\t\t\t** 1.奖惩录入系统 **\n"); printf("\t\t\t\t\t\t** 2.奖惩修改系统 **\n"); printf("\t\t\t\t\t\t** 3.奖惩查询系统 **\n"); printf("\t\t\t\t\t\t** 0.返回 **\n"); printf("\t\t\t\t请选择");scanf("%d",&choice);switch(choice){ case 1:input_reward();break;case 2:change_reward();break;case 3:seek_reward();break;case 0:printf("返回上一级");break;default :break; } getch(); }}void input_reward()// 奖惩录入函数录入信息保存至D盘根目录下information3.txt文件下{ int j; printf("学生奖惩信息录入"); printf("\n班内序号: "); scanf("%s",&r_stu[count3].number); printf("\n姓名: "); gets(r_stu[count3].name); gets(r_stu[count3].name); printf("\n奖惩时间: "); scanf("%d.%d.%d",&r_stu[count3].time.year,&r_stu[count3].time.month,&r_stu[count3].time.day); printf("\n具体事件: "); gets(r_stu[count3].rewarding); gets(r_stu[count3].rewarding); printf("\n是否保存该学生信息?1-保存 2-放弃"); scanf("%d",&j); if(j==1) {FILE *info; //保存至本地文件 info=fopen("d:\\information3.txt","at+");fwrite(&r_stu[count3],sizeof(struct reward),1,info);fclose(info);printf("\n信息已保存至D:\\information3.tex 文件下,按任意键继续");count3++;} else printf("放弃保存,按任意键继续"); }void seek_reward()//查询奖惩信息函数 根据学生班级序号{ FILE *info;info=fopen("d:\\information3.txt","rt"); int i;printf("\n\t请输入该学生班内序号");scanf("%d",&i);if(i<1||i>50) printf("\n抱歉,该学生信息不存在");else { rewind(info); fseek(info,sizeof(struct reward)*(i-1),0); fread(&r_stu[i-1],sizeof(struct reward),1,info); printf("\n\n序号姓名奖惩时间 具体事件\t\t"); printf("\n\n%d %s %d.%d.%d %s ",i,r_stu[i-1].name,r_stu[i-1].time.year,r_stu[i-1].time.month,r_stu[i-1].time.day,r_stu[i-1].rewarding);}}void change_reward()//修改奖惩信息{ int i=0,j,k,f;char h[100000]; FILE *info;info=fopen("d:\\information3.txt","rt"); printf("\n\t请输入要修改奖惩信息的学生班内序号");scanf("%d",&j);if(j<1||j>50) printf("\n抱歉,该学生信息不存在");else { rewind(info); while(fgets(h,sizeof(struct reward),info)!=NULL) { fseek(info,sizeof(struct reward)*i,0); fread(&r_stu[i],sizeof(struct reward),1,info); i++; }}fclose(info); printf("\n该学生目前奖惩信息为\n:"); printf("\n\n序号 姓名奖惩时间具体事件"); printf("\n\n%d %s %d.%d.%d %s ",j,r_stu[j-1].name,r_stu[j-1].time.year,r_stu[j-1].time.month,r_stu[j-1].time.day,r_stu[j-1].rewarding);printf("\n\t确定修改此学生信息?1- 修改 2- 返回");scanf("%d",&k); if(k==1) { printf("\n姓名: "); gets(r_stu[j-1].name); gets(r_stu[j-1].name); printf("\n奖惩时间: "); scanf("%d.%d.%d",&r_stu[j-1].time.year,&r_stu[j-1].time.month,&r_stu[j-1].time.day); printf("\n具体事件: "); scanf("%s",&r_stu[j-1].rewarding); }else return; info=fopen("d:\\information3.txt","wt"); for(f=0;f<i;f++) {fseek(info,sizeof(struct reward)*f,0);fwrite(&r_stu[f],sizeof(struct reward),1,info); } fclose(info); printf("修改成功!"); }int now1_student() //录入户籍信息学生数目 在菜单中 显示 已经录入学生人数{ int i=1; char j[100000]; FILE *info; info=fopen("D:\\information1.txt","rt"); rewind(info); while(fgets(j,sizeof(struct student),info)!=NULL) { fseek(info,sizeof(struct student)*i,0); i++;}fclose(info);printf("%d",i-1); } int now2_student() //录入成绩信息学生数目 在菜单中 显示 已经录入学生人数{ int i=1; char j[100000]; FILE *info; info=fopen("D:\\information2.txt","rt"); rewind(info); while(fgets(j,sizeof(struct grade),info)!=NULL) { fseek(info,sizeof(struct grade)*i,0); i++;}fclose(info);printf("%d",i-1); } int now3_student() //录入奖惩信息学生数目 在菜单中 显示 已经录入学生人数{ int i=1; char j[100000]; FILE *info; info=fopen("D:\\information3.txt","rt"); rewind(info); while(fgets(j,sizeof(struct reward),info)!=NULL) { fseek(info,sizeof(struct reward)*i,0); i++;}fclose(info);printf("%d",i-1); }void class_print()// 录入专业班级信息,并保存至D盘根目录下information0.txt文件下{ char major[20],class_number[4]; FILE *info; info=fopen("d:\\information0.txt","rt"); if(info==NULL) {info=fopen("d:\\information0.txt","wt");printf("\n请输入专业 :");scanf("%s",&major);printf("\n请输入班级 :");scanf("%s",&class_number);fwrite(major,sizeof(major),1,info);fwrite(class_number,sizeof(class_number),1,info);printf("\n\t已保存专业班级信息!");fclose(info);info=fopen("d:\\information0.txt","rt");fread(&major,sizeof(major),1,info);fread(&class_number,sizeof(class_number),1,info);printf("%s",major);printf("%s班",class_number);} else{fread(&major,sizeof(major),1,info);fread(&class_number,sizeof(class_number),1,info);printf("%s",major);printf("%s班",class_number);fclose(info); }}void men4_student()//二级菜单(班级财务管理){int choice=-1;while(choice!=0){printf("\n");printf("\t\t\t\t\t\t****************************\n");printf("\t\t\t\t\t********财务信息不可修改,请谨慎录入!******\n"); printf("\t\t\t\t\t\t**班级学生财务信息管理**\n");printf("\t\t\t\t\t\t** 1.财务录入系统**\n"); printf("\t\t\t\t\t\t** 2.财务报表系统**\n"); printf("\t\t\t\t\t\t** 0.返回 **\n"); printf("\t\t\t\t\t\t请选择");scanf("%d",&choice);switch(choice){ case 1:input_finance();break;case 2:report_finance(); break;case 3:case 0:printf("返回上一级");break;default :break; } getch(); }} void input_finance()// 财务录入函数 ,录入后信息保存到D盘根目录下 information4.txt文件中{ int j; printf("班级财务信息录入"); printf("\n收支时间: "); scanf("%d.%d.%d",&f_class.time2.year,&f_class.time2.month,&f_class.time2.day ); printf("\n具体事件: "); gets(f_class.thing); gets(f_class.thing); printf("\n财务类型(收入或开支): "); scanf("%s",&f_class.type); printf("\n流动金额(收入为正开支为负): "); scanf("%d",&f_class.i); printf("\n是否保存该财务信息?1-保存 2-放弃"); scanf("%d",&j); if(j==1) {FILE *info; //保存至本地文件 info=fopen("d:\\information4.txt","at+");fwrite(&f_class,sizeof(struct finance),1,info);fclose(info);printf("\n信息已保存至D:\\information4.txt 文件下,按任意键继续");} else printf("放弃保存,按任意键继续"); }void report_finance() // 财务信息报表{char h[100000];int sum=0,i=0; printf("\n收支时间 具体事件\t财务类型流动金额\n"); FILE *info;info=fopen("d:\\information4.txt","rt"); while(fgets(h,sizeof(struct finance),info)!=NULL) { fseek(info,sizeof(struct finance)*i,0); fread(&f_class,sizeof(struct finance),1,info);i++; printf("\n%d.%d.%d %s%s %d\n",f_class.time2.year,f_class.time2.month, f_class.time2.day,f_class.thing,f_class.type,f_class.i); sum=sum+f_class.i ; }printf("\n\t\t班费余额:%d元",sum); }
7、C语言超市收款系统
#include#include#include#define M 10typedef struct//定义商品的结构体 {char name[10];int num;floatprice; int ID; }Store;Store store[M];void shuru( ) //定义输入商品的函数 {int i;int isgo;int n;for(i=0;i<M;i++){store[i].ID=i+1;//自定义商品的ID printf("输入商品的名字\n");scanf("%s",&store[i].name); printf("输入商品的价格\n");scanf("%f",&store[i].price); printf("还需要输入商品吗?\n");printf("1.YES,2.NO\n");scanf("%d",&isgo);if(isgo!=2)//判断是否继续输入商品 {}else//不输入商品直接跳出,展示所有商品的信息 {for(n=0;n<=i;n++){printf("商品ID\t商品名字\t商品价格\n");printf("%d\t,%s\t,%f\n",store[n].ID,store[n].name,store[n].price);}i=10;}}}void shopp()//购物函数 {int isgo=1;int i;int n;int number;int shoppid; float pricenum;float pricesum=0.0;float priceguke; printf("库存的商品如下\n");for(n=0;n<M;n++){printf("商品ID\t商品名字\t商品价格\n"); printf("%d\t,%s\t,%f\n",store[n].ID,store[n].name,store[n].price); if(store[n].price==0)//这里是为了不把没定义的商品不显示出来 { n=M;} }while(isgo)//购物 {printf("按下你需要的商品ID和数量\n");scanf("%d,%d",&shoppid,&number);pricenum=(store[shoppid-1].price)*number;pricesum=pricesum+pricenum;printf("你购买的商品%s是%d个,价格是%f,总金额是%f\n",store[shoppid-1].name,number,pricenum,pricesum);printf("是否继续购物\n");printf("0.退出,1.继续\n");scanf("%d",&isgo); }printf("输入顾客支付的金钱\n");//结算 scanf("%f",&priceguke);if(priceguke>=pricesum){printf("收到顾客%f元,找回顾客%f元\n",priceguke,priceguke-pricesum);}else{printf("收到的钱小于总金额\n");}printf("谢谢光临\n");}main(){int xitong=1;while(xitong!=3){printf("-------------------欢迎登录好浪费超市系统-----------------------\n"); printf("1.输入商品信息\n");printf("2.购买商品\n");printf("3.退出系购买商品统\n");scanf("%d",&xitong);switch(xitong){case 1:shuru();break;case 2:shopp();break;case 3:printf("欢迎再次使用好浪费超市系统\n"); }}}
8、C语言超市信息管理系统
/*ifndef/define/endif”主要目的是防止头文件的重复包含和编译*/#ifndef MARKET_H#define MARKET_H #include#include#include#include#include#include#define N 5 struct mar//该结构体为存储货物信息 { char ID[10];//货号 char brand[20];//品名double inprice;//进价double outprice;//售价int storage;//存储量 }; struct node//该结构体为存储购物车中的信息 {struct mar buy;//货物信息 int number;//购物的数量 struct node *next; //指向下一个购物节点 }; struct mar goods[N];//结构体数组 struct node *cart;//购物车头结点 int menu();//打印主菜单函数 void WenJianKucun();//文件建立库存信息 void ShouJianKucun();//手动建立库存信息void DisKucun();//显示库存信息函数void ShopCart();//对购物车进行操作int ShopCartMenu();//打印购物车菜单void XianShi();//显示购物列表void add();//在购物车里添加商品 void UpdateKu();//更新购物车信息函数 voidcalculate();//计算所购商品价格并修改保存 #endifint main(){//主函数 printf("*********************************************************\n");printf("欢迎进入超市信息管理系统\n");printf("*********************************************************\n");int find=0;while(1){switch(menu()) {//menu()函数为输入选项case 1: WenJianKucun();find=1; break;//选项1 文件建立库存信息 case 2:if(find==0)ShouJianKucun();elseprintf("库存信息已存在\n");break;//选项2 手动建立库存信息case 3: DisKucun(); break;//选项3 显示库存信息函数case 4: ShopCart(); break;//选项4 对购物车进行操作case 5: calculate(); break;//选项5 计算所购商品价格并修改保存 case 6: system("cls");printf("感谢您的使用,再见!\n");Sleep(2000);exit(0);}}return 0;}int menu(){//打印主菜单函数char s[5];//输入的选项 int n;//选项为整数 printf("\n\n请选择下面的数字进行操作:\n");printf("--------------------------------------------\n"); printf("1. 文件建立库存信息\n");printf("2. 手动建立库存信息\n");printf("3. 显示所有商品信息\n");printf("4. 购物车\n");printf("5. 结算\n");printf("6. 退出程序\n"); printf("--------------------------------------------\n");printf("请选择对应数字1~6(1,2选其一)\n");printf("本程序制作人: 2017物联网工程 胡斌、刘萌欣、陈俊飞\n");while(1){fflush(stdin);//清空输入流gets(s);//输入一串字符串 n=atoi(s);//字符串转化为整数 if(n<1||n>6)//判断输入的选项是否正确printf("输入错误,请重新输入:\n");else//输入正确,结束循环 break; }return n;//返回输入选项 } void WenJianKucun(){//文件建立库存信息FILE *fp;//定义文件指针 if((fp=fopen("kucun.txt","w"))==NULL) {//创建文件printf("创建文件失败\n");return ;}//向文件中写入信息 fprintf(fp,"69011111 飘柔日常护理 12.00 15.00 30\n");fprintf(fp,"69021111 优酸乳 1.50 2.00 200\n");fprintf(fp,"69031111 红富士苹果 2.50 3.50 300\n");fprintf(fp,"69041111 晨光笔芯 0.70 1.00 150\n");fprintf(fp,"69051111 胡医生面膜 12.00 16.50 100\n");fclose(fp);//关闭文件 printf("成功导入信息\n"); return ;}void ShouJianKucun(){//手动建立库存信息FILE *fp;//定义文件指针 int i;printf("请依次输入货物信息:\n");printf("--------------------------------------------\n");for(i=0;i<N;i++) {//向文件中写入信息printf("第%d个商品:\n",i+1);printf("货号:"); fflush(stdin);//清空输入流gets(goods[i].ID);//输入货号printf("品名:");fflush(stdin);//清空输入流gets(goods[i].brand);//输入品名 printf("进价: ");fflush(stdin);//清空输入流scanf("%lf",&goods[i].inprice);//输入进价 printf("售价:");fflush(stdin);//清空输入流scanf("%lf",&goods[i].outprice);//输入售价 printf("数量:");fflush(stdin);//清空输入流scanf("%d",&goods[i].storage);//输入数量 printf("\n");}if((fp=fopen("kucun.txt","w"))==NULL) {//判断文件是否创建失败printf("创建文件失败。\n");return ;}//fprintf(fp,"货号 品名单价库存量\n");for(i=0;i<N;i++){//把货物信息写进文件fprintf(fp,"%-11s%-15s%-8.3f%-8.3f%-7d\n",goods[i].ID,goods[i].brand,goods[i].inprice,goods[i].outprice,goods[i].storage);}//fwrite(goods,sizeof(struct mar),N,cun);二进制文件输出 fclose(fp);//关闭文件 printf("信息导入成功\n");return ;}void DisKucun(){//显示库存信息函数FILE *fp;//定义文件指针 int i;if((fp=fopen("kucun.txt","r"))==NULL) {//判断是否打开了文件printf("打开文件失败\n");return ;}for(i=0;i<N;i++) {//输出物品信息//读取文件信息 fscanf(fp,"%s%s%lf%lf%d",goods[i].ID,goods[i].brand,&goods[i].inprice,&goods[i].outprice,&goods[i].storage);printf("--------------------------------------------\n");//输出文件信息 printf("货号 品名 单价库存量\n");printf("%-11s%-15s%-8.3f%-7d\n",goods[i].ID,goods[i].brand,goods[i].outprice,goods[i].storage);}fclose(fp);//关闭文件 }voidShopCart(){//对购物车进行操作while(1){switch(ShopCartMenu()) {//ShopCartMenu()为输入选项case 1: XianShi();break;//选项1 显示购物车物品信息case 2: add();break;//选项2 添加购物车物品//case 3: deleteitem();//删除购物车中的某项商品//break;case 3: return ;//选项3 退出 }}}int ShopCartMenu(){//打印购物车菜单int n;//为整数 char s[5];//输入一串字符串 printf("\n\n请选择操作:\n");printf("--------------------------------------------\n");printf("1. 显示当前可以购物列表\n");printf("2. 在购物车里添加商品\n");//printf("3. 删除购物车商品\n"); printf("3. 退出\n");printf("--------------------------------------------\n");printf("请选择对应数字1~3\n");while(1){fflush(stdin);//清空输入流gets(s);//输入一串字符串 n=atoi(s);//字符串转化为整数 if(n<1||n>3)//判断输入的选项是否正确 printf("输入错误,请重新输入: \n");else//输入正确,结束循环 break;}return n;//返回输入选项 }void XianShi(){//显示购物列表struct node *p; //定义节点指针 p=cart;//把购物车链表赋给p FILE *fp1;//定义文件指针 if((fp1=fopen("cart.txt","r"))==NULL){//判断是否打开了文件 printf("打开文件失败\n");//return ;}if(p==NULL){//购物车为空 printf("目前购物车为空:");return ;}else{//购物车不为空 while(p!=NULL) {//把购物车物品不断减一,直到为空printf("--------------------------------------------\n");printf("货号 品名 单价数量\n");printf("%-11s%-15s%-8.3f%-7d\n",p->buy.ID,p->buy.brand,p->buy.outprice,p->number);//输出物品信息 p=p->next; }}fclose(fp1);//关闭文件 return ;}void add(){//在购物车里添加商品 FILE *fp,*fp1;//fp表示kucun文件,fp1表示cart文件 struct node *p1,*p,*p2;//定义节点指针 int i,n;//n表示商品数量 char s[20];//表示货号或者品名 char choice1[20],choice2[20];//选择项(y继续,n继续) do{printf("输入所需物品的名称或货号:\n");fflush(stdin);//清空输入流 gets(s);//输入货号或者品名 if((fp=fopen("kucun.txt","r"))==NULL){//打开文件 printf("打开文件失败\n");continue;}for(i=0;i<N;i++){//循环N次 fscanf(fp,"%s%s%*lf%lf%d",goods[i].ID,goods[i].brand,&goods[i].outprice,&goods[i].storage);//从文件中读取货物信息 if((strcmp(goods[i].ID,s)==0||strcmp(goods[i].brand,s)==0)&&goods[i].storage>0)//找输入对应的商品 {printf("已找到所需商品:\n");printf("--------------------------------------------\n");printf("货号 品名 单价数量\n");printf("%-11s%-15s%-8.3f%-7d\n",goods[i].ID,goods[i].brand,goods[i].outprice,goods[i].storage);//输出所需物品信息 printf("请输入所需商品数量: ");scanf("%d",&n);//商品数量 if(n>goods[i].storage){//判断是否还可以购买不 printf("该商品库存不足\n");break;}printf("\n是否购买此商品?(Y/N)\n");fflush(stdin);//清空输入流 scanf("%s",choice1);//输入一个选项 if(strcmp(choice1,"Y")==0||strcmp(choice1,"y")==0){//输入y/Y表示购买此商品 p=new node;//生成新节点 if(p==NULL){//分配内存失败 printf("内存申请失败!\n");exit(1);//退出 }p->number=n;//商品数量 p->buy=goods[i];//货物信息 p->next=NULL;p1=cart;//把头结点赋给p1 if(cart==NULL)//头结点为空 cart=p;//把节点赋值给头结点 else{while(p1->next!=NULL)p1=p1->next;//把链表循环到尾部 p->next=p1->next; p1->next=p;//把新节点p挂在链表尾部 }}elseprintf("输入错误\n");break;//找到商品结束循环 }}if(i==N)//循环完毕还没找到对应商品 printf("未找到所需物品\n");fclose(fp);//关闭文件 UpdateKu();//更新库存信息函数 printf("是否继续购物?(Y/N)\n");fflush(stdin);//清空输入流 scanf("%s",choice2);//输入选项if(strcmp(choice2,"y")!=0&&strcmp(choice2,"Y")!=0)printf("放弃购买,返回菜单!\n");}while(strcmp(choice2,"y")==0||strcmp(choice2,"Y")==0);//y/Y继续循环 return ;} //更新购物车信息函数 void UpdateKu(){FILE *fp;struct node *p;if((fp=fopen("cart.txt","w"))==NULL) {//判断文件是否创建失败printf("创建文件失败。\n");return ;}p=cart;//把购物车链表赋给p2 while(p!=NULL){//fprintf(fp1,"货号 品名单价库存量\n");//把货物信息写进文件fprintf(fp,"%-11s%-15s%-8.3f%-7d\n",p->buy.ID,p->buy.brand,p->buy.outprice,p->number);p=p->next;//指向下一个节点 }fclose(fp);//关闭文件 return ; }void calculate(){//计算所购商品价格并修改保存struct node *p;p=cart;//把购物车(cart)链表赋给p double sum=0;//购买商品应付钱数 double pay;//购买商品实付钱数 FILE *fp;//定义文件指针 int i;printf("以下是购物清单:\n");XianShi();//调用函数显示购物列表信息 if((fp=fopen("kucun.txt","r"))==NULL){//打开文件printf("打开文件失败\n");return ;}for(i=0;i<N;i++){//从文件中读取信息 fscanf(fp,"%s%s%*lf%lf%d",goods[i].ID,goods[i].brand,&goods[i].outprice,&goods[i].storage);}fclose(fp);//关闭文件 while(p!=NULL){//当链表不为空时 sum=sum+p->buy.outprice*p->number;//计算应付钱数 for(i=0;i<N;i++){//找出对应商品库存量 if(strcmp(goods[i].ID,p->buy.ID)==0){//找到对应商品 goods[i].storage=goods[i].storage-p->number;//库存量变化 break;}} p=p->next;//指向下一个节点 }printf("总计:%-8.3f\n",sum);//输出购买物品共需多少钱 printf("输入实付金额:");scanf("%lf",&pay);printf("实付:%-8.3f找零:%8.3f\n",pay,pay-sum);if((fp=fopen("kucun.txt","w"))==NULL){//打开文件 printf("写入文件失败\n");return ;}for(i=0;i<N;i++){//向文件中写入信息 fprintf(fp,"%-11s%-15s%-8.3f%-8.3f%-7d\n",goods[i].ID,goods[i].brand,goods[i].inprice,goods[i].outprice,goods[i].storage);}fclose(fp);//关闭文件 return ;}
字数太多发在下一篇文章
C语言期末作业15个(下)
9、C语言货物管理系统
10、C语言歌曲信息管理系统
11、C语言职工信息管理系统源码
12、C语言学生打卡系统
13、C语言小学生计算机辅助教学系统
14、C语言门禁系统
15、C语言银行管理系统(文件操作)
建议点赞收藏