STL stack top()函数即使在pop()之后也会读取相同的值
STL stack top() function reads same value even after pop().
我有一个将前缀字符串转换为中缀的代码。我用过stl堆栈。测试输入:*/ab+-cde
#include<iostream>
#include<stack>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
stack<char*> s;
char prefix[100],c;
int l,i,flag[27]={0},pos;
char *o1,*o2,*op,temp[10];
cout<<"Prefix expression : ";
cin>>prefix;
l=strlen(prefix);
op=(char *)malloc(sizeof(char)*10);
o1=new char[10];
o2=new char[10];
for(i=l-1;i>=0;i--)
{
if(prefix[i]>=97 && prefix[i]<=122)
{
if(i!=l-1) cout<<s.top()<<endl;
cout<<"Operand"<<endl;
temp[0]=prefix[i];
temp[1]=' ';
strcpy(op,temp);
s.push(op);
}
else
{
cout<<"Operator"<<endl;
cout<<"Top element : "<<s.top()<<endl;
o1=s.top();
strcpy(temp,o1);
s.pop();
cout<<"Top element : "<<s.top()<<endl;
temp[strlen(temp)]=prefix[i];
o2=s.top();
strcat(temp,o2);
s.pop();
temp[strlen(temp)]=' ';
//cout<<o1<<" "<<o2<<endl;
strcpy(op,temp);
s.push(op);
cout<<op<<endl;
}
}
o1=s.top();
s.pop();
cout<<"Evaluated expression is "<<o1<<endl;
return 0;
}
现在,当遇到第一个操作数时,o1应该存储c,而o2应该存储d。但我得到的输出如下,
输出
有人能帮忙吗?
我在您的代码中看到的问题:
循环中重用op
在循环开始之前,您已经为op
分配了内存。
op=(char *)malloc(sizeof(char)*10);
然后在for
循环中使用相同的内存。
在if
块中:
strcpy(op,temp);
s.push(op);
以及在CCD_ 5块中。
strcpy(op,temp);
s.push(op);
每次都需要为op
分配内存。
使用带有非空终止字符串的strcat
在else
块中,您有:
temp[strlen(temp)]=prefix[i];
o2=s.top();
strcat(temp,o2);
这些行中的第一行将temp
的空字符替换为prefix[i]
。此时,temp
不是以null结尾的字符串。上面第三行中对strcat
的调用导致了未定义的行为。
您需要使用以下内容:
char temp2[2] = {0};
temp2[0] = prefix[i];
strcat(temp, temp2);
o2=s.top();
strcat(temp,o2);
混合malloc
和new
混合使用malloc
和new
并不是您所看到的内存问题的原因,但最好坚持使用new
,因为您在C++领域。
以下是您的程序的修复版本:
#include<iostream>
#include<stack>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
stack<char*> s;
char prefix[100];
int l,i;
char *o1,*o2,*op,temp1[10],temp2[10];
cout<<"Prefix expression : ";
cin>>prefix;
l=strlen(prefix);
o1=new char[10];
o2=new char[10];
for(i=l-1;i>=0;i--)
{
if(prefix[i]>=97 && prefix[i]<=122)
{
if(i!=l-1) cout<<s.top()<<endl;
cout<<"Operand"<<endl;
temp1[0]=prefix[i];
temp1[1]=' ';
op = new char[10];
strcpy(op,temp1);
s.push(op);
cout<<"Symbol"<<endl;
cout<<"Top element : "<<s.top()<<endl;
}
else
{
cout<<"Operator"<<endl;
cout<<"Top element : "<<s.top()<<endl;
o1=s.top();
strcpy(temp1,o1);
s.pop();
cout<<"Top element : "<<s.top()<<endl;
temp2[0]=prefix[i];
temp2[1]=' ';
strcat(temp1,temp2);
o2=s.top();
strcat(temp1,o2);
s.pop();
op = new char[10];
strcpy(op,temp1);
s.push(op);
cout<<op<<endl;
}
}
o1=s.top();
s.pop();
cout<<"Evaluated expression is "<<o1<<endl;
return 0;
}
更新
通过使用std::string
而不是char*
,可以避免为字符串分配和释放内存的麻烦。
#include <iostream>
#include <string>
#include <stack>
#include <cstring>
using namespace std;
void test(char prefix[])
{
stack<std::string> s;
int l,i;
char temp[10] = {0};
std::string op;
l = std::strlen(prefix);
for(i=l-1;i>=0;i--)
{
if(prefix[i]>=97 && prefix[i]<=122)
{
if(i!=l-1) cout<<s.top()<<endl;
cout<<"Operand"<<endl;
temp[0]=prefix[i];
s.push(temp);
cout<<"Symbol"<<endl;
cout<<"Top element : "<<s.top()<<endl;
}
else
{
cout<<"Operator"<<endl;
cout<<"Top element : "<<s.top()<<endl;
op = s.top();
s.pop();
cout<<"Top element : "<<s.top()<<endl;
temp[0]=prefix[i];
op += temp;
op += s.top();
s.pop();
s.push(op);
cout<<op<<endl;
}
}
op=s.top();
s.pop();
cout<<"Evaluated expression is "<<op<<endl;
}
int main()
{
char prefix[100];
cout<<"Prefix expression : ";
cin>>prefix;
test(prefix);
return 0;
}
相关文章:
- 我们如何重新制作程序以打开xml文件并在价格单词之后读取百分比值并更改此值
- 在写依赖性之后,读取了什么
- 按Enter在Enter之后,ReadConsoleInput仅读取第一个符号
- read() 在 select() 阻塞之后,当从生成的进程从管道读取时
- 不会让我在 IF 语句(家庭作业)之后读取用户输入
- EOF 位:读取失败之前或之后
- STL stack top()函数即使在pop()之后也会读取相同的值
- 在dup2()之后使用std::cin从管道读取
- 在 Ctrl+Z (EOF) 之后从 iostream::cin 恢复读取?("ignore"不起作用)
- C++在eof()之后继续读取文件
- C++读取字符串的字符时出错|发生在第10个字符之后
- 在C++中读取一行之后的行不起作用
- 分部分读取文件,在最后读取的部分之后继续
- 读取char中特定行之后的所有内容
- 从file中读取,gcount()总是0,即使在几个file.get()之后
- TCP套接字:传入数据在ack之后(离开TCP读取缓冲区)但在read()/recv()之前的位置
- 仅读取文本文件中的数字并存储在数组中.之后,读取第一行来存储在数组中
- Boost::Asio服务器从客户端读取空白消息,即使在写句柄完成之后
- 如何在读取文本文件时跳到每行最后一个数字之后的下一行
- 正在读取imageStore()之后的texel