[offtopic] @qwertmaniac and @t159 Do you really think that in India they teach Prefix/Postfix at school level ? [/offtopic]
@a5hr1th Use descriptive variable names dude! Its considered a good programming practice.
Anyways since in using this prefix-postfix thingy.. You can look at my code.. see if this makes any sense to you... If not, I suggest don't bother. Its not worth it.
Code:
/*
Coded By n00bish_king_of_Jellula aka Quiz_Master.
*/
#include <iostream>
#include <stack>
#include <string>
using namespace std;
void main()
{
int i, selection = 1;
string postfixExp;
char token;
float value, value1, value2;
stack<float> s; //Declare a stack of floats
while (selection != 0)
{
cout << "1. Evaluate a postfix expression" << endl;
cout << "0. Exit " << endl;
cout << "Enter the number for the option: ";
cin >> selection;
switch(selection)
{
case 1: cout << "Evaluate a postfix expression\n";
cout << "Enter the expression: ";
cin >> postfixExp;
i = 0;
token = postfixExp[i];
while((i < postfixExp.size()) && (token != '='))
{
if(isdigit(token))
{
value = token - '0';
s.push(value);
}
else
{
value2 = s.top();
s.pop();
value1 = s.top();
s.pop();
switch(token)
{
case '+': value = value1 + value2;
break;
case '-': value = value1 - value2;
break;
case '*': value = value1*value2;
break;
case '/': value = value1/value2;
break;
}
s.push(value);
}
i++;
token = postfixExp[i];
}
value = s.top();
s.pop();
cout << postfixExp << " " << value << endl;
break;
case 0: cout << "Exiting the program\n";
break;
default: cout << "Invalid option\n";
break;
}
cout << endl;
}
}
Opps sorry.. I forgot..I am no more visiting this forum! Ignore this please! I was NOT even here... An AI spyware is impersonating me!