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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
|
#include <iostream>
#include <fstream>
#include <stack>
#include <string>
using namespace std;
class Expression{
public:
string inToPost();
string inToPre();
string postToIn();
string preToIn();
string convertThis; // Expression that we want converted
double evaluate(); // Evaluate expression
Expression(string input, int direction); //constructor
bool isOperator(char character);
bool isOperand(char character);
int isHigherWeight(char character);
bool isHigherPrecedence(char op1, char op2);
void printResult();
private:
string infix;
string postfix;
string prefix;
};
//Constructor function
Expression::Expression(string input, int direction){
switch (direction){
case 1: infix = input;
case 2: postfix = input;
case 3: prefix = input;
}
}
//Operator Function checks to see if character is a legal symbol
bool Expression::isOperator(char character){
if((character == '*')||(character == '+')||(character == '-')||(character == '/'))
return true;
else
return false;
}
//Operand Function checks to see if character is a legal character
bool Expression::isOperand(char character){
if(character >= 'a' && character <= 'z')
return true;
if(character >= 'A' && character <= 'Z')
return true;
if(character >= '0' && character <= '9')
return true;
else
return false;
}
//Function determines the weight of Operator.
int Expression::isHigherWeight(char character){
int weight = 0; // or -1?
switch(character){
case '+':
case '-':
weight = 1;
case '*':
case '/':
weight = 2;
}
return weight;
}
//Function that compares weights of two different Operators.
bool Expression::isHigherPrecedence(char oper1, char oper2){
int op1Weight = isHigherWeight(oper1);
int op2Weight = isHigherWeight(oper2);
// If operators have equal precedence, return true
// return false
return op1Weight > op2Weight ? true: false;{
}
}
string Expression::inToPost(){
/** ->Initialize a stack of characters to hold the operation symbols and parentheses
-> do
if(the next input is a left parenthesis)
1-Read the left parenthesis and push it onto the stack
2-else if( the next input is a number or other operand)
3-Read the operand and write it to the output
4-else if( the next input is one of the operation symbols)
5-Print the top operation and pop it
**/
stack<char> Stack;
string postfix = ""; // Initialize postfix as empty string.
for(int i = 0;i< convertThis.length();i++){ //go through array of string convertThis
if (convertThis[i] == '('){ //1-Read the left parenthesis and push it onto the stack
Stack.push(convertThis[i]);
}
else if(isOperand(convertThis[i])){ //2-else if( the next input is a number or other operand)
cout << convertThis[i]; //3-Read the operand and write it to the output
}
else if(isOperator(convertThis[i])){ //4-else if the next input is one of the operation symbols)
cout << Stack.top();
Stack.pop(); //5-Print the top operation and pop it
}
/**
(while none of these three conditions are true){
6- The stack becomes empty, or
- The next symbol on the stack is a a left parenthesis, or
- The next symbol on the stack is an operation with lower precedence than the next input symbol
7- Read the next input symbol, and push this symbol onto the stack
}
else **/ //6-
while(!Stack.empty() && Stack.top() != '(' && isHigherPrecedence(Stack.top(),convertThis[i])){
Stack.push(convertThis[i]); //7- Read the next input symbol, and push this symbol onto the stack
}
/**
8- Read and discard the next input symbol(which should be a right parenthesis).
9- Print the top operation and pop it; Keep printing and popping until
- the next symbol on the stack is a left parenthesis.(If no parenthesis
- is encountered, then print an error message indicating unbalanced
- parentheses, and halt.)
10- Finally, pop the left parenthesis.
**/
// 8- Read and discard the next input symbol(which should be a right parenthesis).
if (convertThis[i] == ')'){
// 9- Print the top operation and pop it; Keep printing and popping until
while (!Stack.top() == '('){
cout << Stack.top();
Stack.pop();
}
}
Stack.pop(); //10- Finally, pop the left parenthesis.
/**while(there is more of the expression to read);
11- Print and pop any remaining operations on the stack.
(There should be no remaining left parentheses; if there are,
the input expression did not have balanced parentheses.)
**/
while(!Stack.empty()){
cout << Stack.top();
}
return postfix;
cout << postfix;
}
}
string Expression::inToPre(){
}
string Expression::postToIn(){
}
string Expression::preToIn(){
}
double Expression::evaluate(){
}
void Expression::printResult(){
cout << inToPost();
}
int main(){
string convertThis;
int choice;
cout << "|-----Here is my conversion menu-----|" << endl;
cout << "|----What are you converting to?-----|" << endl << endl;
cout << "1- Infix to postfix" << endl;
cout << "2- Infix to prefix" << endl;
cout << "3- postfix to infix?" << endl;
cout << "4- prefix to infix?" << endl;
//cin >> choice;
//cin.ignore();
cout << "Now enter the expression you want to convert ";
getline(cin,convertThis);
printResult();
}
|