c++ primer plus第二章习题答案(原创请参考)

第一次发微博不知道发点什么内容,就把以前写的一些小程序发上来先垫个底吧。

程序在visual studio下运行正常,其他编译器下没试过。如果有什么漏洞,还请各位不吝赐教。

没怎么写注释,这几天时间紧,以后慢慢补上。


第一题

#include <iostream>
//using namespace std;// choose either this one or "using std::..."
int main()
{
using std::cout;
using std::endl;
cout << "My name is MiaoXingRen" << endl;
cout << "My address is No.92 of XIDAZHI street of Harbin, Heilongjiang, China" << endl;

return 0;
}


第二题

#include <iostream>
using namespace std;

int Convert(int dis);

int main()
{
int distan_long, distan_ma;

cout << "Please input the distance with the unit 'long':";
cin >> distan_long;

distan_ma = Convert(distan_long);

cout << "The distance you have input is " << distan_long << " in the unit long, ";
cout << "equals to " << distan_ma << " in the unit ma." << endl;

return 0;
}

int Convert(int dis)
{
int a;
return (dis * 220);
}


第三题

#include <iostream>
using namespace std;

void Output1(void);
void Output2(void);

int main()
{
Output1();
Output1();

Output2();
Output2();

return 0;
}

void Output1(void)
{
cout << "Three blind mice" << endl;
}

void Output2(void)
{
cout << "See how they run" << endl;
}


第四题

#include <iostream>
using namespace std;

int Convert(int year);

int main()
{
int year, month;

cout << "Enter your age: ";
cin >> year;

month = Convert(year);
cout << "Your age contains " << month << " months" << endl;

return 0;
}


int Convert(int year)
{
return (year * 12);
}


第五题

#include <iostream>
using namespace std;

double Convert(int C);

int main()
{
int centigrade;
double fahrenheit;

cout << "Please enter a Celsius value: ";
cin >> centigrade;

fahrenheit = Convert(centigrade);

cout << centigrade << " degrees Celsius is ";
cout << fahrenheit << " degrees Fahrenheit." << endl;

return 0;
}


double Convert(int C)
{
double F;

F = (double)C * 1.8 + 32.0;
return F;
}


第六题

#include <iostream>
using namespace std;

double Convert(double ly);

int main()
{
double light_year, astron_unit;

cout << "Enter the number of light years: ";
cin >> light_year;

astron_unit = Convert(light_year);

cout << light_year << " light years = ";
cout << astron_unit << " astronomical units.";
cout << endl;

return 0;
}

double Convert(double ly)
{
double au;
au = ly * 63240;
return au;
}


第七题

#include <iostream>
using namespace std;

void Format(int hour, int min);

int main()
{
int hour, minute;

cout << "Enter the number of hours: ";
cin >> hour;
cout << "Enter the number of minutes: ";
cin >> minute;

Format(hour, minute);

return 0;
}


void Format(int hour, int min)
{
cout << "Time: " << hour << ":" << min << endl;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值