原创 计算学生成绩 C++

2009-3-16 13:48 2173 4 4 分类: 软件与OS

https://static.assets-stash.eet-china.com/album/old-resources/2009/3/14/5e6798e2-e894-4641-b0d9-2fca9e18b417.rar


#include <iomanip>
#include <ios>
#include <iostream>
#include <string>


using std::cin;     using std::cout;
using std::endl;    using std::string;
using std::setprecision;  using std::streamsize;


int main()
{
 // ask for and read the student's name
 cout << "Please enter your first name: ";
 string name;
 cin >> name;
 cout << "Hello, " << name << "!" << endl;
 // ask for and read the midterm and final grades
 cout << "Please enter your midterm and final exam grades: ";
 double midterm, final;
 cin >> midterm >> final;


 // ask for the homework grades
 cout << "Enter all your homework grades, "
  "followed by end-of-file: ";


 // the number and sum of grades read so far
 int count = 0;
 double sum = 0;


 // a variable into which to read
 double x;


 // invariant:
 //   we have read count grades so far, and


 //   sum is the sum of the first count grades
 while (cin >> x) {
  ++count;
  sum += x;
 }


 // write the result
 streamsize prec = cout.precision();
 cout << "Your final grade is " << setprecision(3)
   << 0.2 * midterm + 0.4 * final + 0.4 * sum / count
   << setprecision(prec) << endl;
 system("PAUSE");
 return 0;
}

文章评论0条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /2 下一条