C + + to implement 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<< "School No.:" <<st.no<<endl;
cout<< "Name:" <<st.name<<endl;
cout<< "Math score:" <<st.math<<endl;
cout<< "Language score:" <<st.chi<<endl;
cout<< "Average score:" <<st.ave<<endl;
cout<< "----------------------------" <<endl;
}
};
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 option you want to operate:" <<endl;
cout<< "1. Add original Record" <<endl;
cout<< "2. Show all records in average order <<endl;
cout<< "3. Save original file" <<endl;
cout<< "4. Read Original file" <<endl;
cout<< "5. Delete original file" <<endl;
cout<< "6. Insert a single record" <<endl;
cout<< "7. Delete single record" <<endl;
cout<< "8. Show total number of records" <<endl;
cout<< "9. End program Run" <<endl<<endl;
cout<< "Left digital corresponding function selection, please select 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<< Record total bar number 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<< "record total number of zeros" <<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, enter the serial number:";
cin>>num;
cout<< "Edit the record to insert:" <<endl;
cout<< "School Number:";
cin>>st.no;
cout<< "Name:";
cin>>st.name;
cout<< "Mathematical results:";
cin>>st.math;
cout<< "Language score:";
cin>>st.chi; if (Insert_list (phead,num-1,st)) {cout<< "Insert succeeded!"
<<endl; } else {cout<< "Insert failed!"
<<endl;
} getchar ();
GetChar ();
System ("CLS");
Menu_select ();
Break
Case 7: {if (null==phead) {cout<< Record total bar number 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<< "successfully deleted the record as follows:" <<endl;
cout<< "School No.:" <<st->no<<endl<< "Name:" <<st->name<<endl; } else {cout<< "Delete failed!"
<<endl;
} getchar ();
GetChar ();
System ("CLS");
Menu_select ();
Break
Case 8: {if (null!=phead) {System ("CLS");
cout<< "Total number of records:" <<length_list (phead) << "Bar" <<endl;
GetChar ();
GetChar ();
System ("CLS");
Menu_select ();
else {cout<< "record total bar number zero" <<endl;
GetChar ();
GetChar ();
System ("CLS");
Menu_select ();
} break;
Case 9: {System ("CLS"); cout<< "Exit successfully!"
<<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 failed, program terminated!"
<<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 failed, program terminated \ 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<< "file does not exist, do not forget to save file when exiting!"
<<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 failed, program terminated!"
<<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" <<i+1<< "Student's School Number:";
cin>>st.no;
cout<< "Please enter the first" <<i+1<< "Student's name:";
cin>>st.name;
cout<< "Please enter the" <<i+1<< "Student's math score:";
cin>>st.math; cout<< "Please enter the <<i+1<<"A student's language score: ";
cin>>st.chi;
Student * pnew=new Student ();
if (null==pnew) {printf ("Allocation failed, program terminated \ 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<< "serial number:" <<i<<endl;
Temp->show ();
temp=temp->next;
i++;
} bool Is_empty (Student * phead) {if (Null==phead->next) {return true;
else {return false;
an 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 failed, program terminated" <<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 the entire contents of this article, I hope you can enjoy.