- 一、第二章
- 一、第一题
- 二、第二题
- 三、第三题
- 四、第四题
- 五、第五题
- 六、第六题
- 七、第七题
- 二、第三章
- 一、第一题
- 二、第二题
- 三、第三题
- 四、第四题
- 五、第五题
- 六、第六题
- 七、第七题
1.编写一个C++程序,它显示您的姓名和地址。
#include二、第二题using namespace std; int main() { cout << "xx" << endl;//姓名 cout << "xx市" << endl;//地址 return 0; }
2.编写一个C++程序,它要求用户输人一个以long为单位的距离,然后将它转换为码(一long等于220码)。
#includeusing namespace std; int main() { int long1 = 0; cin >> long1; int ma = 220 * long1; cout << ma << endl; return 0; }
三、第三题
3.编写一个C++程序,它使用3个用户定义的函数(包括main),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行;另一个函数也被调用两次,并生成其余的输出。
#include四、第四题using namespace std; void fun1() { cout << "Three blind mice" << endl; } void fun2() { cout << "Three blind mice" << endl; } void fun3() { cout << "See how they run" << endl; } void fun4() { cout << "See how they run" << endl; } int main() { fun1(); fun2(); fun3(); fun4(); return 0; }
4.编写一个程序,让用户输入其年龄,然后显示该年龄包含多少个月,如下所示:
Enter your age:29
#include五、第五题using namespace std; int main() { int age = 0; cout << "Enter your age:"; cin >> age; int month = 12 * age; cout << month << endl; return 0; }
5.编写一个程序,其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求输入摄氏温度值,并显示结果:
Please enter a Celsius value:20
20 degrees Celsius is 68 degrees Fahrenheit.
下面是转换公式:
华氏温度=1.8x摄氏温度+32.0
#include六、第六题using namespace std; double fun(double a) { return 1.8 * a + 32.0; } int main() { double sheshi = 0; cout << "Please enter a Celsius value:"; cin >> sheshi; double z = fun(sheshi); cout << sheshi << " degrees Celsius is " << z << " degrees Fahrenheit." << endl; return 0; }
6.编写一个程序,其main调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输人光年值,并显示结果:
Enter the number of light years: 4.2
4.2 light years = 265608 astronomical units.
天文单位是从地球到太阳的平均距离(约150000000公里或93000000英里),光年是光一年走的距离(约10万亿公里或6万亿英里)(除太阳外,最近的恒星大约离地球 4.2光年)。请使用double类型(参见输出流成为 程序清单2.4),转换公式为:
1光年=63240天文单位
#include七、第七题using namespace std; double fun(double year) { return year * 63240; } int main() { double year = 0; cout << "Enter the number of light years:"; cin >> year; double a = fun(year); cout << year << " light years = " << a << " astronomical units." << endl; return 0; }
7.编写一个程序,要求用户输人小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours:9
Enter the number of minutes: 28
Time:9:28
#include二、第三章 一、第一题using namespace std; void fun(int a, int b) { cout << "Time:" << a << ":" << b << endl; } int main() { int hours = 0; int minutes = 0; cout << "Enter the number of hours:"; cin >> hours; cout << "Enter the number of minutes:"; cin >> minutes; fun(hours, minutes); return 0; }
#include二、第二题using namespace std; const double a = 12.0;//(1英尺为12英寸) int main() { int height = 0; cout << "输入你的身高(英寸):______bbbbbb"; cin >> height; cout << "身高为 " << height / a << " 英尺 " << height << " 英寸 " << endl; return 0; }
#include三、第三题#include using namespace std; const double inches_per_foot = 12.0;//(一英尺为12英寸) const double metres_per_inch = 0.0254;//(一英寸=0.0254米) const double pounds_per_kilogram = 2.2;//(一千克=2.2磅) int main() { double foot, inch, pound; cout << "请输入身高(英寸):" << endl; cin >> foot; cout << "请输入身高(英尺):" << endl; cin >> inch; cout << "请输入体重(磅)::" << endl; cin >> pound; double height = (foot * inches_per_foot + inch) * metres_per_inch; double weight = pound / pounds_per_kilogram; double BMI = weight / pow(height, 2);//pow函数计算身高的二次方 cout << "BMI指数是 " << BMI << endl; return 0; }
#include四、第四题using namespace std; const double degrees_per_minutes = 60.0;//(一度为60分) const double minutes_per_seconds = 60.0;//(一分为60秒) const double degrees_per_seconds = 3600.0;//(一度为3600秒) int main() { double degrees, minutes, seconds, x; cout << "Enter a latitude in degrees, minutes, and seconds:" << endl; cout << "First, enter the degrees: "; cin >> degrees; cout << "Next, enter the minutes of arc: " ; cin >> minutes; cout << "Finally, enter the seconds of arc:" ; cin >> seconds; x = degrees + minutes / degrees_per_minutes + seconds / degrees_per_seconds; cout << degrees << " degrees," << minutes << " minutes," << seconds << " seconds" << " = " << x << " degrees" << endl; return 0; }
#include五、第五题using namespace std; const int minutes_per_seconds = 60;//(一分=60秒) const int hours_per_seconds = 3600;//(一小时=3600秒) const int days_per_seconds = 86400;//(一天=86400秒) int main() { long long sec = 0; cout << "Enter the number of seconds:"; cin >> sec; int days, hours, minutes, seconds; days = sec / days_per_seconds; hours = sec % days_per_seconds / hours_per_seconds; minutes = sec % days_per_seconds % hours_per_seconds / minutes_per_seconds; seconds= sec % days_per_seconds % hours_per_seconds % minutes_per_seconds; cout << sec << " seconds" << " = " << days << " days," << hours << " hours," << minutes << " minutes," << seconds << " seconds" << endl; return 0; }
#include六、第六题using namespace std; int main() { long long world_population = 0; long long population = 0; cout << "Enter the world's population:" ; cin >> world_population; cout << "Enter the population of our country:"; cin >> population; double percentage = (double)population / (double)world_population * 100; cout << "The population of our country is " << percentage << "% of the world population" << endl; return 0; }
#include七、第七题using namespace std; int main() { double kilometre, litre, FCi; cout << "请输入公里数:" ; cin >> kilometre; cout << "请输入消耗的汽油:" ; cin >> litre; FCi = kilometre / litre * 100; cout << "每一百公里的耗油量:" << FCi << endl; return 0; }
#includeusing namespace std; const double mile = 100; const double liter = 3.875; int main() { double gasoline; cout << "按欧洲风格输入汽车的耗油:" ; cin >> gasoline; double gallon = 62.14 / (gasoline / 3.875); cout << "美式风格的耗油量:" << gallon << endl; return 0; }



