1. 为什么 c++ 有多种整形?
C++有多种整型类型,可以根据特定需求选择最合适的类型,,来应对开发的需求。例如,可以使用 short 来存储空格,使用 long 来确保存储容量,也可以寻找可提高特定计算的速度的类型。
2. 声明与下述描述相符的变量。
a. short 整数,值为 80
b. unsigned int 整数,值为 42110
c. 值为 3000000000 的整数
short num1 = 80;
unsigned int num2 = 42110;
long long num3 = 3000000000;
3.C++ 提供了什么措施来防止超出整形的范围?
C++ 没有提供自动防止超出整型限制的功能,可以使用头文件 climits 来确定限制情况。
4. 33L 与 33 之间有什么区别?
常量 33L 的类型为 long,常量 33 的默认类型为 int。
5. 下面两条 c++ 语句是否等价?
char grade =45;
char grade = ‘A’;
这两条语句并不真正等价,虽然对于某些系统来说,它们是等效的。最重要的是,只有在使用 ASCII 码的系统上,第一条语句才将得分设置为字母 A,而第二条语句还可用于使用其他编码的系统(更推荐使用这种方式)。其次,45 是一个 int 常量,而’A’是一个 char 常量。
6. 如何使用 c++ 来找出编码 88 表示的字符?指出至少两种方法
char c = 88;
cout << c<<endl;
cout << char (88) << endl;
cout << (char)88<<endl;
7. 将 long 赋值给 float 变量会导致舍入误差,将 long 值赋给 double 变量呢?将 long long 值赋给 double 变量呢?
这个问题的答案取决于这两个类型的长度。如果 long 为 4 个字节,则没有损失。因为最大的 long 值将是 20 亿,即有 10 位数。由于 double 提供了至少 13 位有效数字,因而不需要进行任何舍入。long long 类型可提供 19 位有效数字,超过了 double 保证的 13 位有效数字。
8. 下列 c++ 表达式的结果分别是多少?
a.89+2
b.63/4
c.3/46
d.6.03/4
e.15%4
a.91
b.15
c.0
d.1.50
e.3
9. 假设 x1 和 x2 是两个 double 变量,你要将他们作为整数相加,再将结果赋给一个整型变量。请编写一条完成这项任务的 c++ 语句。如果要将他们作为 double 值相加并转换为 int 呢?
int num = long long (x1) + long long (x2);
int num1 = 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 ('U'标识)
e.double,2.5没有标识所以是double类型,8.25f是float类型,但是在相除过程中对float做了类型提升,所以结果是double类型。
编程题
1. 编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺,该程序使用下划线字符来指示输入位置。另外,使用一个 const 符号来表示转换因子。
#include <iostream>
using namespace std;
const double index = 12;
void test01();
int main()
{
test01();
return 0;
}
void test01()
{
double height;
cout << "请输入您的身高(英寸):______\b\b\b\b";
cin >> height;
cout << "您的身高是" << height / index << "英尺" << endl;
}
- 编写一个程序,要求以几英尺几英寸的方式输入其身高,并以磅为单位输入其体重。(使用 3 个变量来存储这些信息。)该程序报告其 BMI(Body Mass Index , 体重指数)。为了计算 BMI,该程序以英寸的方式指出用户的身高(一英尺为十二英寸),并将以英寸为单位的身高身高转换为以米为单位的身高(1 英寸 = 0.0254 米)。然后,将以磅为单位的体重转换为千克为单位的体重(1 千克 = 2.2 磅)。最后,计算相应的 BMI - 体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
#include <iostream>
#include <math.h>
using namespace std;
const double inches_per_foot = 12.0;
const double metres_per_inch = 0.0254;
const double pounds_per_kilogram = 2.2;
int main()
{
double foot, inch, pound;
cout << "Enter the height of foot:" << endl;
cin >> foot;
cout << "Enter the height of inch:" << endl;
cin >> inch;
cout << "Enter the height of pound:" << 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);
cout << "the BMI is " << BMI << endl;
return 0;
}
- 编写一个程序,要求用户以度分秒的方式输入一个纬度,然后以度为单位显示该纬度。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>
#include <math.h>
using namespace std;
const double degree_per_minutes = 60;
const double minutes_per_seconds = 60;
void test01();
int main()
{
test01();
return 0;
}
void test01()
{
int degrees;
cout << "First,enter the degrees :_____\b\b\b\b" ;
cin >> degrees;
int minutes;
cout << "Next,enter the minutes of arc :______\b\b\b\b" ;
cin >> minutes;
int seconds;
cout << "Finally,enter the seconds of arc :______\b\b\b\b" ;
cin >> seconds;
double latitude;
latitude = degrees + minutes / degree_per_minutes + seconds / minutes_per_seconds/ degree_per_minutes;
cout << degrees << "degrees, " << minutes << "minutes, " << seconds << "seconds = " << latitude <<" degrees" << endl;
}
- 编写一个程序,要求用户以整数方式输入秒数(使用 long 或 long long 变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
Enter the number of seconds:31600000
31600000 seconds = 365 days,17 hours,46 minutes,40 seconds
#include <iostream>
#include <math.h>
using namespace std;
const int min_per_sec = 60;
const int hours_per_sec = 60 * 60;
const long day_per_sec = 24 * 60 * 60;
const long long year_per_sec = 365 * 24 * 60 * 60;
void test01();
int main()
{
test01();
return 0;
}
void test01()
{
long long seconds;
cout << "Enter the number of seconds :__________\b\b\b\b\b\b\b\b" ;
cin >> seconds;
int years = seconds / year_per_sec;
int days = (seconds % year_per_sec )/ day_per_sec;
int hours = (seconds % day_per_sec) / hours_per_sec;
int minutes = (seconds % hours_per_sec) / min_per_sec;
cout << seconds << "seconds = " << years << "years, " << days<< "days ," << hours <<" hours," << minutes << " minutes," << seconds % min_per_sec << " seconds" << endl;
}
- 编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或其他国家的人口)。将这些信息存储在 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;
void test01();
int main()
{
test01();
return 0;
}
void test01()
{
long long worldPopulation;
cout << "Enter the world’s population :__________\b\b\b\b\b\b\b\b" ;
cin >> worldPopulation;
long long USPopulation;
cout << "Enter the population of the US :__________\b\b\b\b\b\b\b\b";
cin >> USPopulation;
double pencentage = double(USPopulation) / worldPopulation *100;
cout << "The population of the US is " << pencentage << "% of the world population." << endl;
}
6. 编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后指出欧洲风格的结果
#include <iostream>
using namespace std;
void test01(int choice);
int main()
{
cout << "选择单位 1:公里 2:英里" << endl;
int choice;
cin >> choice;
test01(choice);
return 0;
}
void test01(int choice)
{
if (choice != 1) {
long distance;
cout << "输入里程(英里):__________\b\b\b\b\b\b\b\b";
cin >> distance;
int Gasoline;
cout << "输入汽油量(加仑) :__________\b\b\b\b\b\b\b\b";
cin >> Gasoline;
double Miles_per_gallon = static_cast<double> (Gasoline) / distance;
cout << "每英里" << Miles_per_gallon << "加仑." << endl;
}
else
{
long distance;
cout << "输入里程(公里):__________\b\b\b\b\b\b\b\b";
cin >> distance;
int Gasoline;
cout << "输入汽油量(升) :__________\b\b\b\b\b\b\b\b";
cin >> Gasoline;
double kilom_per_gallon = static_cast<double> (Gasoline) / distance;
cout << "每公里" << kilom_per_gallon << "升." << endl;
}
}
- 编写一个程序,要求每个用户按欧洲风格输入汽车的耗油量(每 100 公里消耗汽油量(升 )),然后将其转换为美国风格的耗油量 - 每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离 / 燃料)与欧洲方法(燃料 / 距离)相反。100 公里等于 62.14 英里,1 加仑等于 3.875 升。因此,19mpg 大约合 12.41/100km,127mpg 大约合 8.71/100km。
#include <iostream>
using namespace std;
int main()
{
double fuel_per_km;
cout << "Fuel consumption of car(Fuel consumption per 100 km(L)):" << endl;
cin >> fuel_per_km;
double fuel_per_gallon = 62.14 / (fuel_per_km / 3.875);
cout << "The mileage per gallon:" << fuel_per_gallon << endl;
system("PAUSE");
return 0;
}