конвертиране на infix в Rpn (маневрена площадка)

ето моят код за конвертиране на infix в ron с помощта на маневрена площадка. Знам как алгоритъмът работи добре и нямам проблем с това. но когато стартирам това просто нищо не се случва. когато го отстранявам, получавам неизвестна грешка в реда за инициализация на стека

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <stack>
using namespace std ;
void convert (char *) ;
double eval(char *) ;
int precedence(char);

int main() {

 cout << "\n Enter an expression :\n" ;
 cout << " >>  " ;
 char input[51] ;
 cin.get(input,50);
 convert (input) ;
 system("PAUSE");

}
int precedence(char op) {
  switch (op) {
     case '-' : case '+' :
        return 1 ;

     case '*' : case '/' :
        return 2 ;

     case '^' :
        return 3 ;

     default :
        return 0 ;

  }

}

   void convert(char* exp) {
   stack <char> stc;
   string outputQueue ;
//    string temp ;
 while (*exp) {

while (isspace(*exp)){
   *exp++ ;
}

if (*exp == '(') {
        stc.push(*exp++) ;
    }
else if (*exp == ')'){
    while (stc.top()!='(') {
            outputQueue += stc.top() ;
            stc.pop();
            *exp++;
           }
           stc.pop() ; /** Havaset bashe */
}
else if (isdigit(*exp)){
    while (isdigit(*exp)) {
        outputQueue += *exp++ ;
    }
    outputQueue += " " ;

}
else if (strchr("+-*/^",*exp)) {
        if (precedence(*exp) > precedence(stc.top())) {
                stc.push(*exp++) ;
        }
        else {
            if (precedence (*exp) == precedence (stc.top()) && precedence(*exp) == 3) {
                stc.push(*exp++) ;
            }
            else {
                outputQueue += stc.top() ;
                outputQueue += ' ' ;
                stc.pop();
            }
        }

}
 }

 while (!stc.empty()) {
    outputQueue =+ stc.top() ;
    stc.pop();
 }
cout << outputQueue ;
}

person PedramH    schedule 06.07.2013    source източник


Отговори (1)


Не знам какво имаш предвид, поправих кода. В 2 реда се опитвате да получите горния елемент на празен стек. Това води до прекъсване на твърдението.

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <stack>
using namespace std ;
void convert (char *) ;
double eval(char *) ;
int precedence(char);

int main() 
{
    cout << "\n Enter an expression :\n" ;
    cout << " >>  " ;
    char input[51] ;
    cin.get(input,50);
    convert (input) ;
    system("PAUSE");
}

int precedence(char op) 
{
    switch (op) 
    {
        case '-' : 
        case '+' :
            return 1 ;

        case '*' : 
        case '/' :
           return 2 ;

        case '^' :
            return 3 ;

        default :
            return 0 ;
    }
}

void convert(char* exp) 
{
    stack <char> stc;
    string outputQueue ;

    while (*exp) 
    {
        while (isspace(*exp))
        {
            *exp++ ;
            }

        if (*exp == '(') 
        {
            stc.push(*exp++) ;
        }
        else if (*exp == ')')
        {
            while (stc.top()!='(') 
        {
            outputQueue += stc.top() ;
            stc.pop();
            *exp++;
        }
        stc.pop() ; /** Havaset bashe */
        }
        else if (isdigit(*exp))
        {
            while (isdigit(*exp)) 
        {
            outputQueue += *exp++ ;
        }
        outputQueue += " " ;
        }
        else if (strchr("+-*/^",*exp))
        {
            if (precedence(*exp) > precedence(stc.top())) 
        {
            stc.push(*exp++) ;
        }
        else 
        {
            if (precedence (*exp) == precedence (stc.top()) && precedence(*exp) == 3) 
            {
                stc.push(*exp++) ;
            }
            else 
            {
                outputQueue += stc.top() ;
            outputQueue += ' ' ;
            stc.pop();
            }
        }
        }
     }

     while (!stc.empty()) 
     {
        outputQueue =+ stc.top() ;
        stc.pop();
     }
    cout << outputQueue ;
}
person Mathias Gontek    schedule 05.11.2013