一、软件环境

1. 编辑软件的使用

2. 作业的评测与提交


二、编写一个简单的C++程序——手速练习

Hello World 程序

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello World" << endl;
    return 0;
}

三、语法基础

1. 变量的定义

基本规则

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

变量定义示例

#include <iostream>

using namespace std;

int main()
{
    int a = 5;
    int b, c = a, d = 10 / 2;

    return 0;
}
变量类型 32位系统占用字节 取值范围 用途
int 4 -2³¹ ~ 2³¹-1(约 ±21 亿) 存储整数
float ±3.4×10³⁸ 单精度浮点数(小数)
double 8 ±1.8×10³⁰⁸ 双精度浮点数(高精度小数)
char 1 -128~127 或 0~255 存储单个字符(如 'a'、'*')

2. 输入输出

整数的输入输出

#include <iostream>

using namespace std;

int main()
{
    int a, b;
    cin >> a >> b;
    cout << a + b << endl;
    return 0;
}

字符串的输入输出

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string str;
    cin >> str;
    cout << str;
    return 0;
}

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

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a, b;
    string str;

    cin >> a;
    cin >> b >> str;

    cout << str << " !!! " << a + b << endl;

    return 0;
}

3. 表达式

整数的加减乘除四则运算

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a = 6 + 3 * 4 / 2 - 2;

    cout << a << endl;

    int b = a * 10 + 5 / 2;

    cout << b << endl;

    cout << 23 * 56 - 78 / 3 << endl;

    return 0;
}

浮点数(小数)的运算

#include <iostream>
#include <string>

using namespace std;

int main()
{
    float x = 1.5, y = 3.2;

    cout << x * y << ' ' << x + y << endl;

    cout << x - y << ' ' << x / y << endl;

    return 0;
}

整型变量的自增、自减

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a = 1;
    int b = a ++ ;

    cout << a << ' ' << b << endl;

    int c = ++ a;

    cout << a << ' ' << c << endl;

    return 0;
}

变量的类型转换

#include <iostream>
#include <string>

using namespace std;

int main()
{
    float x = 123.12;

    int y = (int)x;

    cout << x << ' ' << y << endl;

    return 0;
}

4. 顺序语句

(1) 输出第二个整数

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a, b, c;
    cin >> a >> b >> c;
    cout << b << endl;
    return 0;
}

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

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a, b, c;

    cin >> a >> b >> c;

    cout << (a + b) * c << endl;

    return 0;
}

(3) 带余除法

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a, b;

    cin >> a >> b;

    int c = a / b, d = a % b;

    cout << c << ' ' << d << endl;

    return 0;
}

(4) 求反三位数

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n;
    cin >> n;

    int a = n % 10;
    n = n / 10;
    int b = n % 10;
    n = n / 10;
    int c = n;

    cout << a << b << c << endl;

    return 0;
}

(5) 交换两个整数

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a = 3, b = 4;

    int t = a;
    a = b;
    b = t;

    cout << a << ' ' << b << endl;

    return 0;
}

(6) 输出菱形

#include <iostream>
#include <string>

using namespace std;

int main()
{
    char c;

    cin >> c;

    cout << "  " << c << endl;
    cout << " " << c << c << c << endl;
    cout << c << c << c << c << c << endl;
    cout << " " << c << c << c << endl;
    cout << "  " << c << endl;

    return 0;
}

学习要点总结

基础概念

  • 变量定义:必须先定义后使用,不能重名
  • 输入输出:使用 cincout 进行标准输入输出
  • 表达式:支持四则运算、自增自减、类型转换等操作

编程技巧

  • 顺序结构:程序按语句顺序执行
  • 变量交换:使用临时变量进行交换
  • 数字处理:通过取余和整除操作处理数字位

0 条评论

目前还没有评论...