C++问题,设计一个评委评分程序,即去掉最高分和最低分求平均分值?
发布网友
发布时间:2022-04-24 04:19
我来回答
共3个回答
热心网友
时间:2023-10-27 11:04
你定义的double类型数组a[]没有进行初始化。所以像max=a[0]就会报错。在double a[10]; 后加 memset(a,0,sizeof(0)); 语句即可。另一种做法:既然是在去掉最高分和最低分。你完全可以对你输入的数组进行排序,然后用for循环对a[1]--a[n-1]进行ave处理。
热心网友
时间:2023-10-27 11:04
a使用之前没有被初始化
这样改嘛double a[10] = {0};
max = a[0] 使用了a,你之前没有初始化a,所以出错。
热心网友
时间:2023-10-27 11:05
我这个是C++的代码 里面含有标准库。
#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
#include<ctime>
using namespace std;
class Person
{
public:
Person(){};
Person(string name, double score)
{
this->m_Name = name;
this->m_Score = score;
}
string m_Name;
double m_Score; //平均分
};
void creatPerson(vector<Person>&v,int n)
{
int score = 0;
for (int i = 0; i < n; ++i)
{
string name1 = to_string(i+1); //int类型转化为string类型
string name2 = "号选手";
name1 += name2;
Person p(name1, score);
v.push_back(p);
}
}
void printVector(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Name << "最终平均分数为:" << it->m_Score << endl;
}
}
void printVector2(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Name << " ";
}
cout << endl;
}
void setVector(vector<Person>&v,int m)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
deque<int>d;
cout << "已为" << it->m_Name <<"打了"<<m <<"次分:" << endl;
for (int i = 0; i < m; ++i)
{
int score = rand() % 41 + 60; // 打分区间60~100
d.push_back(score);
}
for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
cout << *dit << " ";
}
cout << endl;
sort(d.begin(), d.end());
d.pop_front();
d.pop_back();
/*for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
cout << *dit << " ";
}
cout << endl;*/
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
sum += *dit;
}
//cout << "sum=" << sum << "数量" << d.size() << endl;
double avg = sum*1.0 / d.size();
it->m_Score = avg;
}
cout << endl;
}
void sortScore(vector<Person>&v)
{
vector<Person>::iterator it = v.begin();
Person t;
for (unsigned int i = 0; i < v.size(); ++i)
{
for (unsigned int j = 0; j < v.size() - i - 1; ++j)
{
if (it[j].m_Score>it[j + 1].m_Score) //比较分数
{
t = it[j]; //更换整体
it[j] = it[j + 1];
it[j + 1] = t;
}
}
}
}
int main(void)
{
cout << "软件说明:" << endl;
cout << "该软件为N个评委为M个选手打分,这样每一个选手就收到N次打分,去掉一个最低分和一个最高分,算出选手的平均分,并为这些选手按平均分从低到高排序。" << endl;
cout << endl;
cout << "请输入选手的个数N=";
int n;
while (true)
{
char c = cin.peek();
if (c > '0'&& c <= '9')
{
cin >> n;
cout << "你设置的选手人数为:" << n << endl;
break;
}
cin.clear(); //重置标志位
cin.sync(); //清空缓冲区
cout << "你输入不正确,请重新输入" << endl;
}
getchar();
cout << endl;
cout << "请输入评委的个数M=";
int m;
while (true)
{
char c = cin.peek();
if (c > '0'&& c <= '9')
{
cin >> m;
cout << "你设置的评委人数为:" << m << endl;
break;
}
cin.clear(); //重置标志位
cin.sync(); //清空缓冲区
cout << "你输入不正确,请重新输入" << endl;
}
cout << endl;
cout << "请这"<<m<<"个评委为选手打分:"<<endl;
cout << "(为了避免手动输入分数浪费时间,下面让计算机随机为选手打"<<m<<"次分。)" << endl;
cout << endl;
srand((unsigned int)time(NULL)); //随机数种子
vector<Person>v;
creatPerson(v,n);
//printVector(v);
setVector(v,m);
cout << endl;
cout << "去掉一个最低分,去掉一个最高分:"<<endl;
printVector(v);
cout << endl;
cout << "选手成绩由低到高排序如下:" << endl;
sortScore(v);
printVector2(v);
system("pause");
return 0;
}
热心网友
时间:2023-10-27 11:04
你定义的double类型数组a[]没有进行初始化。所以像max=a[0]就会报错。在double a[10]; 后加 memset(a,0,sizeof(0)); 语句即可。另一种做法:既然是在去掉最高分和最低分。你完全可以对你输入的数组进行排序,然后用for循环对a[1]--a[n-1]进行ave处理。
热心网友
时间:2023-10-27 11:04
a使用之前没有被初始化
这样改嘛double a[10] = {0};
max = a[0] 使用了a,你之前没有初始化a,所以出错。
热心网友
时间:2023-10-27 11:05
我这个是C++的代码 里面含有标准库。
#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
#include<ctime>
using namespace std;
class Person
{
public:
Person(){};
Person(string name, double score)
{
this->m_Name = name;
this->m_Score = score;
}
string m_Name;
double m_Score; //平均分
};
void creatPerson(vector<Person>&v,int n)
{
int score = 0;
for (int i = 0; i < n; ++i)
{
string name1 = to_string(i+1); //int类型转化为string类型
string name2 = "号选手";
name1 += name2;
Person p(name1, score);
v.push_back(p);
}
}
void printVector(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Name << "最终平均分数为:" << it->m_Score << endl;
}
}
void printVector2(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Name << " ";
}
cout << endl;
}
void setVector(vector<Person>&v,int m)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
deque<int>d;
cout << "已为" << it->m_Name <<"打了"<<m <<"次分:" << endl;
for (int i = 0; i < m; ++i)
{
int score = rand() % 41 + 60; // 打分区间60~100
d.push_back(score);
}
for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
cout << *dit << " ";
}
cout << endl;
sort(d.begin(), d.end());
d.pop_front();
d.pop_back();
/*for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
cout << *dit << " ";
}
cout << endl;*/
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
sum += *dit;
}
//cout << "sum=" << sum << "数量" << d.size() << endl;
double avg = sum*1.0 / d.size();
it->m_Score = avg;
}
cout << endl;
}
void sortScore(vector<Person>&v)
{
vector<Person>::iterator it = v.begin();
Person t;
for (unsigned int i = 0; i < v.size(); ++i)
{
for (unsigned int j = 0; j < v.size() - i - 1; ++j)
{
if (it[j].m_Score>it[j + 1].m_Score) //比较分数
{
t = it[j]; //更换整体
it[j] = it[j + 1];
it[j + 1] = t;
}
}
}
}
int main(void)
{
cout << "软件说明:" << endl;
cout << "该软件为N个评委为M个选手打分,这样每一个选手就收到N次打分,去掉一个最低分和一个最高分,算出选手的平均分,并为这些选手按平均分从低到高排序。" << endl;
cout << endl;
cout << "请输入选手的个数N=";
int n;
while (true)
{
char c = cin.peek();
if (c > '0'&& c <= '9')
{
cin >> n;
cout << "你设置的选手人数为:" << n << endl;
break;
}
cin.clear(); //重置标志位
cin.sync(); //清空缓冲区
cout << "你输入不正确,请重新输入" << endl;
}
getchar();
cout << endl;
cout << "请输入评委的个数M=";
int m;
while (true)
{
char c = cin.peek();
if (c > '0'&& c <= '9')
{
cin >> m;
cout << "你设置的评委人数为:" << m << endl;
break;
}
cin.clear(); //重置标志位
cin.sync(); //清空缓冲区
cout << "你输入不正确,请重新输入" << endl;
}
cout << endl;
cout << "请这"<<m<<"个评委为选手打分:"<<endl;
cout << "(为了避免手动输入分数浪费时间,下面让计算机随机为选手打"<<m<<"次分。)" << endl;
cout << endl;
srand((unsigned int)time(NULL)); //随机数种子
vector<Person>v;
creatPerson(v,n);
//printVector(v);
setVector(v,m);
cout << endl;
cout << "去掉一个最低分,去掉一个最高分:"<<endl;
printVector(v);
cout << endl;
cout << "选手成绩由低到高排序如下:" << endl;
sortScore(v);
printVector2(v);
system("pause");
return 0;
}
热心网友
时间:2023-10-27 11:04
你定义的double类型数组a[]没有进行初始化。所以像max=a[0]就会报错。在double a[10]; 后加 memset(a,0,sizeof(0)); 语句即可。另一种做法:既然是在去掉最高分和最低分。你完全可以对你输入的数组进行排序,然后用for循环对a[1]--a[n-1]进行ave处理。
热心网友
时间:2023-10-27 11:04
a使用之前没有被初始化
这样改嘛double a[10] = {0};
max = a[0] 使用了a,你之前没有初始化a,所以出错。
热心网友
时间:2023-10-27 11:05
我这个是C++的代码 里面含有标准库。
#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
#include<ctime>
using namespace std;
class Person
{
public:
Person(){};
Person(string name, double score)
{
this->m_Name = name;
this->m_Score = score;
}
string m_Name;
double m_Score; //平均分
};
void creatPerson(vector<Person>&v,int n)
{
int score = 0;
for (int i = 0; i < n; ++i)
{
string name1 = to_string(i+1); //int类型转化为string类型
string name2 = "号选手";
name1 += name2;
Person p(name1, score);
v.push_back(p);
}
}
void printVector(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Name << "最终平均分数为:" << it->m_Score << endl;
}
}
void printVector2(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Name << " ";
}
cout << endl;
}
void setVector(vector<Person>&v,int m)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
deque<int>d;
cout << "已为" << it->m_Name <<"打了"<<m <<"次分:" << endl;
for (int i = 0; i < m; ++i)
{
int score = rand() % 41 + 60; // 打分区间60~100
d.push_back(score);
}
for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
cout << *dit << " ";
}
cout << endl;
sort(d.begin(), d.end());
d.pop_front();
d.pop_back();
/*for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
cout << *dit << " ";
}
cout << endl;*/
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
sum += *dit;
}
//cout << "sum=" << sum << "数量" << d.size() << endl;
double avg = sum*1.0 / d.size();
it->m_Score = avg;
}
cout << endl;
}
void sortScore(vector<Person>&v)
{
vector<Person>::iterator it = v.begin();
Person t;
for (unsigned int i = 0; i < v.size(); ++i)
{
for (unsigned int j = 0; j < v.size() - i - 1; ++j)
{
if (it[j].m_Score>it[j + 1].m_Score) //比较分数
{
t = it[j]; //更换整体
it[j] = it[j + 1];
it[j + 1] = t;
}
}
}
}
int main(void)
{
cout << "软件说明:" << endl;
cout << "该软件为N个评委为M个选手打分,这样每一个选手就收到N次打分,去掉一个最低分和一个最高分,算出选手的平均分,并为这些选手按平均分从低到高排序。" << endl;
cout << endl;
cout << "请输入选手的个数N=";
int n;
while (true)
{
char c = cin.peek();
if (c > '0'&& c <= '9')
{
cin >> n;
cout << "你设置的选手人数为:" << n << endl;
break;
}
cin.clear(); //重置标志位
cin.sync(); //清空缓冲区
cout << "你输入不正确,请重新输入" << endl;
}
getchar();
cout << endl;
cout << "请输入评委的个数M=";
int m;
while (true)
{
char c = cin.peek();
if (c > '0'&& c <= '9')
{
cin >> m;
cout << "你设置的评委人数为:" << m << endl;
break;
}
cin.clear(); //重置标志位
cin.sync(); //清空缓冲区
cout << "你输入不正确,请重新输入" << endl;
}
cout << endl;
cout << "请这"<<m<<"个评委为选手打分:"<<endl;
cout << "(为了避免手动输入分数浪费时间,下面让计算机随机为选手打"<<m<<"次分。)" << endl;
cout << endl;
srand((unsigned int)time(NULL)); //随机数种子
vector<Person>v;
creatPerson(v,n);
//printVector(v);
setVector(v,m);
cout << endl;
cout << "去掉一个最低分,去掉一个最高分:"<<endl;
printVector(v);
cout << endl;
cout << "选手成绩由低到高排序如下:" << endl;
sortScore(v);
printVector2(v);
system("pause");
return 0;
}
热心网友
时间:2023-10-27 11:04
你定义的double类型数组a[]没有进行初始化。所以像max=a[0]就会报错。在double a[10]; 后加 memset(a,0,sizeof(0)); 语句即可。另一种做法:既然是在去掉最高分和最低分。你完全可以对你输入的数组进行排序,然后用for循环对a[1]--a[n-1]进行ave处理。
热心网友
时间:2023-10-27 11:04
a使用之前没有被初始化
这样改嘛double a[10] = {0};
max = a[0] 使用了a,你之前没有初始化a,所以出错。
热心网友
时间:2023-10-27 11:05
我这个是C++的代码 里面含有标准库。
#include<iostream>
#include<vector>
#include<deque>
#include<string>
#include<algorithm>
#include<ctime>
using namespace std;
class Person
{
public:
Person(){};
Person(string name, double score)
{
this->m_Name = name;
this->m_Score = score;
}
string m_Name;
double m_Score; //平均分
};
void creatPerson(vector<Person>&v,int n)
{
int score = 0;
for (int i = 0; i < n; ++i)
{
string name1 = to_string(i+1); //int类型转化为string类型
string name2 = "号选手";
name1 += name2;
Person p(name1, score);
v.push_back(p);
}
}
void printVector(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Name << "最终平均分数为:" << it->m_Score << endl;
}
}
void printVector2(vector<Person>&v)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
cout << it->m_Name << " ";
}
cout << endl;
}
void setVector(vector<Person>&v,int m)
{
for (vector<Person>::iterator it = v.begin(); it != v.end(); ++it)
{
deque<int>d;
cout << "已为" << it->m_Name <<"打了"<<m <<"次分:" << endl;
for (int i = 0; i < m; ++i)
{
int score = rand() % 41 + 60; // 打分区间60~100
d.push_back(score);
}
for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
cout << *dit << " ";
}
cout << endl;
sort(d.begin(), d.end());
d.pop_front();
d.pop_back();
/*for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
cout << *dit << " ";
}
cout << endl;*/
int sum = 0;
for (deque<int>::iterator dit = d.begin(); dit != d.end(); ++dit)
{
sum += *dit;
}
//cout << "sum=" << sum << "数量" << d.size() << endl;
double avg = sum*1.0 / d.size();
it->m_Score = avg;
}
cout << endl;
}
void sortScore(vector<Person>&v)
{
vector<Person>::iterator it = v.begin();
Person t;
for (unsigned int i = 0; i < v.size(); ++i)
{
for (unsigned int j = 0; j < v.size() - i - 1; ++j)
{
if (it[j].m_Score>it[j + 1].m_Score) //比较分数
{
t = it[j]; //更换整体
it[j] = it[j + 1];
it[j + 1] = t;
}
}
}
}
int main(void)
{
cout << "软件说明:" << endl;
cout << "该软件为N个评委为M个选手打分,这样每一个选手就收到N次打分,去掉一个最低分和一个最高分,算出选手的平均分,并为这些选手按平均分从低到高排序。" << endl;
cout << endl;
cout << "请输入选手的个数N=";
int n;
while (true)
{
char c = cin.peek();
if (c > '0'&& c <= '9')
{
cin >> n;
cout << "你设置的选手人数为:" << n << endl;
break;
}
cin.clear(); //重置标志位
cin.sync(); //清空缓冲区
cout << "你输入不正确,请重新输入" << endl;
}
getchar();
cout << endl;
cout << "请输入评委的个数M=";
int m;
while (true)
{
char c = cin.peek();
if (c > '0'&& c <= '9')
{
cin >> m;
cout << "你设置的评委人数为:" << m << endl;
break;
}
cin.clear(); //重置标志位
cin.sync(); //清空缓冲区
cout << "你输入不正确,请重新输入" << endl;
}
cout << endl;
cout << "请这"<<m<<"个评委为选手打分:"<<endl;
cout << "(为了避免手动输入分数浪费时间,下面让计算机随机为选手打"<<m<<"次分。)" << endl;
cout << endl;
srand((unsigned int)time(NULL)); //随机数种子
vector<Person>v;
creatPerson(v,n);
//printVector(v);
setVector(v,m);
cout << endl;
cout << "去掉一个最低分,去掉一个最高分:"<<endl;
printVector(v);
cout << endl;
cout << "选手成绩由低到高排序如下:" << endl;
sortScore(v);
printVector2(v);
system("pause");
return 0;
}