声明与下述描述相符的变量。
a. short整数,值为80 答:short s = 80;
b. unsigned int整数,值为42110 答:unsigned int ui = 42110;
c. 值为3000000000的整数 答:long i = 3000000000; 答:unsigned long l = 3000000000; 或者 long long ll = 3000000000;不要指望int。
下列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。
假设x1和x2是两个double变量,您要将它们作为整数相加,再将结果赋给一个整型变量。请编写一条完成这项任务的C++语句。如果将它们作为double值相加并转换为int呢? 答:int ret = (int)x1 + (int)x2; 或者 int ret = int(x1) + int(x2);。 答:int ret = (int)(x1 + x2);或者 int ret = int(x1 + x2);。
下面每条语句声明的变量都是是什么类型?
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; 答:float。 答:double。
#include <iostream>
using namespace std;
const int g_s4Foot2Inch = 12;
int main(void)
{
float fHeight = 0;
cout << "Enter your height(inches): ";
cin >> fHeight;
cout << fHeight << " inches is " << (int)fHeight/g_s4Foot2Inch << " foots and " << fHeight-((int)fHeight/g_s4Foot2Inch*g_s4Foot2Inch) << " inches." << endl;
return 0;
}
编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以榜为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI---体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。1度为60分,1分等于60秒,请以符号常量的方式表示这些值。对于每个输入值,应使用一个独立的变量存储它,下面是该程序运行时的情况: Enter a latitude in degrees, minutes, and seconds: First, enter the degrees: 37 Next, enter the minutes:51 Finally, enter the seconds of arc: 19 37 degrees, 51 minutes, 19 seconds = 37.8553 degrees
#include <iostream>
using namespace std;
const int g_s4ConversionFactor = 60;
int main(void)
{
int s4Degrees = 0;
int s4Minutes = 0;
int s4Seconds = 0;
float fDegrees = 0.0f;
cout << "Enter a latitude in degrees, minutes, and seconds:";
cin >> s4Degrees >> s4Minutes >> s4Seconds;
cout << "First, enter the degrees: " << s4Degrees << endl;
cout << "Next, enter the minutes: " << s4Minutes << endl;
cout << "Finally, enter the seconds of arc: " << s4Seconds << endl;
fDegrees = s4Degrees + float((float(s4Seconds) / g_s4ConversionFactor + s4Minutes)) / g_s4ConversionFactor;
cout << s4Degrees << " degrees, " << s4Minutes << " minutes, " << s4Seconds << " seconds = " << fDegrees << " degrees" << endl;
return 0;
}
编写一个程序,要求用户以整数方式输入秒数(使用long或long long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似: Enter the number of seconds: 31600000 31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或者其他国家的人口)。将这些信息存储在long long变量中,并让程序显示美国(或者其他国家)的人口占全球人口的百分比。该程序的输出应与下面类似: Enter the world's population: 6898758899 Enter the population of the US: 310783781 The population of the US is 4.50942% of the world population.
#include <iostream>
using namespace std;
int main(void)
{
long long llWordPopulation, llUsPopulation;
cout << "Enter the world's population: ";
cin >> llWordPopulation;
cout << "Enter the population of the US: ";
cin >> llUsPopulation;
cout << "The population of the US is " << float(llUsPopulation)/llWordPopulation * 100 << "% of the world population." << endl;
return 0;
}