##2021.9.28 周二
变量
——————————————————————————————————————————————————
数据类型
- 整型
//整型
int num1 = 10;
int num2 = 10;
int sum = num1 + num2;
cout << "num1=" << num1 << endl;
cout << "二者的和为" << sum << endl;
- 实型/浮点型
//实型/浮点型
//单精度 float
float f1 = 3.14f;
cout << f1 << endl;
//双精度 double
double d1 = 3.14;
cout << d1 << endl;
//e记法
double d2 = 3e2;//3e2=3 * 10^2
double d3 = 3e-3;//3e-2=3 * 0.1^2
cout << d2 << endl;
cout << d3 << endl;
- 字符型
//创建一个字符型变量 单引号 一个字符
char ch = 'a';
cout << ch << endl;
//查看ASCII
//int将ch强制变成十进制的数字
//最后输出对应的ASCII码
cout << (int)ch << endl;
//a-97 A-65
- 字符串
#includeusing namespace std; #include //使用c++风格字符串,应包含string头文件 int main() { //c风格字符串 //注意: 要加"[]" ,双引号 char str1[] = "Hello World"; cout << str1 << endl; //c++风格字符串 //要包含一个头文件 string str2 = "Fuck"; cout << str2 << endl; }
- 布尔类型
//1,创建一个bool类型
bool flag = true;//true代表真
cout << flag << endl;
//输出:1
bool flag1 = false;//false代表假
cout << flag1 << endl;
- 等等



