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 181 182 183 184 185
|
#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>
#include<string>
using namespace std;
void Read(EXPRESSION&);
void Convert(const EXPRESSION&,EXPRESSION&);
float Evaluate(const EXPRESSION&);
void print(const EXPRESSION&,const EXPRESSION&,float);
bool item_HEAVIER_top (char,int);
ItemIDTYPE FindItemID(char);
void ProcessOperator(char);
float PopComputePush(char);
void PopUptoLtParen(char);
void EmptyStack(char);
int main()
{
EXPRESSION Infix, Postfix;
float Value;
ifstream infile;
infile.open("E:\\expressions.txt");
while(!infile)
{
Read(Infix);
Convert(Infix,Postfix);
Value =Evaluate(Postfix);
print(Infix, Postfix, Value);
}
infile.close();
system("pause");
return 0;
}
void Convert(EXPRESSION const& Infix,EXPRESSION& Postfix)
{
char item;
ItemIDTYPE itemID;
Stack<char>OpStack;
while(itemID!=SENTINEL)
{
item=Infix.items[Infix.index];
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)
{
int Top=0;
Stack<char>OpStack;
while (OpStack.IsFull())
{
OpStack.Pop(item);
if (item_HEAVIER_top(item, Top))
{
OpStack.Push(item);
break;
}
else
cout<<OpStack<<endl;
OpStack.Push(item);
}
}
float Evaluate(EXPRESSION const& Postfix)
{
float value;
char item;
Stack<float>opndStack;
ItemIDTYPE itemID;
while(Postfix.items[Postfix.index]!=0)
{
item=Postfix.items[Postfix.index];
itemID = FindItemID(item);
switch (itemID)
{
case OPERAND:opndStack.Push(item); break;
case OPERATOR:PopComputePush(item); break;
}
}
opndStack.Pop(value);
}
ItemIDTYPE 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(char item)
{
ItemIDTYPE itemID;
Stack<char>OpStack;
while(itemID!=LT_PAREN)
{
OpStack.Pop(item);
}
cout<<OpStack<<endl;
}
bool item_HEAVIER_top (char item,int top)
{
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(char item)
{
float a;
float b;
char c;
Stack<char>OpStack;
Stack<float>opndStack;
opndStack.Pop(a);
opndStack.Pop(b);
OpStack.Pop(c);
float total=(a,c,b);
opndStack.Push(total);
}
void Read(EXPRESSION& Infix)
{
ifstream infile;
infile>>Infix.items[Infix.index];
cout<<Infix.items[Infix.index];
}
void print(EXPRESSION const& Infix,EXPRESSION const& Postfix,float Value)
{
cout<<"the infix is"<<Infix.items[Infix.index]<<endl;
cout<<"the postfix is"<<Postfix.items[Postfix.index]<<endl;
cout<<"The value is "<<Value;
}
void EmptyStack(char item)
{
ItemIDTYPE itemID;
Stack<char>OpStack;
while(itemID!=SENTINEL)
{
OpStack.Pop(item);
}
cout<<OpStack<<endl;
}
| |