利用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;
}