1、复习题
1.为什么C++有多种整型?
答:有多种整型类型,可以根据特定需求选择最合适的类型。例如,可以使用short来存储空格,使用long来确保存储容量,也可以寻找可提高特定计算的速度的类型。
2.声明与下述描述相符的变量。
a.short整数,值为80
b.unsigned int 整数,值为42110
c.值为3000000000的整数
答:
a.short rbis=80;(或short int rbis=80;)
b.unsigned int q=42110;(或unsigned q=42110;)
c.unsigned long ants=3000000000;(或long long ants=3000000000;)
3.C++提供了什么措施来防止超出整型的范围?
答:C++没有提供自动防止超出整型限制的功能,可以使用头文件climits来确定限制 情况。
4.33L与33之间有什么区别?
答:常量33L的类型为long,常量33的类型为int
5.下面两条C++语句是否等价?
char grade=65;
char grade='A';
答:这两条语句并不真正等价,虽然对于某些系统来说,他们是等效的。最重要的是,只有在使用ASCII码的系统上,第一条语句才将得分设置为字母A,而第二条语句还可用于使用其他编码的系统。其次,65是一个int常量,而‘A’是一个char常量。
6.如何使用C++来找出编码88表示的字符?指出至少两种方法。
答:下面是4种方式
char c=88;
cout<<c<<endl;//char type prints as character
cout.put(char(88));//put() prints char as character
cout<<char(88)<<endl;//new-style type cast value to char
cout<<(char)88<<endl;//old-style type cast value to char
7.将long值赋给float变量会导致舍入误差,将long值赋给double变量呢?将long long值赋给double变量呢?
答:这个问题的答案取决于这两个类型的长度。如果long为4个字节,则没有损失。因为最大的long值将是20亿,即有10位数。由于double提供了至少13位有效数字,因而不需要进行任何舍入。long long类型可提供19位有效数字,超过了double保证的13位有效数字。
8.下列C++表达式的结果分别是多少?
a.8*9+2
b.6*3/4
c.3/4*6
d.6.0*3/4
e.15%4
答:
a.74 b.4 c.0 d.4.5 e.3
9.假设x1和x2是两个double变量,您要将它们作为整数相加,再将结果赋给一个整型变量。请编写一条完成这项任务的C++语句。如果要将它们作为double值相加并转换为int呢?
答:
下面的代码都可用于完成第一项任务:
int pos=(int)x1+(int)x2;
int pos=int(x1)+int(x2);
要将它们作为double类型相加,再进行转换,可采取下属方式之一:
int pos=(int)(x1+x2);
int pos=int(x1+x2);
10.下面每条语句声明的变量都是什么类型?
a.auto cars=15
b.auto iou=150.37f
c.auto level='B'
d.auto crat=U'/U00002155'
e.auto fract=8.25f/2.5
答:a.int b.float c.char d.char32_t e.double
2、编程练习
1.编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。
答:
代码:
#include<iostream>
using namespace std;
const int conversion(12);//定义常量并赋初值
//1英尺=12英寸
int main()
{
int height;
cout<<"Enter your height:__\b\b";
cin>>height;
cout<<"Yor are "<<height/conversion<<" feet and "<<height%conversion<<" inches high."<<endl;
return 0;
}
2.编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI——体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
答:
代码:
#include<iostream>
using namespace std;
const int FoottoInch(12);//1英尺=12英寸
const double InchtoMeter(0.0254);//1英寸=0.0254米
const double KilogramtoPound(2.2);//1千克=2.2磅
int main()
{
float foot,inch,pound;
double height,weight,BMI;
cout<<"Enter your height(foot and inch):";
cin>>foot>>inch;
cout<<"Your height is "<<foot<<" feet "<<inch<<" inches."<<endl;
cout<<"Enter your weight(pound):";
cin>>pound;
cout<<"Your height is "<<pound<<" pounds."<<endl;
//转身高以米为单位
height=(foot*FoottoInch+inch)*InchtoMeter;
weight=pound/KilogramtoPound;
BMI=weight/(height*height);
cout<<"体重指数BMI为:"<<BMI<<endl;
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
答:
代码:
#include<iostream>
using namespace std;
const int conversion(60);
int main()
{
double degrees,minutes,seconds;
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;
cout<<degrees<<" degrees,"<<minutes<<" minutes,"<<seconds<<" seconds="<<(degrees+(seconds/conversion+minutes)/conversion)<<" degrees"<<endl;
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;
const int DaytoHour(24);
const int HourtoMinute(60);
const int MinutetoSecond(60);
int main()
{
long seconds;
int day,hour,minute,second;
cout<<"Enter the number of seconds:";
cin>>seconds;
second=seconds%MinutetoSecond;
minute=seconds/MinutetoSecond%HourtoMinute;
hour=seconds/MinutetoSecond/HourtoMinute%DaytoHour;
day=seconds/MinutetoSecond/HourtoMinute/DaytoHour;
cout<<second<<" seconds="<<day<<" days,"<<hour<<" hours,"<<minute<<" minutes,"<<second<<" seconds"<<endl;
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()
{
long long world,us;//MicrosoftVisual C++ 6.0编译器不支持long long类型
cout<<"Enter the world's population:";
cin>>world;
cout<<"Enter thr population of the US:";
cin>>us;
cout<<"The population of the US is "<<(us*100.0)/world<<"% of the world population."<<endl;
return 0;
}
6.编写一个程序,要求用户输入驱车例程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果——即每100公里的耗油量(升)。
答:
代码:
#include<iostream>
using namespace std;
int main()
{
double distance,volume;
cout<<"请输入驱车里程(英里):";
cin>>distance;
cout<<"请输入使用汽油量(加仑):";
cin>>volume;
cout<<"汽车耗油量为一加仑的里程为:"<<distance/volume<<endl;
cout<<"请输入驱车里程(公里):";
cin>>distance;
cout<<"请输入使用汽油量(升):";
cin>>volume;
cout<<"汽车每100公里耗油量为:"<<volume/distance*100<<endl;
return 0;
}
运行结果:
7.编写一个程序,要求用户按欧洲风格输入汽车耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量——每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料除以距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此,19mpg大约合12.41/100km,27mpg大约合8.71/100km。
答:
代码:
#include<iostream>
using namespace std;
const double KilometreToMile(62.14);//100公里=62.14英里
const double GallontoLiter(3.875);//1加仑=3.875升
int main()
{
double liter,gallon;
cout<<"请输入使用汽车耗油量(每100公里消耗的汽油量(升)):";
cin>>liter;
gallon=liter/GallontoLiter;
cout<<"汽车耗油量为一加仑的里程为:"<<KilometreToMile/gallon<<"; 即"<<liter<<"/100km="<<KilometreToMile/gallon<<"mpg"<<endl;
return 0;
}
运行结果: