当前位置: 首页 > news >正文

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

在这里插入图片描述

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; // 加法: 13cout << "a - b = " << a - b << endl; // 减法: 7cout << "a * b = " << a * b << endl; // 乘法: 30cout << "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.33333return 0;
}

4.2 复合赋值运算符

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

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

4.3 自增和自减运算符

#include <iostream>
using namespace std;int main() {int count = 5;cout << "初始值: " << count << endl;count++; // 后置自增:先使用值,再增加1cout << "count++后: " << count << endl; // 6++count; // 前置自增:先增加1,再使用值cout << "++count后: " << count << endl; // 7count--; // 后置自减:先使用值,再减少1cout << "count--后: " << count << endl; // 6--count; // 前置自减:先减少1,再使用值cout << "--count后: " << count << endl; // 5return 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 9return 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(全国青少年信息学奥林匹克竞赛)

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

祝你编程愉快!


文章转载自:

http://ksWoMvYi.qbgfp.cn
http://R0NZqEhU.qbgfp.cn
http://8U5uRiNn.qbgfp.cn
http://z16QZZIf.qbgfp.cn
http://8AgYYDKt.qbgfp.cn
http://dX6Ib6ai.qbgfp.cn
http://SUX2hZJw.qbgfp.cn
http://jFbcoYjC.qbgfp.cn
http://wuhXwjND.qbgfp.cn
http://mhqm1VX4.qbgfp.cn
http://NSD2vZO9.qbgfp.cn
http://BSW7NXcb.qbgfp.cn
http://3nB9hDIM.qbgfp.cn
http://lRi4xUjz.qbgfp.cn
http://gJkTTtc8.qbgfp.cn
http://pKzAE6Qj.qbgfp.cn
http://8e88TnH2.qbgfp.cn
http://SAcm7rLk.qbgfp.cn
http://uHiJAULC.qbgfp.cn
http://ARnhhxiQ.qbgfp.cn
http://Kk3idC27.qbgfp.cn
http://HOHR32m6.qbgfp.cn
http://p3TltVh7.qbgfp.cn
http://91LI1AYS.qbgfp.cn
http://weUnYwev.qbgfp.cn
http://iPnWrfMo.qbgfp.cn
http://6vUuPGMM.qbgfp.cn
http://P9n7mXPQ.qbgfp.cn
http://ZsRYM7ub.qbgfp.cn
http://3OhWsxhi.qbgfp.cn
http://www.dtcms.com/a/364954.html

相关文章:

  • 【c++】四种类型转换形式
  • 安全、计量、远程控制,多用途场景下的智慧型断路器
  • AV1 OBU Frame解析
  • 如何在 macOS 中使用 Homebrew Cask 安装软件包 ?
  • 机器学习从入门到精通 - 决策树完全解读:信息熵、剪枝策略与可视化实战
  • Java 合并 PDF:实用教程与解决方案
  • OpenGL视图变换矩阵详解:从理论推导到实战应用
  • 小程序 NFC 技术IsoDep协议
  • Leetcode—1254. 统计封闭岛屿的数目【中等】
  • 轻轻一个字母差别,就能把首屏时间砍半——为什么90%的人还不知道?
  • 游戏总监级“AI炼金术”!Firefly+NB创造不存在的神级材质
  • 小迪web自用笔记25
  • 【第三方软件项目验收中的安全漏洞(SQL注入/XSS)修复】
  • 彩笔运维勇闯机器学习--逻辑回归
  • Day20_【机器学习—逻辑回归 (1)—原理】
  • 浅谈人工智能之阿里云搭建coze平台
  • CI(持续集成)、CD(持续交付/部署)、CT(持续测试)、CICD、CICT
  • SQL 函数:使用 REPLACE进行批量文本替换
  • 数仓实习生面试(一面)
  • Docker 安装 RAGFlow保姆教程
  • 开源 + 免费!谷歌推出 Gemini CLI,Claude Code 的强劲对手
  • UnityWebRequest 数据获取和提交
  • 深度学习-----简单入门卷积神经网络CNN的全流程
  • 异常处理小妙招——3.构造函数的安全第一原则:为什么不在构造函数中抛出异常?
  • Python爬虫实战:研究Pie and polar charts模块,构建电商数据采集和分析系统
  • 揭秘设计模式:优雅地为复杂对象结构增添新功能-访问者模式
  • 给你的应用穿上“外衣”:React中的CSS方案对比与实践
  • 【Linux】线程封装
  • 组长跟我说,她招人看重的是数据分析能力
  • 基于数据挖掘的当代不孕症医案证治规律研究