Created
August 15, 2015 11:43
-
-
Save mukuljainx/97ef13f175884aa47c1d to your computer and use it in GitHub Desktop.
A program thats converts Infix to prefix and then calculates result using prefix equation only in a proper way.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************** | |
Author : Mukul Jain | |
Date : 15 Aug 2015 | |
Note : There are lot of cout which I created to | |
help me to write this program you can | |
erase them or covert to comment | |
according to convience. | |
*************************************************************/ | |
#include <iostream> | |
#include <string> | |
#include <algorithm> | |
#include <cmath> | |
using namespace std; | |
#define max 100 | |
class stack | |
{ | |
public : | |
void push(char x); | |
char pop(void); | |
bool isEmpty(void); | |
bool isFull(void); | |
char stackTop(void); | |
stack(); // Constructor this must be working as createstack | |
private : | |
int top; | |
char a[max]; | |
}; | |
stack :: stack( void ) | |
{ | |
cout << "Stack has been defined" << endl; | |
top = -1; | |
} | |
void stack :: push(char x) | |
{ | |
if(!this->isFull()) | |
{ | |
cout << "HEY, I just pushed " << x << endl; | |
top ++; | |
a[top] = x; | |
} | |
else | |
{ | |
cout << "Sorry Stack is full \n error : Overflow caused" << endl; | |
} | |
} | |
char stack :: pop (void) | |
{ | |
if(this->isEmpty()) | |
{ | |
cout << "Sorry the stack is empty" << endl; | |
return -1; | |
} | |
else | |
{ | |
char x = a[top]; | |
top--; | |
cout << "I just returned " << x << " through pop" << endl; | |
return x; | |
} | |
} | |
bool stack :: isEmpty (void) | |
{ | |
if(top == -1) | |
{ | |
return true; | |
} | |
else | |
return false; | |
} | |
bool stack :: isFull(void) | |
{ | |
if(top == (max-1)) | |
{ | |
return true; | |
} | |
else | |
return false; | |
} | |
char stack:: stackTop(void) | |
{ | |
if(!this->isEmpty()) | |
{ | |
cout << "Topmost value is " << a[top] << endl; | |
return a[top]; | |
} | |
else | |
{ | |
cout << "Stack is empty" << endl; | |
return -1; | |
} | |
} | |
int getweight(char); | |
int main() | |
{ | |
stack stack; | |
string S="",T = ""; | |
int len,i; | |
cout << "Target string : " << T << endl; | |
cout << "Enter a infix equation : "; | |
getline(cin, S); | |
len = S.size(); | |
cout << "processing ......." << endl; | |
reverse(S.begin(), | |
S.end()); | |
cout << "... " << S << " ..."<< endl; | |
/**************************************************************** | |
AFTER REVRESING LETS CONVERT | |
INFIX TO POSTFIX | |
****************************************************************/ | |
for(i=0; i< len; i++) | |
{ | |
if(S[i] == ')') | |
{ | |
stack.push(S[i]); | |
} | |
else if(S[i] == '(') | |
{ | |
while( stack.stackTop() != ')') | |
{ | |
//cout << "T right now is " << T << endl; | |
char t = stack.pop(); | |
T += t; | |
} | |
stack.pop(); | |
} | |
else if( (S[i] <= 95 && S[i] >= 63) | |
|| (S[i] <= 122 && S[i] >= 97) | |
|| (S[i] <= 57 && S[i] >= 48) ) | |
{ | |
T += S[i]; | |
} | |
else if(S[i] == '$' || '*' || '/' || '+' || '-') | |
{ | |
if(getweight(S[i]) >= getweight(stack.stackTop())) | |
{ | |
stack.push(S[i]); | |
} | |
else | |
{ | |
while(stack.stackTop() > S[i]) | |
{ | |
T += stack.pop(); | |
} | |
stack.push(S[i]); | |
} | |
} | |
} | |
bool s = stack.isEmpty(); | |
cout << "main loop is over" << s << stack.stackTop() << endl; | |
while(!stack.isEmpty()) | |
{ | |
char t2 = stack.pop(); | |
T += t2; | |
} | |
cout << "...... \n ......... \n " << T << "\n........ \n.........." << endl; | |
reverse(T.begin(), | |
T.end()); | |
cout << "..........\n" << T << endl; | |
/***************************************************************** | |
NOW COMPUTATION PART | |
****************************************************************/ | |
len = T.size(); | |
float temp[2], value=0; | |
int j = 0; | |
char k; | |
for(i=0; i<len; i++) | |
{ | |
if( (T[i] <= 95 && T[i] >= 63) | |
|| (T[i] <= 122 && T[i] >= 97) | |
|| (T[i] <= 57 && T[i] >= 48) ) | |
{ | |
cout << "Enter the value of " << T[i] << " : "; | |
cin >> temp[j]; | |
cout << "THe value of j is " << j << " and of temp[j] is " << temp[j] << endl; | |
j++; | |
} | |
else if(T[i] == '$' || '*' || '/' || '+' || '-') | |
{ | |
stack.push(T[i]); | |
} | |
if(j==2) | |
{ | |
k = stack.pop(); | |
switch(k) | |
{ | |
case '$': | |
value = pow(temp[0],temp[1]); | |
break; | |
case '*': | |
value = temp[0]*temp[1]; | |
break; | |
case '/': | |
value = temp[0]/temp[1]; | |
break; | |
case '+': | |
value = temp[0]+temp[1]; | |
break; | |
case '-': | |
value = temp[0]-temp[1]; | |
break; | |
} | |
cout << "value of value is : " << value << endl; | |
temp[0] = value; | |
j--; | |
} | |
} | |
return 0; | |
} | |
int getweight(char s) | |
{ | |
switch(s) | |
{ | |
case '$' : return 3; | |
case '*' : | |
case '/' : return 2; | |
case '+' : | |
case '-' : return 1; | |
default : return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment