C++学习之旅
- 今天学完第三章
1 byte= 8 位
short为16位,long为32位,long long为64位
sizeof返回类型或变量的长度,单位为字节
cout<<hex;//改变输出格式为16进制
cout<<oct;//改变输出格式为8进制
对字符使用单引号,对字符串使用双引号
\a表示一个振铃
/*振铃*/
#include <iostream>
using namespace std;
int main()
{
char alarm='\a';
cout << alarm<<"Don't do that again!\a\n "<<endl;
cin.get();
return 0;
}
当两个整数进行除法的时候,得到的结果将会被保留为整数(直接丢弃小数部分);
若其中有一个或两个是浮点数,则得到的结果也为浮点数。
第三章书后习题
3-2
/*以几英尺几英寸的方式输入身高,以磅为单位输入体重,报告其BMI值*/
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << "Please input your height in foot and inch" << endl;
cout << "foot:_\b ";//利用\b退格符来控制光标
int foot;
cin >> foot;
cout<< "inch:_\b ";
int inch;
cin >> inch;
cout << "Please input your weight in pounds" << endl;
cout << "pounds:_\b ";
double pounds;
cin >> pounds;
const int foot_to_inch = 12;
const double inch_to_meter = 0.0254;
const double kg_to_pound = 2.2;
int meters;
meters = (foot_to_inch*foot + inch)*inch_to_meter;
double kilos;
kilos = pounds / kg_to_pound;
double BMI;
BMI = kilos / (meters ^ 2);
cout << "Your BMI is: " << BMI << endl;
cin.get();
cin.get();
return 0;
}
剩下的习题和3-2类似,因此不做练习~