用堆栈加密字符串 上星期我收到了一个C++课程的作业。我想你们中的一些人会发现它很有趣!我设法写下了大部分代码,但我被卡住了,我一辈子也弄不明白这一点。。。以下是我必须输入代码的加密过程指南:

用堆栈加密字符串 上星期我收到了一个C++课程的作业。我想你们中的一些人会发现它很有趣!我设法写下了大部分代码,但我被卡住了,我一辈子也弄不明白这一点。。。以下是我必须输入代码的加密过程指南:,c++,encryption,stack,C++,Encryption,Stack,消息发送者输入一个四个字母的单词,CCCC,以及另一个四个字母的单词, XXXX 然后,消息发送者输入要加密的消息。 程序一次扫描消息一个字符,每个字符在堆栈中被推入,直到 扫描的字符在单词CCCC中,或者消息的结尾是 遇到 当扫描的字符是CCCC中的字符之一时,打印该字符并继续 打印并弹出堆栈顶部的字符,直到堆栈为空或 堆栈顶部的字符是XXXX中的字符之一。当比赛结束时 遇到消息时,打印堆栈顶部的字符并继续弹出 并从堆栈顶部打印,直到堆栈为空。 这里有一个提示:“好”运气”,它“对我来说听起来

消息发送者输入一个四个字母的单词,CCCC,以及另一个四个字母的单词, XXXX

然后,消息发送者输入要加密的消息。

程序一次扫描消息一个字符,每个字符在堆栈中被推入,直到 扫描的字符在单词CCCC中,或者消息的结尾是 遇到

当扫描的字符是CCCC中的字符之一时,打印该字符并继续 打印并弹出堆栈顶部的字符,直到堆栈为空或 堆栈顶部的字符是XXXX中的字符之一。当比赛结束时 遇到消息时,打印堆栈顶部的字符并继续弹出 并从堆栈顶部打印,直到堆栈为空。

这里有一个提示:“运气”,它“对我来说听起来很简单,”,或者 您的程序会说:“OSDNOT EEM LPMIS SU”

这就是实际的任务

我遇到的问题是最后一点:

当比赛结束时 遇到消息时,打印堆栈顶部的字符并继续弹出 并从堆栈顶部打印,直到堆栈为空

下面是我目前掌握的代码:

#include <string>
#include <iostream>
using namespace std;
class Stack
{
   private:
   char Chars[50];
   int top;
   public:
   int push(char);
   char pop();
   bool isEmpty();
   bool isFull();
   Stack()
   {
      top = 0;
   }
};

int main()
{
Stack theStack;
   char word1[4];
   char word2[4];
   for(int i=0; i < 4; i++){
      word1[i] = ' ';
      word2[i] = ' ';
   }
   char message[500];   
   cout << "Please enter a 4 letter word: ";
   cin >> word1;
   while(word1[4] || !word1[3])
   {
      cout << "Word must be 4 chars long. Try again: ";
      cin >> word1;
   }
   cout << "Please enter another 4 letter word: ";
   cin >> word2;
   while(word2[4] || !word2[3])
   {
      cout << "Word must be 4 chars long. Try again: ";
      cin >> word2;
   }
   cout << "Please enter the phrase to be encrypted (50 chars max): ";
   cin.ignore(1000, '\n');
   cin.getline(message,500);
   int length = strlen(message);
   int count = 0;
   char finalMsg[length];
   //scanner
   for(int i = 0; i < length; i++)
   {
      if(message[i] == word1[0] ||
         message[i] == word1[1] ||
         message[i] == word1[2] ||
         message[i] == word1[3])
      {
         finalMsg[count] = message[i];
         count++;
         if(message[i-1] != word2[0] ||
            message[i-1] != word2[1] ||
            message[i-1] != word2[2] ||
            message[i-1] != word2[3])
         {
            finalMsg[count] =  message[i-1];
            count++;
         }
      }
      else
      {
         theStack.push(message[i]);
      }
   }
   cout << finalMsg << endl;
return 0;
}

int Stack::push(char data)
{
   Chars[top] = data;
   top++;
return top;
}

char Stack::pop()
{
   char ret = Chars[top-1];
   top--;
return ret;
}

bool Stack::isEmpty()
{
   if(top <= 0)
      return true;
   else return false;
}

bool Stack::isFull()
{
   if(top >= 50)
      return true;
   else return false;
}
#包括
#包括
使用名称空间std;
类堆栈
{
私人:
半焦[50];
int top;
公众:
int-push(char);
char-pop();
bool是空的();
bool已满();
堆栈()
{
top=0;
}
};
int main()
{
堆叠钉子;
char-word1[4];
char-word2[4];
对于(int i=0;i<4;i++){
字1[i]='';
字2[i]='';
}
字符消息[500];
cout>word1;
while(word1[4]| | |!word1[3])
{
cout>word1;
}
cout>word2;
while(word2[4]| | |!word2[3])
{
cout>word2;
}

cout这是正确的代码。您没有正确地编写算法。我已经对代码中所做的更改进行了注释。 首先,当您在扫描时遇到CCCC中存在的字符时,您没有弹出堆栈的元素。此外,在扫描结束时,您也没有清空堆栈。包括
cstring
,而不是
string
。正如注释中指出的,您对word1和word2的声明不正确

   char finalMsg[200];
   //scanner
   for(int i = 0; i < length; i++)
   {
      if(message[i] == word1[0] ||
         message[i] == word1[1] ||
         message[i] == word1[2] ||
         message[i] == word1[3])
      {
         finalMsg[count] = message[i];
         count++;

         //pop out elements from the stack till it is empty or an character of XXXX is encountered
         while(!theStack.isEmpty())     
         {
             char tmp=theStack.pop();
             if(tmp==word2[0] ||
                tmp==word2[1] ||
                tmp==word2[2] ||
                tmp==word2[3])
                {
                    theStack.push(tmp);
                    break;
                }
            finalMsg[count++]=tmp;
         }

      }
      else
      {
         theStack.push(message[i]);
      }
   }

  //empty the stack
   while(!theStack.isEmpty())
   {
       finalMsg[count++]=theStack.pop();
   }
   finalMsg[count++]=0;
   cout << finalMsg << endl;
charfinalmsg[200];
//扫描器
for(int i=0;i我不打算通读所有的代码,因为已经很晚了,我也很累了,但是对于我所有复杂的计算机科学家庭作业,我要做的是:确保打印出每一步都在变化的所有变量值。这样你就可以看看问题出在哪里。@sblom嗯,你为什么这么说?看起来不错me@AlexW你说得对,我应该早点发布或者等到明天早上。谢谢你的提示,不过我会试试的!哇,这更像是一个字谜而不是一个加密。把这个留给你知道即将到来的字谜作业。很好用!我想这就是发生的事情,非常感谢。是的,教授正在教我们关于课程的知识,他用这个作为一种教学方式。再次感谢