3.1 简单变量
OOP的本质是设计并扩展自己的数据类型
C++的类型分为:基本类型和复合类型
int b;
b = 5;
找到一个能存储整数的内存 将内存单元标记为b 将5复制到内存单元中 可以使用b来访问该内存单元 使用&来检索b的内存地址
3.1.1 变量名
数字 字母字符 下划线 不能以数字开头 区分大小写 以 __ 或者 _和大写字母 开头的被保留实现
3.1.2 整型
char、 short、 int、 long 、long long
3.1.3 整型short、int、long和long long
1字节(byte) = 8位(bit)
1KB = 1024字节 1MB = 1024KB
short 至少 16位
int 至少与 short 一样长
long 至少 32位 至少与 int 一样长
long long 至少 64位 至少与 long 一样长
sizeof 返回 字节长度
C++初始化
int owls = 101;
int wrens(432);
short year; year = 1492;
int hamburgers = {24};
int emus{7};
int rocs = {};
int psychics {};
3.1.4 无符号整型
unsigned
3.1.5 选择整型类型
大于16位 使用long
一个字节用char
3.1.6 整型字面值
0X/0x42(hex) 042(octual) 42(decimal)
计算中都使用二进制存储
cout << hex; //以16进制显示
cout << oct;
3.1.7 C++如何确定常量类型
通常为int
看后缀 ul/uL/UL = unsigned long ll/LL = long long ull Ull uLL ULL = unsigned long long
3.1.8 char类型:字符和小整数
存储字符 char类型是另一种整型
char ch;
cin >> ch; //输入M
cout << ch; //输出M
cin 将M转化为 77 ; cout 将77转化为M
对于一些字符需要使用转义字符
3.1.9 bool类型
true(非0) / false(0)
int ans = true; // 将Bool类型转换为int类型 1
bool start = -100; // 将Int类型转化为bool类型 true
3.2 const限定符
const int Months = 12;
常量(Months)的值被初始化后,值被固定,不允许修改。
常见写法 首字母大写 或者 全部字符大写 或者k开头
const type name = value;
3.3 浮点数
对于34.1245和34124.5可以看作0.341245分别和100 / 10000的缩放因子
标准小数点表示法 / 科学计数法 (E/e表示法)
2.52e+8 //2.52*10^8
float,double,long double 有效位数不一样
浮点数有效范围大 但运算速度慢 精度将降低
float a = 2.34e+22f;
float b = a+1.0f;
b-a = 0
这是因为float只能表示前6位或者前7位
3.4 C++算数运算符
+ - * / % 浮点数尽量使用double
3.4.1 运算符优先级和结合性
3.4.2 除法分支
/ 行为取决于操作数类型
3.4.3 求模运算符
%
3.4.4 类型转换
将一种算术类型值赋给另一种算术类型 (将小范围赋给大范围,列表初始化不允许缩窄)
表达式中包含不同参数类型
short c = 20; 获取c值 转换为int 然后将int 转换为short
参数传递给函数
强制类型转换 int ('Q')
static_cast<type> (value)
int auks,bats,coots;
auks = 19.99 + 11.99; // auks 31
bats = (int) 19.99 + (int) 11.99; //bats 30
coots = int(19.99) + int (11.99); //coots 30
3.4.5 C++中auto声明
auto n = 100; //int
auto x = 1.5 //double
auto y = 1.3e12L //long double
复习题
1.C++有多种整型 根据数据运算需要选择合适类型
2.按需声明 short a = 80; unsigned int b = 42110; long long c= 3 000 000 000;
3. 防止超出整型范围 short int long longlong
4.33L和33区别 L :long U:unsigned
5.char grade = 65; char grade = 'A'; 等价 类型转换
6.编码88的字符 char(88); (char) 88
7.long 赋给double ;longlong 赋给 double long 4/8 byte 没有误差/舍入误差
8. 8*9+2 74 6*3/4 4 3/4 *6 0 6.0*3/4 4.5 15%4 3
9. int res = int (x1) + int (x2); int res = int (x1 + x2)
10 auto cars = 15; int auto iou = 15.37f f auto level = 'B' char auto crat = U'\U00002155' char32_t
auto fract = 8.25f / 2.5 double
编程练习
1.输入英寸 转换为英尺和英寸
#include <iostream>
int main() {
using namespace std;
const int Foot_to_Inch = 12;
int height;
//输入英寸
cout << "Enter your heiht(inch):";
cin >> height;
//转化
cout << "Your height is " << height / 12
<< "Foot & " << height % 12 << "Inch" << endl;
return 0;
}
2. 输入英尺英寸 输入磅 计算BMI
#include <iostream>
int main() {
using namespace std;
// Foot:英尺 Inch:英寸 Meter:米 Kilogram:千克 Pounds:磅
const int Foot_to_Inch = 12;
const float Inch_to_Meter = 0.0254;
const int Kilogram_to_Pounds = 2.2;
int Foot,Inch;
double Pounds,Height,Weight,BMI;
//计算身高
cout << "Enter your heiht(Foot & Inch):" << endl;
cin >> Foot;
cin >> Inch;
Height = Foot * Foot_to_Inch * Inch_to_Meter + Inch * Inch_to_Meter;
cout << "Your height is " << Height << "Meters" << endl;
//计算体重
cout << "Enter your weight(pounds):" << endl;
cin >> Pounds;
Weight = Pounds / Kilogram_to_Pounds;
cout << "Your weight is " << Weight << "Kilogram" << endl;
//计算BMI
BMI = Weight / (Height * Height);
cout << "Your BMI is " << BMI << endl;
return 0;
}
3. 输入 度分秒 显示度
#include <iostream>
int main() {
using namespace std;
//degree:度 minute:分 second:秒
const int Degree_to_Minute = 60;
const int Minute_to_Second = 60;
int degree, minute, second;
double latitude;
//输入
cout << "Enter the latitude in degree, minute, second " << endl;
cout << "First,enter the degree: ";
cin >> degree;
cout << "Next,enter the minute: ";
cin >> minute;
cout << "Finally,enter the second: ";
cin >> second;
//转化为degree
latitude = degree +
double(minute) / Degree_to_Minute +
double(second) / Minute_to_Second / Degree_to_Minute;
cout << degree << "degrees, " << minute << "minutes, "
<< second << "seconds" << "=" << latitude << "degrees" << endl;
return 0;
}
4.以整数形式输入秒 以天 小时 分钟 秒 显示
#include <iostream>
int main() {
using namespace std;
//day:天 hour:小时 minute:分钟 second:秒
const int Day_to_Hour = 24;
const int Hour_to_Minute = 60;
const int Minute_to_Second = 60;
long second, tem_second;
int day, hour, minute;
//输入秒
cout << "Enter the number of second: ";
cin >> second;
tem_second = second;
//计算day hour minute 以及 余下的second
day = second / Minute_to_Second / Hour_to_Minute / Day_to_Hour;
second = second % (day * Day_to_Hour * Hour_to_Minute * Minute_to_Second);
hour = second / Minute_to_Second / Hour_to_Minute;
second = second % (hour * Hour_to_Minute * Minute_to_Second);
minute = second / Minute_to_Second;
second = second % (minute * Minute_to_Second);
cout << tem_second << "seconds = "
<< day << " days, "
<< hour << " hours, "
<< minute << " minutes, "
<< second << " seconds, " << endl;
return 0;
}
5.输入全球人口和美国人口 存储在longlong 显示占比
#include <iostream>
int main() {
using namespace std;
long long world_population, US_population;
double percentage;
cout << "Enter the world's population: ";
cin >> world_population;
cout << "Enter the population of US: ";
cin >> US_population;
percentage = double(US_population) / double(world_population) * 100;
cout << "The population of US is "
<< percentage << "%" << " of the world population" << endl;
return 0;
}
6. 输入英里 加仑 指出耗油量为一加仑的里程
#include <iostream>
int main() {
using namespace std;
//mile:英里 gallon:加仑
double mile, gallon, Distance_In_oneGallon;
//输入mile gallon
cout << "Please enter the mile: ";
cin >> mile;
cout << "Please enter the gallon: ";
cin >> gallon;
Distance_In_oneGallon = mile / gallon;
// 耗油量为1gallon的里程
cout << "distance (when Gasoline_consume is one gallon): "
<< Distance_In_oneGallon << endl;
return 0;
}
输入 公里 升 指出每100公里的耗油量
#include <iostream>
int main() {
using namespace std;
//Kilometer:公里 Liter:升
//Liter_Per_Kilometer:每公里耗油量(升)
//Liter_Per_onehundredKilometer:每100公里耗油量(升)
double kilometer, liter, Liter_Per_Kilometer,Liter_Per_onehundredKilometer;
//输入公里 升
cout << "Please enter the kilometer: ";
cin >> kilometer;
cout << "Please enter the liter: ";
cin >> liter;
Liter_Per_Kilometer = liter / kilometer;
Liter_Per_onehundredKilometer = 100 * Liter_Per_Kilometer;
//每100公里的耗油量(升)
cout << "Gasoline_consume of one kilometers is: "
<< Liter_Per_Kilometer << endl;
cout << "Gasoline_consume of onehundred kilometers is: "
<< Liter_Per_onehundredKilometer << endl;
return 0;
}
7.输入每100公里的耗油量(liter) 转化为每加仑多少英里
#include <iostream>
int main() {
using namespace std;
//distance (mile:英里 kilmeter:公里)
//gasoline_consume(gallon:加仑 liter:升)
const double OnehundredKilometers_to_Mile = 62.14;
const double Gallon_to_Liter = 3.785;
double Liter_Per_onehundredKilometer, gallon, mile, Mile_Per_Gallon;
//输入每100公里耗油量
cout << "Enter the gasoline_consume(Liter per onehundredKilometers): ";
cin >> Liter_Per_onehundredKilometer;
//liter to gallon
gallon = Liter_Per_onehundredKilometer / Gallon_to_Liter;
//kilometer to mile
mile = OnehundredKilometers_to_Mile;
Mile_Per_Gallon = mile / gallon;
cout << "mile per gallon: " << Mile_Per_Gallon << endl;
return 0;
}