?? 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 + -