infix to postfix

I need help with a program for converting infix to postfix.

heres my main program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#define Sentinel ‘=’
#define SIZE 15
enum ItemIDTYPE {INVALIDCHAR, LT_PAREN, RT_PAREN, OPERAND, OPERATOR, SENTINEL};
struct EXPRESSION
{ 
       char items[SIZE];
       int index;
};
#include "D_Stack.h"
#include <iostream>
#include<fstream>
char item;
using namespace std;

int main( )
{ 
  EXPRESSION Infix, Postfix;
  float Value;
  ifstream infile;
  infile.open("expressions.txt");
while(!infile)
         {
         int Read(Infix);
         void Convert(Infix,Postfix);
         Value ={float Evaluate(Postfix);};
         float Print(Infix, Postfix, Value);
         }
         return 0;
}

     
void Convert(EXPRESSION Infix,EXPRESSION Postfix)
{ 
     ItemIDTYPE itemID;
     Stack<char>OpStack;
     while(itemID!=SENTINEL)
         { 
         OpStack.Push(item);
         itemID=char FindItemID(OpStack);
         switch (itemID)
         { 
         case OPERAND:cout<<OPERAND; break;
         case LT_PAREN:OpStack.Push('('); break;
         case RT_PAREN:{char PopUptoLtParen(item);}; break;
         case OPERATOR:{char ProcessOperator(item);}; break;
         case INVALIDCHAR: cout<<"Invalid char"<<endl;break;
         }
         }
        char EmptyStack(item);
}

char ProcessOperator(item)
{
Stack<char>OpStack;
while (OpStack!=NULL)
{
int Top = OpStack.Pop();
if (void item_HEAVIER_top(item, Top))
      {
      OpStack.Push(item);
      break;
      }
     else
      {
      cout<<OpStack<<endl;
     OpStack.Push(item);
     }
}

float Evaluate(EXPRESSION Postfix)
{
Stack<float>opndStack;

ItemITYPE itemID;
while(opndStack!=NULL)
{
   item = opndStack.Push();
   itemID = FindItemID(item);
   switch (itemID)
   {
   case OPERAND:opndStack.Push(item); break;
   case OPERATOR:int PopComputePush(opndStack,OpStack); break;
   }
}
opndStack.Pop(item);
)

char FindItemID(item)
{
     if(item='+')
        itemID=OPERATOR;
     else if(item='-')
        itemID=OPERATOR;
     else if(item='*')
        itemID=OPERATOR;
     else if(item='/')
        itemID=OPERATOR;
     else if(item='(')
          itemID=LT_PAREN;
     else if(item=')')
          itemID=RT_PAREN;
     else
         itemID=OPERAND;
}
char PopUptoLtParen(OpStack)
{
     while(ItemTypeID!=LT_PAREN)
     {
       OpStack.Pop(OPERATOR);
     }
     cout<<OpStack.Pop();
}
void item_HEAVIER_top (char item)
{
int level0 = 0;
int level1 = 1;
int level2 = 2;

if (item == '*')
   return level2;
else if (item == '/')
     return level2;
else if (item== '+')
     return level1;
else if (item == '-')
     return level1;
else
    return level0;

}
int PopComputePush(int a,int b,char c,int total)
{
    int a=opndStack.Pop;
    int b=opndStack.Pop;
    char c=OpStack.Pop;
    total=(a,c,b);
    opndStack.Push(total);
)
int Read(ifstream& infile,char items[index])
{
     int index = 0; 
     int i;
     while (infile) 
     {
     inFile >> items [index];
     index++;
     }
     int expressionlength = index - 1;
     for (i = 0; i < expressionLength; i++) 
     {
     cout << items[i];
     }
}
float Print(Infix, Postfix, Value)
{
     cout<<"The infix expression is "<<Infix;
     cout<<"The Postfix expression is "<<Postfix;
     cout<<"The value is "<<Value; 
}
char EmptyStack(item)
{
     while(OpStack!=NULL)
     {
       OpStack.Pop(OPERATOR);
     }
     cout<<OPERATOR<<endl;
}


header that i am using
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef D_Stack_h_
#define D_Stack_h_
#include <iostream>
template<class StackItem>
class Stack
{
      template <typename t>      
      friend std::ostream& operator<<(std::ostream & , const Stack<t>&);     
      private:
             StackItem *Items;
             int StackSize;
             int Top;
      public:
             Stack(int=1000);
             //pre:no items are in stack
             //post:1 item is in stack
             void Push(StackItem);
             //pre:no items are in stack
             //post:1 item is in stack
             void Pop(StackItem &);
             //StackItem TopItem();
             //bool IsEmpty(){return (Top==0);};
             //bool IsFull () {return (Top==StackSize);};
             void clear();
             ~Stack();
};
#endif 


implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "D_Stack.h"
#include<iostream>
using namespace std;

template<class StackItem>
Stack<StackItem>::Stack(int size)
{
   Items= new StackItem[size];
   StackSize=size;
   Top = 0;
}
template<class StackItem>
void Stack<StackItem>::Push(StackItem x)
{
     Items[Top]=x;
     Top++;
}
template<class StackItem>
void Stack<StackItem>::Pop(StackItem &x)
{
     x=Items[Top-1];
     Top--;
}
template<class StackItem>
StackItem Stack<StackItem>::TopItem()
{
          return (Items[Top-1]);
}
template <class StackItem>
void Stack<StackItem>::clear()
{
     Top=0;
}
template<class StackItem>
Stack<StackItem>::~Stack()
{
   delete []Items;
}
 
template <class t> 
ostream& operator<<(ostream &output,Stack<t> const&s)
{          
           for(int i=0; i<s.Top; i++)
                   output<<s.Items[i]<<"|";
           return output;
}
template class Stack<float>;
template ostream& operator<< (ostream &output, Stack<float> const&l);
template class Stack<char>;
template ostream& operator<< (ostream &output, Stack<char> const&l);


im getting errors:
23 F:\373\InfixtoPostfix.cpp cannot convert `EXPRESSION' to `int' in initialization
24 F:\373\InfixtoPostfix.cpp variable or field `Convert' declared void
24 F:\373\InfixtoPostfix.cpp initializer expression list treated as compound expression
24 F:\373\InfixtoPostfix.cpp cannot convert `EXPRESSION' to `int' in initialization
25 F:\373\InfixtoPostfix.cpp expected primary-expression before '{' token
25 F:\373\InfixtoPostfix.cpp expected `;' before '{' token
26 F:\373\InfixtoPostfix.cpp initializer expression list treated as compound expression
39 F:\373\InfixtoPostfix.cpp expected primary-expression before "char"
39 F:\373\InfixtoPostfix.cpp expected `;' before "char"
39 F:\373\InfixtoPostfix.cpp At global scope:
53 F:\373\InfixtoPostfix.cpp expected `,' or `;' before '{' token

i really need help with this.im sorry if ive pissed people off in this forum and didnt realize it but if anyone can help me i would appreciate it.
What is at lines 25-26???
1
2
 Value ={float Evaluate(Postfix);};
         float Print(Infix, Postfix, Value);


Are these function invokations???
Sine of x is calculated as double y = sin(x);, not as double y = double sin(x) ! You have many such errors.

Also, many your functions (for ex. char PopUptoLtParen(OpStack) ) must return a value but actually do not contain a "return" statement.

Also, in the definition of function ProcessOperator there are more '{' than '}'.

Also, ...
The reeason you didn't get a reply before isn't because we're mad at you, it's because you post lots of code and basically just say "it's broken, please fix it". These types of posts are usually very tiresome to answer.

Anyway.. I didn't look through it all but it looks like you are confusing function calls with function prototypes:

1
2
3
int SomeFunction();  // a prototype because it has a return type

SomeFunction();  // calling the function 


Take a look at your main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main( )
{ 
  EXPRESSION Infix, Postfix;
  float Value;
  ifstream infile;
  infile.open("expressions.txt");
while(!infile)
         {
         int Read(Infix);                    // function prototype -- doesn't make sense here
         void Convert(Infix,Postfix);        // again
         Value ={float Evaluate(Postfix);};  // wtf is with the braces -- and again... prototype
         float Print(Infix, Postfix, Value); // ditto
         }
         return 0;
}


You probably meant to do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// declare the prototypes before main so you can use the functions.
//   note I'm guessing as to what the parameters for these functions should be based on what you
//   have in your main(), although the actual function bodies you wrote don't seem to match
int Read(EXPRESSION&);
void Convert(const EXPRESSION&,EXPRESSION&);
float Evaluate(const EXPRESSION&);
float Print(const EXPRESSION&,const EXPRESSION&,float);

// then in main... you call the functions:
int main()
{
  EXPRESSION Infix, Postfix;
  float Value;
  ifstream infile;
  infile.open("expressions.txt");
while(!infile)
         {
         Read(Infix);                     // <- notice, no 'int'
         Convert(Infix,Postfix);          //    no void
         Value = Evaluate(Postfix);       //    no braces, no float
         Print(Infix, Postfix, Value);    //    no float
         }
         return 0;
}


You seem to do this all over the place. You'll have to go through and double check all of your function calls to make sure they're not prototypes.


EDIT -- doh, too slow
Last edited on
they are function invokations, so for line 25 it would be float value=evaluate postfix?
since you already declare 'Value' on line 17, you don't need to declare it again on line 25.

Just: Value = Evaluate(Postfix);
ok,i actually have a question that has nothing to do with errors. My main problem is im trying to write a simple line of code in certain parts of the program that is supposed to make item equal to the item from the expression and that gets passed through the program.i know my line is beyond wrong. does anyone know how i can make it work? the problem line(s) will be commented.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#define Sentinel ‘=’
#define SIZE 15
enum ItemIDTYPE {INVALIDCHAR, LT_PAREN, RT_PAREN, OPERAND, OPERATOR, SENTINEL};
struct EXPRESSION
{ 
       char items[SIZE];
       int index;
};
#include "D_Stack.h"
#include <iostream>
#include<fstream>
using namespace std;
int Read(EXPRESSION&);
void Convert(const EXPRESSION&,EXPRESSION&);
float Evaluate(const EXPRESSION&);
float Print(const EXPRESSION&,const EXPRESSION&,float);
bool item_HEAVIER_top (char,int);
char FindItemID(char);
void ProcessOperator(char);
int PopComputePush(int,int,char,int);
char PopUptoLtParen(char);
char EmptyStack(char);
int main()
{ 
  EXPRESSION Infix, Postfix;
  float Value;
  ifstream infile;
  infile.open("expressions.txt");
while(!infile)
         {
         Read(Infix);
         Convert(Infix,Postfix);
         Value =Evaluate(Postfix);
         Print(Infix, Postfix, Value);
         }
         return 0;
}

     
void Convert(EXPRESSION Infix,EXPRESSION Postfix)
{ 
     char item;
     ItemIDTYPE itemID;
     Stack<char>OpStack;
     while(itemID!=SENTINEL)
         { 
         OpStack.Push(item);// problem line
         itemID= FindItemID(item);
         switch (itemID)
         { 
         case OPERAND:cout<<OPERAND; break;
         case LT_PAREN:OpStack.Push('('); break;
         case RT_PAREN:PopUptoLtParen(item); break;
         case OPERATOR:ProcessOperator(item); break;
         case INVALIDCHAR: cout<<"Invalid char"<<endl;break;
         }
         }
        EmptyStack(item);
}

void ProcessOperator(char item)
{
Stack<char>OpStack;
while (OpStack!=NULL)
{
int Top = OpStack.Pop();
if (item_HEAVIER_top(item, Top))
      {
      OpStack.Push(item);
      break;
      }
     else
      cout<<OpStack<<endl;
     OpStack.Push(item);
}
}

float Evaluate(EXPRESSION Postfix)
{
char item;
Stack<float>opndStack;
ItemIDTYPE itemID;
while(opndStack!=NULL)
{
   item = opndStack.Push();//problem line
   itemID = FindItemID(item);
   switch (itemID)
   {
   case OPERAND:opndStack.Push(item); break;
   case OPERATOR:PopComputePush(item); break;
   }
}
opndStack.Pop(item);
)

char FindItemID(char item)
{
     if(item='+')
        itemID=OPERATOR;
     else if(item='-')
        itemID=OPERATOR;
     else if(item='*')
        itemID=OPERATOR;
     else if(item='/')
        itemID=OPERATOR;
     else if(item='(')
          itemID=LT_PAREN;
     else if(item=')')
          itemID=RT_PAREN;
     else
         itemID=OPERAND;
}
char PopUptoLtParen(char item)
{
     while(ItemTypeID!=LT_PAREN)
     {
       OpStack.Pop(OPERATOR);
     }
     cout<<OpStack.Pop();
}
bool item_HEAVIER_top (char item)
{
int level0 = 0;
int level1 = 1;
int level2 = 2;

if (item == '*')
   return level2;
else if (item == '/')
     return level2;
else if (item== '+')
     return level1;
else if (item == '-')
     return level1;
else
    return level0;

}
int PopComputePush(int a,int b,char c,int total)
{
    int a=opndStack.Pop;
    int b=opndStack.Pop;
    char c=OpStack.Pop;
    total=(a,c,b);
    opndStack.Push(total);
)
int Read(ifstream& infile,char items[index])
{
     int index = 0; 
     int i;
     while (infile) 
     {
     inFile >> items [index];
     index++;
     }
     int expressionlength = index - 1;
     for (i = 0; i < expressionLength; i++) 
     {
     cout << items[i];
     }
}
float Print(Infix, Postfix, Value)
{
     cout<<"The infix expression is "<<Infix;
     cout<<"The Postfix expression is "<<Postfix;
     cout<<"The value is "<<Value; 
}
char EmptyStack(item)
{
     while(OpStack!=NULL)
     {
       OpStack.Pop(OPERATOR);
     }
     cout<<OPERATOR<<endl;
}


My main problem is im trying to write a simple line of code in certain parts of the program that is supposed to make item equal to the item from the expression and that gets passed through the program


Err... what?

if you want to make 'item' equal to something, you just assign it:

 
item = _whatever_;


The lines of code you marked as problem lines appear to be pushing something onto your stack, which doesn't really match what you're asking. I'm afraid I don't understand your question.
its just that i didnt know how to do it.

so it would be something like

item=_Infix_

?
Last edited on
If '_Infix' is the same type as 'item', yes, that's all there is to it.

Are you working with someone else's program or something? I find it hard to believe you wrote all of that without understanding the assignment operator.
thanks and actually yeah,the outline of the program was actually given by my professor.
Last edited on
ok,i have other questions that have nothing to do with errors.im having trouble writing the code for the function that is supposed to read the function.i know im completely off base iin writing it either making it to hard or to simple.

my function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Read(ifstream& infile,char items[index])
{
     int index = 0; 
     int i;
     while (infile) 
     {
     inFile >> items [index];
     index++;
     }
     int expressionlength = index - 1;
     for (i = 0; i < expressionLength; i++) 
     {
     cout << items[i];
     }
}


if it helps the entire program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#define Sentinel ‘=’
#define SIZE 15
enum ItemIDTYPE {INVALIDCHAR, LT_PAREN, RT_PAREN, OPERAND, OPERATOR, SENTINEL};
struct EXPRESSION
{ 
       char items[SIZE];
       int index;
};
#include "D_Stack.h"
#include <iostream>
#include<fstream>
using namespace std;
int Read(EXPRESSION&);
void Convert(const EXPRESSION&,EXPRESSION&);
float Evaluate(const EXPRESSION&);
void Print(const EXPRESSION&,const EXPRESSION&,float);
bool item_HEAVIER_top (char,int);
void  FindItemID(char);
void ProcessOperator(char);
float PopComputePush(int,int,char,float);
void PopUptoLtParen(const ItemIDTYPE&);
char EmptyStack(char);
int main()
{ 
  EXPRESSION Infix, Postfix;
  float Value;
  ifstream infile;
  infile.open("expressions.txt");
while(!infile)
         {
         Read(Infix);
         Convert(Infix,Postfix);
         Value =Evaluate(Postfix);
         Print(Infix, Postfix, Value);
         }
         return 0;
}

     
void Convert(EXPRESSION Infix,EXPRESSION Postfix)
{ 
     char item;
     ItemIDTYPE itemID;
     Stack<char>OpStack;
     while(itemID!=SENTINEL)
         { 
         item=_Infix_;
         itemID= FindItemID(item);
         switch (itemID)
         { 
         case OPERAND:cout<<OPERAND; break;
         case LT_PAREN:OpStack.Push('('); break;
         case RT_PAREN:PopUptoLtParen(item); break;
         case OPERATOR:ProcessOperator(item); break;
         case INVALIDCHAR: cout<<"Invalid char"<<endl;break;
         }
         }
        EmptyStack(item);
}

void ProcessOperator(char item)
{
Stack<char>OpStack;
while (OpStack!=NULL)
{
int Top = OpStack.Pop();
if (item_HEAVIER_top(item, Top))
      {
      OpStack.Push(item);
      break;
      }
     else
      cout<<OpStack<<endl;
     OpStack.Push(item);
}
}

float Evaluate(EXPRESSION Postfix)
{
char item;
Stack<float>opndStack;
ItemIDTYPE itemID;
while(opndStack!=NULL)
{
   //item= items[];
   itemID = FindItemID(item);
   switch (itemID)
   {
   case OPERAND:opndStack.Push(item); break;
   case OPERATOR:PopComputePush(item); break;
   }
}
opndStack.Pop(item);
}

void FindItemID(char item)
{
     ItemIDTYPE itemID;
     if(item='+')
        itemID=OPERATOR;
     else if(item='-')
        itemID=OPERATOR;
     else if(item='*')
        itemID=OPERATOR;
     else if(item='/')
        itemID=OPERATOR;
     else if(item='(')
          itemID=LT_PAREN;
     else if(item=')')
          itemID=RT_PAREN;
     else
         itemID=OPERAND;
}
void PopUptoLtParen(ItemIDTYPE itemID)
{
     Stack<char>OpStack;
     while(itemID!=LT_PAREN)
     {
       OpStack.Pop(OPERATOR);
     }
     cout<<OPERATOR;
}
bool item_HEAVIER_top (char item)
{
int level0 = 0;
int level1 = 1;
int level2 = 2;

if (item == '*')
   return level2;
else if (item == '/')
     return level2;
else if (item== '+')
     return level1;
else if (item == '-')
     return level1;
else
    return level0;

}
float PopComputePush(int a,int b,char c,float total)
{
    Stack<char>OpStack;
    Stack<float>opndStack;
    int a=opndStack.Pop(item);
    int b=opndStack.Pop(item);
    char c=OpStack.Pop(item);
    float total=(int a,char c,int b);
    opndStack.Push(total);
}
void Read(ifstream& infile,char items[index])
{
     int index = 0; 
     int i;
     while (infile) 
     {
     inFile >> items [index];
     index++;
     }
     int expressionlength = index - 1;
     for (i = 0; i < expressionLength; i++) 
     {
     cout << items[i];
     }
}
void Print(Infix,Postfix,float Value)
{
     cout<<"The infix expression is "<<Infix;
     cout<<"The Postfix expression is "<<Postfix;
     cout<<"The value is "<<Value; 
}
char EmptyStack(char item)
{
      Stack<char>OpStack;
     while(OpStack!=NULL)
     {
       OpStack.Pop(item);
     }
     cout<<OPERATOR<<endl;
}


if someone can help me i would appreciate it.
Topic archived. No new replies allowed.