少儿编程C++快速教程之——1. 基础语法和输入输出

发布于:2025-09-04 ⋅ 阅读:(13) ⋅ 点赞:(0)

在这里插入图片描述

1. 欢迎来到C++编程世界!

1.1 什么是编程?

编程就像是给计算机写一份详细的"说明书",告诉它该做什么、怎么做。C++是一种强大的编程语言,可以用来创建游戏、应用程序和各种有趣的软件!

1.2 第一个C++程序:Hello World!

让我们从一个简单的程序开始,它会在屏幕上显示"Hello World!":

#include <iostream>  // 这行代码告诉计算机我们要使用输入输出功能
using namespace std; // 这行代码让我们可以使用简写方式(如cout而不是std::cout)

int main() {         // 每个C++程序都必须有一个main函数,程序从这里开始执行
    cout << "Hello World!" << endl; // 在屏幕上显示文字
    return 0;        // 告诉计算机程序成功结束
}

试一试:

  1. 打开代码编辑器(如Code::Blocks、Dev-C++或Visual Studio)
  2. 输入上面的代码
  3. 点击"运行"或"编译并运行"按钮
  4. 看看屏幕上显示了什么!

2. 变量:计算机的"记忆盒子"

2.1 什么是变量?

变量就像是计算机中的"记忆盒子",我们可以把数据放进去,需要的时候再取出来。每个盒子都有一个名字(变量名)和类型(能放什么样的数据)。

2.2 基本数据类型

数据类型 用途 示例
int 存储整数 int age = 15;
float 存储小数(单精度) float height = 1.75f;
double 存储小数(双精度,更精确) double pi = 3.14159;
char 存储单个字符 char grade = 'A';
string 存储文本(多个字符) string name = "小明";
bool 存储真/假值 bool isRaining = true;

注意: 使用string类型需要包含<string>头文件。

2.3 声明和使用变量

#include <iostream>
#include <string>   // 使用string类型需要这个
using namespace std;

int main() {
    // 声明变量并赋值
    int age = 15;
    float height = 1.75f;
    string name = "小明";
    char initial = 'X';
    bool likesPizza = true;
    
    // 使用变量
    cout << "姓名: " << name << endl;
    cout << "年龄: " << age << endl;
    cout << "身高: " << height << "米" << endl;
    cout << "名字首字母: " << initial << endl;
    
    if (likesPizza) {
        cout << name << "喜欢披萨!" << endl;
    } else {
        cout << name << "不喜欢披萨!" << endl;
    }
    
    return 0;
}

小练习: 创建一个程序,声明几个变量存储你的个人信息并显示出来。

3. 与计算机"对话":输入和输出

3.1 输出信息 (cout)

我们已经使用了cout来输出信息。cout就像是计算机的"嘴巴",可以说出我们想说的话。

#include <iostream>
using namespace std;

int main() {
    // 输出文字
    cout << "欢迎来到C++世界!" << endl;
    
    // 输出变量的值
    int score = 95;
    cout << "你的分数是: " << score << endl;
    
    // 一次输出多个内容
    string player = "小明";
    int level = 5;
    cout << "玩家:" << player << " 等级:" << level << endl;
    
    return 0;
}

3.2 获取输入 (cin)

cin就像是计算机的"耳朵",可以听到我们说的话(输入)。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    int age;
    
    cout << "请输入你的名字: ";
    cin >> name;  // 获取输入并存储到name变量中
    
    cout << "请输入你的年龄: ";
    cin >> age;   // 获取输入并存储到age变量中
    
    cout << "你好," << name << "! 你今年" << age << "岁了。" << endl;
    
    return 0;
}

注意: 当使用cin >>读取字符串时,它会在遇到空格时停止。如果要读取一整行文字,可以使用getline函数:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string fullName;
    
    cout << "请输入你的全名: ";
    cin.ignore(); // 清除之前的输入缓冲区
    getline(cin, fullName); // 读取一整行
    
    cout << "你的全名是: " << fullName << endl;
    
    return 0;
}

3.3 格式化输出

有时候我们希望输出的数据看起来更整齐,可以使用一些格式化方法:

#include <iostream>
#include <iomanip> // 用于格式化输出
using namespace std;

