Hi!
I am trying to write calculator in c++ that takes an infix expression, converts it to postfix, and calculates.
I have the code that will do this written, but when I try to turn it into a class it gives my a Segmentation Fault.
Here is my code...
//calc.h
#ifndef INFIX_TO_POSTFIX_H
#define INFIX_TO_POSTFIX_H
#include "stack.h"
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
class Calculator
{
public:
void start();
void check();
bool error(string);
void convert_postfix(string);
void evaluate_postfix(string);
private:
MyStack<char> s;
MyStack<int> ints;
int priority;
std::string exp;
std::string postfix;
};
#endif
calc.cpp
#include "calc.h"
#include "stack.h"
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
void Calculator::start()
{
char ans;
string exp;
cout << "Enter an infix expression: ";
cin >> exp;
if ( error(exp) )
{
// CHECK FOR ERRORS IN EXPRESSION*********
cout << "Program will not execute expression is incorrect\n";
}
else
{
convert_postfix(exp);
cout << "Here is your postfix expression " << postfix << endl;
//evaluate_postfix(postfix);
}
}
void Calculator::check()
{
switch (s.get_top())
{
case '*':
case '/':
case '%':
priority=1; break;
case '+':
case '-':
priority=2; break;
}
}
void Calculator::convert_postfix(string post)
{
char ch;
string temp;
string infix = exp;
for (int i = 0; i < exp.length(); i++)
{
ch = exp[i];
if ( exp[i] == '+' || exp[i] == '-')
{
if (priority <= 2 )
{
while(!s.is_empty() && s.get_top() != '(')
{
postfix += s.get_top();
s.pop();
}
priority = 0;
s.push(ch);
check();
}
else
s.push(ch);
check();
}
else if ( exp[i] == '*' || exp[i] == '/' || exp[i] == '%' )
{
if (priority == 1 )
{
while(!s.is_empty() && s.get_top() != '(')
{
postfix += s.get_top();
s.pop();
check();
}
s.push(ch);
check();
}
else
s.push(ch);
check();
}
else if ( exp[i] == '(' )
{
s.push(ch);
check();
}
else if ( exp[i] == ')' )
{
while(!s.is_empty() && s.get_top() != '(')
{
postfix += s.get_top();
s.pop();
check();
}
s.pop();
check();
}
else if ( isdigit(infix[i]) )
{
if ( isdigit(infix[i+1]) )
{
int b=i+1;
temp = infix[i];
while ( isdigit(infix[b]) )
{
ch=infix[b];
temp = temp + ch;
++b;
}
postfix = postfix + ' ' + temp;
i=b-1;
}
else
{
postfix = postfix + ' ' + ch;
}
}
}
while(!s.is_empty())
{
char k = s.get_top();
postfix = postfix + ' ' + k;
s.pop();
check();
}
// FOR MULTIPLE AND SINGLE DIGIT NUMBERS
cout << "\nPOSTFIX "<< postfix << endl;
}
void Calculator::evaluate_postfix(string post)
{
MyStack<int> s;
int i, n = post.length();
int op1, op2;
for(i=0; i<post.length();)
{
if(isdigit(post[i]))
{
int temp = post[i]-'0';
if(isdigit(post[i+1]))
{
int b=i+1;
while(isdigit(post[b]))
{
temp = temp*10 + (post[i+1]-'0');
b++;
}
s.push(temp);
i++;
}
else
s.push(temp);
i++;
}
else if(post[i] == '+')
{
op1 = s.get_top();
s.pop();
op2= s.get_top();
s.push(op1+op2);
i++;
}
else if(post[i] == '-')
{
op1 = s.get_top();
s.pop();
op2= s.get_top();
s.push(op2-op1);
i++;
}
else if(post[i] == '*')
{
op1 = s.get_top();
s.pop();
op2= s.get_top();
s.push(op1*op2);
i++;
}
else if(post[i] == '/')
{
op1 = s.get_top();
s.pop();
op2= s.get_top();
if(op1==0)
{
break;
}
else
s.push(op2/op1);
i++;
}
else if(post[i] = ' ')
{
i++;
}
}
if(!s.is_empty())
{
cout<<"\nThe result is: " <<s.get_top()<<endl;
}
else
cout<<"\nCalculation error. \n";
cout<<endl<<endl;
}
// CHECKS EXPRESSION FOR ERRORS***********************************************************
bool Calculator::error(string exp)
{
int count = 0;
int count1 = 0;
int len = exp.length();
int a = 0;
int b = 0;
while (a <= len)
{
if ( exp[a] == '(' ) {++count;}
++a;
}
while (b <= len)
{
if ( exp[b] == ')' ) {++count1;}
++b;
}
if (count == count1) return false; else return true;
}
And main.cpp
#include "stack.h"
#include "calc.h"
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
int main()
{
Calculator mycalc;
mycalc.start();
}
EDIT
Ok... I no longer get a segmentation fault. Now I am not getting any thing in the postfix string