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

《C++ Primer Plus 第六版》编程练习参考答案(第二章)

2018-12-06 18:15 686 查看

第二章

第一题:

[code]#include <iostream>
using namespace std;

int main()
{
cout<<"Horace.Kem"<<" Ganjiang East Road NO.333, Suzhou, Jiangsu, China";
}

第二题:

[code]#include <iostream>
using namespace std;

int main()
{
int dist;
cout<<"Please enter the distance in long:";
cin>>dist;
cout<<"The distance in yards is:"<<220*dist<<endl;
}

第三题:

[code]#include <iostream>
using namespace std;

void fun1();
void fun2();

int main()
{
fun1();
fun1();
fun2();
fun2();
}

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

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

第四题:

[code]#include <iostream>
using namespace std;

int main()
{
int age;
cout<<"Enter your age: ";
cin>>age;
cout<<"The number of months is: "<<12*age<<endl;
}

第五题:

[code]#include <iostream>
using namespace std;

int main()
{
double Celsius,Fahrenheit;
cout<<"Please enter a Celsius value: ";
cin>>Celsius;
Fahrenheit=1.8*Celsius+32;
cout<<Celsius<<" degrees Celsius is "<<Fahrenheit<<
" degrees Fahrenheit"<<endl;
}

第六题:

[code]#include <iostream>
using namespace std;

double astro(double);

int main()
{
double lightyear,astronomical;
cout<<"Enter the number of light years: ";
cin>>lightyear;
astronomical=astro(lightyear);
cout<<lightyear<<" light years = "<<
astronomical<<" astronomical units."<<endl;
}

double astro(double ly)
{
return 63240*ly;
}

第七题:

[code]#include <iostream>
using namespace std;

void clock(int,int);

int main()
{
int hours,minutes;
cout<<"Enter the number of hours: ";
cin>>hours;
cout<<"Enter the number of minutes: ";
cin>>minutes;
clock(hours,minutes);
}

void clock(int h,int m)
{
cout<<"Time: "<<h<<":"<<m<<endl;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: