目标是有两个字符串,在这个字符串中,有一个退格按钮,表示为<
。但是,具有不同位置退格按钮的两个字符串的输出应该相等。
对于该InputsEqual
函数,它基本上会在看到退格按钮时从堆栈中弹出顶部项目。
我用不同的文件对其进行了测试,但它仍然无法正常工作。你能检查一下这段代码吗?
#include <iostream>
#include <cstring>
#include <string>
#include <stack>
using namespace std;
bool EqualStacks(stack<char> a, stack<char> b);
bool InputsEqual(string inputA, string inputB);
int main()
{
string inputA = "abcde<<";
string inputB = "abcd<e<";
if(InputsEqual(inputA,inputB))
{
cout << "Inputs Equal.\n";
}
else
{
cout << "Inputs are NOT Equal.\n";
}
return 0;
}
bool EqualStack(stack<char> a, stack<char> b)
{
for(int i = 0; i < a.size(); i++)
{
if(a.top() == b.top())
{
a.pop();
b.pop();
}
}
return (a.empty() && b.empty()) ? true:false;
}
//If both stacks are empty, they equal
bool InputsEqual(string inputA,string inputB)
{
stack<char> aStack;
stack<char> bStack;
// string inputA;
// cout << "Please enter a string. Press < if you want to delete something"
// cin >> inputA;
for(int i = 0 ; i < inputA.length() + 1; i++)
{
if(inputA[i] != '\0')
{
if(inputA[i] != '<')
{
aStack.push(inputA[i]);
}
else if(!aStack.empty())
{
aStack.pop();
}
else
{
aStack.pop();
}
}
}
for(int i = 0 ; i < inputB.length() + 1; i++)
{
if(inputB[i] != '\0')
{
if(inputB[i] != '<')
{
bStack.push(inputA[i]);
}
else if(!bStack.empty())
{
bStack.pop();
}
else
{
bStack.pop();
}
}
}
return (EqualStack(aStack,bStack));
}
//输出:字符串不相等