📅 C++ 第1天:变量、数据类型与基本语法
学习目标
- 掌握C++基本数据类型和变量声明
- 理解常量与字面量的概念
- 熟悉运算符优先级和控制结构
- 能够编写基本的C++程序
📚 学习内容
1. 基本数据类型
C++提供了丰富的数据类型,主要包括:
整数类型
1 2 3
| int age = 25; long long bigNumber = 1234567890LL; short smallNumber = 100;
|
浮点类型
1 2 3
| float price = 19.99f; double pi = 3.14159265359; long double precise = 3.141592653589793238L;
|
字符类型
1 2 3 4
| char grade = 'A'; wchar_t wideChar = L'中'; char16_t utf16Char = u'中'; char32_t utf32Char = U'中';
|
布尔类型
1 2
| bool isStudent = true; bool isGraduated = false;
|
2. 变量声明与初始化
声明方式
1 2 3 4 5 6 7 8 9 10 11
| int x; x = 10;
int y{20}; int z = {30};
auto name = "Hello"; auto count = 42;
|
常量声明
1 2 3 4 5
| const int MAX_SIZE = 100; constexpr int BUFFER_SIZE = 256;
const double PI = 3.14159;
|
3. 运算符优先级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <iostream> using namespace std;
int main() { int a = 10, b = 3, c = 2; int result1 = a + b * c; int result2 = (a + b) * c; bool isGreater = a > b; bool isEqual = a == b; bool logicalAnd = (a > 5) && (b < 10); bool logicalOr = (a < 5) || (b > 10); cout << "result1: " << result1 << endl; cout << "result2: " << result2 << endl; cout << "isGreater: " << isGreater << endl; return 0; }
|
4. 控制结构
条件语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <iostream> using namespace std;
int main() { int score; cout << "请输入分数: "; cin >> score; if (score >= 90) { cout << "优秀" << endl; } else if (score >= 80) { cout << "良好" << endl; } else if (score >= 70) { cout << "中等" << endl; } else if (score >= 60) { cout << "及格" << endl; } else { cout << "不及格" << endl; } char grade; cout << "请输入等级: "; cin >> grade; switch (grade) { case 'A': case 'a': cout << "优秀" << endl; break; case 'B': case 'b': cout << "良好" << endl; break; case 'C': case 'c': cout << "中等" << endl; break; default: cout << "其他等级" << endl; break; } return 0; }
|
循环语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <iostream> using namespace std;
int main() { cout << "for循环示例:" << endl; for (int i = 1; i <= 5; i++) { cout << "i = " << i << endl; } cout << "\nwhile循环示例:" << endl; int j = 1; while (j <= 3) { cout << "j = " << j << endl; j++; } cout << "\ndo-while循环示例:" << endl; int k = 1; do { cout << "k = " << k << endl; k++; } while (k <= 3); return 0; }
|
💻 实践练习
练习1:数据类型范围
编写一个程序,显示不同数据类型的范围:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #include <iostream> #include <limits> using namespace std;
int main() { cout << "=== C++数据类型范围 ===" << endl; cout << "int范围: " << INT_MIN << " 到 " << INT_MAX << endl; cout << "long范围: " << LONG_MIN << " 到 " << LONG_MAX << endl; cout << "float范围: " << FLT_MIN << " 到 " << FLT_MAX << endl; cout << "double范围: " << DBL_MIN << " 到 " << DBL_MAX << endl; cout << "char范围: " << CHAR_MIN << " 到 " << CHAR_MAX << endl; return 0; }
|
练习2:计算器程序
编写一个简单的计算器程序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <iostream> using namespace std;
int main() { double num1, num2; char operation; cout << "请输入第一个数字: "; cin >> num1; cout << "请输入运算符 (+, -, *, /): "; cin >> operation; cout << "请输入第二个数字: "; cin >> num2; double result; switch (operation) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': if (num2 != 0) { result = num1 / num2; } else { cout << "错误:除数不能为零!" << endl; return 1; } break; default: cout << "错误:不支持的运算符!" << endl; return 1; } cout << num1 << " " << operation << " " << num2 << " = " << result << endl; return 0; }
|
练习3:数字游戏
编写一个猜数字游戏:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream> #include <cstdlib> #include <ctime> using namespace std;
int main() { srand(time(0)); int secretNumber = rand() % 100 + 1; int guess; int attempts = 0; cout << "=== 猜数字游戏 ===" << endl; cout << "我想了一个1到100之间的数字,你能猜出来吗?" << endl; do { cout << "请输入你的猜测: "; cin >> guess; attempts++; if (guess < secretNumber) { cout << "太小了!再试试。" << endl; } else if (guess > secretNumber) { cout << "太大了!再试试。" << endl; } else { cout << "恭喜你!猜对了!" << endl; cout << "你用了 " << attempts << " 次就猜对了!" << endl; } } while (guess != secretNumber); return 0; }
|
📝 学习总结
重点概念
- 数据类型:C++提供了丰富的数据类型,选择合适的类型很重要
- 变量声明:理解声明和初始化的区别
- 常量:使用const和constexpr声明常量
- 运算符:掌握运算符优先级和结合性
- 控制结构:if-else、switch、for、while、do-while的使用
常见错误
- 未初始化变量:使用未初始化的变量会导致未定义行为
- 类型不匹配:注意隐式类型转换
- 运算符优先级:使用括号明确运算顺序
- 循环条件:避免无限循环
最佳实践
- 使用有意义的变量名:提高代码可读性
- 初始化变量:总是初始化变量
- 使用const:尽可能使用const声明常量
- 代码格式化:保持代码整洁和一致
🚀 下一步
明天我们将学习函数与作用域,包括:
- 函数声明与定义
- 参数传递方式
- 函数重载
- 作用域规则
继续学习:第2天 函数与作用域 →
记住:基础语法是C++学习的基石,一定要多练习! 💪