请问怎样把这个 C++ 改成 C 程序,求大神指教方法
#include<iostream> #include<fstream>
#include<process.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
class Student
{
public:
char name[20];
char Id[20];
char sex[4];
int age;
int Cnum;
int Mnum;
int Enum;
int math;
int sum;
int average;
Student * Next;
void Input()
{
cout<<"\t\t请输入学生的姓名:"; cin>>name;
cout<<"\t\t请输入学生的学号:"; cin>>Id;
cout<<"\t\t请输入学生的性别:"; cin>>sex;
cout<<"\t\t请输入学生的年龄:"; cin>>age;
cout<<"\t\t请输入C++课程的成绩:"; cin>>Cnum;
cout<<"\t\t请输入汇编课程的成绩:"; cin>>Mnum;
cout<<"\t\t请输入嵌入式操作系统课程的成绩:"; cin>>Enum;
cout<<"\t\t请输入数学成绩:"; cin>>math;
sum=Cnum+Mnum+Enum+math;
average=sum/4;
}
void ReadFile(istream & in)
{
in>>name>>Id>>sex>>age>>Cnum>>Mnum>>Enum>>math>>sum>>average;
}
void Show()
{
cout<<"姓名:"<<name<<endl<<"学号:"<<Id<<endl<<"性别:"<<sex<<endl<<"年龄:"<<age<<endl<<"C++:"<<Cnum<<endl
<<"汇编:"<<Mnum<<endl<<"嵌入式操作系统:"<<Enum<<endl<<"数学:"<<math<<endl<<"总成绩:"<<sum<<endl<<"平均成绩:"<<average<<endl<<endl<<endl;
}
};
class Studentmassage
{
public:
Studentmassage();
~Studentmassage();
void ShowMenu();
void Find();
void Save();
void ModifyItem();
void RemoveItem();
void Swap(Student *,Student *);
void Sort();
void t(); //statistic
//void Unpass();
int ListCount();
//void Average();
void Display()
{
for(Student * p=Head->Next;p!=End;p=p->Next)
p->Show();
cout<<"输入任意字符!继续……";
getch();
}
void AddItem()
{
End->Input();
End->Next=new Student;
End=End->Next;
cout<<"添加成功!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
private:
Student * Head,* End;
ifstream in;
ofstream out;
Student *FindItem(char * name)
{
for(Student * p=Head;p->Next!=End;p=p->Next)
if(!strcmp(p->Next->name,name))return p;
return NULL;
}
Student *FindID(char * Id)
{
for(Student * p=Head;p->Next!=End;p=p->Next)
if(!strcmp(p->Next->Id,Id))return p;
return NULL;
}
};
Studentmassage::Studentmassage()
{
Head=new Student;
Head->Next=new Student;
End=Head->Next;
in.open("sort.txt");
if(!in)
{
cout<<"这是一个新系统,无学生信息。请先输入。"<<endl; }
else
{
while(!in.eof())
{
End->ReadFile(in);
if(End->name[0]=='\0')break;
End->Next=new Student;
End=End->Next;
}
in.close();
cout<<"\t\t读取学生信息成功!"<<endl;
}
}
Studentmassage::~Studentmassage()
{
Save();
for(Student * temp;Head->Next!=End;)
{
temp=Head->Next;
Head->Next=Head->Next->Next;
delete temp;
}
delete Head,End;
}
void Studentmassage::ShowMenu()
{
cout<<"********************************************************************************"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓〓 ☆ 学 生 成 绩 管 理 系 统 ☆ 〓〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓★★★★★ ★★★★★★★ ★★★★★〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ A.增加学生成绩 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ S.显示学生成绩 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ H.排序统计成绩 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ Q.查找学生成绩 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ D.删除学生成绩 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ M.修改学生信息 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ T.统计学生信息 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"〓〓〓〓〓〓〓〓〓★ ☆ E.安全退出系统 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl;
cout<<"\n\t\t\n\t\t请选择:";
}
void Studentmassage::Find()
{
char name[20] ,Id[10];
int x;
Student * p=NULL;
cout<<"\n\t\t*********************************\n";
cout<<"\t\t※ 1.按学生的姓名查找\n\t\t※ 2.按学生学号查找";
cout<<"\n\t\t*********************************\n请选择:";
cin>>x;
switch(x)
{
case 1:{cout<<"\t\t请输入要查找的学生的姓名:";cin>>name;
if(p=FindItem(name))
{
p->Next->Show();
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"\t\t没有找到该姓名的学生!"<<'\n'<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}break;
case 2:
{
cout<<"\t\t请输入要查找的学生的学号:";cin>>Id;
if(p=FindID(Id))
{
p->Next->Show();
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"\t\t没有找到该学好的学生!"<<'\n'<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}break;
}
}
void Studentmassage::t()
{
int e=0,g1=0,g2=0,g3=0,g4=0,g5=0,n=0;
for(Student * p=Head->Next;p!=End;p=p->Next)
{
n++;
e=e+p->Cnum;
if(p->Cnum>=90)
g1++;
else if(p->Cnum>=80)
g2++;
else if(p->Cnum>=70)
g3++;
else if(p->Cnum>=60)
g4++;
}
cout<<"c++的平均分是:"<<e/n<<endl;
cout<<"90~100"<<ends<<g1<<endl<<"80~89"<<ends<<g2<<endl<<"70~79"<<ends<<g3<<endl<<"60~69"<<ends<<g4<<endl<<"<60"<<ends<<n-g1-g2-g3-g4<<endl;
e=0;g1=0;g2=0;g3=0;g4=0;
for(Student * q=Head->Next;q!=End;q=q->Next)
{
e=e+q->Mnum;
if(q->Mnum>=90)
g1++;
else if(q->Mnum>=80)
g2++;
else if(q->Mnum>=70)
g3++;
else if(q->Mnum>=60)
g4++;
}
cout<<"汇编课程的平均分是:"<<e/n<<endl;
cout<<"90~100"<<ends<<g1<<endl<<"80~89"<<ends<<g2<<endl<<"70~79"<<ends<<g3<<endl<<"60~69"<<ends<<g4<<endl<<"<60"<<ends<<n-g1-g2-g3-g4<<endl;
e=0;g1=0;g2=0;g3=0;g4=0;
for(Student * h=Head->Next;h!=End;h=h->Next)
{
e=e+h->Enum;
if(h->Enum>=90)
g1++;
else if(h->Enum>=80)
g2++;
else if(h->Enum>=70)
g3++;
else if(h->Enum>=60)
g4++;
}
cout<<"嵌入式操作系统的平均分是:"<<e/n<<endl;
cout<<"90~100"<<ends<<g1<<endl<<"80~89"<<ends<<g2<<endl<<"70~79"<<ends<<g3<<endl<<"60~69"<<ends<<g4<<endl<<"<60"<<ends<<n-g1-g2-g3-g4<<endl;
e=0;g1=0;g2=0;g3=0;g4=0;
for(Student * k=Head->Next;k!=End;k=k->Next)
{
e=e+k->math;
if(k->math>=90)
g1++;
else if(k->math>=80)
g2++;
else if(k->math>=70)
g3++;
else if(k->math>=60)
g4++;
}
cout<<"数学的平均分是:"<<e/n<<endl;
cout<<"90~100"<<ends<<g1<<endl<<"80~89"<<ends<<g2<<endl<<"70~79"<<ends<<g3<<endl<<"60~69"<<ends<<g4<<endl<<"<60"<<ends<<n-g1-g2-g3-g4<<endl;
getch();
}
void Studentmassage::ModifyItem()
{
char name[20];
Student * p=NULL;
cout<<"\t\t请输入要修改的人的姓名:";cin>>name;
if(p=FindItem(name))
{
cout<<"\t\t已找到学生的信息,请输入新的信息!"<<endl;
p->Next->Input();
cout<<"修改成功!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"\t\t没有找到!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}
void Studentmassage::RemoveItem()
{
char name[20];
Student * p=NULL,*temp=NULL;
cout<<"\t\t请输入要删除的学生的姓名:"<<endl;cin>>name;
if(p=FindItem(name))
{
temp=p->Next;
p->Next=p->Next->Next;
delete temp;
cout<<"\t\t删除成功!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
else
{
cout<<"\t\t没有找到!"<<endl;
cout<<"输入任意字符!继续……";
getch();
}
}
void Studentmassage::Swap(Student *p1, Student *p2)
{
Student *temp=new Student;
strcpy(temp->name,p1->name);
strcpy(temp->Id,p1->Id);
strcpy(temp->sex,p1->sex);
temp->age=p1->age;
temp->Cnum=p1->Cnum;
temp->Mnum=p1->Mnum;
temp->Enum=p1->Enum;
temp->math=p1->math;
temp->sum=p1->sum;
temp->average=p1->average;
strcpy(p1->name,p2->name);
strcpy(p1->Id,p2->Id);
strcpy(p1->sex,p2->sex);
p1->age=p2->age;
p1->Cnum=p2->Cnum;
p1->Mnum=p2->Mnum;
p1->Enum=p2->Enum;
p1->math=p2->math;
p1->sum=p2->sum;
p1->average=p2->average;
strcpy(p2->name,temp->name);
strcpy(p2->Id,temp->Id);
strcpy(p2->sex,temp->sex);
p2->age=temp->age;
p2->Cnum=temp->Cnum;
p2->Mnum=temp->Mnum;
p2->Enum=temp->Enum;
p2->math=temp->math;
p2->sum=temp->sum;
p2->average=temp->average;
}
int Studentmassage::ListCount()
{
if(! Head)
return 0;
int n=0;
for(Student * p=Head->Next;p!=End;p=p->Next)
{
n++;
}
return n;
}
void Studentmassage::Sort()
{
cout <<"Sorting..."<<endl;
Student *p=NULL,*p1=NULL,*k=NULL;
int n=Studentmassage::ListCount();
if(n<2)
return;
for(p=Head->Next;p!=End;p=p->Next)
for(k=p->Next;k!=End;k=k->Next)
{
if(p->sum>k->sum)
{
Studentmassage::Swap(p,k);
}
}
cout <<"排序完成!"<<endl;
getch();
return;
}
void Studentmassage::Save()
{
out.open("sort.txt");
for(Student *p=Head->Next;p!=End;p=p->Next)
out<<p->name<<"\t"<<p->Id<<"\t"<<p->Cnum<<"\t"
<<p->Mnum<<"\t"<<p->Enum<<"\t"<<p->sum<<'\n';
out.close();
}
int main()
{
system("color f5");
int i=0;
char x;
bool quit=false;
cout<<"\t\t§§§§§§§§§§§§§§§§§§§§§§§§§§"<<endl;
for(i=0;i<3;i++)
cout<<"\t\t◎\t\t\t\t\t\t ◎"<<endl;
cout<<"\t\t◎★★★★【 欢迎进入学生成绩管理系统 】★★★★◎"<<endl;
for(i=0;i<3;i++)
cout<<"\t\t◎\t\t\t\t\t\t ◎"<<endl;
cout<<"\t\t§§§§§§§§§§§§§§§§§§§§§§§§§§\n"<<endl;;
Studentmassage Grade;
cout<<"按任意键开始……";
getch();
while(!quit)
{
system("cls");
Grade.ShowMenu();
cin>>x;
switch(x)
{
case 'E':quit=true;break;
case 'A':Grade.AddItem();break;
case 'S':Grade.Display();break;
case 'H':Grade.Sort();break;
case 'Q':Grade.Find();break;
case 'D':Grade.RemoveItem();break;
case 'T':Grade.t();break;
case 'M':Grade.ModifyItem();break;
}
}
return 0;
}