C++ to achieve a simple student management system
- 2020-04-02 03:07:41
- OfStack
C++ to achieve a simple student management system
/ / Student. CPP
#include<iostream>
using namespace std;
struct Stu
{
char no[10];
char name[16];
int math;
int chi;
double ave;
};
class Student
{
public:
Stu st;
Student * next;
public:
Student(){}
Student(Stu s)
{
st=s;
next=NULL;
st.ave=(st.math+st.chi)/2.0;
}
void setst(Stu s)
{
st=s;
st.ave=(st.math+st.chi)/2.0;
}
Stu getst()
{
return st;
}
void show()
{
cout<<"----------------------------"<<endl;
cout<<" Student id :"<<st.no<<endl;
cout<<" The name :"<<st.name<<endl;
cout<<" Math scores :"<<st.math<<endl;
cout<<" Language result :"<<st.chi<<endl;
cout<<" The average scores :"<<st.ave<<endl;
cout<<"----------------------------"<<endl;
}
};
/ / the main CPP
#include<fstream>
#include"Student.cpp"
using namespace std;
Student * create_List();
void traverse_List(Student * pHead);
bool is_empty(Student * pHead);
int length_List(Student * pHead);
bool insert_List(Student * pHead,int position,Stu st);
bool delete_List(Student * pHead,int position,Stu * st);
void sort_List(Student * pHead);
void menu_select();
void handle_menu(int s);
void outFile();
Student * inFile();
void delFile();
Student * pHead;
void main()
{
menu_select();
}
void menu_select()
{
int s;
cout<<" Please enter the options you want to operate on :"<<endl;
cout<<"1. Add original record "<<endl;
cout<<"2. Displays all records sorted by average score "<<endl;
cout<<"3. Save the original file "<<endl;
cout<<"4. Read the original file "<<endl;
cout<<"5. Delete the original file "<<endl;
cout<<"6. Insert a single record "<<endl;
cout<<"7. Delete a single record "<<endl;
cout<<"8. Displays the total number of records "<<endl;
cout<<"9. End program run "<<endl<<endl;
cout<<" The number on the left corresponds to the function selection , Please select a 1-9:";
cin>>s;
handle_menu(s);
}
void handle_menu(int s)
{
switch (s)
{
case 1:
{
system("cls");
pHead=create_List();
system("cls");
menu_select();
break;
}
case 2:
{
if(NULL==pHead)
{
cout<<" The total number of records is zero "<<endl;
getchar();
getchar();
system("cls");
menu_select();
}
system("cls");
sort_List(pHead);
traverse_List(pHead);
getchar();
getchar();
system("cls");
menu_select();
break;
}
case 3:
{
if(pHead!=NULL)
{
system("cls");
outFile();
system("cls");
menu_select();
}
system("cls");
menu_select();
break;
}
case 4:
{
system("cls");
pHead=inFile();
system("cls");
menu_select();
break;
}
case 5:
{
system("cls");
delFile();
system("cls");
menu_select();
break;
}
case 6:
{
if(NULL==pHead)
{
cout<<" The total number of records is zero "<<endl;
getchar();
getchar();
system("cls");
menu_select();
}
system("cls");
int num;
Stu st;
traverse_List(pHead);
cout<<" Which record do you want to insert after , Please enter the serial number :";
cin>>num;
cout<<" Edit the record to insert :"<<endl;
cout<<" Student id :";
cin>>st.no;
cout<<" The name :";
cin>>st.name;
cout<<" Math scores :";
cin>>st.math;
cout<<" Language result :";
cin>>st.chi;
if(insert_List(pHead,num-1,st))
{
cout<<" Insert the success !"<<endl;
}
else
{
cout<<" Insert the failure !"<<endl;
}
getchar();
getchar();
system("cls");
menu_select();
break;
}
case 7:
{
if(NULL==pHead)
{
cout<<" The total number of records is zero "<<endl;
getchar();
getchar();
system("cls");
menu_select();
}
int num;
Stu * st=(Stu *)malloc(sizeof(Stu));
traverse_List(pHead);
cout<<endl<<" Please enter the serial number of the record you want to delete :";
cin>>num;
if(delete_List(pHead,num,st))
{
cout<<endl<<" The record of successful deletion is as follows :"<<endl;
cout<<" Student id :"<<st->no<<endl<<" The name :"<<st->name<<endl;
}
else
{
cout<<" Delete failed !"<<endl;
}
getchar();
getchar();
system("cls");
menu_select();
break;
}
case 8:
{
if(NULL!=pHead)
{
system("cls");
cout<<" Record the total number of entries :"<<length_List(pHead)<<" article "<<endl;
getchar();
getchar();
system("cls");
menu_select();
}
else
{
cout<<" The total number of records is zero "<<endl;
getchar();
getchar();
system("cls");
menu_select();
}
break;
}
case 9:
{
system("cls");
cout<<" Successful exit !"<<endl;
exit(0);
break;
}
}
}
void delFile()
{
ofstream fileout;
fileout.open("c:\kcsj.txt",ios_base::out);
fileout<<"";
fileout.close();
}
Student * inFile()
{
Student * pHead=(Student *)malloc(sizeof(Student));
if(NULL==pHead)
{
cout<<" Allocation failure , The program terminates !"<<endl;
exit(0);
}
Student * pTail=pHead;
pTail->next=NULL;
ifstream in("c:\kcsj.txt");
if (!in.is_open())
{
cout << "Error opening file"<<endl;
exit(0);
}
while (!in.eof())
{
Stu st;
in.read(reinterpret_cast<char *>(&st), sizeof(st));
if (in.fail())
{
break;
}
Student * pNew=new Student();
if(NULL==pNew)
{
printf(" Allocation failure , The program terminates n");
exit(0);
}
pNew->setst(st);
pTail->next=pNew;
pNew->next=NULL;
pTail=pNew;
}
in.close();
return pHead;
}
void outFile()
{
ofstream out;
out.open("c:\kcsj.txt",ios_base::out|ios_base::app|ios::binary);
if(!out)
{
cout<<" The file does not exist. Don't forget to save the file when you exit !"<<endl;
out.close();
out.open("stu.dat",ios_base::out|ios::binary);
}
else
{
out.close();
out.open("c:\kcsj.txt",ios_base::out|ios_base::app|ios::binary);
}
Student * temp=pHead->next;
while(temp!=NULL)
{
Stu st=temp->getst();
out.write(reinterpret_cast<char *>(&st), sizeof(st));
temp=temp->next;
}
out.close();
}
Student * create_List()
{
int len;
Student * pHead=(Student *)malloc(sizeof(Student));
if(NULL==pHead)
{
cout<<" Allocation failure , The program terminates !"<<endl;
exit(0);
}
Student * pTail=pHead;
pTail->next=NULL;
cout<<" Please enter the number of students to store :";
cin>>len;
for(int i=0;i<len;i++)
{
Stu st;
cout<<" Please enter the first "<<i+1<<" Student's student number :";
cin>>st.no;
cout<<" Please enter the first "<<i+1<<" Student's name :";
cin>>st.name;
cout<<" Please enter the first "<<i+1<<" Students' math scores :";
cin>>st.math;
cout<<" Please enter the first "<<i+1<<" Students' Chinese scores :";
cin>>st.chi;
Student * pNew=new Student();
if(NULL==pNew)
{
printf(" Allocation failure , The program terminates n");
exit(0);
}
pNew->setst(st);
pTail->next=pNew;
pNew->next=NULL;
pTail=pNew;
}
return pHead;
}
void traverse_List(Student * pHead)
{
int i=1;
Student * temp=pHead->next;
while(temp!=NULL)
{
cout<<endl<<" The serial number :"<<i<<endl;
temp->show();
temp=temp->next;
i++;
}
}
bool is_empty(Student * pHead)
{
if(NULL==pHead->next)
{
return true;
}
else
{
return false;
}
}
int length_List(Student * pHead)
{
int len=0;
Student * temp=pHead->next;
while(temp)
{
len++;
temp=temp->next;
}
return len;
}
bool insert_List(Student * pHead,int position,Stu st)
{
int i=0;
Student * p=pHead;
while(NULL!=p&&i<position-1)
{
p=p->next;
i++;
}
if(i>position-i||NULL==p)
{
return false;
}
Student * pNew=(Student *)malloc(sizeof(Student));
if(NULL==pNew)
{
cout<<" Allocation failure , The program terminates "<<endl;
exit(0);
}
pNew->setst(st);
pNew->next=p->next;
p->next=pNew;
return true;
}
bool delete_List(Student * pHead,int position,Stu * st)
{
int i=0;
Student * p=pHead;
while(NULL!=p->next&&i<position-1)
{
p=p->next;
i++;
}
Student * q=p->next;
*st=q->getst();
p->next=p->next->next;
free(q);
q=NULL;
return true;
}
void sort_List(Student * pHead)
{
Student * p,* q;
Stu temp;
int i,j;
int len=length_List(pHead);
for(i=0,p=pHead->next;i<len-1;i++,p=p->next)
{
for(j=i+1,q=p->next;j<len;j++,q=q->next)
{
if(q->st.ave>p->st.ave)
{
temp=q->st;
q->st=p->st;
p->st=temp;
}
}
}
}
The above is all the content of this article, I hope you can enjoy it.