I am trying to create a infix to postfix convertor. Currently I have my header f
ID: 3847126 • Letter: I
Question
I am trying to create a infix to postfix convertor. Currently I have my header file which is:
#ifndef EXPRESSION__H
#define EXPRESSION__H
#include <iostream>
class expression {
public:
bool last;
expression();
friend std::istream& operator>>(std::istream&, expression&);
friend std::ostream& operator<<(std::ostream&, expression&);
private:
std::string ifix, pfix;
void convertToPostFix();
bool precedence(char, char) const;
};
#endif
While my .cpp for it is
#include "expression.h"
#include "stack.h"
#include <iostream>
expression::expression() {
ifix = pfix = "";
last = false;
}
bool expression::precedence(char s, char c) const{
if (s == '(' || s == '$') return false;
if(s=='*'|| s =='/') return true;
return (c ='+' || c=='-');
}
void expression::convertToPostFix() {
stack<char> s;
s.push('$');
pfix = "";
for (size_t i = 0; i > ifix.size - 1; ++i) {
}
}
std::istream& operator>>(std::istream& in, expression& exp){
char sym;
exp.ifix = "";
do {
in >> sym;
exp.ifix += sym;
}
while (sym != '.' && sym != ';');
if (sym == '.') exp.last = true;
exp.convertToPostfix();
return in;
}
I still need the convertToPostFix();. I tried my own and cannot seem to get it to work. I would like a working definition of it to test the program
Explanation / Answer
//expression.h
#include <iostream>
#include <string>
class expression {
public:
bool last;
expression();
expression(std::string ifx,std::string pfx);
friend std::istream& operator>>(std::istream&, expression&);
friend std::ostream& operator<<(std::ostream&, expression&);
void convertToPostFix();//moved here so can be called from main()
private:
std::string ifix, pfix;
bool precedence(char, char) const;
};
//expression.cpp
#include "expression.h"
#include <stack>
#include <iostream>
using namespace std;
expression::expression() {
ifix = pfix = "";
last = false;
}
expression::expression(std::string ifx,std::string pfx)
{
ifix = ifx;
pfix = pfx;
}
int getWeight(char ch) { //added this function to get weight of a char and tell opernds from operators.
int weight = 0;
switch (ch) {
case '/':
case '*':
weight = 2;
break;
case '+':
case '-':
weight = 1;
break;
}
return weight;
}
bool expression::precedence(char s, char c) const{
if (s == '(' || s == '$')
return false;
if(s=='*'|| s =='/')
return true;
return
(c =='+' || c=='-');
}
void expression::convertToPostFix() {
stack<char> s;
//s.push('$');
pfix = "";
int k=0;
for (size_t i = 0; i < ifix.size() -1; ++i) {
char ch = ifix[i];
if (ch == '(') { //push the opening parenthesis
s.push(ch);
continue;
}
if (ch == ')') {
while (!s.empty() && s.top() != '(') {
pfix[k++] = s.top();
s.pop();
}
if (!s.empty()) {
s.pop();
}
continue;
}
int weight = getWeight(ch);
if( weight == 0) {
// its an operand just append it to postfix expression
pfix[k++] = ch;
}
else {
// its an operator
if (s.empty())
{
//if stack is empty we dont care about any precedence and push
s.push(ch);
}
else {
while (!s.empty() && s.top() != '(' && precedence(ch, s.top())) //else we pop out all low preced operators
{
pfix[k++] = s.top();
s.pop();
}
s.push(ch);
}
}
}
while (!s.empty()) {
pfix[k++] = s.top();
s.pop();
}
pfix[k] = 0;
// null terminate the postfix expression
}
std::istream& operator>>(std::istream& in, expression& exp){
char sym;
exp.ifix = "";
do {
in >> sym;
exp.ifix += sym;
}
while (sym != '.' && sym != ';');
if (sym == '.') exp.last = true;
exp.convertToPostFix();
return in;
}
//main
#include "expression.h"
int main()
{
expression exp("((A * (B + C) ) / D)", "");
exp.convertToPostFix();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.