📄 cpp1.cpp
字号:
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>
static int n=0;
int Comparison(char s1[],char s2[])
{
int i=0;
while (s1[i]!='\0' && s2[i]!='\0' && s1[i]==s2[i])
i++;
if(s1[i]=='\0' || s2[i]=='\0')
return 1;
else
return 0;
}
class Student
{
char tag; //定义档案标志
unsigned int OrdNum; //定义学生序号
char RegNum[12]; //定义学生学号
char Name[7]; //定义学生姓名
unsigned int Age; //定义学生年龄
char Sex[3]; //定义学生性别
char Bedroom[6]; //定义学生宿舍
char TelNum[9]; //定义学生电话
public:
// int getStudentNo()
// {
// return Age;
// }
void Getdata() //输入学生档案
{
tag='#';
cout<<"(序号 学号 姓名 年龄 性别 宿舍 电话):";
cin>>OrdNum>>RegNum>>Name>>Age>>Sex>>Bedroom>>TelNum;
}
char Gettag() //获得档案标志
{
return tag;
}
void Display() //显示档案信息
{
if(tag=='#') cout<<setiosflags(ios::left)
<<setw(7)<<OrdNum<<setw(16)<<RegNum<<setw(11)<<Name
<<setw(7)<<Age<<setw(7)<<Sex<<setw(10)<<Bedroom
<<setw(13)<<TelNum<<endl;
}
char *getname() //获得学生姓名
{
return Name;
}
};
void Input()
{
ofstream output("Student.dat");
Student s;
cout<<"输入学生档案"<<endl;
cout<<"输入学生人数:";
cin>>n;
for (int i=0; i<n; i++)
{
cout<<"第"<<i+1<<"个学生";
s.Getdata();
output.write((char *)&s,sizeof(s));
}
output.close();
}
void Output()
{
ifstream input("Student.dat");
Student s;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<" 学生档案 "<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<setiosflags(ios::left)
<<setw(7)<<"序号"<<setw(16)<<"学号"<<setw(11)<<"姓名"<<setw(7)<<"年龄"<<setw(7)<<"性别"
<<setw(10)<<"宿舍"<<setw(13)<<"电话"<<endl;
cout<<"-------------------------------------------------------------------------------"<<endl;
input.read((char *)&s, sizeof(s));
while(input)
{
s.Display();
input.read((char*)&s,sizeof(s));
};
cout<<"-------------------------------------------------------------------------------"<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
input.close();
char p;
do
{
cout<<"请按Q键返回主菜单......";
cin>>p;
}while((p!='q')&(p!='Q'));
cout<<endl;
cout<<endl;
}
void Search()
{
char sname[10];
ifstream file("Student.dat");
Student one;
file.seekg(0);
cout<<"输入要查询的姓名(可输入姓氏):";
cin>>sname;
cout<<"输出查询结果:"<<endl;
cout<<setiosflags(ios::left)
<<setw(7)<<"序号"<<setw(16)<<"学号"<<setw(11)<<"姓名"<<setw(7)<<"年龄"<<setw(7)<<"性别"
<<setw(10)<<"宿舍"<<setw(13)<<"电话"<<endl;
file.read((char *)&one,sizeof(one));
while (file)
{
if(Comparison(one.getname(),sname)==1)
one.Display();
file.read((char*)&one,sizeof(one));
};
file.close();
}
void Increase()
{
fstream outapp("Student.dat",ios::app);
Student one;
cout<<"添加数据:";
one.Getdata();
outapp.write((char*)&one,sizeof(one));
outapp.close();
}
void LogicDel()
{
char sname[12];
int i=0;
fstream outdel("Student.dat",ios::in|ios::out);
Student one ;
outdel.seekg(0);
cout<<"输入要删除的姓名(可只输入姓氏):";
cin>>sname;
while(!outdel.eof())
{
outdel.seekp(sizeof(Student)*i);
outdel.read((char*)&one,sizeof(one));
if (Comparison(one.getname(),sname)==1 && one.Gettag()=='#')
{
outdel.seekp(sizeof(Student)*i);
outdel.put('*');
}
i++;
}
outdel.close();
}
void PhysicsDel()
{
fstream outdel("Student.dat",ios::in);
fstream temp("temp",ios::out|ios::trunc);
Student s;
while(!outdel.eof())
{
outdel.read((char *)&s,sizeof(Student));
if (s.Gettag()=='#')
temp.write((char *)&s,sizeof(Student));
}
outdel.close();
temp.close();
fstream outdel1("Student",ios::out|ios::trunc);
fstream temp1("temp",ios::in);
while (!temp1.eof())
{
temp1.read((char *)&s,sizeof(Student));
outdel1.write((char *)&s,sizeof(Student));
}
outdel1.close();
temp1.close();
cout<<"此记录已物理删除"<<endl;
}
void MainMenu()
{
system("cls");
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" 计算机系学生档案管理系统 "<<endl;
cout<<" ************************************************** "<<endl;
cout<<" $ 〖1.输入学生档案〗 $ "<<endl;
cout<<" $ $ "<<endl;
cout<<" $ 〖2.输出学生档案〗 $ "<<endl;
cout<<" $ $ "<<endl;
cout<<" $ 〖3.查询学生档案〗 $ "<<endl;
cout<<" $ $ "<<endl;
cout<<" $ 〖4.添加学生档案〗 $ "<<endl;
cout<<" $ $ "<<endl;
cout<<" $ 〖5.逻辑删除档案〗 $ "<<endl;
cout<<" $ $ "<<endl;
cout<<" $ 〖6.物理删除档案〗 $ "<<endl;
cout<<" $ $ "<<endl;
cout<<" $ 〖7.按任意键退出〗 $ "<<endl;
cout<<" ************************************************** "<<endl;
cout<<" "<<endl;
}
void main()
{
int select;
MainMenu();
do
{
cout<<" 请输入您所要选择的功能(1-7):";
cin>>select;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
switch(select)
{
case 1:Input(); MainMenu(); break; //选择1时执行『输入学生档案』
case 2:Output(); MainMenu(); break; //选择2时执行『输出学生档案』
case 3:Search(); MainMenu(); break; //选择3时执行『查询学生档案』
case 4:Increase(); MainMenu(); break; //选择4时执行『添加学生档案』
case 5:LogicDel(); MainMenu(); break; //选择5时执行『逻辑删除档案』
case 6:PhysicsDel(); MainMenu(); break; //选择6时执行『物理删除档案』
}
}while(select>=1 && select<=6);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -