存粹考代码
思路:
- 建立两个容器,加密字母大小变换后推,数字后推,其他不变
- 解密字母大小变换前推,数字前推,其他不变
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<char> v1;
vector<char> v2;
void EnCode(char ch)
{
if (ch>= 'a' && ch <= 'z')
{
if (ch == 'z') ch = 'A';
else
ch= ch - 31;
}
else if (ch >= 'A' && ch <= 'Z')
{
if (ch == 'Z') ch = 'a';
else
ch = ch +33;
}
else if (ch >= '0' && ch <= '9')
{
if (ch == '9') ch = '0';
else
ch = ch+1;
}
v1.push_back(ch);
}
void DeCode(char ch)
{
if (ch >= 'a' && ch <= 'z')
{
if (ch == 'a') ch = 'Z';
else
ch = ch - 33;
}
else if (ch >= 'A' && ch <= 'Z')
{
if (ch =='A') ch = 'z';
else
ch = ch + 31;
}
else if (ch >= '0' && ch <= '9')
{
if (ch == '0') ch = '9';
else
ch = ch - 1;
}
v2.push_back(ch);
}
int main()
{
string str1;
string str2;
while (getline(cin, str1))
{
getline(cin, str2);
if (str1 == "" || str2 == "") break;
int len1 = str1.size();
int len2 = str2.size();
for (int i = 0; i < len1; i++)
{
EnCode(str1[i]);
}
for (int i = 0; i < len2; i++)
{
DeCode(str2[i]);
}
for (int i = 0; i < v1.size(); i++)
cout << v1[i];
cout << endl;
for (int i = 0; i < v2.size(); i++)
cout << v2[i];
cout << endl;
v1.clear();
v2.clear();
}
}