rco*_*rie 6 c++ encryption stack
我上周收到了C++课程的作业.我想你们中的一些人会发现它很有趣!我设法将大部分代码都删除但是我被困住了,并且无法解决这个问题,因为我的生活......以下是我必须在代码中加入的加密过程的指导原则:
消息发送者输入一个四个字母的单词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;
}
Run Code Online (Sandbox Code Playgroud)
编译时,最终输出给了我" OSDNOT ",这是我教授提供的示例,所以我知道我正在走向正确的轨道..任何帮助都会很棒,我甚至不知道从哪里开始检查代码.
这是更正后的代码。您没有正确编写算法代码。我已经评论了我在代码中所做的更改。首先,当你在扫描时遇到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;
Run Code Online (Sandbox Code Playgroud)
PS:最好使用 std::stack 和 std::string。