📄 database.cpp
字号:
#include"student.h"
#include<iostream.h>
#include<fstream.H>
#include<stdlib.h>
#include<iomanip.h>
#include<string.h>
#include<conio.h>
//a function that output using streams
//(just the function "storeFile" needs it~~~ +_+
void outputLine(ostream&,Student&);
//make a new blank file
void makeNewFile();
//return a integar to compare with the enums
char enterChoice();
//used to change the integar "choiceAfter" to
//decide whether exit or continiue
char afterChoice();
//store data in a file called data.dat
void storeFile(fstream&);
//show a student's data according to the ID the user gives
void show(fstream&);
//add a new student according to the ID the user gives
void add(fstream&);
//delete a student
void deleteStudent(fstream&);
//clean all the data in database
void cleanDatabase();
//display my information
void aboutMe();
int main()
{
//the fstream object used to input and out from the file
fstream inOutStream("data.dat",ios::in|ios::out);
//check errors
if (!inOutStream)
{
cerr<<"File coule not be opened."<<endl;
exit(1);
}
//used with enterChoice function to
//judge the user'choice
int choiceBefore;
//judge the user'choice after the "choiceBefore"
//to display"Press Y to continue or any other key to EXIT"
char choiceAfter='y';
while(choiceAfter=='y' && (choiceBefore=enterChoice())!='7')
{
switch(choiceBefore)
{
case '1':
show(inOutStream);
choiceAfter=afterChoice();
break;
case '2':
add(inOutStream);
choiceAfter=afterChoice();
break;
case '3':
deleteStudent(inOutStream);
choiceAfter=afterChoice();
break;
case '4':
cleanDatabase();
choiceAfter=afterChoice();
break;
case '5':
aboutMe();
choiceAfter=afterChoice();
break;
case '6':
storeFile(inOutStream);
choiceAfter=afterChoice();
break;
default:
cerr<<"Incorrect choice\n";
break;
}
inOutStream.clear();
}
return 0;
}
void makeNewFile()
{
ofstream outPut("data.dat");
//check errors
if(!outPut)
{
cerr<<"file cound not be opered."<<endl;
exit(1);
}
//the blank data used to fill in the Data.dat
Student blankStudent;
//from 1~1000,fill the blank data into the file
for(int i=0;i<1000;i++)
outPut.write(reinterpret_cast<char*>(&blankStudent),
sizeof(Student));
}
char enterChoice()
{
cout<<" WELCOME TO ALEX'S STUDENT DATABASE\n"
<<" *******************************************************************\n"
<<" ** * * Enter your choice * * **\n"
<<" * * * * 1--show an student * * * *\n"
<<" * * * * 2--add a new student * * * *\n"
<<" * * * * 3--delete a student * * * *\n"
<<" * * * * 4--clean all data in database * * * *\n"
<<" * * * * 5--about me * * * *\n"
<<" * * * * 6--store data in print.txt * * * *\n"
<<" * * ** 7--end program ** * *\n"
<<" *******************************************************************\n"
<<"?";
char menuChoice;
cin>>menuChoice;
return menuChoice;
}
//used in the funtion "storeFile" to output in a formatted form
void outputLine(ostream &output,Student &tempStudent)
{
output<<setiosflags(ios::left)
<<setw(8)<<tempStudent.getId()
<<setw(10)<<tempStudent.getLastName()
<<setw(10)<<tempStudent.getFirstName()
<<setw(7)<<tempStudent.getAge()
<<setw(5)<<tempStudent.getScore()
<<endl;
}
char afterChoice()
{
char cAfter;
cout<<"Press Y to continue or any other key to EXIT.\n";
cin>>cAfter;
if(cAfter=='y'||cAfter=='Y')
cAfter='y';
else
cAfter='n';
return cAfter;
}
void storeFile(fstream &readFromFile)
{
//the file "print.txt" was connected to the
//stream class object storeFile
ofstream storeFile("print.txt",ios::out);
if(!storeFile)
{
cerr<<"File could not be opened."<<endl;
exit(1);
}
storeFile<<setiosflags(ios::left)
<<setw(8)<<"ID"
<<setw(10)<<"LastName"
<<setw(10)<<"FirstName"
<<setw(7)<<"Age"
<<setw(5)<<"Score"
<<endl;
//move the get file pointer to the begining of the "data.dat"
readFromFile.seekg(0);
Student student;
readFromFile.read(reinterpret_cast<char*>(&student),
sizeof(Student));
while(!readFromFile.eof())
{
//the data will not be stored when the id is 0
if((student.getId())!=0)
outputLine(storeFile,student);
readFromFile.read(reinterpret_cast<char*>(&student),
sizeof(Student));
}
cout<<"The data has been stored in the file:print.txt.\n";
}
void show(fstream &showStudent)
{
Student student,student2;
int id,idOfName;
char firstName[10],lastName[10];
cout<<"1-Show the student by id\n"
<<"2-Show the student by name\n";
int choice;
cin>>choice;
do
{
switch(choice)
{
case 1:
//id that the user input to show
cout<<"Please input the student ID:\n(1~1000)?";
cin>>id;
//check the id,it must be a number between 1~1000
do
{
if(id<1 || id>1000)
{
cout<<"Incorrect student ID";
cout<<"\nPlease input the student ID:\n(1~1000)?";
cin>>id;
}
}while(id<1 || id>1000);
showStudent.seekg((id-1)*sizeof(Student));
showStudent.read(reinterpret_cast<char*>(&student)
,sizeof(Student));
if(student.getId()==0)
cout<<"This ID is empty.\n";
else
{
cout<<setiosflags(ios::left)
<<setw(8)<<"ID"
<<setw(15)<<"LastName"
<<setw(15)<<"FirstName"
<<setw(7)<<"Age"
<<setw(5)<<"Score"
<<endl;
student.showData();
}
break;
case 2:
cout<<"Please input the first name\n:";
cin>>firstName;
cout<<"Please input the last name\n:";
cin>>lastName;
{
for(int i=1;i<=1000;i++)
{
showStudent.seekg((i-1)*sizeof(Student));
showStudent.read(reinterpret_cast<char*>(&student),
sizeof(Student));
if(!strcmp(student.getFirstName(),firstName)&&
!strcmp(student.getLastName(),lastName))
{
showStudent.seekg((i-1)*sizeof(Student));
showStudent.read(reinterpret_cast<char*>(&student2),
sizeof(Student));
idOfName=i;
cout<<setiosflags(ios::left)
<<setw(8)<<"ID"
<<setw(15)<<"LastName"
<<setw(15)<<"FirstName"
<<setw(7)<<"Age"
<<setw(5)<<"Score"<<endl;
student2.showData();
}
}
if(!strcmp(student2.getFirstName(),"no one"))
{
cout<<"Can not find this name in database.\n";
}
}
break;
default:
cout<<"Incorrect choice";
}
}while(choice!=1&&choice!=2);
}
void add(fstream &addStudent)
{
Student studentcmp;
int id,age;
char lastName[15],firstName[15];
char score;
cout<<"Please input the new student ID:\n(1~1000)?";
cin>>id;
//check if the ID is between 1~1000
do
{
if(id<1 || id>1000)
{
cout<<"Incorrect student ID";
cout<<"\nPlease input the new student ID again:\n(1~1000)?";
cin>>id;
}
}while(id<1 || id>1000);
//move the "get" file pointer to the position and read the
//data into the ""studentcmp"
addStudent.seekg((id-1)*sizeof(Student));
addStudent.read(reinterpret_cast<char*>(&studentcmp),
sizeof(Student));
//if the position has no data(ID=0)
if(studentcmp.getId()==0)
{
cout<<"Please input the first name:";
cin>>firstName;
cout<<"Please input the last name:";
cin>>lastName;
cout<<"Please input the age:";
cin>>age;
cout<<"Please input the score(A,B,C,D,E):";
cin>>score;
//set a Student object using the data
Student student(id,lastName,firstName,age,score);
cout<<"the new student you input is:\n"
<<"Name:"<<student.getFirstName()<<student.getLastName()
<<"\nID:"<<student.getId()
<<"\nAge:"<<student.getAge()
<<"\nScore:"<<student.getScore()
<<"\nThe data has been added to the database"
<<endl;
//write the "student" into the potition
addStudent.seekp((id-1)*sizeof(Student));
addStudent.write(reinterpret_cast<char*>(&student),
sizeof(Student));
}
else
{
cout<<"This ID already exists!"
<<" Fail to add. Please delete the ID first.\n";
}
}
void deleteStudent(fstream &deleteStudent)
{
Student student,student2,blankStudent;
int choice;
int id,idOfName;
char firstName[10],lastName[10];
cout<<"Please choose:"
<<"\n1--search and delete by ID"
<<"\n2--search and delete by name\n?";
cin>>choice;
switch (choice)
{
case 1:
cout<<"Please input the ID:";
cin>>id;
//move the get file pointer to the position
deleteStudent.seekg((id-1)*sizeof(Student));
deleteStudent.read(reinterpret_cast<char*>(&student),
sizeof(Student));
//if the position has data (strcmp will return no 0)
if(strcmp(student.getFirstName(),"no one"))
{
cout<<"the ID you want to delete is:\n"
<<setiosflags(ios::left)
<<setw(8)<<"ID"
<<setw(15)<<"LastName"
<<setw(15)<<"FirstName"
<<setw(7)<<"Age"
<<setw(5)<<"Score";
student.showData();
cout<<"\nIt has been deleted.\n";
//move the put file pointer to the position
deleteStudent.seekp((id-1)*sizeof(Student));
//delete it
deleteStudent.write(reinterpret_cast<char*>(&blankStudent),
sizeof(Student));
}
else
cout<<"This ID has no data.\n";
break;
case 2:
cout<<"Please input the firstname:";
cin>>firstName;
cout<<"Please input the lastname:";
cin>>lastName;
// the "{" prevent "initialization of 'studentNum' is
//skipped by 'default' label"
{ //studentNum is the numbers of student that have
//the same name
for(int i=1,studentNum=0;i<=1000;i++)
{
deleteStudent.seekg((i-1)*sizeof(Student));
deleteStudent.read(reinterpret_cast<char*>(&student),
sizeof(Student));
if(!strcmp(student.getFirstName(),firstName)&&
!strcmp(student.getLastName(),lastName))
{
//the numbers of the student that match the
//user's input.
studentNum++;
deleteStudent.seekg((i-1)*sizeof(Student));
deleteStudent.read(reinterpret_cast<char*>(&student2),
sizeof(Student));
//if the ID(=i) has data,asigh it to "idOfName"
idOfName=i;
if(studentNum==1)
cout<<"Find "<<studentNum<<" student\n";
else
cout<<"Find "<<studentNum<<" students\n";
cout<<"the ID you want to delete is:\n"
<<setiosflags(ios::left)
<<setw(8)<<"ID"
<<setw(15)<<"LastName"
<<setw(15)<<"FirstName"
<<setw(7)<<"Age"
<<setw(5)<<"Score"<<endl;
student2.showData();
cout<<"It has been deleted."
<<endl;
//delete it
deleteStudent.seekp((idOfName-1)*sizeof(Student));
deleteStudent.write(reinterpret_cast<char*>(&blankStudent),
sizeof(Student));
}
}
}
if(!strcmp(student2.getFirstName(),"no one"))
{
cout<<"Can not find this name in database.\n";
}
break;
default:
cout<<"Incorrect choice\n";
}
}
void cleanDatabase()
{
char choice='N';
cout<<"Really clean all the data?"
<<"\npress Y to confirm or press any other key to cancel\n";
cin>>choice;
if(choice=='y'||choice=='Y')
{
cout<<"All the data has been cleaned.\n";
//make a new blank file
makeNewFile();
}
}
void aboutMe()
{
cout<<"\n"
<<"This is my pensonal information:\n"
<<"Name:Alex Sue\n"
<<"Age:20\n"
<<"Email:alexsue@163.net\n"
<<"Interest:computer,game,music....\n"
<<"School:South China University of Technology\n"
<<"Class:2000 CS(4)\n"
<<"Student ID:200035003203160\n";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -