c++求平均成绩和总成绩

要求:用结构体或数组写一个程序能输出3名学生的学号,姓名,3科(如:语·数·英)成绩的总分,平均分。要在编译器里通过再拿上来,最好简短。谢谢!

#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;class Student
{
public:
Student(string strName,string strID,double chn,double math,double english)
{
m_strName=strName;
m_strID=strID;
m_dChineseScore=chn;
m_dMathScore=math;
m_dEnglishScore=english;
}
string m_strName;
string m_strID;
double m_dChineseScore;
double m_dMathScore;
double m_dEnglishScore;

};
void main()
{
vector<Student> students;
students.push_back(Student("01","jim",90,90,90));
students.push_back(Student("02","Tom",85,92,95)); for_each(students.begin(),students.end(),[=](const Student& st )
{
cout<<"学号:"<<st.m_strID<<"姓名: "<<st.m_strName<<"语文: "<<st.m_dChineseScore<<"数学: "<<st.m_dMathScore<<"英语: "<<st.m_dEnglishScore<<std::endl;
cout<<"总分:"<<st.m_dChineseScore+st.m_dMathScore+st.m_dEnglishScore<<std::endl;
cout<<"平均分:"<<(st.m_dChineseScore+st.m_dMathScore+st.m_dEnglishScore)/3.0f<<std::endl;
});} 1 本段代码已经在VS2010(也就是VC10)下运行通过。因为里面运用了lambda表达式,所以其他VC版本不支持。当然,你可以通过简单修改,使其支持任意VC版本。2 Class和结构体区别不大,所以满足要求。3 没发现你的需求里哪些地方需要用什么共用体共同体之类的东西,不但没有必要,在正常开发中也很少用。4 通过动态添加学生,可以实现任意数量的学生成绩输出,当然也可以实现3名学生。5 本段代码为了简短说明问题,将内部变量全部公有化,在正规开发中,要视情况而定。6 代码段运用OO以及STL标准库,代码已经精简为最小。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-09-12
刚刚写了一个程序的,你看一下,其实跟楼上的大同小异。呵呵
#include<iostream>
#include<string>

using namespace std;

#define max 3//学生的数量

string numero[max]={"First", "Second", "Third"};

class student {
public:
// void set_name(string name){
// student_name=name;
// }
// void get_name(string &name){
// name=student_name;
// }

string student_name;
string student_number;
float chinese;
float math;
float english;
float average;
private:
};

int main(int argc, char **argv) {
student a[max];
int i;
for(i=0;i<max;i++)
{
cout<<"input the "<<numero[i]<<" student's name: "<<endl;
cin>>a[i].student_name;
cout<<"input the "<<numero[i]<<" student's student number: "<<endl;
cin>>a[i].student_number;
do {
cout<<"input the "<<numero[i]<<" student's chinese note: "<<endl;
cin>>a[i].chinese;
if((a[i].chinese<0)||(a[i].chinese>100))
cout<<"The note must be between 0 and 100!!!"<<endl;
} while ((a[i].chinese<0)||(a[i].chinese>100));

do {
cout<<"input the "<<numero[i]<<" student's math note: "<<endl;
cin>>a[i].math;
if((a[i].math<0)||(a[i].math>100))
cout<<"The note must be between 0 and 100!!!"<<endl;
} while ((a[i].math<0)||(a[i].math>100));

do {
cout<<"input the "<<numero[i]<<" student's english note: "<<endl;
cin>>a[i].english;
if((a[i].english<0)||(a[i].english>100))
cout<<"The note must be between 0 and 100!!!"<<endl;
} while ((a[i].english<0)||(a[i].english>100));

a[i].average=(a[i].chinese+a[i].english+a[i].math)/(float)max;
}
for(i=0;i<max;i++)
{
cout<<"name\t"<<a[i].student_name<<endl;
cout<<"student number:\t"<<a[i].student_number<<endl;
cout<<"chinese note:\t"<<a[i].chinese<<endl;
cout<<"math note:\t"<<a[i].math<<endl;
cout<<"english note:\t"<<a[i].english<<endl;
cout<<"average note:\t"<<a[i].average<<endl;
}
return 0;
}

输出结果:
name panlei
student number: 21
chinese note: 100
math note: 100
english note: 100
average note: 100
name liyan
student number: 76
chinese note: 77
math note: 65
english note: 98
average note: 80
name rt
student number: 54
chinese note: 67
math note: 66
english note: 54
average note: 62.3333
第2个回答  2013-09-12
我刚刚帮你赶制了一份代码,不知道符合你的要求不,你运行一下试试,如果有问题请立即提出来,我马上改。。。#include<stdio.h>
#define max 3//这里可以手动的控制学生的数量struct student
{
char name[20];
char number[20];
double chinese;
double math;
double english;
double average;
};int main()
{
student a[max];
int i;
for(i=0;i<max;i++)
{
printf("请输入学生%d姓名\n",i+1);
scanf("%s",a[i].name);
printf("请输入学生%d学号\n",i+1);
scanf("%s",a[i].number);
printf("请输入学生%d语文成绩\n",i+1);
scanf("%lf",&a[i].chinese);
printf("请输入学生%d数学成绩\n",i+1);
scanf("%lf",&a[i].math);
printf("请输入学生%d英语成绩\n",i+1);
scanf("%lf",&a[i].english);
a[i].average=(a[i].chinese+a[i].english+a[i].math)/3.0;
}
for(i=0;i<max;i++)
{
printf("姓名:%s\n",a[i].name);
printf("学号:%s\n",a[i].number);
printf("语文成绩:%lf\n",a[i].chinese);
printf("数学成绩:%lf\n",a[i].math);
printf("英语成绩:%lf\n",a[i].english);
printf("平均分:%lf\n",a[i].average);
printf("\n");
}
return 0;
}本回答被网友采纳