DSAL Stack - 240926 - 230009
DSAL Stack - 240926 - 230009
stack.h :
#ifndef stack
#define stack
#include<iostream>
using namespace std;
#endif
stack.cpp :
#include "stack.h"
#include<strings.h>
#include<iostream>
using namespace std;
#include "stack.h"
#include "stack.cpp"
#include<iostream>
#include<strings.h>
#include<cmath>
using namespace std;
struct op_val
{
. string operand;
. float value;
};
class expression
{
. public :
. int precedence(char c);
. string infix_to_postfix(string str);
. string rev_string(string str);
. string infix_to_prefix(string str);
. void postfix_eval(string str);
. void prefix_eval(string str);
};
int expression :: precedence(char c)
{
. int p;
. if(c == '+' || c == '-')
. . p = 1;
. else if(c == '*' || c == '/')
. . p = 2;
. else
. . p = 3;
. return p;
}
int main()
{
. expression e;
. string s;
. int num;
. char ch;
. string post, pre;
. cout<<"\nEnter string : ";
. cin>>s;
. do
. {
. . cout<<"\n\nEnter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : ";
. . cin>>num;
. . switch(num)
. . {
. . . case 1 : post = e.infix_to_postfix(s);
. . . cout<<"\nPostfix : "<<post<<endl;
. . . break;
. . . case 2 : pre = e.infix_to_prefix(s);
. . . cout<<"\nPrefix : "<<pre<<endl;
. . . break;
. . . case 3 : post = e.infix_to_postfix(s);
. . . cout<<"\nPostfix : "<<post<<endl;
. . . e.postfix_eval(post);
. . . break;
. . . case 4 : pre = e.infix_to_prefix(s);
. . . cout<<"\nPrefix : "<<pre<<endl;
. . . e.prefix_eval(pre);
. . . break;
. . . default : cout<<"\nERROR! TRY AGAIN!!"<<endl;
. . . break;
. . }
. . cout<<"\nDo you wish to continue? Y/N : ";
. . cin>>ch;
. }while(toupper(ch) == 'Y');
. return 0;
}
Output :-
Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 1
Postfix : abcd*-+
Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 2
Prefix : abcd*-+
Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 3
Postfix : abcd*-+
a Value : 10
b Value : 50
c Value : 10
d Value : 5
Result = 10
Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 4
Prefix : abcd*-+
a Value : 10
b Value : 5
c Value : 10
d Value : 5
Result = -35
Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 5