#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
#define ID_LEN 11
#define NAME_LEN 8
#define MARGIN_LEN 12 //输出时用于控制间隔
struct Student
{
char id[ID_LEN+1];
char name[NAME_LEN];
int major;
};
class StudentFile
{
private:
fstream stu_file;
public:
StudentFile(const char *filePath)
{
stu_file.open(filePath,ios::in | ios::out | ios::binary);
}
~StudentFile()
{
stu_file.close();//关闭文件流
}
//判断文件是否打开
bool isOpen()
{
return stu_file.is_open();
}
//显示功能菜单
void showMenu()
{
cout<<"======功能菜单======"<<endl;
cout<<" 0:退出程序\n 1:增加学生信息\n 2:修改学生专业\n 3:显示所有学生信息\n";
}
//打印专业信息
void printfMajor()
{
cout<<"可供选择的专业有"<<endl;
cout<<"1.数学 2.英语 3.C语言 4.C++"<<endl;
cout<<"5.数构 6.操作系统 7.数据库 8.UML"<<endl;
cout<<"请选择上面的一个数字进行输入(1--8)"<<endl;
}
//专业由数字转换成相对应的名称
char* changeMajorInfo(int major)
{
char* str;
switch(major)
{
case 1:str="数学";break;
case 2:str="英语";break;
case 3:str="C语言";break;
case 4:str="C++";break;
case 5:str="数构";break;
case 6:str="操作系统";break;
case 7:str="数据库";break;
case 8:str="UML";break;
}
return str;
}
//添加学生信息
void addStudent()
{
char id[ID_LEN];
char name[NAME_LEN];
int major;
cout<<"请输入一个学号(11个字符之内) ";
cin>>id;
cout<<"请输入姓名(7个字符之内) ";
cin>>name;
printfMajor();
while(true)
{
cin>>major;
if(major >=1 && major<=8)
break;
else
cout<<"您输入的数字不合法,请重新输入一个数字(1-8) ";
}
Student st;
strcpy(st.id,id);
strcpy(st.name,name);
st.major=major;
stu_file.seekg(0,ios::end);//从文件的结尾添加学生信息
stu_file.write((char*)&st,sizeof(st));
}
//通过id查找符和条件的学生的位置
int findById(char id[],Student& st)
{
int index=0;
stu_file.seekg(0);
stu_file.read((char*)&st,sizeof(st));
while(!stu_file.eof())
{
if(strcmp(st.id,id)==0)
return index;
index++;
stu_file.read((char*)&st,sizeof(st));
}
return -1;
}
//修改学生的信息
void updateMajor()
{
cout<<"请输入您要进行修改的学生的学号";
char cid[ID_LEN];
int index;
Student st;
while(true)
{
cin>>cid;
index=findById(cid,st);
if(index==-1) {
cout<<"您输入的学生学号找不到,请重新输入\n";
break;
}
}
stu_file.seekp(sizeof(st)*index);
stu_file.read((char*)&st,sizeof(st));
cout<<"查找到的学生信息为"<<endl;
cout<<left;
cout<<setw(MARGIN_LEN)<<"学号"<<setw(MARGIN_LEN)<<"姓名"<<setw(MARGIN_LEN)<<"专业"<<endl;
cout<<setw(MARGIN_LEN)<<st.id<<setw(MARGIN_LEN)<<st.name<<setw(MARGIN_LEN)<<changeMajorInfo(st.major)<<endl;
printfMajor();
int m;
cin>>m;
st.major=m;
stu_file.seekp(sizeof(st)*index);
stu_file.write((char*)&st,sizeof(st));
stu_file.flush();
}
//打印文件中的所有学生的信息
void printfAllStuInfo()
{
cout<<left;
cout<<setw(MARGIN_LEN)<<"学号"<<setw(MARGIN_LEN)<<"姓名"<<setw(MARGIN_LEN)<<"专业"<<endl;
stu_file.seekp(0);//将文件指针移动到文件头
Student st;
stu_file.read((char*)&st,sizeof(st));
while(!stu_file.eof())
{
cout<<setw(MARGIN_LEN)<<st.id<<setw(MARGIN_LEN)<<st.name<<setw(MARGIN_LEN)<<changeMajorInfo(st.major)<<endl;
stu_file.read((char*)&st,sizeof(st));
}
}
};
int main()
{
StudentFile student_file("stu.dat");
if(!student_file.isOpen())
{
cerr<<"打开文件失败\n"<<endl;
return 1;
}
student_file.showMenu();
int i;
cin>>i;
while(i!=0)
{
switch (i)
{
case 1:student_file.addStudent();break;
case 2:student_file.updateMajor();break;
case 3:student_file.printfAllStuInfo();break;
default:cout<<"输入的数字不正确"<<endl;
}
student_file.showMenu();
cin>>i;
}
return 0;
}
C++问题,遇到过的最难解决的问题。。
最新推荐文章于 2024-07-24 20:08:12 发布