首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么更好的实现这个程序

2012-02-28 
如何更好的实现这个程序#include iostream#include math.husingnamespacestdclassStudent{charstuden

如何更好的实现这个程序
#include <iostream>
#include <math.h>
using   namespace   std;

class   Student{
char   student_name[20],student_number[20];//这里必须设置为字符数组,否则的话程序将无法得到预期的结果
float   student_math,student_english,student_computer,sum;float   average;

public:
void   Input_student()//输入姓名以及学号的函数
{
cout < < "请输入姓名: " < <endl;
cin> > student_name;
cout < < "请输入学号: " < <endl;
cin> > student_number;
}

void   Input_student_math()//输入数学成绩
{
cout < < "请输入数学成绩: " < <endl;
cin> > student_math;
if(0 <=student_math&&student_math <=100)
cout < < "该学生的数学成绩是: " < <student_math < <endl;
else
{
cout < < "输入错误,请重新输入: " < <endl;
Input_student_math();
cin.clear();
cin.ignore();
}
}

void   Input_student_english()//输入英语成绩
{
cout < < "请输入英语成绩: " < <endl;
cin> > student_english;
if(0 <=student_english&&student_english <=100)
cout < < "该学生的英语成绩是: " < <student_english < <endl;
else
{
cout < < "输入错误,请重新输入: " < <endl;
Input_student_english();
cin.clear();
cin.ignore();
}
}

void   Input_student_computer()//输入计算机成绩
{
cout < < "请输入计算机成绩: " < <endl;
cin> > student_computer;
if(0 <=student_computer&&student_computer <=100)
cout < < "该学生的计算机成绩是: " < <student_computer < <endl;
else
{
cout < < "输入错误,请重新输入: " < <endl;
Input_student_computer();
cin.clear();
cin.ignore();
}
}

void   Average()//求平均数
{
sum=student_math+student_english+student_computer;
average=sum/3;
}
void   Print()//输出各种数据
{
cout < < "该学生的姓名及学号分别是: " < <endl < <student_name < < '\t ' < <student_number < <endl;
cout < < "该学生的数学,英语,计算机成绩分别是: " < <student_math < < '\t ' < <student_english < < '\t ' < <student_computer < <endl;
cout < < "该学生的平均成绩是: " < <average < <endl;
}
};
int   main()
{
Student   S;
S.Input_student();
S.Input_student_math();
S.Input_student_english();
S.Input_student_computer();
S.Average();
S.Print();
return   0;
}


现在这个程序我已经调试成功了
相信大家可以看的懂它是干吗用的
现在我的问题是
我觉得这样来实现这个程序的功能有些太烦了
有没有更好的方法呢?
特别是每输一次成绩就要调用一次函数我自己都觉得这样不大好
能不能想办法把这三个函数合并一起呢?
盼望得到高手的指点,万分感激...

[解决办法]
有点错~~~这个
void Input()
{
Student P;
P.Input_student();
P.Input_student_math();
P.Input_student_english();
P.Input_student_computer();
}

热点排行