在C++中字符值为0的是空格,字符0的值是48
void 密码译成明文()
{//缘由https://bbs.youkuaiyun.com/topics/395002145
string a = ""; cin >> a;
int i = 0, s = 0;
while (a[i] != '\0')
{
s = (int)a[i];
if (s >= 65 && s <= 90)s += 32;
cout << ((s >= 97 && s <= 122) ? (char)((s + 3) > 122 ? (s - 23) : (s + 3)) : a[i]);
++i;
}
cout << endl;
}
不用求余的方法
(s + 3) > 122 ? (s - 23) : (s + 3)等同于((s + 3) % 122 + 96)
假如判断一个数字在小写26个字符中的字符则(s + 3) > 122 ? (s - 26) : (s + 3)
if(s > 122 )s -= 26;等同于s % 122 + 96
void 密码译成明文()
{//缘由https://bbs.youkuaiyun.com/topics/395002145
string a = ""; cin >> a;
int i = 0, s = 0;
while (a[i] != '\0')
{
s = (int)a[i];
if (s >= 65 && s <= 90)cout << (char)((s + 35 > 122) ? (s + 9) : (s + 35));
else if ((s >= 97 && s <= 122))cout << (char)(((s + 3) > 122) ? (s - 23) : (s + 3));
else cout << a[i];
++i;
}
cout << endl;
}
cin函数无法读取空格字符要特殊处理,或使用gets_s函数。char s[100];gets(s);int len = strlen(s);
void 密码译成明文()
{//缘由https://bbs.youkuaiyun.com/topics/395002145 ctrl+z退出
cin >> noskipws;
char a;
int s = 0;
while (cin >> a)
{
s = (int)a;
if (s >= 65 && s <= 90)
cout << (char)((s + 35 > 122) ? (s + 9) : (s + 35));
else if ((s >= 97 && s <= 122))
cout << (char)(((s + 3) > 122) ? (s - 23) : (s + 3));
else if (a == '@')
cout << ",";
else
cout << a;
}
cout << endl;
}
void 密码译成明文()
{//缘由https://bbs.youkuaiyun.com/topics/395002145
string a = "";
getline(cin, a);//可读取空格
int s = 0, i = 0;
while (a[i] != '\0')//while (a[i])
{
s = (int)a[i];
if (s >= 65 && s <= 90)
cout << (char)((s + 35 > 122) ? (s + 9) : (s + 35));
else if ((s >= 97 && s <= 122))
cout << (char)(((s + 3) > 122) ? (s - 23) : (s + 3));
else if (a[i] == '@')
cout << ",";
else
cout << a[i]; ++i;
}
cout << endl;
}