记录学习c++的点滴!
编制程序,将输入的一行字符以加密的形式输出, 然后将其解密,解密的字符序列与输入的正文进行比较, 吻合时输出解密的正文, 否则输出解密失败。
加密时,将每个字符的 ASCII码依次反复加上“4962873”中的数字,并在32(' ')~122('z') 之间做模运算。解密与加密的顺序相反。例如,对于输入正文“the result of 3 and 2 is not 8”, 则运行结果为:
xqk口zlvyuzqn口6$jtf(9#m! &pw #
the result of 3 and 2 is not 8
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<cmath>
#include<iomanip>
using namespace std;
void code(string & s,const int *a);
void code(string & s, const int *a)
{
for (int i = 0; i < s.size(); i++)
{
s[i] += a[i % 7];
if (s[i]>122)
{
s[i] = s[i] % 91;
}
cout<<s[i];
}
}
void encode(string & s, const int *a)
{
for (int i = 0; i < s.size(); i++)
{
s[i] -= a[i % 7];
if (s[i]<32)
{
s[i] = s[i] + 91;
}
cout << s[i];
}
}
int main()
{
string s;
int key[7] = { 4, 9, 6, 2, 8, 7, 3 };
cout << "please input the string:" << endl;
getline(cin, s);
//cout << s<<endl<<s.size();
code(s, key);
cout << endl;
encode(s, key);
system("pause");
}