📄 student copy.cpp
字号:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct Stu
{
long NO;
char name[15];
char sex;
int Class;
};
class Student
{
public:
// long NO;
// char name[25];
// char sex;
// int Class;
//void read_Init_student_data(Student *stu,int n);
void read_keyboard_student_date();
void find_from_student_no();
void find_from_student_name();
void delete_from_student_no();
void delete_from_student_name();
};
//===========================================================================
/*读初始的学生个人信息:*/
//===========================================================================
/*
void read_Init_student_data(Stu *stu,int n)
{
cout<<"Reading main's three student..."<<endl;
ofstream outfile("f3.txt",ios::binary);
if(!outfile)
{
cerr<<"outfile error!"<<endl;
exit(1);
}
for(int i=0;i<4;i++)
outfile.write((char*)&stu[i],sizeof(stu[i]));
ifstream infile("f3.txt",ios::in);
if(!infile)
{
cerr<<"infile error!"<<endl;
exit(1);
}
for(int j=0;j<3;j++)
{
//infile.read((char*)&stu[j],sizeof(stu[j]));
infile.read((char*)&stu[j],sizeof(stu[j]));
cout<<stu[j].NO<<" "<<stu[j].name<<" "<<stu[j].sex<<" "<<stu[j].Class<<endl;
}
infile.close();
outfile.close();
}
*/
//====================================================================
/*读入从键盘输入的学生个人信息:*/
//====================================================================
void Student::read_keyboard_student_date()
{
Stu stud;
Stu studtemp;
//统计结构体中学生的个数:
//=====================================================
int flags=0;
ifstream ifile("f3.txt",ios::binary);
ofstream ofile("temp2.txt",ios::binary);
ifile.read((char*)(&studtemp),sizeof(studtemp));
while(!ifile.eof())
{
ofile.write((char*)(&studtemp),sizeof(studtemp));
ifile.read((char*)(&studtemp),sizeof(studtemp));
flags+=1;
}
ifile.close();
ofile.close();
//=====================================================
ifstream infile("f3.txt",ios::binary);
ofstream temp_outfile("temp1.txt",ios::binary);
cout<<">>请输入你要输入的学生信息:"<<endl;
A: cout<<"NO,name,sex,Class:";
cin>>stud.NO>>stud.name>>stud.sex>>stud.Class;
// Stu studtemp;
// ifstream infile("f3.txt",ios::binary);
int flags1=1;
int flags2=0;
int flags3=0;
infile.read((char*)(&studtemp),sizeof(studtemp));
while(!infile.eof())
{
if(stud.NO==studtemp.NO)
{
cout<<">>此学号已经存在,请重新输入学生信息:"<<endl;
goto A;
}
//按学号的大小由小到大排序:
//hy1=========================================================================
else if(stud.NO > studtemp.NO)
{
temp_outfile.write((char*)(&studtemp),sizeof(studtemp));
flags2+=1;
flags3+=1;
}
else
if(flags1==1)
{
temp_outfile.write((char*)(&stud),sizeof(stud));
flags1=0;
flags3+=1;
}
if(flags1==0)
temp_outfile.write((char*)(&studtemp),sizeof(studtemp));
if(flags2==flags)
temp_outfile.write((char*)(&stud),sizeof(stud));
infile.read((char*)(&studtemp),sizeof(studtemp));
//hy1========================================================================
}
if(flags3==0)
temp_outfile.write((char*)(&stud),sizeof(stud));
infile.close();
temp_outfile.close();
/* Stu studtemp1;
infile.read((char*)(&studtemp1),sizeof(studtemp1));
while(!infile.eof())
{
if(stud.NO < studtemp1.NO)
{
temp_outfile.write((char*)(&stud),sizeof(stud));
temp_outfile.write((char*)(&studtemp1),sizeof(studtemp1));
}
else
{
temp_outfile.write((char*)(&studtemp1),sizeof(studtemp1));
}
infile.read((char*)(&studtemp1),sizeof(studtemp1));
}
// outfile.write((char*)(&stud),sizeof(stud));
temp_outfile.close();
*/
// Stu studtemp2;
ifstream temp_infile("temp1.txt",ios::binary);
ofstream outfile("f3.txt",ios::binary);
temp_infile.read((char*)(&studtemp),sizeof(studtemp));
while(!temp_infile.eof())
{
// temp_infile.read((char*)(&studtemp2),sizeof(studtemp2));
outfile.write((char*)(&studtemp),sizeof(studtemp));
temp_infile.read((char*)(&studtemp),sizeof(studtemp));
}
temp_infile.close();
outfile.close();
cout<<">>输入信息成功!"<<endl;
/* ofstream outfile("f3.txt",ios::app | ios::binary);
cout<<"Please input a student information:"<<endl;
cout<<"NO,name,sex,Class:";
cin>>stud.NO>>stud.name>>stud.sex>>stud.Class;
outfile.write((char*)(&stud),sizeof(stud));
outfile.close();*/
/* char ch;
cout<<">>是否要显示所有学生信息(Y/N):";
cin>>ch;
if(ch=='Y' || ch=='y')
{
ifstream infile("f3.txt",ios::binary);
if(!infile)
{
cerr<<"infile error!"<<endl;
exit(1);
}
cout<<">>已有的所有学生的信息如下:"<<endl;
infile.read((char*)(&stud),sizeof(stud));
while(!infile.eof())
{
cout<<stud.NO<<" "<<stud.name<<" "<<stud.sex<<" "<<
stud.Class<<endl;
infile.read((char*)(&stud),sizeof(stud));
}
infile.close();
}
*/
}
//=================================================================
/*按学号查找学生信息*/
//=================================================================
void Student::find_from_student_no()
{
long num;
cout<<">>输入你要查找的学生号:";
cin>>num;
Stu studtemp;
ifstream infile("f3.txt",ios::binary);
infile.read((char*)(&studtemp),sizeof(studtemp));
while(!infile.eof())
{
if(num==studtemp.NO)
{
cout<<">>你要找的学生信息是:";
cout<<studtemp.NO<<" "<<studtemp.name<<" "<<studtemp.sex<<" "<<
studtemp.Class<<endl;
break;
}
infile.read((char*)(&studtemp),sizeof(studtemp));
}
if(num!=studtemp.NO)
cout<<">>对不起,没有你要找的学生信息!"<<endl;
infile.close();
}
//=================================================================
/*按姓名查找学生信息*/
//=================================================================
void Student::find_from_student_name()
{
//***********************************
char nametemp[15];
cout<<">>输入要查找的学生名字:";
cin>>nametemp;
// cin.getline(nametemp,15);
//***********************************
// cout<<nametemp;
cout<<">>输入你要查找的学生姓名:";
int flag1=1;
int flag2=0;
Stu studtemp;
ifstream infile("f3.txt",ios::binary);
infile.read((char*)(&studtemp),sizeof(studtemp));
while(!infile.eof())
{
if(strcmp(nametemp,studtemp.name)==0)
{
flag2++;
if(flag1==1)
{
cout<<">>你要找的学生信息是:";
flag1--;
}
cout<<studtemp.NO<<" "<<studtemp.name<<" "<<studtemp.sex<<" "<<
studtemp.Class<<endl;
}
infile.read((char*)(&studtemp),sizeof(studtemp));
}
if(flag2==0)
cout<<">>对不起,没有你要找的学生信息!"<<endl;
infile.close();
}
//=================================================================
/*按学号删除某学生信息*/
//=================================================================
void Student::delete_from_student_no()
{
int flags=0;
ifstream infile("f3.txt",ios::binary);
ofstream outfile("temp.txt",ios::binary);
long num;
cout<<">>输入你要删除的学生号:";
cin>>num;
Stu studtemp;
infile.read((char*)(&studtemp),sizeof(studtemp));
while(!infile.eof())
{
if(num==studtemp.NO)
{
cout<<">>你要删除的学生信息是:";
cout<<studtemp.NO<<" "<<studtemp.name<<" "<<studtemp.sex<<" "<<
studtemp.Class<<endl;
flags=1;
}
else
outfile.write((char*)(&studtemp),sizeof(studtemp));
infile.read((char*)(&studtemp),sizeof(studtemp));
}
// if(num!=studtemp.NO)
// cout<<">>没有你要删除的学生信息!"<<endl;
infile.close();
outfile.close();
char ch3;
if(flags==1)
{
cout<<">>你是否真要删除此学生信息?(Y/N):";
cin>>ch3;
}
if((flags==1) && (ch3 == 'Y' || ch3 == 'y'))
{
ifstream infile("temp.txt",ios::binary);
ofstream outfile("f3.txt",ios::binary);
infile.read((char*)(&studtemp),sizeof(studtemp));
while(!infile.eof())
{
outfile.write((char*)(&studtemp),sizeof(studtemp));
infile.read((char*)(&studtemp),sizeof(studtemp));
}
cout<<">>删除成功!"<<endl;
infile.close();
outfile.close();
}
if(flags==0)
{
cout<<">>对不起,没有你要删除的学生信息!"<<endl;
}
}
//=================================================================
/*按姓名删除某学生信息*/
//=================================================================
void Student::delete_from_student_name()
{
int flags=0;
int flag1=1,flag2=0;
ifstream infile("f3.txt",ios::binary);
ofstream outfile("temp.txt",ios::binary);
//******************************
cout<<">>输入你要删除的学生姓名:";
char find_name[15];
cin>>find_name;
// cin.getline(find_name,15,'\n');
// char ch;
// while((ch=getchar())!='\n')
// cout<<ch;
//******************************
Stu studtemp;
infile.read((char*)(&studtemp),sizeof(studtemp));
while(!infile.eof())
{
if(strcmp(find_name,studtemp.name)==0)
{
if(flag1==1)
{
cout<<">>你要删除的学生信息是:";
flag1--;
}
cout<<studtemp.NO<<" "<<studtemp.name<<" "<<studtemp.sex<<" "<<
studtemp.Class<<endl;
flags=1;
flag2++;
}
else
outfile.write((char*)(&studtemp),sizeof(studtemp));
infile.read((char*)(&studtemp),sizeof(studtemp));
}
// if(num!=studtemp.NO)
// cout<<">>没有你要删除的学生信息!"<<endl;
infile.close();
outfile.close();
char ch3;
if(flags==1)
{
cout<<">>你是否真要删除这<<flag2<<个学生信息?(Y/N):";
cin>>ch3;
}
if((flags==1) && (ch3 == 'Y' || ch3 == 'y'))
{
ifstream infile("temp.txt",ios::binary);
ofstream outfile("f3.txt",ios::binary);
infile.read((char*)(&studtemp),sizeof(studtemp));
while(!infile.eof())
{
outfile.write((char*)(&studtemp),sizeof(studtemp));
infile.read((char*)(&studtemp),sizeof(studtemp));
}
cout<<">>删除成功!"<<endl;
infile.close();
outfile.close();
}
if(flags==0)
{
cout<<">>对不起,没有你要删除的学生信息!"<<endl;
}
}
void main()
{
/* Stu stu[3]=
{
{2007001,"huangyoun",'M',1},
{2007002,"xuqian",'F',1},
{2007003,"xiezhonghua",'M',2}
};
*/
int x;
A: cout<<">>欢迎使用学生管理系统!请选择你要的服务:"<<endl;
cout<<"=>显示所有学生信息请选择 1 "<<endl;
cout<<"=>输入学生信息请选择 2 "<<endl;
cout<<"=>查找某学生信息请选择 3 "<<endl;
cout<<"=>删除某学生信息请选择 4 "<<endl;
cout<<"=>退出此系统请选择 5 "<<endl;
cout<<">>你将选择?(1/2/3/4/5):";
cin>>x;
Student ST;
// read_Init_student_data(stu,3);
if(x==2)
{
char ch_read;
B: ST.read_keyboard_student_date();
cout<<">>是否还要输入?(Y/N):";
cin>>ch_read;
if(ch_read=='Y' || ch_read=='y')
goto B;
else
{
x=0;
goto A;
}
}
if(x==1)
{
Stu stud;
// cout<<strlen(Stu);
ifstream infile("f3.txt",ios::binary);
if(!infile)
{
cerr<<"infile error!"<<endl;
exit(1);
}
cout<<">>已有的所有学生的信息如下:"<<endl;
cout<<" 学号 姓名 性别 班级"<<endl;
infile.read((char*)(&stud),sizeof(stud));
while(!infile.eof())
{
cout<<stud.NO<<" "<<stud.name<<" "<<stud.sex<<" "<<
stud.Class<<endl;
infile.read((char*)(&stud),sizeof(stud));
}
infile.close();
x=0;
goto A;
}
if(x==3)
{
char ch_find;
char ch2;
cout<<">>按学号(X)查找还按名字(M)查找?(X/M):";
cin>>ch2;
if(ch2=='X' || ch2=='x')
{
C: ST.find_from_student_no();
cout<<">>是否还要查找?(Y/N):";
cin>>ch_find;
if(ch_find=='Y' || ch_find=='y')
goto C;
else
{
x=0;
goto A;
}
}
if(ch2=='M' || ch2=='m')
{
D: ST.find_from_student_name();
cout<<">>是否还要查找?(Y/N):";
cin>>ch_find;
if(ch_find=='Y' || ch_find=='y')
goto D;
else
{
x=0;
goto A;
}
}
}
// ST.delete_from_student_no();
if(x==4)
{
char ch_delete;
char ch4;
cout<<">>按学号(X)删除还按名字(M)删除?(X/M):";
cin>>ch4;
if(ch4=='X' || ch4=='x')
{
E: ST.delete_from_student_no();
cout<<">>是否还要删除?(Y/N):";
cin>>ch_delete;
if(ch_delete=='Y' || ch_delete=='y')
goto E;
else
{
x=0;
goto A;
}
}
if(ch4=='M' || ch4=='m')
{
F: ST.delete_from_student_name();
cout<<">>是否还要删除?(Y/N):";
cin>>ch_delete;
if(ch_delete=='Y' || ch_delete=='y')
goto F;
else
{
x=0;
goto A;
}
}
}
if(x==5)
{
x=0;
goto G;
}
G: cout<<">>谢谢使用!"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -