编程要素课堂笔记
#include <bits/stdc++.h>
using namespace std;
int main() {
// 1. 数据类型
int a, b; //integer 整型 -21亿 ~ +21亿,10位数字
float a, b; // 7位有效数字, 123456.78
double a, b; // double 浮点数 17位有效数字
long long a, b; // 长整型:-922亿亿 ~ 922亿亿, 3 + 8 + 8 = 19 兆 M Million 百万
cin >> a >> b;
cout << a + b << endl;
bool n = (100 < 0); // 逻辑型:
cout << n << endl;
// 字符型
char c = 'f' - 'a' + 'A'; // 97 : a, 65 : A
cout << int(c) << endl;
int c2 = 'Z'; // 100 , 10000
cout << char(c2) << endl;
for (int i = 1; i <= 200; i++) {
c = i;
cout << c << " ";
}
// 1321 我, 3452 你, 2344 他
// 2. 变量和常量
const int c3 = 100;
const float pi1 = 3.14159265358979; // 单精度
cout << fixed << setprecision(14) << pi1 << endl;
const double pi2 = 3.14159265358979; // 双精度
cout << fixed << setprecision(3) << pi2 << endl;
// 3. 运算符
// (1) 算术运算符: +, -, *, /, %
long long n = 3 % 5;
cout << n << endl;
double db = 1.0 * 3 / 5 ; // 0.6 -> 小数点抹掉 0
cout << db * 100 << "%" << endl;
// (2) 关系运算符: >, >=, <, <=, ==, !=
bool flag = (100 == 101); // false : 0, true : 1
cout << flag << endl;
// (3) 逻辑运算符: && 逻辑与,且 相当于 and ; || 逻辑或 or; ! 逻辑非 not
n = 195;
flag = !(n < 0);
cout << flag << endl;
n < 0;
//4. 数学函数
long long m = pow(2, 8); // 乘方
double m = sqrt(27); // 平方
double m = cbrt(27); // 立方
double m = pow(256, 1.0/8) ; // 开方
double m = 3.234; // 3.24
cout << int(m) << endl; // 抹掉小数
cout << ceil(m) << endl; // 向上取整
cout << floor(m) << endl; // 向下取整
cout << "round = " << round(m) << endl; // 四舍五入
cout << "round = " << round(m * 100) / 100 << endl; // 四舍五入
cout << abs(-1) << endl; // 整数取绝对值
cout << fabs(-1.23) << endl; // 小数取绝对值
return 0;
}