编写一个程序,实现以下功能:
(1)输入一系列的学生数据(包括学生的学号、姓名和成绩等基本信息),将学生信息写入二进制文件 student . dat 中。(2)从 student . dat 文件中读出这些数据并显示出来。
(3)在 student . dat 文件中按姓名进行查询,如输入“李”,则将所有姓“李”的学生的数据显示出来。
(4)可对指定学生的数据进行修改。
(5)可以删除指定的学生数据。
2022.4.4
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
class Person//学生的类
{
public:
int id;
int age;
char name[20];
};
void addperson()//添加函数
{
fstream file;
Person p;
char again;
int i;
file.open("student.dat",ios::app|ios::out|ios::binary);//以追加形式打开二进制文件
if(!file)
{
cout<<"error";
exit(0);
}
do{
cout<<"请输入以下数据:"<<endl;
cout<<"姓名:";
cin>>p.name;
cout<<"年龄:";
cin>>p.age;
cin.ignore();
cout<<"学号:";
cin>>p.id;
cin.ignore();
file.write((char *)&p,sizeof(p));
cout<<"是否还要输入一个学生的数据(y or n):";
cin>>again;
cin.ignore();
}while(again=='y');//循环输入添加学生信息
file.close();
}
void deleteperson()//删除函数
{
fstream file,file1;
file.open("student.dat",ios::binary|ios::in);//打开文件
file1.open("delete.dat",ios::binary|ios::out);//打开中间文件
if(!file)
{
cout<<"error";
exit(0);
}
if(!file1)
{
cout<<"error";
exit(0);
}
int t;
Person p;
cout<<"请输入要删除的学号:";
cin>>t;
while(!file.eof())//循环将除了要删除的学生之外的学生信息存入中间文件
{
file.read((char *)&p,sizeof(p));
if(file.fail())
break;
if(p.id==t)
continue;
file1.write((char *)&p,sizeof(p));
}
file.close();
file1.close();
file.open("student.dat",ios::out|ios::binary);//改变打开方式
file1.open("delete.dat",ios::in|ios::binary);
while(!file1.eof())//将中间文件存入原文件
{
file1.read((char *)&p,sizeof(p));
if(file1.fail())
break;
file.write((char *)&p,sizeof(p));
}
file.close();
file1.close();
cout<<"删除成功!"<<endl;
}
void changeperson()//修改函数
{
fstream file,file1;
file.open("student.dat",ios::binary|ios::in);//打开文件和中间文件
file1.open("delete.dat",ios::binary|ios::out);
if(!file)
{
cout<<"error";
exit(0);
}
if(!file1)
{
cout<<"error";
exit(0);
}
int t;
Person p;
cout<<"请输入要修改的学号:";
cin>>t;
while(!file.eof())//循环将原文件和修改的数据存入中间文件
{
file.read((char *)&p,sizeof(p));
if(file.fail())
break;
if(p.id==t)
{
cout<<"请输入要修改的姓名:";
cin>>p.name;
cout<<"请输入要修改的年龄:";
cin>>p.age;
cout<<"请输入要修改的学号:";
cin>>p.id;
}
file1.write((char *)&p,sizeof(p));
}
file.close();
file1.close();
file.open("student.dat",ios::out|ios::binary);
file1.open("delete.dat",ios::in|ios::binary);
while(!file1.eof())//将中间文件数据存入原文件
{
file1.read((char *)&p,sizeof(p));
if(file1.fail())
break;
file.write((char *)&p,sizeof(p));
}
cout<<"修改成功!"<<endl;
file.close();
file1.close();
}
void findperson()//查找函数
{
fstream file;
file.open("student.dat",ios::binary|ios::in);//打开文件
file.clear();
file.seekg(0L,ios::beg);//将指针指向开头
Person p;
int t;
cout<<"请输入学号:";
cin>>t;
while(!file.eof())//循环判断打印查找的信息
{
file.read((char *)&p,sizeof(p));
if(file.fail())
break;
file.seekg(0L,ios::cur);
if(t==p.id)
{
cout<<"查找成功!"<<endl;
cout<<"姓名:"<<p.name<<endl;
cout<<"年龄:"<<p.age<<endl;
cout<<"学号:"<<p.id<<endl;
break;
}
}
file.close();
}
void listperson()//列出所有函数
{ fstream file;
file.open("student.dat",ios::binary|ios::in);//打开文件
if(!file)
{
cout<<"error";
exit(0);
}
Person p;
while(!file.eof())//循环打印
{
file.read((char *)&p,sizeof(p));
if(file.fail())
break;
cout<<"姓名:"<<p.name<<endl;
cout<<"年龄:"<<p.age<<endl;
cout<<"学号:"<<p.id<<endl;
}
file.close();
}
void findsurname()//按姓氏查找
{
fstream file;
file.open("student.dat",ios::binary|ios::in);//打开文件
file.clear();
file.seekg(0L,ios::beg);
Person p;
char t[10];
cout<<"请输入姓氏:";
cin>>t;
while(!file.eof())//循环判断前两个字符是否相等来查找打印学生信息
{
file.read((char *)&p,sizeof(p));
if(file.fail())
break;
file.seekg(0L,ios::cur);
if(t[0]==p.name[0]&&t[1]==p.name[1])
{
cout<<"查找成功!"<<endl;
cout<<"姓名:"<<p.name<<endl;
cout<<"年龄:"<<p.age<<endl;
cout<<"学号:"<<p.id<<endl;
}
}
file.close();
}
void menu()//菜单函数
{
int ch;
while(1)//显示功能
{
cout<<"请选择:"<<endl;
cout<<"1:添加"<<endl;
cout<<"2.删除"<<endl;
cout<<"3.修改"<<endl;
cout<<"4.查找"<<endl;
cout<<"5.列出"<<endl;
cout<<"6.按姓名查找"<<endl;
cout<<"7.退出系统"<<endl;
cin>>ch;
if((ch<=7) && (ch>=1))
{
switch(ch)
{
case 1:addperson();break;
case 2:deleteperson();break;
case 3:changeperson();break;
case 4:findperson();break;
case 5:listperson();break;
case 6:findsurname();break;
case 7:exit(0);break;
}
}
else
{
cout<<"输入错误";
}
}
}
int main()
{
menu();
}