簡體   English   中英

即使在pop()之后,STL堆棧top()函數也會讀取相同的值。

[英]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]='\0';
            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)]='\0';
            //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);

然后在else塊中。

        strcpy(op,temp);
        s.push(op);

您需要每次為op分配內存。

strcat與非null終止的字符串一起使用

else塊中,您具有:

        temp[strlen(temp)]=prefix[i];
        o2=s.top();
        strcat(temp,o2);

這些行的第一行用prefix[i]替換temp的空字符。 那時, temp不是以null結尾的字符串。 上面第三行中對strcat的調用導致未定義的行為。

您需要使用以下方法:

        char temp2[2] = {0};
        temp2[0] = prefix[i];
        strcat(temp, temp2);
        o2=s.top();
        strcat(temp,o2);

混合mallocnew

混合使用mallocnew不是您看到的內存問題的原因,但是最好堅持使用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]='\0';
         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]='\0';

         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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM