Sample Output
// this program code is a postfix expression can be evaluated using a Stack given.
// created by: jieng46
// created by: jieng46
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
struct Node{
float number;
Node *next;
struct Node{
float number;
Node *next;
};
Node* push(Node *stack, float data){
Node *utility;
utility = new Node;
utility -> number = data;
utility -> next = stack;
return utility;
}
Node* pop(Node *stack, float &data){
Node *temp;
if (stack != NULL){
temp = stack;
data = stack -> number;
stack = stack -> next;
delete temp;
}
else cout << "\nERROR: Empty stack.\n";
return stack;
}
int main()
{
float jd_answer=0.f, jd_operand1=0.f, jd_operand2=0.f;
char jd_ch=' ';
char jd_ans;
Node *utility, *top;
utility = new Node;
utility -> number = 0.f;
utility -> next = NULL;
top = new Node;
top -> number = 0.f;
top -> next = utility;
cout << "\n[press spacebar after you input a number and operation]";
cout << "\nEnter the postfix expression: ";
cout << "\nInput: ";
while(jd_ch != '\n')
{
cin >> noskipws >> jd_ch; // "noskipws" do not skip whitespaces
int jd_operand = 0;
while(jd_ch == ' '){
cin >> jd_ch;
}
if((jd_ch >= '0')&&(jd_ch <= '9')){
while(jd_ch != ' '){
jd_operand = jd_operand*10 + (jd_ch-48);
cin >> jd_ch;
}
top = push(top, jd_operand);
}else{
top=pop(top, jd_operand1);
top=pop(top, jd_operand2);
//use of operations
switch(jd_ch){
case '+': jd_answer = jd_operand2 + jd_operand1;break;
case '-': jd_answer = jd_operand2 - jd_operand1;break;
case '*': jd_answer = jd_operand2 * jd_operand1;break;
case '/': jd_answer = jd_operand2 / jd_operand1;break;
}
top=push(top, jd_answer);
}
}
pop(top, jd_answer);
cout << "Output: " << jd_answer << endl; //display the result after evaluation
getche();
return 0;
}
//end....
Node* push(Node *stack, float data){
Node *utility;
utility = new Node;
utility -> number = data;
utility -> next = stack;
return utility;
}
Node* pop(Node *stack, float &data){
Node *temp;
if (stack != NULL){
temp = stack;
data = stack -> number;
stack = stack -> next;
delete temp;
}
else cout << "\nERROR: Empty stack.\n";
return stack;
}
int main()
{
float jd_answer=0.f, jd_operand1=0.f, jd_operand2=0.f;
char jd_ch=' ';
char jd_ans;
Node *utility, *top;
utility = new Node;
utility -> number = 0.f;
utility -> next = NULL;
top = new Node;
top -> number = 0.f;
top -> next = utility;
cout << "\n[press spacebar after you input a number and operation]";
cout << "\nEnter the postfix expression: ";
cout << "\nInput: ";
while(jd_ch != '\n')
{
cin >> noskipws >> jd_ch; // "noskipws" do not skip whitespaces
int jd_operand = 0;
while(jd_ch == ' '){
cin >> jd_ch;
}
if((jd_ch >= '0')&&(jd_ch <= '9')){
while(jd_ch != ' '){
jd_operand = jd_operand*10 + (jd_ch-48);
cin >> jd_ch;
}
top = push(top, jd_operand);
}else{
top=pop(top, jd_operand1);
top=pop(top, jd_operand2);
//use of operations
switch(jd_ch){
case '+': jd_answer = jd_operand2 + jd_operand1;break;
case '-': jd_answer = jd_operand2 - jd_operand1;break;
case '*': jd_answer = jd_operand2 * jd_operand1;break;
case '/': jd_answer = jd_operand2 / jd_operand1;break;
}
top=push(top, jd_answer);
}
}
pop(top, jd_answer);
cout << "Output: " << jd_answer << endl; //display the result after evaluation
getche();
return 0;
}
//end....