#include <stdio.h> #include<conio.h> #include <memory.h> #include <vector> #include <iostream> using namespace std; #define MaxStudent 100 #define MaxSize 1000 typedef struct student { char name[20]; int Number; char Sex; int Age; int TeamNo; char Duty[50]; }Student; void look(); int add(); int del(); int change(); void find(); void exit(); void save(); FILE *fp; char *filename = "ClassHR.Info"; /*文件名*/ char buf[MaxSize]; int j; Student student; vector<Student> stu; vector<Student>::iterator iter; int main() { //文件的读入 if((fp=fopen(filename,"r+t"))==NULL) { return 0; } bool flag; memset(&student,0,sizeof(student)); while(fgets(buf,MaxSize,fp)) { if(!memcmp(buf,"BeginStudentInfo:",17)) flag=true; if(flag) { if(!memcmp(buf,"name:",5)) sscanf(buf,"name:%s",student.name); if(!memcmp(buf,"Number:",7)) sscanf(buf,"Number:%d",&student.Number); if(!memcmp(buf,"Sex:",4)) sscanf(buf,"Sex:%c",&student.Sex); if(!memcmp(buf,"Age:",4)) sscanf(buf,"Age:%d",&student.Age); if(!memcmp(buf,"TeamNo:",7)) sscanf(buf,"TeamNo:%d",&student.TeamNo); if(!memcmp(buf,"Duty:",5)) { sscanf(buf,"Duty:%s",student.Duty); stu.push_back(student); flag=false; } } } fclose(fp); int n; while(1) { printf("1.查看所有记录\n"); printf("2.添加新的记录\n"); printf("3.删除记录\n"); printf("4.修改所有记录\n"); printf("5.查找学生记录\n"); printf("6.退出\n"); cin>>n; switch(n) { case 1:look();break; case 2:add();break; case 3:del();break; case 4:change();break; case 5:find();break; case 6:exit();break; } } return 0; } //查看记录 void look() { for(iter=stu.begin();iter!=stu.end();iter++) { printf("Name:%s\n",iter->name); printf("Number:%d\n",iter->Number); printf("Sex:%c\n",iter->Sex); printf("Age:%d\n",iter->Age); printf("TeamNo:%d\n",iter->TeamNo); printf("Duty:%s\n",iter->Duty); cout<<endl; } } //添加记录 int add() { printf("请输入学号:"); cin>>student.Number; for(iter=stu.begin();iter!=stu.end();iter++) { if (student.Number==iter->Number) { cout<<endl<<endl; printf("此学号已存在,退出!\n\n"); return 0; } } printf("请输入姓名:"); cin>>student.name; printf("请输入性别:(M||W)"); cin>>student.Sex; printf("请输入年龄:"); cin>>student.Age; printf("请输入组号:"); cin>>student.TeamNo; printf("请输入职务:"); cin>>student.Duty; stu.push_back(student); save(); fclose(fp); //关闭文件 printf("添加成功!\n"); return 0; } //删除记录 int del() { int num; bool flag=0; printf("请输入编号:"); cin>>num; for(iter=stu.begin();iter!=stu.end();iter++) { if(iter->Number==num) { stu.erase(iter); flag=1; break; } } if(flag==0) printf("无效编号!"); else save(); return 0; } int change() { int num; bool flag=0; printf("请输入编号:"); cin>>num; for(iter=stu.begin();iter!=stu.end();iter++) { if(iter->Number==num) { printf("请输入新的编号:"); cin>>iter->Number; printf("请输入新的姓名:"); cin>>iter->name; printf("请输入新的年龄:"); cin>>iter->Age; printf("请输入新的组号:"); cin>>iter->TeamNo; printf("请输入新的职位:"); cin>>iter->Duty; flag=1; break; } } if(flag==0) printf("没有此人!\n"); else save(); return 0; } void find() { bool flag=0; char name1[20]; printf("请输入要查找的姓名:"); cin>>name1; for(iter=stu.begin();iter!=stu.end();iter++) if(!strcmp(iter->name,name1)) { printf("Name: %s\n",iter->name); printf("Number: %d\n",iter->Number); printf("Age: %d\n",iter->Age); printf("TeamNo: %d\n",iter->TeamNo); printf("Duty: %s\n\n",iter->Duty); flag=1; } if(flag==0) printf("没有此人!\n"); } void exit() { exit(1); } //保存记录 void save() { if((fp=fopen(filename,"w+t"))==NULL) { printf("添加失败!"); return ; } for(iter=stu.begin();iter!=stu.end();iter++) { fprintf(fp,"\nBeginStudentInfo:\n"); fprintf(fp,"name:%s\n",iter->name); fprintf(fp,"Number:%d\n",iter->Number); fprintf(fp,"Sex:%c\n",iter->Sex); fprintf(fp,"Age:%d\n",iter->Age); fprintf(fp,"TeamNo:%d\n",iter->TeamNo); fprintf(fp,"Duty:%s\n",iter->Duty); fprintf(fp,"StudentInfoEnd\n"); } fclose(fp); } 这是我们小组做的项目,是用C++写的,你的那个要求太多了,你看着代码,也许能给你点提示。
温馨提示:内容为网友见解,仅供参考