第3章 处理数据

2018-12-02  本文已影响0人  肥树仙僧

复习题

  1. 为什么C++有多种整型?
    答:母鸡啊。
    答:有多种整型类型,可以根据特定需求选择最合适的类型。
  2. 声明与下述描述相符的变量。
    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
  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表示的字符?指出至少两种方法。
    答:使用字符跟‘X’比较;把字符转成int类型后跟88比较。(原因:题意理解错。)
    答:下面是四种方法:
char c = 88;
cout << c << endl;

cout.put(char(88));

cout << char(88) << endl;

cout << (char)88 << endl;
  1. long值赋给float变量会导致舍入误差,将long值赋给double变量呢?将long long值赋给double变量呢?
    答:需要了解各个类型的长度,以后再补上。
  2. 下列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
  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);
  4. 下面每条语句声明的变量都是是什么类型?
    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

编程练习

  1. 编写一个小程序,要求用户使用一个整数指出自己的身高(单位为英寸),然后将身高转换为英尺和英寸。该程序使用下划线字符来指示输入位置。另外,使用一个const符号常量来表示转换因子。
    注:1英尺(ft)=12英寸(in)。
#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;
}
  1. 编写一个小程序,要求以几英尺几英寸的方式输入其身高,并以榜为单位输入其体重。(使用3个变量来存储这些信息。)该程序报告其BMI(Body Mass Index,体重指数)。为了计算BMI,该程序以英寸的方式指出用户的身高(1英尺为12英寸),并将以英寸为单位的身高转换为以米为单位的身高(1英寸=0.0254米)。然后,将以磅为单位的体重转换为以千克为单位的体重(1千克=2.2磅)。最后,计算相应的BMI---体重(千克)除以身高(米)的平方。用符号常量表示各种转换因子。
#include <iostream>
using namespace std;

const int       g_s4Foot2Inch = 12;
const float     g_s4Kg2Lb = 2.2f; //要加“f”,否则会报警告。不写“f”默认是double
const float     g_s4Inch2M = 0.0254f;

int main(void)
{
    int     s4Foots = 0;
    float   fInches = 0;
    float   fLb = 0;

    float   fHeight, fWeight;

    cout << "Enter your height(foots and inches): ";
    cin >> s4Foots >> fInches;
    cout << "Enter your weight(LB): ";
    cin >> fLb;

    fHeight = (s4Foots * g_s4Foot2Inch + fInches) * g_s4Inch2M;
    fWeight = fLb / g_s4Kg2Lb;

    cout << "Your BMI is: " << fWeight / (fHeight*fHeight) << endl;
    
    return 0;
}
  1. 编写一个程序,要求用户以度、分、秒的方式输入一个纬度,然后以度为单位显示该纬度。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;
}
  1. 编写一个程序,要求用户以整数方式输入秒数(使用longlong long变量存储),然后以天、小时、分钟和秒的方式显示这段时间。使用符号常量来表示每天有多少小时、每小时有多少分钟以及每分钟有多少秒。该程序的输出应与下面类似:
    Enter the number of seconds: 31600000
    31600000 seconds = 365 days, 17 hours, 46 minutes, 40 seconds
#include <iostream>
using namespace std;

int main(void)
{
    int     s4TotalSeconds = 0;
    int     s4Days, s4Hours, s4Minutes, s4Seconds;

    cout << "Enter the number of seconds: ";
    cin >> s4TotalSeconds;

    s4Days = s4TotalSeconds / (60 * 60 * 24);
    s4Hours = (s4TotalSeconds % (60 * 60 * 24)) / (60 * 60);
    s4Minutes = (s4TotalSeconds % (60 * 60)) / 60;
    s4Seconds = s4TotalSeconds % 60;

    cout << s4TotalSeconds << " seconds = " << s4Days << " days, " << s4Hours << " hours, " << s4Minutes << " minutes, " << s4Seconds << " seconds." << endl;

    return 0;
}
  1. 编写一个程序,要求用户输入全球当前的人口和美国当前的人口(或者其他国家的人口)。将这些信息存储在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;
}
  1. 编写一个程序,要求用户输入驱车里程(英里)和使用汽油量(加仑),然后指出汽车耗油量为一加仑的里程。如果愿意,也可以让程序要求用户以公里为单位输入距离,并以升为单位输入汽油量,然后计指出欧洲风格的结果---即每100公里的耗油量(升)。
#include <iostream>
using namespace std;

int main(void)
{
    float       fMlies = 0;
    float       fGallon = 0;

    cout << "输入此次行驶中的里程(英里)和耗油(加仑): ";
    cin >> fMlies >> fGallon;

    cout << "耗油量:" << fMlies / fGallon << "英里/加仑。" << endl;

    return 0;
}
  1. 编写一个程序,要求用户按欧洲风格输入汽车的耗油量(每100公里消耗的汽油量(升)),然后将其转换为美国风格的结果的耗油量---每加仑多少英里。注意,除了使用不同的单位计量外,美国方法(距离/燃料)与欧洲方法(燃料/距离)相反。100公里等于62.14英里,1加仑等于3.875升。因此,19mpg大约合12.4/100km,127mpg大约合8.71/100km。
#include <iostream>
using namespace std;

const float g_fMile2KM = 1.609344f;
const float g_fGallon2Liter = 3.7854118f;

int main(void)
{
    float       fUsStyleGallonMile = 0;
    float       fEuropeStyleKmLiter = 0;
    float       fKiloMeter = 0;
    float       fLiter = 0;

    cout << "请输入美国风格油耗值: ";
    cin >> fUsStyleGallonMile;

    fKiloMeter = fUsStyleGallonMile * g_fMile2KM;
    fLiter = 1 * g_fGallon2Liter;

    fEuropeStyleKmLiter = fLiter / fKiloMeter * 100;

    cout << fUsStyleGallonMile << "的美国风格耗油值转换为欧洲风格后的结果:" << fEuropeStyleKmLiter << endl;

    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读