c++ primer plus(第6版)中文版 第二章编程练习答案

来源:互联网 发布:冒险岛数据库不能用了 编辑:程序博客网 时间:2024/06/11 14:21

第二章编程联练习答案:

2.1编写一个c++程序,它显示您的姓名和住址

// 2.1编写一个c++程序,它显示您的姓名和住址#include <iostream>using namespace std;int main(){cout << "My name is dongge" << endl;cout << "My address is China"<< endl;}

2.2编写一个c++程序,它要求用户输入一个以long为单位的距离,然后把它转换为码(1 long=220码)

// 2.2编写一个c++程序,它要求用户输入一个以long为单位的距离,然后把它转换为码(1 long=220码)#include <iostream>using namespace std;int main(){int n;cout<<"输入一个以long为单位的距离:";cin>> n;cout<< "转换之后为:" << n*220 << "码" << endl; return 0;      }


2.3编写一个c++程序,它使用3个用户定义的函数,并生成输出

//2.3编写一个c++程序,它使用3个用户定义的函数,并生成输出#include <iostream>using namespace std;void myFuction1(){cout<<"Three blind mice"<<endl;}void myFuction2(){cout<<"see how they run"<<endl; }int main(){    myFuction1();     myFuction1();     myFuction2();     myFuction2();    return 0;   }

2.4 编写一个程序,让用户输入其年龄,然后显示包含多少个月

//2.4 编写一个程序,让用户输入其年龄,然后显示包含多少个月#include <iostream>using namespace std;int main(){ int age;     cout << "请输入你的年龄:";     cin >> age;     cout << "包含" << age*12 << "个月" << endl;}


2.5输入摄氏温度值,显示华氏温度=1.8*摄氏温度+32.0


//2.5输入摄氏温度值,显示华氏温度=1.8*摄氏温度+32.0#include <iostream>using namespace std;double change(double n){      return n*1.8+32.0;       }int main(){     int n;     cout << "please enter a Celsius valus:";     cin >> n;     cout << "20 degrees Celsius is " << change(n) <<" degrees Fashrenheit" << endl;}


2.6输入摄光年,显示天文单位(1光年=63240天文单位)

//2.6输入摄光年,显示天文单位(1光年=63240天文单位)#include <iostream>using namespace std;double change(double n){ return n*63240;    }int main(){     double n;     cout << "Enter the number of light years:";     cin >> n;     cout << n << " light years = " << change(n) << " astronmical units" << endl;}

2.7编写一个程序,输入小时和分钟,显示时间

//2.7编写一个程序,输入小时和分钟,显示时间#include <iostream>using namespace std;void show(int h,int m){cout << "Time: " << h << ":" << m << endl;}int main(){int h,m;cout << "Enter the number of hours:";cin >> h;cout << "Enter the number of minutes:";cin >> m;show(h,m);}


1 0
原创粉丝点击