C++ Primer Plus第六版 第二章 编程练习答案

来源:互联网 发布:补牙多少钱 知乎 编辑:程序博客网 时间:2024/04/30 03:34

最近在期末考试...高数求过啊...30号考完就可以安安静静的看C++ Primer Plus了。。。


/*******************************************************************Author : YuujiBlog : blog.csdn.net/acm_yuujiTime : 2014/06/22From : C++ Primer Plus第六版第二章编程练习 第1题Problem : 编写一个C++程序,它显示您的姓名和地址。*******************************************************************/#include <iostream>using namespace std;int main(){cout << "NAME :Yuuji" << endl; //使用cout打印姓名cout << "ADRESS :AHU" << endl;   //使用cout打印地址return 0;}


/*******************************************************************Author : YuujiBlog : blog.csdn.net/acm_yuujiTime : 2014/06/22From : C++ Primer Plus第六版第二章编程练习 第2题Problem : 编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long等于220码)。*******************************************************************/#include <iostream>using namespace std;int main(){cout << "请输入一个距离(单位long) : ";//打印提示信息int dis;//定义int型变量dis存储输入的距离cin >> dis;//输入数据cout << "您输入的距离是" << dis << "long , 它等于" << dis * 220 << "码。" << endl;//打印结果return 0;}


/*******************************************************************Author : YuujiBlog : blog.csdn.net/acm_yuujiTime : 2014/06/22From : C++ Primer Plus第六版第二章编程练习 第3题Problem : 编写一个C++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出:Three blind miceThree blind miceSee how they runSee how they run其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。*******************************************************************/#include <iostream>using namespace std;void prt_1(void);//函数原型,打印第一种语句void prt_2(void);//函数原型,打印第二种语句int main(){prt_1();//调用函数,打印语句prt_1();prt_2();prt_2();return 0;}void prt_1(void)//定义函数{cout << "Three blind mice" << endl;}void prt_2(void)//定义函数{cout << "See how they run" << endl;}



/*******************************************************************Author : YuujiBlog : blog.csdn.net/acm_yuujiTime : 2014/06/22From : C++ Primer Plus第六版第二章编程练习 第4题Problem : 编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:Enter your age: 29*******************************************************************/#include <iostream>using namespace std;int main(){int age;//定义变量age存储年龄cout << "请输入您的年龄:";//打印提示信息cin >> age;//输入年龄 cout << "您的年龄包含" << age * 12 << "个月" << endl;//打印结果return 0;}



/*******************************************************************Author : YuujiBlog : blog.csdn.net/acm_yuujiTime : 2014/06/22From : C++ Primer Plus第六版第二章编程练习 第5题Problem : 编写一个程序,期中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。改程序按下面的格式要求用户输入摄氏温度值,并显示结果:Please enter a Celsius value: 2020 degrees Celsius is 68 degrees Fahrenheit.下面是转换公式:华氏温度 = 1.8 * 摄氏温度 + 32.0*******************************************************************/#include <iostream>using namespace std;double change(double n);//函数原型,接受类型为double的参数,返回值为doubleint main(){cout << "Please enter a Celsiius value: ";//打印提示信息double c;//定义变量,存储摄氏温度值cin >> c;//输入数据cout << c << " degrees Celsius is " << change(c) << " degrees Fahrenheit." << endl;//打印结果return 0;}double change(double n)//定义函数{return 1.8 * n + 32.0;}



/*******************************************************************Author : YuujiBlog : blog.csdn.net/acm_yuujiTime : 2014/06/22From : C++ Primer Plus第六版第二章编程练习 第6题Problem : 编写一个程序,其main()调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:Enter the number of light years: 4.24.2 light years = 265608 astronomical units.天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型,转换公式为:1光年 = 63240天文单位*******************************************************************/#include <iostream>using namespace std;double change(double n);//函数原型int main(){cout << "Enter the number of light years: ";//打印提示信息double ly;//定义变量,存储光年cin >> ly;//输入数据cout << ly << " light years = " << change(ly) << " astronomical units." << endl;//打印结果return 0;}double change(double n)//定义函数{return n * 63240;}


/*******************************************************************Author : YuujiBlog : blog.csdn.net/acm_yuujiTime : 2014/06/22From : C++ Primer Plus第六版第二章编程练习 第7题Problem : 编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:Enter the number of hours: 9Enter the number of minutes: 28Time: 9:28*******************************************************************/#include <iostream>using namespace std;void print_time(int h , int m);//函数原型int main(){cout << "Enter the number of hours: ";//打印提示信息int hours;//定义变量,存储小时数cin >> hours;//输入小时数cout << "Enter the number of minutes: ";//打印提示信息int minutes;//定义变量,存储分钟数cin >> minutes;//输入分钟数print_time(hours , minutes);//调用函数return 0;}void print_time(int h , int m)//定义函数{cout << "Time: " << h << ":" << m << endl;}



0 0
原创粉丝点击