一、数据类型
- 字符串类型
- C风格字符串:
char 变量名[] = "字符串值"
- C++风格字符串:
string 变量名 = “字符串值”
- C风格字符串:
#include<iostream>
using namespace std;
#include<string> // 用C++风格字符串的需要包含这个头文件
int main()
{
// C语言风格
char str1[] = "hello world";
cout << "str1 = " << str1 << endl;
// C++风格,需要包含头文件
string str2 = "hello world";
cout << "str2 = " << str2 << endl;
system("pause");
return 0;
}
打印结果:
str1 = hello world
str2 = hello world
- 布尔类型 bool
只占 1 个字节大小
true false
代码示例:
#include<iostream>
using namespace std;
int main()
{
bool flag1 = true;
cout << "flag1 = " << flag1 << endl;
bool flag2 = true;
cout << "flag2 = " << flag2 << endl;
cout << "bool占内存:" << sizeof(bool) << endl;
system("pause");
return 0;
}
打印结果:
flag1 = 1
flag2 = 1
bool占内存:1
- 数据的输入
作用:从键盘获取数据
关键字:cin
语法:cin >> 变量
代码示例:
#include<iostream>
using namespace std;
#include<string>
int main()
{
//1、整型
int a = 0;
cout << "请给整型变量 a 赋值:" << endl;
cin >> a;
cout << "a = " << a << endl;
//2、浮点型
float f = 3.14;
cout << "请给浮点型变量 f 赋值:" << endl;
cin >> f;
cout << "f = " << f << endl;
//3、字符型
char ch = 'a';
cout << "请给字符型变量 ch 赋值:" << endl;
cin >> ch;
cout << "ch = " << ch << endl;
//字符串型
string str = "hello";
cout << "请给字符串型变量 str 赋值:" << endl;
cin >> str;
cout << "str = " << str << endl;
//布尔型
bool flag = true;
cout << "请给字符串型变量 flag 赋值:" << endl;
cin >> flag;
cout << "flag = " << flag << endl;
system("pause");
return 0;
}
打印结果:
请给整型变量 a 赋值:
5
a = 5
请给浮点型变量 f 赋值:
3.14
f = 3.14
请给字符型变量 ch 赋值:
a
ch = a
请给字符串型变量 str 赋值:
hello
str = hello
请给字符串型变量 flag 赋值:
false
flag = 0
请按任意键继续. . .
二、运算符
- 算术运算符 ->用于处理四则运算
- 赋值运算符 ->用于将表达式的值赋给变量
- 比较运算符 ->用于表达式的比较,并返回一个真值或假值
- 逻辑运算符 ->用于根据表达式的值返回真值或假值
- 算术运算符
代码示例–加减乘除:
#include<iostream>
using namespace std;
int main()
{
int a1 = 10;
int b1 = 3;
cout << "a1 + b1 = " << a1 + b1 << endl;
cout << "a1 - b1 = " << a1 - b1 << endl;
cout << "a1 * b1 = " << a1 * b1 << endl;
// 两个整数相除,结果仍然是整数,将小数部分去除
cout << "a1 / b1 = " << a1 / b1 << endl;
int a2 = 10;
int b2 = 20;
cout << "a2 / b2 = " << a2 / b2 << endl;
// 两个数相除,除数不能为0
//int a3 = 10;
//int b3 = 0;
//cout << "a3 / b3 = " << a3 / b3 << endl;
//两个小数可以相除,运算结果可以是小数,也可以是整数
double d1 = 0.5;
double d2 = 0.22;
cout << "d1 / d2 = " << d1 / d2 << endl;
system("pause");
return 0;
}
打印结果:
a1 + b1 = 13
a1 - b1 = 7
a1 * b1 = 30
a1 / b1 = 3
a2 / b2 = 0
d1 / d2 = 2.27273
代码示例–取模运算:
#include<iostream>
using namespace std;
int main()
{
//取模运算的本质就是求余数
int a1 = 10;
int b1 = 3;
cout << "a1 % b1 = " << a1 % b1 << endl;
int a2 = 10;
int b2 = 20;
cout << "a2 % b2 = " << a2 % b2 << endl;
//两个数相除,除数不可以为0,所以也做不了取模运算
//int a3 = 10;
//int b3 = 0;
//cout << "a3 % b3 = " << a3 % b3 << endl;
//两个小数不可以做取模运算
//double d1 = 3.14;
//double d2 = 1.1;
//cout << "d1 % d2 = " << d1 % d2 << endl;
system("pause");
return 0;
}
打印结果:
a1 % b1 = 1
a2 % b2 = 10
请按任意键继续. . .
代码示例–递增递减:
#include<iostream>
using namespace std;
int main()
{
//前置递增
int a = 10;
++a;
cout << "a = " << a << endl;
//后置递增
int b = 10;
b++;
cout << "b = " << b << endl;
int a2 = 10;
int b2 = ++a2 * 10; // a2先自加1,再 * 10
cout << "a2 = " << a2 << endl;
cout << "b2 = " << b2 << endl;
int a3 = 10;
int b3 = a3++ * 10; // a2先 * 10,再自加 1
cout << "a3 = " << a3 << endl;
cout << "b3 = " << b3 << endl;
system("pause");
return 0;
}
打印结果:
a = 11
b = 11
a2 = 11
b2 = 110
a3 = 11
b3 = 100
请按任意键继续. . .
- 赋值运算符
- 比较运算符
代码示例:
#include<iostream>
using namespace std;
int main()
{
int a = 10;
int b = 20;
cout << (a == b) << endl;
cout << (a != b) << endl;
cout << (a <= b) << endl;
cout << (a >= b) << endl;
cout << (a > b) << endl;
cout << (a < b) << endl;
system("pause");
return 0;
}
打印结果:
0
1
1
0
0
1
请按任意键继续. . .
- 逻辑运算符