Q1.整型溢出之后会发生什么情况?
假如一个short的值为32767,那么再进行+1操作之后,该值会变为-32768,也就意味着整型的最大值和最小值是链接的,溢出之后则回到值域的另一端。
Q2.如何输出八进制数?
默认情况下,cout以十进制数显示整数,而不管这些数在程序中是如何书写的。cout提供了控制符dec,hex,oct分别以十进制,十六进制,八进制显示整数,使用方式为cout << dec;
Q3.编译器如何确定常量的类型?
除非有特殊理由,C++编译器均将整形常量存储为int类型。
Q4.使用cout.setf将不会忽略小数点后的0。
Q5.C++强制类型转换 typeName(value),更严格的情况下可使用static_cast<typeName>(value) 可减少错误。
课后练习
//
// main.cpp
// exam3
//
// Created by L7ynn on 2017/2/24.
// Copyright © 2017年 L7ynn. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
const int In_To_Ft = 12;
const int Change = 60;
const long Day_To_Hour = 24;
const long Hour_To_Min = 60;
const long Min_To_Sec = 60;
int main(int argc, const char * argv[]) {
//e1 编写一个小程序,要求用户使用一个帧数指出自己的身高,单位为英寸,然后将身高转换为英尺和英寸,该程序使用下划线支付来之时输入位置,另外,使用一个const符号常量来表示转换因子。
int high = 0;
int in = 0;int ft = 0;
cout << "enter your high:___ ";
cin >> high;
in = high / In_To_Ft;
ft = high % In_To_Ft;
cout << endl << "your in is:" << in << "your ft is :" << ft << endl;
//e2 编写一个程序,要求用户以度,分,秒输入一个维度,然后以度为单位显示该纬度,以符号变量表示转换因子,对于每个输入址,应该使用一个独立的变量存储它。
int degrees = 0;int minutes = 0;int seconds = 0;
cout << endl << "enter a latitude in degrees,minutes and seconds:";
cout << endl << "first enter the degrees:";
cin >> degrees;
cout << endl << "and enter the minutes:";
cin >> minutes;
cout << endl << " finally enter the seconds:";
cin >> seconds;
cout << endl << degrees << " degrees, " << minutes << " minutes, " << seconds << " seconds = ";
cout << degrees * Change * Change + minutes * Change + seconds << "degrees";
//e3 编写一个程序,要求用户以整数方式输入秒数,然后以天,小时,分钟,和秒的形式显示这段时间。
long seconds1 = 0;long days = 0;long min = 0;
cout << endl << "enter the munber of seconds:";
cin >> seconds1;
days = seconds1 / (Day_To_Hour * Hour_To_Min * Min_To_Sec);
min = seconds1 % (Day_To_Hour * Hour_To_Min * Min_To_Sec) / (Hour_To_Min * Min_To_Sec);
cout << endl << seconds1 << "seconds = " << days << " days" << min << " minutes";
cout << seconds1 % (Day_To_Hour * Hour_To_Min * Min_To_Sec) % (Hour_To_Min * Min_To_Sec) /Min_To_Sec << " seconds";
//e4 编写一个程序,要求用户输入全球当前的人口喝美国当前的人口,将这些信息存储在long long变量中,并让程序显示美国的人口占全球人口的百分比。
long long world = 0;long long us = 0;
cout << endl << "enter the world's population:";
cin >> world;
cout << endl << "enter the population of the us:";
cin >> us;
cout << endl << "the population iof the us is : " << us / world * 100 << "% of the world population.";
//e5 编写一个程序,要求用户输入驱车历程和使用汽油量,然后指出汽车耗油量为一公里的里程。
int distance = 0;int gas = 0;
cout << endl << "enter the distance:";
cin >> distance;
cout << endl << "enter the gas:";
cin >> gas;
cout << endl << "the per gas of one kilometer is:" << distance / gas;
return 0;
}