Hi basically i need help , i am not good with c++ but i am trying my very best. i am currently stuck with trying to do an infix to postfix conversion that reads from a textfile. The text file contains the infix notations and the code is suppose to convert the infix to postfix notations and then solve it. My code displays the contents of the text file fine but i am lost on how to connect it with my converting code. If anybody can help i would greatly appreciate it.
for example the text file infix.txt would contain
3+7
3+7*2
(3+7)*2
3+(7-2)*((5-8)
3+((7(2-4)+5)2+9)*(4-8)-10
My main cpp file is
#include <iostream>
using namespace std;
#include "Stacks.h"
#include <fstream>
#include <string>
#include <cassert>
#include <cctype>
int main()
{
string infixExp;
cout << "The current infix expressions are: " << endl;
string getcontent;
ifstream openfile ("infix.txt");
if(openfile.is_open())
{
while(! openfile.eof())
{
openfile >> getcontent;
cout << getcontent << endl;
}
}
cout << "Now to convert into post-fix expression and compute the result: " << endl;
//i dont know what to put here yet
cin.get();
cin.get();
return 0;
}
string postfix(string exp)
////////////////////////////////////////////////////////////////////////////////////
// Converts infix to postfix expression
{
char token;
char topToken;
Stacks opStack;
string postfixExp;
const string BLANK = " ";
for (int i = 0; i < exp.length(); i++)
{
token = exp[i];
switch(token)
{
case ' ' : break;
case '(' : opStack.push(token);
break;
case ')' : for (;;)
{
assert (!opStack.empty());
topToken = opStack.top();
opStack.pop();
if (topToken == '(') break;
postfixExp.append(BLANK + topToken);
}
;
break;
case '+' : case '-' :
case '*' : case '/' : case '%' :
for (;;)
{
if (opStack.empty() ||
opStack.top() == '(' ||
(token == '*' || token == '/' || token == '%') &&
(opStack.top() == '+' || opStack.top() == '-'))
{
opStack.push(token);
break;
}
else
{
topToken = opStack.top();
opStack.pop();
postfixExp.append(BLANK + topToken);
}
}
break;
default : // operand
postfixExp.append(BLANK +token);
for(;;)
{
if (!isalnum(exp[i+1])) break; // end of identifier
i++;
token = exp[i];
postfixExp.append(1, token);
}
}
}
// pop remaining operators on the stack
for (;;)
{
if (opStack.empty()) break;
topToken = opStack.top();
opStack.pop();
if (topToken != '(')
{
postfixExp.append(BLANK + topToken);
}
else
{
cout << " *** Error in infix expression *** \n " ;
break;
}
}
return postfixExp;
}
and my header file class is
#ifndef STACKS_H
#define STACKS_H
#include <iostream>
using namespace std;
const int STACKS_CAPACITY = 128;
typedef int StacksElement;
class Stacks
{
private:
StacksElement myArray[STACKS_CAPACITY];
int myTop;
public:
Stacks();
void push(const StacksElement & value);
void pop();
bool empty() const;
void display(ostream & out) const;
StacksElement top() const;
};
inline Stacks::Stacks()
{
myTop = -1;
}
inline bool Stacks::empty() const
{
return (myTop == -1);
}
inline void Stacks::pop()
{
if(myTop >= 0)
myTop--;
else
cerr << "*** Stack is empty -- " "can't remove a value ***\n";
}
void Stacks::push(const StacksElement & value)
{
if (myTop < STACKS_CAPACITY - 1)
{
++myTop;
myArray[myTop] = value;
}
else cerr << "*** Stack full -- can't add new value ***\n" "Must increase value of STACK_CAPACITY in Stack.h\n";
}
inline void Stacks::display(ostream & out) const
{ for (int i = myTop; i >= 0; i--)
out << myArray[i] << endl;
}
StacksElement Stacks::top() const
{
if (myTop >= 0)
return (myArray[myTop]);
else
{ cerr << "*** Stack is empty " " -- returning garbage ***\n";
return myArray[STACKS_CAPACITY-1];
}
}
#endif