求大神帮忙改一下源程序,小弟初学,很多东西弄不明白。
就是改一下,能不能用一维数组来解题。急求呀!!!#include<iostream>
#include<string>
#include<iomanip>
Using namespace std;
class Stu
{
public:
char number[4];
char name[9];
char Class[9];
float chinese;
float math;
float English;
float computer;
float sport;
Stu *next;
float sum;
};
class Stu
{
public:
Student() {Head=NULL; }
void DisplayList();
void InsertNode(Stu* position);
void EditNode();
Stu *FindNode(char number[4]);
Stu *FindNode1(char Class[9]);
Stu *FindNode2(char name[9]);
void totalScores();
void ScoresAverage();
void SortList();
void AnalyzeScores();
void printData(Stu *c);
private:
Stu *Head;
char FileName[100];
};
bool InputRecord(Stu *q)
{
do
{
cout<<"学号:";
cin>>q->number;
}
while(strlen(q->number)>4);
do
{
cout<<"姓名:";
cin>>q->name;
}
while(strlen(q->name)>9);
do
{
cout<<"班级:";
cin>>q->Class;
}
while(strlen(q->Class)>9);
do
{
cout<<"语文:";
cin>>q->chinese;
}
while(q->chinese>100||q->chinese<0);
do
{
cout<<"数学:";
cin>>q->math;
}
while(q->math>100||q->math<0);
do
{
cout<<"英语:";
cin>>q->English;
}
while(q->English>100||q->English<0);
do
{
cout<<"计算机:";
cin>>q->computer;
}
while(q->computer>100||q->computer<0);
do
{
cout<<"体育:";
cin>>q->sport;
cout<<endl<<"****************************************************";
}
while(q->sport>100||q->sport<0);
return true;
}
void Student::InsertNode(Stu* position)
{
if(Head==NULL)
Head=position;
else
{
if(FindNode(position->number) || FindNode(position->name))
{
cout<<"警告,存在相同学号的学生信息!";
cout<<"****************************************************"<<endl;
return;
}
else
{
Stu *p;
p=Head;
while(p->next!=NULL)
p=p->next;
p->next=position;
}
}
position->next=NULL;
}
void Student::DisplayList()
{
float sum;
if(!Head)
{
cout<<endl<<"内容为空!"<<endl;
return;
}
cout<<endl;
cout<<setw(8)<<"学号";
cout<<setw(8)<<"姓名";
cout<<setw(8)<<"班级";
cout<<setw(8)<<"语文";
cout<<setw(8)<<"数学";
cout<<setw(8)<<"英语";
cout<<setw(8)<<"计算机";
cout<<setw(8)<<"体育";
cout<<setw(8)<<"总分";
cout<<endl<<"****************************************************";
Stu *p;
p=Head;
while(p)
{
cout<<setw(8)<<p->number;
cout<<setw(8)<<p->name;
cout<<setw(8)<<p->Class;
cout<<setw(8)<<p->chinese;
cout<<setw(8)<<p->math;
cout<<setw(8)<<p->English;
cout<<setw(8)<<p->computer;
cout<<setw(8)<<p->sport;
sum = p->chinese+p->computer+p->math+p->English+p->sport;
cout<<setw(8)<<sum<<endl;
p=p->next;
}
cout<<endl<<"*************************************************"; }
void Student::printData(Stu *p)
{
cout<<endl;
cout<<setw(8)<<"班级"<<setw(8)<<"姓名";
cout<<setw(8)<<"学号"<<setw(8)<<"语文";
cout<<setw(8)<<"数学"<<setw(8)<<"英语";
cout<<setw(8)<<"计算机"<<setw(8)<<"体育"<<endl;
cout<<setw(8)<<p->Class;
cout<<setw(8)<<p->name;
cout<<setw(9)<<p->number;
cout<<setw(8)<<p->chinese;
cout<<setw(8)<<p->math;
cout<<setw(8)<<p->English;
cout<<setw(8)<<p->computer;
cout<<setw(8)<<p->sport;
}
void Student::EditNode()
{
char number[4];
do
{ cout<<endl<<"输入修改的学生学号";
cin>>number;
}
while(strlen(number)>4);
Stu *p1;
if(p1=FindNode(number))
{
cout<<endl;
cout<<"请输入该同学的新的信息:";
InputRecord(p1);
cout<<" 修改成功!";
cout<<endl<<"***********************************************";
}
else
cout<<"没有找到该学生信息,修改失败!";
cout<<endl<<"******************************************";
}
void PrintRecord(Stu *p1)
{
cout<<endl;
cout<<setw(4)<<"学号"<<setw(9)<<"姓名";
cout<<setw(9)<<"班级"<<setw(8)<<"语文";
cout<<setw(8)<<"数学"<<setw(8)<<"英语";
cout<<setw(8)<<"计算机"<<setw(8)<<"体育"<<endl;
cout<<setw(4)<<p1->number;
cout<<setw(9)<<p1->name;
cout<<setw(9)<<p1->Class;
cout<<setw(8)<<p1->chinese;
cout<<setw(8)<<p1->math;
cout<<setw(8)<<p1->English;
cout<<setw(8)<<p1->computer;
cout<<setw(8)<<p1->sport;
}
Stu *Student::FindNode(char number[4])
{
Stu *p1;
p1=Head;
while(p1)
{
if(strcmp(p1->number,number))
p1=p1->next;
else break;
}
if(p1)
{
cout<<endl;
cout<<setw(4)<<"学号"<<setw(9)<<"姓名"<<setw(9)<<"班级"<<setw(8)<<"语文";
cout<<setw(8)<<"数学"<<setw(8)<<"英语"<<setw(8)<<"计算机"<<setw(8)<<"体育"<<endl;
cout<<setw(4)<<p1->number;
cout<<setw(9)<<p1->name;
cout<<setw(9)<<p1->Class;
cout<<setw(8)<<p1->chinese;
cout<<setw(8)<<p1->math;
cout<<setw(8)<<p1->English;
cout<<setw(8)<<p1->computer;
cout<<setw(8)<<p1->sport;
cout<<endl<<"*************************************";
}
else cout<<endl;
return p1;
}
Stu *Student::FindNode2(char name[9])
{
Stu *p1;
p1=Head;
while(p1)
{
if(strcmp(p1->name,name))
p1=p1->next;
else break;
}
if(p1)
{
cout<<endl;
cout<<setw(4)<<"学号"<<setw(9)<<"姓名"<<setw(9)<<"班级"<<setw(8)<<"语文";
cout<<setw(8)<<"数学"<<setw(8)<<"英语"<<setw(8)<<"计算机"<<setw(8)<<"体育"<<endl;
cout<<setw(4)<<p1->number;
cout<<setw(9)<<p1->name;
cout<<setw(9)<<p1->Class;
cout<<setw(8)<<p1->chinese;
cout<<setw(8)<<p1->math;
cout<<setw(8)<<p1->English;
cout<<setw(8)<<p1->computer;
cout<<setw(8)<<p1->sport;
cout<<endl;
cout<<endl<<"*******************************";
}
else cout<<endl;
return p1;
}
Stu *Student::FindNode1(char Class[9])
{
int flag=0;
Stu *p;
p=Head;
while(p!=NULL)
{
if(!strcmp(p->Class,Class))
{ printData(p);
flag=1;
}
p=p->next;
}
if(!flag)
cout<<"没有找到你要的信息!";
cout<<endl<<"*************************";
return p;
}
void Student::totalScores()
{
char number[4];
float sum=0;
do
{ cout<<endl<<"输入学生学号";
cin>>number;
}
while(strlen(number)>4);
Stu *p1;
if(p1=FindNode(number))
{ sum = p1->chinese+p1->computer+p1->math+p1->English+p1->sport;
cout<<"***********************************总分:";
cout<<sum<<"*************************************";
cout<<"************************************";
}
else cout<<"输入错误!"<<endl<<"******************";
}
void Student::ScoresAverage()
{
char number[4];
float sum=0,avg;
do{ cout<<endl<<"输入学生学号";
cin>>number;
}
while(strlen(number)>4);
Stu *p1;
if(p1=FindNode(number))
{ sum = p1->chinese+p1->computer+p1->math+p1->English+p1->sport; avg=sum/5;
cout<<"平均分:"<<sum/5<<endl;
cout<<"******************************";
}
}
void Student::SortList()
{
if(!Head)
{ cout<<endl<<"内容为空!"<<endl;
return;
}
Stu *p1,*p2;
p1=Head;
p2=Head;
while(p2)
{
float sum_max=p2->sum;
Stu *t1,*t2,*t3,*t4;
t3=p2;
t4=p2;
t1=p2;
t2=p2->next;
while(t2)
{
if(sum_max<t2->sum)
{ sum_max=t2->sum;
t3=t1;
t4=t2;
}
t1=t2;
t2=t2->next;
}
if(p2->sum<t4->sum)
{
Stu *q3;
if(p2==Head)
{
Head=t4;
q3=t4->next;
if(p2==t3)
{
t4->next=p2;
p2->next=q3;
}
else
{
t4->next=p2->next;
t3->next=p2;
p2->next=q3;
}
p1=Head;
p2=p1->next;
}
else
{
if(p2==t3)
{
p1->next=t4;
p2->next=t4->next;
t4->next=p2;
}
else
{
p1->next=t4;
q3=t4->next;
t4->next=p2->next;
t3->next=p2;
p2->next=q3;
}
p1=p1->next;
p2=p1->next;
}
}
else
{
p1=p2;
p2=p2->next;
}
}
}
void Student::AnalyzeScores() {
Stu *p;
p=Head;
char Class[8];
float sum_chinese=0;
float sum_maths=0;
float sum_english=0;
float sum_computer=0;
float sum_sports=0;
int i=0;
do
{
cout<<"请输入要统计成绩的的班级名称:";
cin>>Class;
cout<<endl<<"*******************************"; }
while(strlen(Class)>8);
while(p)
{
if(!strcmp(p->Class,Class))
{
sum_chinese=sum_chinese+p->chinese;
sum_maths=sum_maths+p->math;
sum_english=sum_english+p->English;
sum_computer=sum_computer+p->computer;
sum_sports=sum_sports+p->sport;
i++;
}
p=p->next;
}
if(i==0)
{
cout<<"无此班级!";
cout<<endl<<"***************************";
return;
}
cout<<endl;
cout<<setw(10)<<"科目"<<setw(10)<<"平均分"<<setw(10)<<"总分"<<endl;
cout<<setw(10)<<"语文"<<setw(10)<<sum_chinese/i<<setw(10)<<sum_chinese<<endl;
cout<<setw(10)<<"数学"<<setw(10)<<sum_maths/i<<setw(10)<<sum_maths<<endl;
cout<<setw(10)<<"外语"<<setw(10)<<sum_english/i<<setw(10)<<sum_english<<endl;
cout<<setw(10)<<"计算机"<<setw(10)<<sum_computer/i<<setw(10)<<sum_computer<<endl;
cout<<setw(10)<<"体育"<<setw(10)<<sum_sports/i<<setw(10)<<sum_sports;
cout<<endl<<"*********************************";
}
void main()
{
int choi;
Student ST;
bool Flag=true;
while(Flag)
{
cout<<" 学生成绩管理系统"<<endl;
cout<<" ################"<<endl;
cout<<" 1、输入学生成绩"<<endl;
cout<<" 2、修改学生成绩"<<endl;
cout<<" 3、删除学生成绩"<<endl;
cout<<" 4、计算每位学生的总分"<<endl;
cout<<" 5、计算每位学生的平均分"<<endl;
cout<<" 6、按学号或姓名查询学生成绩"<<endl;
cout<<" 7、按班级查询学生成绩"<<endl;
cout<<" 8、成绩排序"<<endl;
cout<<" 9、按班级统计学科总分、平均分等"<<endl;
cout<<"请选择(1-9,0:退出):" endl;
cout<<" ##################"<<endl;
int menu,choice;
//for(int i=0;i<10;i++)
// cout<<menu[i]<<endl;
cout<<menu;
cin>>choice;
switch(choice)
{
case 1:
Stu *qq;
qq=new Stu;
if(InputRecord(qq))
ST.InsertNode(qq);
break;
case 2:ST.EditNode();break;
case 3:ST.totalScores();break;
case 4:ST.ScoresAverage();break;
case 5: char number[4];
char name[9];
int key;
cout<<"选择按学号/姓名查找: [1]学号;[2]姓名 ";
cin>>key;
if(key==1)
{
do
{ cout<<endl<<"输入要查询的学生学号:";
cin>>number;
}
while(strlen(number)>4);
ST.FindNode(number);
}
else if(key==2)
{
do
{ cout<<endl<<"输入要查询的学生姓名:";
cin>>name;
}
while(strlen(name)>9);
ST.FindNode2(name);
}
else cout<<"对不起,您的输入有误!";
cout<<endl<<"****************************";
break;
case 6:
char Class[9];
do{ cout<<endl<<"输入要查询的学生班级:";
cin>>Class;
}while(strlen(Class)>9);
ST.FindNode1(Class);break;
case 7:
ST.SortList();
ST.DisplayList();
break;
case 8:ST.AnalyzeScores();break;
case 9:ST.DisplayList(); break;
case 0:Flag=false;
cout<<"谢谢使用,再见!";
cout<<endl<<"***************************";
break;
default:
cout<<"对不起,您的输入有误!";
cout<<endl<<"*************";
}
}
}