目录
复习题
1. 为什么C++有多种整型?
2. 声明与下述描述相符的变量?
3. C++ 提供了什么措施来防止超出整型的范围?
4. 33L和33之间有什么区别?
5. 下面两条C++语句是否等价?
6. 如何使用C++来找出编码88表示的字符?至少找出两种方法。
7. 将long值赋给float变量会导致舍入误差,将long值赋给double变量呢?将long long 值赋给double 变量呢?
8. 下列C++表达式的结果分别是多少?
9. 假设x1和x2是两个double变量,你要将他们作为整数相加,再将结果赋给一个整型变量。请编写一条完成这项任务的c++语句。如果要将他们作为double值相加并转换为int呢?
10. 下面每一条语句的变量都是什么类型?
编程题
复习题
1. 为什么C++有多种整型?
满足不同的计算机系统的所有计算需求。
int、short、long、unsigned int、long long、unsigned long等。
2. 声明与下述描述相符的变量?
short整数,值为80
unsigned int整数,值为42110
值为 3000000000 的整数
short a = 80; /*也可以写为 short int a = 80*/
unsigned int b = 42110;
unsigned long long c = 3000000000;
3. C++ 提供了什么措施来防止超出整型的范围?
C++没有提供自动防止范围越界的措施,但可以使用 climits头文件 来查看范围的最大值,从而达到预防越界。
4. 33L和33之间有什么区别?
33L是long int类型
33 是int 类型。
5. 下面两条C++语句是否等价?
char grade = 65;
char grade = 'A';
看情况而定。
- ASCII码的系统版本一致:两者语句等价。
- 系统不一致:65是int型,'A'是char型。
6. 如何使用C++来找出编码88表示的字符?至少找出两种方法。
int ch = 88;
cout.put(ch);/*方法一*/
cout<<char(ch); /*方法二*/
7. 将long值赋给float变量会导致舍入误差,将long值赋给double变量呢?将long long 值赋给double 变量呢?
> 将long值赋给double变量时,不会导致误差(long最大有10位,而double提供至少13位有效数字)。
>
> 将 long long值赋给double变量时,会导致舍入误差(long long提供了19位有效数字,而double只有13个有效数字)。
8. 下列C++表达式的结果分别是多少?
a.8*9+2 结果为:74
b.6*3/4 结果为:4
c.3/4*6 结果为:0
d.6.0*3/4 结果为:4.5
e.15%4 结果为:3
9. 假设x1和x2是两个double变量,你要将他们作为整数相加,再将结果赋给一个整型变量。请编写一条完成这项任务的c++语句。如果要将他们作为double值相加并转换为int呢?
double x1,x2;
int add = int(x1) + int(x2); /*整数相加,再赋值*/
int add = int(x1 + x2); /*double值相加,再转换为int*/
10. 下面每一条语句的变量都是什么类型?
a.auto cars=15; /*类型:int*/
b.auto iou=150.37f; /*类型:float*/
c.auto level='B'; /*类型:char*/
d.auto crat=U'/U00002155'; /*类型:char32_t*/
e.auto fract=8.25f/2.5; /*类型:double*/
编程题
1. 编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸,该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。
2. 编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI, 该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI——体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
3. 编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它。
4. 编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。
5. 编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或其他国家)的人口占全球人口的百分比。
6. 编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果——即每100公里的耗油量(升)。
7. 编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的耗油量——每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此,19mpg大约合12.41/100km,127mpg大约合8.71/100km。
以下是代码:
#include<iostream>
using namespace std;
#include <string>
void test01()
{
const double Inch_Per_Feet = 12;
double height_inch;
cout << "请输入你的身高:";
cin >> height_inch; // 读取输入英寸身高
cout << "你的身高是:" << height_inch << " 英寸" << endl;
double height_feet = height_inch / Inch_Per_Feet; // 转换后的英尺身高
cout << "你的身高是:" << height_feet << " 英尺" << endl;
}
void test02()
{
const int Inch_Per_Feet = 12;
const float m_Per_Inch = 0.0254;
const float kg_Per_pound = 2.2;
double height, weight, bmi;
float inches, feet;
cout << "please input your height(inch and feet) : " << endl;
cin >> inches >> feet;
cout << "please input your height (pound) : " << endl;
cin >> weight;
height = Inch_Per_Feet * inches * m_Per_Inch * feet;
weight = weight / kg_Per_pound;
bmi = weight / (height * height);
cout << "your height meter is " << height << endl;
cout << "your weight (kg) is " << weight << endl;
cout << "your bmi is " << bmi << endl;
}
void test03()
{
const int Degree_Per_Minute = 60;
const int Minute_Per_second = 60;
float 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;
float total_degrees = degrees + minutes / Degree_Per_Minute + seconds / Minute_Per_second;
cout << "\n" << degrees << " degrees," << minutes << " minutes," << seconds
<< " seconds = " << total_degrees << " degrees" << endl;
}
void test04()
{
const int Hour_Per_Day = 24;
const int Minute_Per_Hour = 60;
const int Second_Per_Minute = 60;
long long seconds;
cout << "Enter the number of seconds : ";
cin >> seconds;
int day = seconds / (Hour_Per_Day * Minute_Per_Hour * Second_Per_Minute);
int hour = seconds / (Minute_Per_Hour * Second_Per_Minute) % Hour_Per_Day;
int minute = seconds / Minute_Per_Hour % Minute_Per_Hour;
int left_second = seconds % Second_Per_Minute;
cout << seconds << " seconds = " << day << " days, " << hour
<< " hour, " << minute << "minutes, " << left_second << " seconds." << endl;
}
void test05()
{
long long chinese, worlds;
cout << "please input your chinese population : ";
cin >> chinese;
cout << "please input your world population : ";
cin >> worlds;
float percent = float(chinese) / float(worlds) * 100;
cout << "chinese percent is " << percent << "%" << endl;
}
void test06()
{
double distance, oil_vol;
cout << "please input your distance : ";
cin >> distance;
cout << "please input your oil volume : ";
cin >> oil_vol;
double mile_per_gallon = distance / oil_vol;
cout << "every gallon's ditance is " << mile_per_gallon << endl;
}
void test07()
{
float consumption;
cout << "please input your oil consumption every KM: ";
cin >> consumption;
float jl = consumption * 100 / 3.875;
cout << 62.14 / jl << endl;
}
int main()
{
test01();
test02();
test03();
test04();
test05();
test06();
test07();
return 0;
}