题解分享
题解分享简介
打印ASCII码 - 题解
/
思路分析
思路一(error) : 直接用ord函数将字符转化为ascll码
——c++中没有ord()和chr() 函数 只有python才有
思路二(yes):直接强制类型转换
学习思路三: 用函数进行转换,自己写个转换函数。
include
using namespace std;
int main()
{
char s;
cin >> s;
cout
using namespace std;
//显式性:static_cast 明确地表达了程序员的意图,即“我正在将一个类型转换为另一个类型”。
//类型安全:static_cast 是一种类型安全的转换方式,编译器会检查转换是否合法。
//代码可读性:使用 static_cast 可以让代码更清晰,避免隐式转换可能带来的混淆。
//字符转ascll
int to_Ascll (char s) {
return static_cast
(s);
}
//ascll转字符
char to_Char (int n) {
return static_cast
(n);
}
//主函数
int main()
{
char s;
cin >> s;
int n;
cin >> n;
//调用to_Ascll函数
cout << to_Ascll(s);
//调用to_Char函数
cout << to_Char(n);
return 0;
}
/
查看全文
0
0
0
0



