利用gets()
#include <iostream>
#include <string>
using namespace std;
char engChars[30] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string aLine;
void decipher()
{
for (int i = 0;i < aLine.length();i ++)
{
//是字母才处理
if (isalpha(aLine[i]))
{
int offset = (aLine[i] - 'A' + 26 - 5) % 26;
aLine[i] = engChars[offset];
}
}
}
int main ()
{
//读入"START"
while (getline(cin,aLine,'\n'))
{
if (aLine == "ENDOFINPUT")
break;
//读入一行
while (1)
{
getline(cin,aLine);
if (aLine == "END")
break;
decipher();
cout << aLine << endl;
}
}
return 0;
}
别人改进,仍利用gets()
#include <iostream>
#include <string>
using namespace std;
char engChars[30] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string aLine;
void decipher()
{
for (int i = 0;i < aLine.length();i ++)
{
//是字母才处理
if (isalpha(aLine[i]))
{
int offset = (aLine[i] - 'A' + 26 - 5) % 26;
aLine[i] = engChars[offset];
}
}
}
int main ()
{
//读入"START"
while (getline(cin,aLine,'\n'))
{
if (aLine == "ENDOFINPUT")
break;
//读入一行
while (1)
{
getline(cin,aLine);
if (aLine == "END")
break;
decipher();
cout << aLine << endl;
}
}
return 0;
}
别人利用getline()
#include <iostream>
#include <string>
using namespace std;
char engChars[30] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
string aLine;
void decipher()
{
for (int i = 0;i < aLine.length();i ++)
{
//是字母才处理
if (isalpha(aLine[i]))
{
int offset = (aLine[i] - 'A' + 26 - 5) % 26;
aLine[i] = engChars[offset];
}
}
}
int main ()
{
//读入"START"
while (getline(cin,aLine,'\n'))
{
if (aLine == "ENDOFINPUT")
break;
//读入一行
while (1)
{
getline(cin,aLine);
if (aLine == "END")
break;
decipher();
cout << aLine << endl;
}
}
return 0;
}
本文介绍了一个使用getline()函数读取输入,并通过字母替换实现简单加密解密的C++程序。程序包含解密函数,能够处理输入字符串中符合特定条件的字母,最终输出解密后的文本。
215

被折叠的 条评论
为什么被折叠?



