科学计数法
# include <iostream>
using namespace std;
int main () {
// 科学计数法
float a = 1e5; // 10^5
cout << a << endl;
float b = -4e3; // -4 x 10^3
cout << b << endl;
double c = 4e-2; // 0.04
cout << c << endl;
double d = 4e-5; // 0.00004
cout << d << endl;
}
字符型 (char型)
作用: 字符型变量⽤于显⽰单个字符
- C和C++中字符型变量只占⽤1个字节
- 字符型变量并不是把字符本⾝放到内存中存储,⽽是将对应的ASCII编码放⼊到存储单元
# include <iostream>
using namespace std;
int main () {
// 科学计数法
float a = 1e5; // 10^5
cout << a << endl;
float b = -4e3; // -4 x 10^3
cout << b << endl;
double c = 4e-2; // 0.04
cout << c << endl;
double d = 4e-5; // 0.00004
cout << d << endl;
}
ASCII 码⼤致由以下两部分组成:
- ASCII ⾮打印控制字符: ASCII 表上的数字 0-31 分配给了控制字符,⽤于控制像打印机等⼀些外围 设备。
ASCII 打印字符:数字 32-126 分配给了能在键盘上找到的字符,当查看或打印⽂档时就会出现。
字符串型
作⽤:⽤于表⽰⼀串字符
两种风格
- C风格字符串 : char 变量名[] = "字符串值"
# include <iostream>
using namespace std;
int main () {
char str[] = "hello world";
cout << str<< endl;
}
- C++风格字符串 : string 变量名 = "字符串值"
# include <iostream>
# include <string>
using namespace std;
int main () {
// c++
string string1 = "hello world";
cout << string1<< endl;
}
c++风格字符串需要引入头文件#include <string>
布尔数据类型 boolean
布尔数据类型代表真或假的值
bool类型只有两个值:
- true --- 真(本质是1)
- false --- 假(本质是0)
bool类型占1个字节⼤⼩
# include <iostream>
# include <string>
using namespace std;
int main () {
// 整型输入
int age;
cout << "please input your age" << endl;
cin>>age; // 将console中的数据赋给变量a
cout << "age = " << age << endl;
// 浮点型输入
double height;
cout << "please input your height" << endl;
cin>>height; // 将console中的数据赋给变量age
cout << "height = " << height << endl;
// 字符型输入
char ch;
cout << "please input a char" << endl;
cin>>ch;
cout << "ch = " << ch << endl;
// 字符串型输入
string name;
cout << "please input your name" << endl;
cin>>name;
cout << "name = " << name << endl;
// 布尔型输入
bool flag;
cout << "please input a flag" << endl;
cin>>flag;
cout << "flag = " << flag << endl;
}
运算符
算术运算符: 四则运算
赋值运算符: 将表达式的值赋给变量
比较运算符: 表达式的比较, 返回布尔值
逻辑运算符: 根据表达式的值返回真或者假
算术运算符
# include <iostream>
# include <string>
using namespace std;
int main () {
int a = 10;
int b = 3;
double c = 3.0;
cout << "a + b = " << a + b << endl;
cout << "a - b = " << a - b << endl;
cout << "a * b = " << a * b << endl;
cout << "a / b = " << a / b << endl; // 整除
cout << "a / c = " << a / c << endl; // 整除
cout << "a % b = " << a % b << endl;
}
++前置和后置区别
# include <iostream>
using namespace std;
int main () {
// 参与运算的时候会有区别
int a = 10;
// 前置++ 先自增, 然后参与运算
// int b = ++a*10;
// cout << "b = " << b << endl;
// 后置++ 先参与运算, 然后自增
int b = a++*10;
cout << "b = " << b << endl;
cout << "a = " << a << endl;
int c = 100;
cout << "c = " << ++c << endl; // 101
cout << "c = " << c++ << endl; // 101
}
赋值运算符
# include <iostream>
using namespace std;
int main () {
int age = 10;
// age = age + 20;
age += 20;
cout << age <<endl;
}
比较运算符
# include <iostream>
using namespace std;
int main () {
int a = 10;
int b = 20;
cout << (a == b) << endl; // 0 false
cout << (a > b) << endl; // 0 false
cout << (a >= b) << endl; // 0 false
cout << (a != b) << endl; // 1 true
cout << (a < b) << endl; // 1 true
cout << (a <= b) << endl; // 1 true
}
网友评论