C++ Primer Plus 第三章编程练习答案

C++ Primer Plus 第三章编程练习答案

之前在学习 C++ Primer Plus这本书的时候,每章后面的练习都没找到答案,在GitHub上找到了一个项目:https://github.com/PytLab/Cpp-Primer-Plus
但是这个项目仔细看虽然答案很齐全 但是小错误有点多,于是我在这里分享下我的答案,不定期更新。
没有先写第二章是因为第二章没写。。我就是从第三章开始的,往后要是有时间我就更新,欢迎留言交流。

话不多说 直接开整

3.7.1 inch2feet.cpp

//inch2feet.cpp --Book execercise 3.7.1
#include <iostream>
int main ()
{
  using namespace std;
  int inch , feet , inct_t ;
  //变量声明必须摆在前面,后面的变量不能平白无故就这样出来
  const int p = 12;
  cout << "Please enter ur height by inch :_____\b\b\b\b\b" ;
  cin >> inch ;
  //使用变量之前必须要声明
  feet = inch / p ;
  inct_t = inch % p ;

  cout << "Ur height by feet is : "
       << feet << " feet "
       << inct_t << " inch " << endl;

  return 0;
}

3.7.2 BMI.cpp

//BMI.cpp --Book execercise 3.7.2
#include <iostream>
int main ()
{
  using namespace std;
  int inch , feet , inct_t ;
  //变量声明必须摆在前面,后面的变量不能平白无故就这样出来
  const int p = 12;
  cout << "Please enter ur height by inch :_____\b\b\b\b\b" ;
  cin >> inch ;
  //使用变量之前必须要声明
  feet = inch / p ;
  inct_t = inch % p ;

  cout << "Ur height by feet is : "
       << feet << " feet "
       << inct_t << " inch " << endl;

  return 0;
}

3.7.3 Degrees.cpp

//Degrees.cpp --Book execercise 3.7.3
#include <iostream>
using namespace std ;
int main()
{
  int degrees,minutes,seconds ;
  cout << "First , enter the degrees : " ;
  cin >> degrees ;

  cout << "\nNext , enter the minutes : " ;
  cin >> minutes ;

  cout << "\nFinally , enter the seconds : " ;
  cin >> seconds ;

  double totall ;
  totall = degrees + (minutes / 60.0) + double(seconds / (60.0*60.0));
  //这里60作为一个Int类型的数字  必须手动改成60.0作为double类型的常量输入
  //不然就会带着整个式子变成int类型的计算
  cout << degrees << " degrees " << minutes << " minutes "
       << seconds << " seconds = " << totall << " degrees ." <<endl;


  return 0 ;
}

3.7.4 timetransformer.cpp

// timetransformer.cpp ----Book execercise 3.7.4
#include <iostream>
using namespace std ;
int main ()
{
  long long int seconds ;
  cout << "Enter the number of seconds : " ;
  cin >> seconds ;
  cout << "\n" << endl ;

  long long int time_min = seconds / 60 ;
  int time_sec_left = seconds % 60 ;

  long long int time_h = time_min / 60  ;
  int time_min_left = time_min % 60 ;

  long int time_day = time_h / 24 ;
  int time_h_left = time_h % 24 ;

  cout << seconds << " seconds = " << time_day << " days "
       << time_h_left << " hours " << time_min_left << " minutes "
       << time_sec_left << " seconds ." << endl ;

  return 0;
}

3.7.5 worldpopulation.cpp

// worldpopulation.cpp ----Book execercise 3.7.5
#include <iostream>
using namespace std;
int main ()
{
  cout << "Enter the world population : " ;
  long long int pplt_wrd ;
  cin >> pplt_wrd ;

  cout << "\nEnter tne population of the US: " ;
  long long int pplt_us ;
  cin >> pplt_us ;

  double percent ;
  percent = double(pplt_us) / double(pplt_wrd) * 100.0 ;
  //这里前面设置的两个llint参数必须要转换成double类型,并且单独转换,才能相除不被归零

  cout << "\nThe population of the US is " << percent
       << "% of the world population."<<endl;

  return 0;

}

3.7.6 fuelconsumption.cpp

// fuelconsumption.cpp ----Book execercise 3.7.6
#include <iostream>
using namespace std ;
int main()
{
  cout << "Please enter the liters of the gas :";
  double liters ;
  cin >> liters ;

  cout <<"Please enter the kilometers : " ;
  double km ;
  cin >> km ;

  double fc = liters / km * 100.0 ;

  cout << "The fuel consumption is : "<< fc << " liters per 100 kilometers" <<endl;
  return 0;

}

3.7.7 fuel_consumption_convert.cpp

// fuel_consumption_convert.cpp ----Book execercise 3.7.7
#include <iostream>

using namespace std ;
int main ()
{
  cout << "Pls enter the fuel consumption by liters per 100 kilometers :" ;
  double gas_l ;
  cin >> gas_l ;

  double gas_g = gas_l / 3.875 ;

  double mpg = 62.14 / gas_g ;

  cout << "Ur fuel consumption convert to mpg is : " << mpg << " Mpg . " <<endl;

  return 0 ;

}

如有错误,欢迎指正。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值