目录
第2章 开始学习C++
第3章 处理数据
1.编写一个C++程序,它显示您的姓名和地址。
#include<iostream> using namespace std; int main(void) { string name; string address; cout << "请输入您的姓名:" << endl; cin >> name; cout << "请输入您的地址:" << endl; cin >> address; cout << "您的姓名是:" <<name<< endl; cout << "您的地址是:" <<address<<endl; system("pause"); return 0; }
2.编写一个C++程序,它要求用户输入一个以long为单位的距离,然后将它转换为码(一long 等于220码)。
#include <iostream> using namespace std; int main(void) { double distence; cout << "请输入一个距离(单位:long):" << endl; cin >> distence; cout << "您输入的距离为 " << distence << " long," << "即 " << 220 * distence << " 码" << endl; system("pause"); return 0; }
3.编写一个C++程序,它使用3个用户定义的函数(包括main()),并生成下面的输出:
Three blind mice
Three blind mice
See how they run
See how they run
其中一个函数要调用两次,该函数生成前两行,另一个函数也被调用两次,并生成其余的输出
#include <iostream> using namespace std; void print1(void) { cout << "Three blind mice" << endl; } void print2(void) { cout << "See how they run" << endl; } int main(void) { print1(); print1(); print2(); print2(); system("pause"); return 0; }
4.编写一个程序,让用户输入其年龄然后显示该年龄包含多少个月,如下所示:
Enter your age:29
#include <iostream> using namespace std; int main(void) { int age; cout << "Enter your age:" << endl; cin >> age; cout << "your age cintains " << age * 12 << "months" << endl; system("pause"); return 0; }
5."编写”个程序;其中的main()调用一个用户定义的函数(以摄氏温度值为参数,并返回相应的华氏温度值)。该程序按下面的格式要求用户输入摄氏温度值,并显示结果:
please, enter a Celsius value: 20
20 degrees Celsius is 68 degrees Fahrenheit
下面是转换公式:
华氏温度=1.8X 摄氏温度+ 32.0
#include <iostream> using namespace std; int main(void) { double value; cout << "please, enter a Celsius value :" << endl; cin >> value; cout << value << " degrees Celsius is " << 1.8 * value + 32.0 << " degrees Fahrenheit" << endl; system("pause"); return 0; }
6.编写一个程序,其main( )调用一个用户定义的函数(以光年值为参数,并返回对应天文单位的值)。该程序按下面的格式要求用户输入光年值,并显示结果:
Enter the number of light years: 4.2
4.2 light years = 265608 astronomical units.
天文单位是从地球到太阳的平均距离(约1000000公里或9300000英里),光年是光一年走的距离
(约10万亿公里或6万亿英里) (除太阳外,最近的恒星大约离地球4.2光年)。请使用double类型(参见
程序清单2.4),转换公式为:
1光年=63240天文单位
#include <iostream> using namespace std; double convert(double L_Y) { return L_Y * 63240; } int main(void) { double L_Y = 0;//光年值 cout << "Enter the number of light years :"; cin >> L_Y; int ast = convert(L_Y); cout << L_Y << " light years = " <<ast << " astronomical units." << endl; system("pause"); return 0; }
7.编写一个程序,要求用户输入小时数和分钟数。在main( )函数中,将这两个值传递给一个void函
数,后者以下面这样的格式显示这两个值:
Enter the number of hours: 9
Enter the number of minutes: 28
Time: 9:28
#include <iostream> using namespace std; void print(int hours, int minutes) { cout <<"Time: "<< hours<<":"<<minutes << endl; } int main(void) { int hours = 0; int minutes = 0; cout << "Enter the number of hours :"; cin >> hours; cout << "Enter the number of minutes :"; cin >> minutes; print(hours, minutes); system("pause"); return 0; }
1.编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。
#include <iostream> using namespace std; int main(void) { //1 英尺=12 英寸 const int convert_factor = 12; int height_inches = 0; cout << "Please enter your height (in inches):"; cin >> height_inches; cout << "your height is " << height_inches / 12 << " foot and " << height_inches % 12 << " inches" << endl; system("pause"); return 0; }
2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI——体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
#include <iostream> using namespace std; int main(void) { //以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。 int height_foot; float height_inches, weight_pounds; cout << "Enter your height (in foot):"; cin >> height_foot; cout << "And enter your height (in inches):"; cin >> height_inches; cout << "Enter your weight(in pound):"; cin >> weight_pounds; cout << "So you are " << height_foot << " foot and " << height_inches << " inches height and " << weight_pounds << " pounds weight." << endl; //以英寸的方式指出用户的身高(1英尺为12英寸) const int foot_inches = 12; float convert_to_inches; convert_to_inches = 12 * height_foot + height_inches; cout << "Your height is " << convert_to_inches << " inches" << endl; //以英寸为单位的身高转换为以米为单位的身高(1英寸 = 0.0254米) const float inches_to_meters = 0.0254; float height_meters = convert_to_inches * inches_to_meters; cout << "Your height is " << height_meters << " meters" << endl; //将以磅为单位的体重转换为以千克为单位的体重(1千克 = 2.2磅) const float kg_to_pounds = 2.2; float weight_kg = weight_pounds / kg_to_pounds; cout << "Your weight is " << weight_kg << " kg." << endl; //计算相应的BMI——体重(千克)除以身高(米)的平方。 float BMI = weight_kg / pow(height_meters, 2); cout << "Your BMI is " << BMI << "!" << endl; system("pause"); return 0; }
3.编写一个程序,要求用户以度、分、秒的方式输入一个纬度;然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。下面是该程序运行时的情况:
Enter a latitude in degrees,minutes, and seconds:
First, enter the degrees: 37
Next, enter the minutes of arc:51
Finally, enter the seconds of arc: 19
37 degrees,51 minutes, 19 seconds = 37.8553 degrees
//1度为60分,1分等于60秒,请以符号常量的方式表示这些值。 //对于每个输入值, 应使用一个独立的变量存储它。下面是该程序运行时的情况: // // // First, enter the degrees : 37 // Next, enter the minutes of arc : 51 // // Finally, enter the seconds of arc : 19 // 37 degrees, 51 minutes, 19 seconds = 37.8553 degrees #include <iostream> using namespace std; int main(void) { //要求用户以度、分、秒的方式输入一个纬度 int degrees, minutes, seconds; float convert_to_degrees; 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; //以度为单位显示该纬度。 const int degrees_minutes = 60; const int degrees_seconds = 360; convert_to_degrees = degrees + (minutes / degrees_minutes) + (seconds / degrees_seconds); cout << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = " << convert_to_degrees << " degrees" << endl; system("pause"); return 0; }
4.编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
Enter the number of seconds: 31600000
31600000 seconds = 365 days,17 hours,46 minutes,40 seconds.
#include <iostream> using namespace std; int main(void) { //要求用户以整数方式输入秒数 long long before_seconds = 0; cout << "Enter the number of seconds :"; cin >> before_seconds; //使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒 const int day_hour = 24; const int hour_minute = 60; const int minute_second = 60; //以天、小时、分钟和秒的方式显示这段时间 int days,hours,minutes,seconds; days = before_seconds / (day_hour * hour_minute * minute_second); hours = (before_seconds % (day_hour * hour_minute * minute_second)) / (hour_minute * minute_second); minutes = (before_seconds - days * day_hour * hour_minute * minute_second - hours * hour_minute * minute_second) / minute_second; seconds = (before_seconds - days * day_hour * hour_minute * minute_second - hours * hour_minute * minute_second) % minute_second; cout << before_seconds << " seconds " << "= " << days << " days,"<< hours << " hours, " << minutes << " minutes," << seconds << " seconds" << endl; system("pause"); return 0; }
5.编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在 long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似:
Enter the world's population: 6898758899
Enter the population of the US : 310783781
The population of the US is 4.50492% of the world population.
#include <iostream> using namespace std; int main(void) { //要求用户输入全球当前的人口和美国当前的人口 long long population_world,population_US; cout << "Enter the world's population:"; cin >> population_world; cout << "Enter the population of the US :"; cin >> population_US; //显示美国的人口占全球人口的百分比 double percent = 100*((double)population_US / (double)population_world); cout << "The population of the US is " << percent<< "% of the world population." << endl; system("pause"); return 0; }
总结:刚开始没有对population_US 和population_world进行类型转换,导致结果不正确,应该注意先进行类型转换。100*((double)population_US / (double)population_world);
6.编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果—-—即每100公里的耗油量(升)。
#include <iostream> using namespace std; int main(void) { double miles, gal; cout << "Please enter the mileage(miles):"; cin >> miles; cout << "Please enter the amount of gasoline used(Gal):"; cin >> gal; cout<<"The fuel consumption of the car is "<<miles/gal<<" miles/gallon"<<endl; system("pause"); return 0; }
7.编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量―—每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此,19mpg大约合12.41/100km,127mpg大约合8.71/100km。(此处有误,应该是27而不是127)
#include <iostream> using namespace std; int main(void) { //用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)) double consumption_Eur; int mpg; cout << "Enter the fuel consumption of the vehicle (per 100 km):"; cin >> consumption_Eur; //将其转换为美国风格的耗油量―每加仑多少英里 const double _100km_mile = 62.14; const double gai_L = 3.875; mpg = _100km_mile/(consumption_Eur / gai_L) ; cout << "So your fuel consumption of the vehicle is " << mpg << "mpg" << endl; system("pause"); return 0; }