ASCII(美国信息交换标准代码)就是给每个字符分配一个数字编号,这样计算机就能识别和处理文字了!
#include <iostream>
using namespace std;
int main() {
// 查看字符的ASCII码
cout << "字符 'A' 的ASCII码是: " << (int)'A' << endl; // 65
cout << "字符 'a' 的ASCII码是: " << (int)'a' << endl; // 97
cout << "字符 '0' 的ASCII码是: " << (int)'0' << endl; // 48
return 0;
}#include <iostream>
using namespace std;
int main() {
cout << "大写字母ASCII码:" << endl;
for (char ch = 'A'; ch <= 'E'; ch++) {
cout << ch << " = " << (int)ch << " | ";
}
cout << endl;
cout << "小写字母ASCII码:" << endl;
for (char ch = 'a'; ch <= 'e'; ch++) {
cout << ch << " = " << (int)ch << " | ";
}
cout << endl;
return 0;
}输出:
大写字母ASCII码:
A = 65 | B = 66 | C = 67 | D = 68 | E = 69 |
小写字母ASCII码:
a = 97 | b = 98 | c = 99 | d = 100 | e = 101 | #include <iostream>
using namespace std;
int main() {
cout << "数字字符ASCII码:" << endl;
for (char ch = '0'; ch <= '9'; ch++) {
cout << ch << " = " << (int)ch << " | ";
}
cout << endl;
return 0;
}输出:
数字字符ASCII码:
0 = 48 | 1 = 49 | 2 = 50 | 3 = 51 | 4 = 52 | 5 = 53 | 6 = 54 | 7 = 55 | 8 = 56 | 9 = 57 | | 字符类型 | 范围 | ASCII码范围 | 重要特点 |
|---|---|---|---|
| 数字字符 | '0'-'9' | 48-57 | 连续,'0'=48 |
| 大写字母 | 'A'-'Z' | 65-90 | 连续,'A'=65 |
| 小写字母 | 'a'-'z' | 97-122 | 连续,'a'=97 |
记忆口诀:
数字48开始,大写65起 小写97开头,中间差32 所有都连续,规律要记牢
#include <iostream>
using namespace std;
int main() {
char letter = 'G';
char digit = '7';
char symbol = '#';
// 方法1:强制类型转换
cout << "'G' 的ASCII码: " << (int)letter << endl; // 71
cout << "'7' 的ASCII码: " << (int)digit << endl; // 55
cout << "'#' 的ASCII码: " << (int)symbol << endl; // 35
// 方法2:赋值给int变量
int asciiValue = letter;
cout << "'G' 的ASCII码(方法2): " << asciiValue << endl;
return 0;
}#include <iostream>
using namespace std;
int main() {
int code1 = 72; // H
int code2 = 101; // e
int code3 = 108; // l
// 方法1:强制类型转换
cout << "ASCII码 " << code1 << " 对应字符: " << (char)code1 << endl;
cout << "ASCII码 " << code2 << " 对应字符: " << (char)code2 << endl;
cout << "ASCII码 " << code3 << " 对应字符: " << (char)code3 << endl;
// 方法2:直接赋值给char变量
char ch = code1;
cout << "ASCII码 " << code1 << " 对应字符(方法2): " << ch << endl;
return 0;
}利用大小写字母ASCII码相差32的规律:
#include <iostream>
using namespace std;
int main() {
char uppercase = 'H';
char lowercase = 'e';
// 大写转小写:+32
cout << uppercase << " → 小写: " << (char)(uppercase + 32) << endl;
// 小写转大写:-32
cout << lowercase << " → 大写: " << (char)(lowercase - 32) << endl;
// 更安全的方法:使用字符运算
char safeLower = uppercase - 'A' + 'a';
char safeUpper = lowercase - 'a' + 'A';
cout << "安全转换 - 小写: " << safeLower << endl;
cout << "安全转换 - 大写: " << safeUpper << endl;
return 0;
}#include <iostream>
using namespace std;
int main() {
// 数字字符 → 数字
char digitChar = '8';
int realNumber = digitChar - '0'; // '8' - '0' = 56 - 48 = 8
cout << "字符 '" << digitChar << "' 转换为数字: " << realNumber << endl;
// 数字 → 数字字符
int number = 5;
char numberChar = number + '0'; // 5 + 48 = 53 = '5'
cout << "数字 " << number << " 转换为字符: '" << numberChar << "'" << endl;
return 0;
}#include <iostream>
using namespace std;
int main() {
char ch;
cout << "请输入一个字符: ";
cin >> ch;
// 使用ASCII码判断字符类型
if (ch >= 'A' && ch <= 'Z') {
cout << "这是一个大写字母" << endl;
} else if (ch >= 'a' && ch <= 'z') {
cout << "这是一个小写字母" << endl;
} else if (ch >= '0' && ch <= '9') {
cout << "这是一个数字字符" << endl;
} else {
cout << "这是一个其他字符" << endl;
}
return 0;
}就像把水从杯子倒进瓶子,数据也可以在不同类型间转换:
int → double // 整数变小数
double → int // 小数变整数(会丢失小数部分)
char → int // 字符变数字(ASCII码)
#include <iostream>
using namespace std;
int main() {
// 例子1:整数与小数运算
int a = 5;
double b = 2.5;
double result1 = a + b; // int自动转double
cout << "5 + 2.5 = " << result1 << endl; // 7.5
// 例子2:字符与整数运算
char ch = 'A';
int result2 = ch + 1; // char自动转int
cout << "'A' + 1 = " << result2 << " (ASCII码)" << endl; // 66
cout << "对应字符: " << (char)result2 << endl; // B
return 0;
}#include <iostream>
using namespace std;
int main() {
double pi = 3.14159;
int integerPi = (int)pi; // 强制转换为int,丢失小数部分
cout << "原始值: " << pi << endl;
cout << "转换后: " << integerPi << endl; // 3
// 字符转换
char letter = 'C';
int ascii = (int)letter;
cout << "'C' 的ASCII码: " << ascii << endl; // 67
return 0;
}#include <iostream>
using namespace std;
int main() {
int students = 5;
int totalScore = 425;
// 错误:整数除法
double averageWrong = totalScore / students;
cout << "错误平均分: " << averageWrong << endl; // 85.0
// 正确:转换为小数除法
double averageCorrect = static_cast<double>(totalScore) / students;
cout << "正确平均分: " << averageCorrect << endl; // 85.0
// 或者让其中一个数为double
double averageBetter = totalScore / 5.0;
cout << "更好的平均分: " << averageBetter << endl; // 85.0
return 0;
}#include <iostream>
using namespace std;
int main() {
// 将数字字符串转换为实际数字
char digit1 = '3', digit2 = '7', digit3 = '2';
int number1 = digit1 - '0'; // 3
int number2 = digit2 - '0'; // 7
int number3 = digit3 - '0'; // 2
int total = number1 * 100 + number2 * 10 + number3;
cout << "字符 '" << digit1 << digit2 << digit3
<< "' 转换为数字: " << total << endl; // 372
return 0;
}#include <iostream>
using namespace std;
// 大写转小写
char toLower(char ch) {
if (ch >= 'A' && ch <= 'Z') {
return ch + 32; // 或者 ch - 'A' + 'a'
}
return ch;
}
// 小写转大写
char toUpper(char ch) {
if (ch >= 'a' && ch <= 'z') {
return ch - 32; // 或者 ch - 'a' + 'A'
}
return ch;
}
int main() {
char letter;
cout << "请输入一个字母: ";
cin >> letter;
cout << "原始: " << letter << endl;
cout << "大写: " << toUpper(letter) << endl;
cout << "小写: " << toLower(letter) << endl;
return 0;
}#include <iostream>
using namespace std;
int main() {
string text;
cout << "🔐 简易密码生成器 🔐" << endl;
cout << "请输入文本: ";
cin >> text;
cout << "加密结果: ";
for (char ch : text) {
if (ch >= 'A' && ch <= 'Z') {
// 大写字母:向后移动3位
char encrypted = ((ch - 'A' + 3) % 26) + 'A';
cout << encrypted;
} else if (ch >= 'a' && ch <= 'z') {
// 小写字母:向后移动3位
char encrypted = ((ch - 'a' + 3) % 26) + 'a';
cout << encrypted;
} else if (ch >= '0' && ch <= '9') {
// 数字:向后移动2位
char encrypted = ((ch - '0' + 2) % 10) + '0';
cout << encrypted;
} else {
// 其他字符不变
cout << ch;
}
}
cout << endl;
return 0;
}#include <iostream>
using namespace std;
int main() {
char ch;
cout << "🔍 字符分析仪 🔍" << endl;
cout << "请输入一个字符: ";
cin >> ch;
cout << "=== 分析结果 ===" << endl;
cout << "字符: " << ch << endl;
cout << "ASCII码: " << (int)ch << endl;
// 判断字符类型
if (ch >= 'A' && ch <= 'Z') {
cout << "类型: 大写字母" << endl;
cout << "小写形式: " << (char)(ch + 32) << endl;
} else if (ch >= 'a' && ch <= 'z') {
cout << "类型: 小写字母" << endl;
cout << "大写形式: " << (char)(ch - 32) << endl;
} else if (ch >= '0' && ch <= '9') {
cout << "类型: 数字字符" << endl;
cout << "对应数字: " << (ch - '0') << endl;
} else {
cout << "类型: 其他字符" << endl;
}
// 显示前后字符
cout << "前一个字符: " << (char)(ch - 1) << " (ASCII: " << (int)(ch - 1) << ")" << endl;
cout << "后一个字符: " << (char)(ch + 1) << " (ASCII: " << (int)(ch + 1) << ")" << endl;
return 0;
}#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int math, english, science;
cout << "📊 成绩计算器 📊" << endl;
cout << "请输入数学成绩: ";
cin >> math;
cout << "请输入英语成绩: ";
cin >> english;
cout << "请输入科学成绩: ";
cin >> science;
// 计算总分和平均分(注意类型转换)
int total = math + english + science;
double average = static_cast<double>(total) / 3;
cout << fixed << setprecision(2);
cout << "=== 成绩报告 ===" << endl;
cout << "数学: " << math << "分" << endl;
cout << "英语: " << english << "分" << endl;
cout << "科学: " << science << "分" << endl;
cout << "总分: " << total << "分" << endl;
cout << "平均分: " << average << "分" << endl;
// 判断等级
char grade;
if (average >= 90) grade = 'A';
else if (average >= 80) grade = 'B';
else if (average >= 70) grade = 'C';
else if (average >= 60) grade = 'D';
else grade = 'F';
cout << "等级: " << grade << endl;
return 0;
}// 错误!
int a = 5, b = 2;
double result = a / b; // 2.0,不是2.5!
// 正确
double result = static_cast<double>(a) / b; // 2.5
// 或者
double result = a / 2.0; // 2.5// 错误:混淆字符和数字
char ch = 65; // 这是字符'A'
char wrong = '65'; // 错误!单引号只能放一个字符
// 正确
char ch = 65; // A
char ch2 = 'A'; // Adouble pi = 3.14159;
int intPi = pi; // 丢失小数部分,变成3
// 如果需要四舍五入
int roundedPi = static_cast<int>(pi + 0.5); // 3.64159 → 3有一段加密文本,每个字符的ASCII码都增加了5,请解密:
加密文本:Mjqqt%
#include <iostream>
using namespace std;
int main() {
string encrypted = "Mjqqt%";
cout << "加密文本: " << encrypted << endl;
cout << "解密结果: ";
// 你的代码在这里
for (char ch : encrypted) {
// 每个字符的ASCII码减5
char decrypted = ch - 5;
cout << decrypted;
}
cout << endl;
return 0;
}生成6位数字验证码,但以字符形式存储:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0));
cout << "生成的验证码: ";
// 你的代码在这里
for (int i = 0; i < 6; i++) {
// 生成0-9的随机数,转换为字符
int digit = rand() % 10;
char digitChar = digit + '0';
cout << digitChar;
}
cout << endl;
return 0;
}输入华氏温度,转换为摄氏温度(保留1位小数):
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double fahrenheit;
cout << "请输入华氏温度: ";
cin >> fahrenheit;
// 转换公式:C = (F - 32) × 5/9
double celsius = (fahrenheit - 32) * 5 / 9;
cout << fixed << setprecision(1);
cout << fahrenheit << "°F = " << celsius << "°C" << endl;
return 0;
}- ASCII编码
- 每个字符都有对应的数字编号
- 重要规律:数字、大写字母、小写字母都是连续的
- 大小写字母相差32
- 数据类型转换
- 隐式转换:自动进行
- 显式转换:强制进行
- 注意整数除法问题
- 实用技巧
- 字符 ↔ ASCII码:
(int)ch和(char)code - 数字字符 ↔ 数字:
ch - '0'和数字 + '0' - 大小写转换:
± 32
- 字符 ↔ ASCII码:
ASCII码像身份证,每个字符有编号 数字大写和小写,连续排列真巧妙
类型转换要小心,整数除法会取整 字符数字互转换,减去'0'就搞定 大小写,差32,转换起来真容易
掌握了ASCII编码和类型转换,你就能在字符和数字之间自由转换了!继续练习,让编程更加得心应手!💪✨