?? classaverage.cpp
字號:
#include"head.h"
#include<iostream.h>
extern void SortEachClass(Student* &);
void ClassAverage(Student* &head)
{
Student *pGuard;
Student *p;
double Average[MAX]; //各科目平均成績
double TotalAverage; //總平均成績
int ClassNum; //鏈表中的班級數
int EachClassNum; //每個班級的學生數
SortEachClass(head);
for(ClassNum=1,pGuard=head,p=pGuard;pGuard!=NULL;pGuard=pGuard->next)
{
if(pGuard->Class!=p->Class)
{
ClassNum++;
}
p=pGuard;
} //求出班級個數
pGuard=head;
for(int icount=0;icount<ClassNum;icount++)
{
for(int i=0;i<MAX;i++)
{
Average[i]=0;
}
TotalAverage=0;
EachClassNum=0; //以上把所有數據置為0
/* p=pGuard;
pGuard=p->next;
do
{
EachClassNum++; //每個班級的學生數自增
TotalAverage+=p->Average;
for(int i=0;i<MAX;i++)
{
Average[i]+=p->Score[i];
}
p=pGuard;
pGuard=pGuard->next;
}
while(pGuard!=NULL&&p->Class==pGuard->Class);*/
for(p=pGuard,pGuard=pGuard->next;pGuard!=NULL&&p->Class==pGuard->Class;pGuard=pGuard->next)
{
EachClassNum++; //每個班級的學生數自增
TotalAverage+=p->Average;
for(int i=0;i<MAX;i++)
{
Average[i]+=p->Score[i];
}
p=pGuard;
}
if(pGuard==NULL) //當pGuard為NULL時,還需要加上p節點否則結果會出錯
{
EachClassNum++;
TotalAverage+=p->Average;
for(int i=0;i<MAX;i++)
{
Average[i]+=p->Score[i];
}
}
TotalAverage/=EachClassNum;
for(int count=0;count<MAX;count++)
{
Average[count]/=EachClassNum;
}
cout<<" "<<p->Class<<"班各科目的平均成績如下:"<<endl;
cout<<" 微積分平均成績: "<<Average[0]<<endl;
cout<<" 物理平均成績: "<<Average[1]<<endl;
cout<<" 英語平均成績: "<<Average[2]<<endl;
cout<<" C語言平均成績: "<<Average[3]<<endl;
cout<<" 體育平均成績: "<<Average[4]<<endl;
cout<<" 所有科目的平均成績為: "<<TotalAverage<<endl<<endl<<endl;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -