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;
}
|