C++primerplus编程练习参考答案
C++primerplus编程练习参考答案⽬录
高速追尾第2章开始学习C++
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
其中⼀个函数要调⽤两次,该函数⽣成前两⾏,另⼀个函数也被调⽤两次,并⽣成其余的输出
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
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
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;
}
第3章处理数据
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——体重(千克)除以⾝⾼(⽶)的平⽅。⽤符号常量表⽰各种转换因⼦。
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

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。