Question: C++ Get program to run. Hi guys. No actual coding involved. I cant get answer code from this assignment to run. Its from another Chegg
C++ Get program to run.
Hi guys. No actual coding involved.
I cant get answer code from this assignment to run. Its from another Chegg Q&A. If you can provide the running code in a Copy Paste format that would be great. I will Rate Thumbs up! Please test it or Let me know whats wrong. I get bugs when i try it. Let me know if you cant open the page. Ill take provide anything if asked.
http://www.chegg.com/homework-help/questions-and-answers/implement-stack-queue-using-linked-list-use-given-stack-queue-class-definitions-need-write-q11088263
ORIGINAL QUESTION AND THE ANSWER PROVIDED


The following file is Stack.cpp , which is the implementation file of Stack.h .Here stack is implemented using Linked list .Dev C++ IDE is Used to build the code.
#include
int Stack:: top() { return top->data; }
void Stack :: pop() { if(top==NULL) { coutnext; free(temp); }
void Stack:: Sdisplay() { struct node *temp; temp=top; while(temp) // while there are elemnt in stack { coutdata; temp=temp->data; } }
Now the following file is Queue.cpp , which is the implementation file of Queue.h header file.Here queue is implemented using Linked list .Dev C++ IDE is Used to build the code.
#include
void Queue:: AddQ(QueueElement x ) { struct node *temp; if (myback == NULL) { myback = (struct node *)malloc(1*sizeof(struct node)); myback->next = NULL; myback->data = x; myfront = myback; } else { temp=(struct node *)malloc(1*sizeof(struct node)); myback->next = temp; temp->data = x; temp->next = NULL; myback = temp; }
}
QueueElement Queue:: Front() { return (myfront->data); }
void Queue :: RemoveQ() { struct node *front1 = front; if (front1 == NULL) { coutnext != NULL) //means more than 1 node are there { front1 = front1->next; free(front); myfront = front1; } else //only one node is there { free(front1); myfront = NULL; myback = NULL; }
}
void Queue :: Qdisplay() { struct node *temp; if( myfront == NULL) coutdata; temp=temp->next; } }
}
Now the following file is Client.cpp. Here evaluation of Reverse Polish Notation Expresssion is done using the above 2 files Stack.cpp and Queue.cpp.
#include"Stack.h" #include"Queue.h" #include
using namespace std int main() { int i; //variable for user choice char str[50]; //temporary character array used for calculation printf(" 1. Enter the expression"); printf(" 2. Evaluate an expression"); printf(" 3. printing all expression that have been evaluated"); printf(" 4. Exit "); while(1) { printf(" Choose Option: "); scanf("%d",&i); switch(i) { case 1: { printf(" Enter the arithmetic expression to be evaluated: "); gets(str) AddQ(str); //the string is added to the queue break; } case 2: { str=Front(); // the front string of queue is copied RemoveQ(); // the front elemnt is copied. evaluate(str);// function is called to evaluate the expression. display(); break; } case 3: { Qdisplay(); // displaying all expression of queue break; } case 4: { exit(0); } default: { printf(" wrong choice for operation"); } } }
return 0; }
void evaluate(char array[50]) { int len,i,j,buf[50],x; stack if(array[i]>='0' && array[i] else if(array[i]==' ') //end of string { if(j>0) { buf[j] = '\0'; x = atoi(buf); //string to integer conversion s.push(x); //using local stack j = 0; } } else if(isoprtr(array[i])) { oprtr1 = s.top(); s.pop(); oprtr2 = s.top(); s.pop(); s.push(performOperation(oprtr1, oprtr2, array[i])); } } printf(" expression is %s and evaluated_answer is %d ",array, s.top()); } bool isoprtr(char ch) //to check weather the character is operator or not? { if (ch=='+' || ch=='-' || ch=='*' || ch=='/') return true; else return false; } int applyOperation(int p1, int p2, char op) //module for applying operation and returning resulted answer { int answer; switch(op){ case '+': answer = p2 + p1; break; case '-': answer = p2 - p1; break; case '*': answer = p2 * p1; break; case '/': answer = p2 / p1; break; } return answer; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