int main() {
    double pi = 3.1415926535;
    
    // 设置小数点后保留2位
    cout << fixed << setprecision(2);
    cout << "π的值是: " << pi << endl;
    
    // 设置输出宽度
    cout << setw(10) << "姓名" << setw(5) << "年龄" << endl;
    cout << setw(10) << "小明" << setw(5) << 15 << endl;
    cout << setw(10) << "小红" << setw(5) << 16 << endl;
    
    return 0;
}

小练习: 创建一个程序,询问用户三个科目的分数,然后计算并显示平均分(保留2位小数)。

4. 数学运算:计算机的"计算器"

4.1 基本算术运算符

C++支持所有基本的数学运算:

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 3;
    
    cout << "a + b = " << a + b << endl; // 加法: 13
    cout << "a - b = " << a - b << endl; // 减法: 7
    cout << "a * b = " << a * b << endl; // 乘法: 30
    cout << "a / b = " << a / b << endl; // 除法: 3 (整数除法)
    cout << "a % b = " << a % b << endl; // 取余: 1 (10除以3余1)
    
    // 浮点数除法
    float result = static_cast<float>(a) / b;
    cout << "浮点数除法: " << result << endl; // 3.33333
    
    return 0;
}

4.2 复合赋值运算符

这些运算符可以简化代码:

#include <iostream>
using namespace std;

int main() {
    int x = 5;
    
    x += 3;  // 等同于 x = x + 3
    cout << "x += 3: " << x << endl; // 8
    
    x -= 2;  // 等同于 x = x - 2
    cout << "x -= 2: " << x << endl; // 6
    
    x *= 4;  // 等同于 x = x * 4
    cout << "x *= 4: " << x << endl; // 24
    
    x /= 3;  // 等同于 x = x / 3
    cout << "x /= 3: " << x << endl; // 8
    
    return 0;
}

4.3 自增和自减运算符

#include <iostream>
using namespace std;

int main() {
    int count = 5;
    
    cout << "初始值: " << count << endl;
    
    count++; // 后置自增:先使用值,再增加1
    cout << "count++后: " << count << endl; // 6
    
    ++count; // 前置自增:先增加1,再使用值
    cout << "++count后: " << count << endl; // 7
    
    count--; // 后置自减:先使用值,再减少1
    cout << "count--后: " << count << endl; // 6
    
    --count; // 前置自减:先减少1,再使用值
    cout << "--count后: " << count << endl; // 5
    
    return 0;
}

小练习: 编写一个程序,将摄氏温度转换为华氏温度。公式:F = C × 9/5 + 32

5. 做出决定:条件语句

5.1 if 语句

if语句让程序能够根据条件做出决定:

#include <iostream>
using namespace std;

int main() {
    int score;
    cout << "请输入你的分数: ";
    cin >> score;
    
    if (score >= 60) {
        cout << "恭喜你及格了!" << endl;
    }
    
    return 0;
}

5.2 if-else 语句

当条件不满足时,可以使用else执行其他操作:

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "请输入一个数字: ";
    cin >> number;
    
    if (number % 2 == 0) {
        cout << "这是一个偶数" << endl;
    } else {
        cout << "这是一个奇数" << endl;
    }
    
    return 0;
}

5.3 else-if 语句(多条件判断)

当有多个条件需要检查时,可以使用else if

#include <iostream>
using namespace std;

int main() {
    int score;
    cout << "请输入你的分数: ";
    cin >> score;
    
    if (score >= 90) {
        cout << "成绩等级: A" << endl;
    } else if (score >= 80) {
        cout << "成绩等级: B" << endl;
    } else if (score >= 70) {
        cout << "成绩等级: C" << endl;
    } else if (score >= 60) {
        cout << "成绩等级: D" << endl;
    } else {
        cout << "成绩等级: F" << endl;
    }
    
    return 0;
}

5.4 购买贺卡题的实现

现在让我们解决购买贺卡的问题:

#include <iostream>
using namespace std;

int main() {
    int quantity;
    cout << "请输入购买贺卡的数量: ";
    cin >> quantity;
    
    int totalCost;
    if (quantity <= 10) {
        totalCost = quantity * 8;
    } else if (quantity <= 20) {
        totalCost = quantity * 6;
    } else {
        totalCost = quantity * 4;
    }
    
    cout << "总费用: " << totalCost << "元" << endl;
    
    return 0;
}

