您的位置:首页 > 编程语言 > C语言/C++

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

2014-11-25 12:54 441 查看
第一次发微博不知道发点什么内容,就把以前写的一些小程序发上来先垫个底吧。

程序在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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: