I'm nearly complete in figuring out this program, but for some odd reason, my program will either output "segmentation fault" or a "floating point exception" when it attempts to evaluate the postfix expression. The objective of the program is to take in an infix expression, convert it to postfix, then evaluate that postfix expression. The structure of my program uses the main function to read in the data and post results of the "in_to_post" function which takes in that line, converts it to postfix, then outputs the postfix expression and the evaluation (which comes from the pf_eval) function. I know the error lies within the for loop, but I don't know how exactly to fix it.

Example Input:

2 + 3 * 5

Corresponding Output:

2 + 3 * 5
235*+
Floating point exception

Input file:

2 + 3 * 5
2 + 3 * 5 ^ 6
2 + 3 - 5 + 6 - 4 + 2 - 1
2 + 3 * ( 5 - 6 ) - 4
2 + 3 ^ 5 * 6 - 4
2 + 3 * 6 ^ 2
( ( ( ( 2 + 3 - 4 ) / 2 + 8 ) * 3 * ( 4 + 5 ) / 2 / 3 + 9 ) )

#include <iostream>
#include <fstream>
#include <stack>
#include <ctype.h>
#include <sstream>
using namespace std;

int prec(char elem)
  {
    switch(elem)
      {
        case '^' : return 3;
        case '*' : return 2;
        case '/' : return 2;
        case '+' : return 1;
        case '-' : return 1;
        case '(' : return 0;

        default  : return -1;
      }
  }

int eval(char opn1, char elem, char opn2)
  {
    int result;
    switch(elem)
      {
        case '+' : result = opn1 + opn2;
        case '-' : result = opn1 - opn2;
        case '*' : result = opn1 * opn2;
        case '/' : result = opn1 / opn2;
        case '^' : result = opn1 ^ opn2;
      }
    return result;
  }

void pf_eval(string st)
  {
    char result;
    char tok, opn1, opn2;
    stack <char> pfx;
    stringstream out1;

    for(int i = 0; i < st.length(); i++)
      {
        if(isdigit(st[i]))
          {
            pfx.push(st[i]);
          }
        else
          {
            tok = st[i];
            opn2 = pfx.top();
            pfx.pop();
            opn1 = pfx.top();
            pfx.pop();
            result = eval(opn1, tok, opn2);
            pfx.push(result);
          }
      }
    while(!pfx.empty())
      {
        out1 << pfx.top();
        pfx.pop();
      }

    cout << out1.str() << endl;
  }

void in_to_post(string st)
  {
    string post;
    stack <char> ifx;
    stringstream output;

    for(int i = 0; i <= st.length(); i++)
      {
        if(st[i] == ' ') i++;
        if(st[i] == '^' || st[i] == '*' || st[i] == '/' ||
           st[i] == '+' || st[i] == '-')
          {
            while(!ifx.empty() && prec(st[i]) <= prec(ifx.top()))
              {
                output << ifx.top();
                ifx.pop();
              }
            ifx.push(st[i]);
          }
        else if(st[i] == '(')
          {
            ifx.push(st[i]);
          }
        else if(st[i] == ')')
          {
            while(ifx.top() != '(')
              {
                output << ifx.top();
                ifx.pop();
              }
            ifx.pop();
          }
        else
          {
            output << st[i];
          }
      }
    while(!ifx.empty())
      {
        output << ifx.top();
        ifx.pop();
      }

    cout << output.str() <<endl;
    post = output.str();

    pf_eval(post);
  }

int main()
  {
    string st, post;
    ifstream infix;
    stringstream output;

    infix.open("x.data");
    while(getline(infix, st))
      {
        cout << st << endl;
        in_to_post(st);
      }
    infix.close();
    return 0;
  }

I used vc++ 2012 RC to debug the program. The problem is that the value of elm in function eval() is '\0' and the switch statement doesn't handle that value so it returns crap since you didn't initialize the return variable. The string returned by in_to_post() has an embedded '\0' which I believe is why eval() is getting '\0' as a parameter. std::string.length() returns the total size allocated to the string, unlike strlen() which stops counting then '\0' is encountered. So the loops using st.length() in the condition may not be working the way you want because of the embedded '\0'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.