Accelerated C++ 习题解答 第3章

部分习题参考了网上已有的解答

Ex.3-0

#include<iostream>
#include<string>
#include<algorithm>
#include<ios>
#include<iomanip>
#include<vector>
#include<conio.h>
 
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::sort;
using std::streamsize;
using std::setprecision;
 
int main()
{
   cout <<"please enter your first name:";
   string name;
   cin >> name;
   cout <<"Hello, " << name <<"!" << endl;
 
   cout <<"please enter your midterm and final exam grades:";
   double midterm, final;
   cin >> midterm>> final;
 
   cout <<"enter all your homework grades, followed by end offile: ";
   vector<double> homework;
   double x;
   while (cin >> x)
      homework.push_back(x);
 
   typedef vector<double>::size_typevec_sz;
   vec_sz size =homework.size();
   if (size == 0) {
      cout << endl<<"you must 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]/2) : homework[mid];
 
   streamsize prec =cout.precision();
   cout <<"Your final grade is " <<setprecision(3)
      << 0.2 *midterm + 0.4 * final + 0.4 * median
      <<setprecision(prec) << endl;
 
   getch();
   return 0;
}


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值