简单的计算学生成绩的程序
上课时写的代码,初学者必会。
#include#include #include #include #include #include using std::cin; using std::cout; using std::endl; using std::setprecision; using std::sort; using std::streamsize; using std::string; using std::vector; int main() { cout << "please enter your first name:"; string name; cin >> name; cout << "hello," << name << "!"< > midterm >> final; cout << "enter all your homework grades,""followed by end-of-file"; vector homework;//定义double类型的向量homrwork double x; //将成绩输入到向量中 while (cin >> x) { homework.push_back(x); } typedef vector ::size_type vec_sz;//定义替代名vec_sz,代替vector ::size_type vec_sz size = homework.size();//定义vec_sz类型的size并将homework的size赋给它 if (size == 0) { cout << endl << "piease enter your grades.""please try again." << endl; return 1; } sort(homework.begin(), homework.end());//对家庭作业成绩进行排序 vec_sz mid = size / 2; double median; median = size % 2 == 0 ? (homework[mid] + homework[mid - 1]) : homework[mid];//三元符号 streamsize prec = cout.precision();//保存当前流的位数到prec cout << "your final grades is" << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * median << setprecision(prec) << endl;//setprecision(prec)为将流的位数返回,setprecision(30)为将流的位数设为3位 return 0; }
一个初学者,代码的后面有注释,若有本人代码理解上有错误希望大佬指出,我一一改正,
第一次发,希望和大家多交流学习。



