0

此代码的目的是获取命令行参数 ./Program -encode|decode 0-9 并将其应用于写入下一行的文本字符串。IF 编码,然后将文本的每个字符移动到字母表中指定的多个位置。解码则相反。我已经做得很好了,编译器没有发现任何错误,但是在运行程序时,输入文本字符串后没有任何返回。

#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <cstring>
using namespace std;

string encode(string& to_encode, int to_shift)
{for(int i = 0; I < to_encode.length(); i++)
 {if(isalpha(to_encode[i]))
  {char sum = to_encode[i] + to_shift;
   if(isupper(to_encode[i]) != 0 && isupper(sum) == 0)
   {to_encode[i] = sum - 'Z' + 'A';
   }
   if(isupper(to_encode[i]) != 0 && isupper(sum) != 0)
   {to_encode[i] = sum;
   }
   if(islower(to_encode[i]) != 0 && islower(sum) == 0)
   {to_encode[i] = sum - 'z' + 'a';
   }
   if(islower(to_encode[i]) != 0 && islower(sum) != 0)
   {to_encode[i] = sum;
   }
  }
 }
 return to_encode;
}

string decode(string& to_decode, int to_shift)
{for(int i = 0; I < to_decode.length(); i++)
 {if(isalpha(to_decode[i]))
  {char difference = to_decode[i] + to_shift;
   if(isupper(to_decode[i]) != 0 && isupper(difference) == 0)
   {to_encode[i] = difference + 'Z' - 'A';
   }
   if(isupper(to_decode[i]) != 0 && isupper(difference) != 0)
   {to_encode[i] = difference;
   }
   if(islower(to_decode[i]) != 0 && islower(difference) == 0)
   {to_encode[i] = difference + 'z' - 'a';
   }
   if(islower(to_decode[i]) != 0 && islower(difference) != 0)
   {to_encode[i] = difference;
   }
  }
 }
 return to_decode;
}

int main(int argc, char* argv[])
{string cryption;
 in shift;
 if(argc != 3)
 {cerr<<"Usage: ./Prog1d -encode|decode 0-9"<<endl;
  return -1;
 }
 if(!(strcmp(argv[1],"-encode") == 0 || strcmp(argv[1],"-decode") == 0)
 {cerr<<"Usage: ./Prog1d -encode|decode 0-9"<<endl;
  return -1;
 }
 shift = atoi(argv[2]);
 if(shift > 0 && shift <= 9)
 {while(getline(cin,cryption))
  {if(argv[1] == "-encode")
   {encode(cryption, shift);
   }
   else
   {decode(cryption, shift);
   }
  }
 }
 else
 {cerr<<"Usage: ./Prog1d -encode|decode 0-9"<<endl;
  return -1;
 }
}
4

0 回答 0