C++有关问题求大神改错!实在是找不出来下面的代码错在哪里;了?

2025-06-21 15:14:17
推荐回答(3个)
回答1:

#include
#include
using namespace std;

const int Judgenum = 5;
const int PlayerNum = 8;
/*
    date:2015-4-10
    author:qcq
    e-mail:qinchuanqing918@163.com
*/
/*
 suggestion:既然你是使用++那么处理字符串的时候不应该使用C语言的字符数组
 而改成使用++的string类,这个代码在codeblock下边编译通过!
*/
class CompetitionResult
{
short num;
char name[20];
float score[Judgenum];
float average;
public:
CompetitionResult();
CompetitionResult(short n, char*ps);
float MaxScore();
float MinScore();
float GetAvg(){ return average; }//返回对象的平均分
void SetAvg();
short GetNo(){ return num; }//返回学生的编号!
void SetNo(int i){ num = i; }//设置学生的编号!
char *GetName(){ return name; }
float GetScore(int j){ return score[j]; }
void SetScore(int k, float avg){ score[k] = avg; }
friend void Sort(CompetitionResult*pr, int n);
};
CompetitionResult::CompetitionResult()
{
num = 0;
strcpy(name, " ");
for (int i = 0; i < Judgenum; i++)
score[i] = 0;
}
CompetitionResult::CompetitionResult(short n, char*ps)
{
num = n;
strcpy(name, ps);
for (int i = 0; i < Judgenum; i++)
score[i] = 0;
}
float CompetitionResult::MaxScore()
{
float max = score[0];
/*
 * i from 1 is ok!
 */
for (int i = 1; i < Judgenum; i++)
{
if (score[i]>max)
max = score[i];
}
return max;
}
float CompetitionResult::MinScore()
{
float min = score[0];
for (int i = 1; i < Judgenum; i++)
{
if (min > score[i])
min = score[i];
}
return min;
}
void CompetitionResult::SetAvg()
{
float total = 0;
for (int i = 0; i < Judgenum; i++)
{
total += score[i];
}
average = (total - MaxScore() - MinScore()) / (Judgenum - 2);
}
void Sort(CompetitionResult *pr, int n)
{
int i, j, k;
CompetitionResult temp;
for (i = 0; i < n - 1; i++)
{
k = i;
for (j = i + 1; j < n; j++)
if (pr[j].average > pr[k].average)
k = j;
if (k != i)
{
temp = pr[i];
pr[i] = pr[k];
pr[k] = temp;
}
}
}
int main()
{
CompetitionResult r[PlayerNum] = { CompetitionResult(1, "郭靖"), CompetitionResult(2, "黄蓉"), CompetitionResult(3, "程灵素"),
CompetitionResult(4, "张三"), CompetitionResult(5, "易志平"), CompetitionResult(5, "李四"),
CompetitionResult(7, "石中玉"), CompetitionResult(8, "霍青桐") };
float temp;
cout << "采用10分制,精确到小数点两位,得分兼用TAB分离" << endl;
for (int i = 0; i < PlayerNum; i++)
{
cout << "\n请评委给第" << r[i].GetNo() << "号选手打分:\n";
for (int j = 0; j < Judgenum; j++)
{
cin >> temp;
r[i].SetScore(j, temp);
}
r[i].SetAvg();
cout << endl;
}
Sort(r, PlayerNum);
cout << "\n\t*****比赛结果******\n";
cout << "\n\t号码\t姓名\t";
for (int j = 0; j < Judgenum; j++)
{
cout << "评委" << j + 1 << "\t";
}
cout << "平均分:\n";
cout.precision(3);
/*
            here i should define first before use it
*/

for (int i = 0; i < PlayerNum; i++)
{
    /*
            The format needed!
    */
    cout<<"\t";
cout << r[i].GetNo() << "\t";
cout << r[i].GetName() << "\t";
/*
            here j should define first before use it
*/
for (int j = 0; j cout << r[i].GetScore(j) << "\t";
/*
                here should need calculate the average score first
                before use it.
*/
        r[i].SetAvg();
cout << r[i].GetAvg() << endl;
}
return 0;
}

回答2:

main函数中,CompetitonResult(4,"张三")这个应该改为CompetitionResult(4,"张三")
for(i=0;i for(j=0;j

回答3:

mpetitionResult(3,"程灵素"),
CompetitonResult(4,"张三"),CompetitionResult(5,"易志平"),CompetitionResult(5,"李四"),
CompetitonResult(4,"张三")这个CompetitonResult写错了,少了个i 还有
for(i=0;i{
cout<cout<for(j=0;jcout<cout<}
给i j个类型