编程是一种控制计算机的方式,和我们平时双击打开文件、关机、重启没有任何区别。

    1.1 编写一个简单的C++程序

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. cout << "Hello World" << endl;
    5. return 0;
    6. }

    1.2 变量
    1.2.1 变量的定义

    变量必须先定义,才可以使用。不能重名。

    变量定义的方式:

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. int a = 5;
    5. int b, c = a, d = 10 / 2;
    6. return 0;
    7. }

    常用变量类型及范围:

    类型 关键字
    布尔型 bool
    字符型 char
    整型 int
    浮点型 float
    双浮点型 double

    1.2.2 输入输出

    整数的输入输出:

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. int a, b;
    5. cin >> a >> b;
    6. cout << a + b << endl;
    7. return 0;
    8. }

    字符串的输入输出:

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. int main() {
    5. string str;
    6. cin >> str;
    7. cout << str;
    8. return 0;
    9. }

    输入输出多个不同类型的变量:

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. int main() {
    5. int a, b;
    6. string str;
    7. cin >> a;
    8. cin >> b >> str;
    9. cout << str << " !!! " << a + b << endl;
    10. return 0;
    11. }

    1.2.3 表达式

    整数的加减乘除四则运算:

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. int a = 6 + 3 * 4 / 2 - 2;
    5. cout << a << endl;//10
    6. int b = a * 10 + 5 / 2;
    7. cout << b << endl;//102
    8. cout << 23 * 56 - 78 / 3 << endl;//1288-26=1262
    9. return 0;
    10. }

    浮点数(小数)的运算:

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. float x = 1.5, y = 3.2;
    5. cout << x * y << ' ' << x + y << endl;
    6. cout << x - y << ' ' << x / y << endl;
    7. return 0;
    8. }

    整型变量的自增、自减:

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. int a = 1;
    5. int b = a++;
    6. cout << a << ' ' << b << endl;
    7. int c = ++a;
    8. cout << a << ' ' << c << endl;
    9. return 0;
    10. }

    变量的类型转换:

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. float x = 123.12;
    5. int y = (int) x;
    6. cout << x << ' ' << y << endl;
    7. return 0;
    8. }

    1.2.4 顺序语句

    (1) 输出第二个整数:

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. int a, b, c;
    5. cin >> a >> b >> c;
    6. cout << b << endl;
    7. return 0;
    8. }

    (2) 计算 (a + b) * c的值

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. int a, b, c;
    5. cin >> a >> b >> c;
    6. cout << (a + b) * c << endl;
    7. return 0;
    8. }

    (3) 带余除法

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. int a, b;
    5. cin >> a >> b;
    6. int c = a / b, d = a % b;
    7. cout << c << ' ' << d << endl;
    8. return 0;
    9. }

    (4) 求反三位数:

    1. #include <iostream>
    2. #include <string>
    3. using namespace std;
    4. int main() {
    5. int n;
    6. cin >> n;
    7. int a = n % 10;
    8. n = n / 10;
    9. int b = n % 10;
    10. n = n / 10;
    11. int c = n;
    12. cout << a << b << c << endl;
    13. return 0;
    14. }

    (5) 交换两个整数

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. int a = 3, b = 4;
    5. int t = a;
    6. a = b;
    7. b = t;
    8. cout << a << ' ' << b << endl;
    9. return 0;
    10. }

    (6) 输出菱形

    1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. char c;
    5. cin >> c;
    6. cout << " " << c << endl;
    7. cout << " " << c << c << c << endl;
    8. cout << c << c << c << c << c << endl;
    9. cout << " " << c << c << c << endl;
    10. cout << " " << c << endl;
    11. return 0;
    12. }