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

C++ Primer Plus 第六版 第二章编程题解题writeup

2018-01-29 20:27 363 查看

Writeup C++ 2.6

复习题

1.    引入iostream库 (这将导致在最终的编译之前,使用iostream中的内容替换该编译指令)

2.    给定一个名称空间(它使得程序可以使用std名称空间中的定义)

3.    Cout<<”helloworld”<<endl;

4.    Int cheeses;

5.    Cheeses = 32;

6.    Cin>>cheeses;

7.    Cout<<”we have “<<cheeses<<”varieties of cheese,”<<endl;

8.    Int froop(double t); 表示定义了一个froop函数,返回值是int类型的整数,接受一个double类型的形参变量    void rattle(int n)返回值为空,接受一个int类型的整数。    Int prune(void)返回值为int类型,接受值为空。

9.    在返回值为空,即void的情况下可以不使用return,然而如果不提供返回值,则可以使用它   return;

10.  没使用名称空间编译指令,法一在该函数内部加上 using namespace std,法二使用std::cout,法三

编程题

1.  #include <iostream>  
2.    
3.  using namespace std;  
4.    
5.  int main()  
6.  {  
7.      cout<<"please input your name."<<endl;  
8.      char name[20];  
9.      cin>>name;  
10.
    cout<<"your name is "<<name<<"."<<endl;  
11.     char address[30];  
12.
    cout<<"please input your address."<<endl;  
13.     cin>>address;  
14.
    cout<<"your address is "<<address<<"."<<endl;  
15.     return 0;  
16.

1.  /* 
2.  problem:编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(1 long 等于220码)。 
3.  author:HuangYixiong 
4.  time:2018.1.29 
5.  */  
6.    
7.  #include <iostream>  
8.    
9.  using namespace std;  
10.
  
11. int main()  
12.
{  
13.     cout<<"请输入一个以long为单位的距离"<<endl;  
14.
    int juli;  
15.     cin>>juli;  
16.
    juli*=220;  
17.     cout<<"距离为"<<juli<<"码."<<endl;  
18.
    return 0;  
19. }  
 
1.  /* 
2.  problem:编写一个C++程序,它使用3个用户定义的函数(包括main()), 
3.  并生成下面的输出: 
4.  Three blind mice 
5.  Three blind mice 
6.  See how they run 
7.  See how they run 
8.  其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次, 
9.  并生成其余的输出。。 
10.
author:HuangYixiong 
11. time:2018.1.29 
12.
*/  
13.   
14.
#include <iostream>  
15.   
16.
using namespace std;  
17. void mice();  
18.
void run();  
19.   
20.
int main()  
21. {  
22.
    mice();  
23.     mice();  
24.
    run();  
25.     run();  
26.
    return 0;  
27. }  
28.
void mice()  
29. {  
30.
    cout<<"Three blind mice"<<endl;  
31. }  
32.
void run()  
33. {  
34.
    cout<<"see how they run"<<endl;  
35. }  

 

1.  /* 
2.  problem:编写一个程序,让用户输入其年龄,然后显示该年龄包含多少 
3.  个月,如下所示: 
4.  Enter your age: 29 
5.  author:HuangYixiong 
6.  time:2018.1.29 
7.  */  
8.    
9.  #include <iostream>  
10.
  
11. using namespace std;  
12.
  
13. int main()  
14.
{  
15.     int age;  
16.
    cout<<"Enter your age: ";  
17.     cin>>age;  
18.
    cout<<endl<<"你已经生长了"<<12*age<<"个月"<<endl;  
19.     return 0;  
20.
}  

 

1.  /* 
2.  problem:编写一个程序,期中的main()调用一个用户定义的函数(以 
3.  摄氏温度值为参数,并返回相应的华氏温度值)。改程序按下面的格式 
4.  要求用户输入摄氏温度值,并显示结果: 
5.  Please enter a Celsius value: 20 
6.  20 degrees Celsius is 68 degrees Fahrenheit. 
7.  下面是转换公式: 
8.  华氏温度 = 1.8 * 摄氏温度 + 32.0 
9.  author:HuangYixiong 
10.
time:2018.1.29 
11. */  
12.
  
13. #include <iostream>  
14.
  
15. using namespace std;  
16.
void wendu(float a)                           //a是摄氏温度,b是华氏温度  
17. {  
18.
    float b;  
19.     b = 1.8 * a + 32.0;  
20.
    cout<<a<<" degrees Celsius is "<<b<<" degrees Fahrenheit."<<endl;  
21. }  
22.
  
23. int main()  
24.
{  
25.     float a;  
26.
    cout<<"Please enter a Celsius value: ";  
27.     cin>>a;  
28.
    cout<<endl;  
29.     wendu(a);  
30.
    return 0;  
31. }  

 

1.  /* 
2.  problem:编写一个程序,其main()调用一个用户定义的函数 
3.  (以光年值为参数,并返回对应天文单位的值)。该程序按下 
4.  面的格式要求用户输入光年值,并显示结果: 
5.  Enter the number of light years: 4.2 
6.  4.2 light years = 265608 astronomical units. 
7.  天文单位是从地球到太阳的平均距离(约150000000公里或 
8.  93000000英里),光年是光一年走的距离(约10万亿公里或 
9.  6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。 
10.
请使用double类型,转换公式为: 
11. 1光年 = 63240天文单位 
12.
author:HuangYixiong 
13. time:2018.1.29 
14.
*/  
15.   
16.
#include <iostream>  
17.   
18.
using namespace std;  
19. double change(double);  
20.
  
21. int main()  
22.
{  
23.     double light_years;  
24.
    cout<<"Enter the number of light years : ";  
25.     cin>>light_years;  
26.
    cout<<endl;  
27.     cout<<light_years<<" light years = "<<change(light_years)<<" astronomical units."<<endl;  
28.
    return 0;  
29. }  
30.
double change(double light_years)  
31. {  
32.
    return light_years * 63240;  
33. }  

 

1.  / * 
2.  问题:编写一个程序,要求用户输入小时数和分钟数在主()函数 
3.  中,将这两个值传递给一个空函数,后者以下面这样的格式显示这两个值: 
4.  输入小时数:9 
5.  输入分钟数:28 
6.  时间:9:28 
7.  作者:HuangYixiong 
8.  时间:2018年1月29日 
9.  * /  
10.
  
11. #include <iostream>  
12.
  
13. 使用命名空间 std;   
14.
void  time(int int );  
15.   
16.
int  main()  
17. {  
18.
     小时,分钟;  
19.     cout<< “输入小时数:” ;  
20.
    CIN>>小时;  
21.     cout<< endl << “输入分钟数:” ;  
22.
    CIN>>分钟;  
23.     时间(小时,分钟);  
24.
    返回 0;  
25. }  
26.
  
27. void  time(int  小时,int  分钟)  
28.
{  
29.     cout<< endl << “时间:” <<小时<< “:” <<分钟<<endl;  
30.
}  

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