【问题标题】:Encrypting a string with a stack使用堆栈加密字符串
【发布时间】:2012-06-15 23:42:55
【问题描述】:

上周我收到了一份 C++ 课程的作业。我想你们中的一些人会觉得这很有趣!我设法把大部分代码都写下来了,但我被困住了,我一辈子都无法解决这个问题……以下是我必须放入代码中的加密过程的指导方针:

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

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

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

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

这里有一个提示:“GOOD”“运气”,它“对我来说很简单”,或作为 你的程序会说:“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;
}

编译后,最终输出给我“OSDNOT”,这是我教授提供的示例中的,所以我知道我正朝着正确的方向前进。任何帮助都会很棒,我什至不知道从哪里开始检查代码。

【问题讨论】:

  • 仔细检查word1word2 的声明。
  • 我不打算通读所有这些代码,因为已经很晚了,而且我很累,但是对于我所有复杂的计算机科学作业,我要做的是:确保您正在打印找出每一步都在变化的所有变量值。这样你就可以看到问题出在哪里。
  • @sblom 嗯,你为什么这么说?我觉得很好
  • @AlexW 你说得对,我应该早点发布或者等到明天早上。感谢您的提示,尽管我会尝试!
  • 哇,这更像是一个字谜而不是加密。把这个留给你知道即将到来的字谜作业。

标签: c++ encryption stack


【解决方案1】:

这是更正后的代码。您没有正确编码算法。我已经评论了我在代码中所做的更改。 首先,当您在扫描时遇到 CCCC 中存在的字符时,您没有弹出堆栈的元素。同样在扫描结束时,您没有清空堆栈。包括cstring 而不是string。正如 cmets 中所指出的,您对 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;

PS:最好使用 std::stack 和 std::string。

【讨论】:

  • 完美运行!我想这就是发生的事情,非常感谢。是的,教授正在教我们关于课程的知识,他正在使用它作为一种教学方式。再次感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 2021-06-25
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多