0% found this document useful (0 votes)
28 views2 pages

Postfix Eval

This document defines a stack data structure using nodes and provides functions to push, pop, and print values to/from the stack. It also contains a function to evaluate a string as a math expression by parsing the string, pushing operands to the stack, and applying operators to pop values off the stack and push the results back on. Main tests it by evaluating the expression "54+38*+".

Uploaded by

aswinth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Postfix Eval

This document defines a stack data structure using nodes and provides functions to push, pop, and print values to/from the stack. It also contains a function to evaluate a string as a math expression by parsing the string, pushing operands to the stack, and applying operators to pop values off the stack and push the results back on. Main tests it by evaluating the expression "54+38*+".

Uploaded by

aswinth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostream>

#include <sstream>
using namespace std;

struct Node;
struct popReturn;
Node* push(Node* top,int val);
void printStack(Node* top);
popReturn pop(Node* top);

struct Node{
int val;
Node* next;
};

struct popReturn{
Node* newtop;
int poppedValue;
};

void evalString(string s,Node* operandStack){


int i = 0;
int n = s.length();
for(i=0;i<n;i++){
cout<<endl;
if(!isdigit(s[i])){

struct popReturn firstNum = pop(operandStack);


operandStack = firstNum.newtop;
struct popReturn secondNum = pop(operandStack);
operandStack = secondNum.newtop;

if(s[i] == '+'){
int result = firstNum.poppedValue + secondNum.poppedValue;
operandStack = push(operandStack,result);
}
else if(s[i] == '-'){
int result = firstNum.poppedValue - secondNum.poppedValue;
operandStack = push(operandStack,result);
}
else if(s[i] == '*'){
int result = firstNum.poppedValue * secondNum.poppedValue;
operandStack = push(operandStack,result);
}
else if(s[i] == '/'){
int result = firstNum.poppedValue / secondNum.poppedValue;
operandStack = push(operandStack,result);
}
else if(s[i] == '^'){
int result = firstNum.poppedValue + secondNum.poppedValue;
operandStack = push(operandStack,result);
}

}
if(isdigit(s[i])){
int val;
stringstream ss;
ss<<s[i];
ss >> val;
cout<<"Number is "<<val;
operandStack = push(operandStack,val);
cout<<"operand stack is";
printStack(operandStack);
cout<<endl;
}
}
}

Node* push(Node* top,int val){


cout<<"Inserting "<<val<<endl;
if(top == NULL){
Node* n = new Node;
n->val = val;
n->next = NULL;
return n;
}
Node* n = new Node;
n->val = val;
n->next = top;
return n;
}

popReturn pop(Node* top){

Node* temp = top;


top = top->next;
struct popReturn r;
r.newtop = top;
r.poppedValue = temp->val;
delete temp;
return r;
}

void printStack(Node* top){

while(top != NULL){
cout<<top->val;
top = top->next;
}

int main(){
Node* operands = NULL;
evalString("54+38*+",operands);

You might also like