小练习: 编写一个程序,根据用户年龄判断是否可以观看PG-13电影(13岁以上可以观看)。

6. 重复执行:循环语句

6.1 for 循环

for循环当我们知道要重复多少次时使用:

#include <iostream>
using namespace std;

int main() {
    // 打印1到10的数字
    for (int i = 1; i <= 10; i++) {
        cout << i << " ";
    }
    cout << endl;
    
    // 计算1到100的和
    int sum = 0;
    for (int i = 1; i <= 100; i++) {
        sum += i;
    }
    cout << "1到100的和是: " << sum << endl;
    
    return 0;
}

for循环的结构:

  • int i = 1:初始化计数器
  • i <= 10:循环条件
  • i++:每次循环后更新计数器

6.2 while 循环

while循环在不确定要循环多少次,但知道循环条件时使用:

#include <iostream>
using namespace std;

int main() {
    // 打印1到10的数字
    int i = 1;
    while (i <= 10) {
        cout << i << " ";
        i++;
    }
    cout << endl;
    
    // 猜数字游戏
    int secretNumber = 42;
    int guess;
    
    cout << "猜一个1到100之间的数字: ";
    cin >> guess;
    
    while (guess != secretNumber) {
        if (guess < secretNumber) {
            cout << "太小了! 再试一次: ";
        } else {
            cout << "太大了! 再试一次: ";
        }
        cin >> guess;
    }
    
    cout << "恭喜你猜对了!" << endl;
    
    return 0;
}

6.3 do-while 循环

do-while循环至少执行一次,然后再检查条件:

#include <iostream>
using namespace std;

int main() {
    int number;
    
    do {
        cout << "请输入一个正数: ";
        cin >> number;
    } while (number <= 0);
    
    cout << "你输入的是: " << number << endl;
    
    return 0;
}

6.4 循环控制语句

  • break:立即退出循环
  • continue:跳过当前循环的剩余部分,直接开始下一次循环
#include <iostream>
using namespace std;

int main() {
    // break示例:当i等于5时退出循环
    for (int i = 1; i <= 10; i++) {
        if (i == 5) {
            break;
        }
        cout << i << " ";
    }
    cout << endl; // 输出: 1 2 3 4
    
    // continue示例:跳过偶数
    for (int i = 1; i <= 10; i++) {
        if (i % 2 == 0) {
            continue;
        }
        cout << i << " ";
    }
    cout << endl; // 输出: 1 3 5 7 9
    
    return 0;
}

小练习: 编写一个程序,使用循环打印出乘法表(如1×1=1, 1×2=2, …, 9×9=81)。

7. 综合练习

7.1 计算阶乘

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "请输入一个正整数: ";
    cin >> n;
    
    long long factorial = 1;
    for (int i = 1; i <= n; i++) {
        factorial *= i;
    }
    
    cout << n << "! = " << factorial << endl;
    
    return 0;
}

7.2 判断素数

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "请输入一个正整数: ";
    cin >> num;
    
    bool isPrime = true;
    if (num <= 1) {
        isPrime = false;
    } else {
        for (int i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }
    }
    
    if (isPrime) {
        cout << num << "是素数" << endl;
    } else {
        cout << num << "不是素数" << endl;
    }
    
    return 0;
}

7.3 斐波那契数列

#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "请输入斐波那契数列的项数: ";
    cin >> n;
    
    long long a = 0, b = 1;
    cout << "斐波那契数列前" << n << "项: ";
    
    for (int i = 0; i < n; i++) {
        cout << a << " ";
        long long next = a + b;
        a = b;
        b = next;
    }
    cout << endl;
    
    return 0;
}

8. 下一步学习建议

恭喜你完成了C++基础语法的学习!接下来你可以:

  1. 多练习:编写更多小程序来巩固知识
  2. 尝试小项目:如计算器、猜数字游戏、简单的文本冒险游戏
  3. 学习下一部分:字符串处理、数组和函数
  4. 参加编程竞赛:如NOIP(全国青少年信息学奥林匹克竞赛)

记住,编程就像学习一门新语言,需要不断练习才能熟练掌握。不要害怕犯错,每个错误都是学习的机会!

祝你编程愉快!


网站公告

今日签到

点亮在社区的每一天
去签到