第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;
}本回答被网友采纳