标签:c++
#include<iostream> #include<ios> #include<iomanip> #include<vector> //使用向量 ,就包含向量的头文件 #include<string> #include<algorithm> using namespace std; int main() { cout << "please enter your first name :" << endl; string name; cin >> name; cout << "Hello " << name <<" !" << endl; cout << "please enter your midterm and final exam grades :" << endl; double midterm , final ; cin >> midterm >> final ; cout << "enter all your homework grades ." "followed by end-of-file :" << endl; vector<double> homework; // 定义向量; double x; while(cin >> x) { homework.push_back(x); // 使用向量的好处是不用再像数组那样事先定义数组大小,因为向量可以动态增长; } typedef vector<double>::size_type vec_sz; //由于觉得size_type类型名太长,所以用另一个名字作为代替 ; vec_sz size = homework.size() ; // 这个函数可以求得向量里面元素的个数 if (size == 0){ cout << "endl" << "your must enter your greads ;" "please try again . " << endl; return 1; } sort(homework.begin() ,homework.end() ); // size函数的参数指定被排序的元素的范围,所以用到了这两个函数。begin指示第一个元素,end函指向紧跟在最后一个元素的之后的位置。 vec_sz mid = size/2; double median; median = size % 2 == 0 ? (homework[mid] + homework[mid - 1])/2 : homework[mid]; streamsize prec = cout.precision(); cout << "Your final grades is " << fixed << setprecision(3) << 0.2 * midterm + 0.4 * final +0.4 * median << setprecision(prec) << endl; return 0; }
标签:c++
原文地址:http://blog.csdn.net/cttacm/article/details/43500423