- 註冊時間
- 2009-3-5
- 最後登錄
- 2017-8-24
- 主題
- 查看
- 積分
- 48
- 閱讀權限
- 20
- 文章
- 63
- 相冊
- 0
- 日誌
- 0
狀態︰
離線
|
看來發文者已經過了交作業期限了 我來回一下這題巴
#include <iostream>
struct Score
{
char Mandarin;
char Math;
char English;
char Total;
};
class StudentData
{
public:
static char count;
char num[10];
char name[15];
struct Score score;
void Avg(StudentData p[]);
void Sqrt(StudentData p[]);
void Swap(StudentData *p1,StudentData *p2);
};
//分數加總/3
void StudentData::Avg(StudentData p[])
{
int temp;
for(char i=0;i<StudentData::count;i++){
temp=p[i].score.English;
temp+=p[i].score.Mandarin;
temp+=p[i].score.Math;
p[i].score.Total = temp/3;
}
return;
}
//排序
void StudentData::Sqrt(StudentData p[]){
for(int i=0;i<StudentData::count-1;i++)
for(int j=i;j < StudentData::count-1;j++)
if(p[j].score.Total>p[j+1].score.Total)
Swap(&p[j],&p[j+1]);
}
//交換
void StudentData::Swap(StudentData *p1,StudentData *p2){
StudentData temp;
char (*ptr1),(*ptr2);
ptr1= temp.num;
ptr2= p1->num;
for(int i=0;i<29;i++){*ptr1 = *ptr2;ptr1++;ptr2++;}
ptr1= p1->num;
ptr2= p2->num;
for(int i=0;i<29;i++){*ptr1 = *ptr2;ptr1++;ptr2++;}
ptr1= p2->num;
ptr2= temp.num;
for(int i=0;i<29;i++){*ptr1 = *ptr2;ptr1++;ptr2++;}
}
char StudentData::count=0;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
StudentData stu[100];
for(char i = 0;i<100;i++){
cout<<i<<"請輸入學號"<<endl;
cin >> stu[i].num ;
if(stu[i].num[0]=='#')
break;
cout<<i<<"請輸入姓名"<<endl;
cin >> stu[i].name ;
cout<<i<<"請輸入國文分數"<<endl;
scanf("%d",&stu[i].score.Mandarin) ;
cout<<i<<"請輸入數學分數"<<endl;
scanf("%d",&stu[i].score.Math) ;
cout<<i<<"請輸入英文分數"<<endl;
scanf("%d",&stu[i].score.English) ;
StudentData::count++;
}
stu[0].Avg(stu);
stu[0].Sqrt(stu);
for(char i=0;i<StudentData::count;i++)
{
cout<<"學號:"<< stu[i].num <<",姓名:"<<stu[i].name
<<"數:"<<int(stu[i].score.Math)
<<"國:"<<int(stu[i].score.Mandarin)
<<"英:"<<int(stu[i].score.English)
<<",總分:"<<int(stu[i].score.Total)<<endl;
}
system("pause");
return 0;
} |
|