In: Computer Science
This code needs to run a working Reverse Polish Calculator, but when I input my commands it only pops my inputs never push them. So my answer is always zero.
Here is my code.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node
{
int element;
struct Node *next;
};
//Global variable
struct Node *top = NULL;
void push(int ele)
{
struct Node *newNode;
newNode = (struct Node *) malloc(sizeof(struct Node));
newNode->element = ele;
newNode->next = top;
top = newNode;
}
void pop()
{
struct Node *temp;
temp = top;
if(top == NULL)
printf("Stack is empty!!!\n");
else
{
top = top->next;
printf("%d is popped from the stack\n", temp->element);
free(temp);
}
}
bool isEmpty()
{
if(top == NULL)
return true;
else
return false;
}
int returnTopElement()
{
return top->element;
}
void printStack()
{
struct Node *temp;
temp = top;
printf("Stack : ");
while(temp != NULL)
{
printf("%d ", temp->element);
temp = temp->next;
}
printf("\n");
}
int oneByOne(){
char test; //taking character input
int num = 0;
int num1;
int num2;
int atleastone = 0;
printf("Enter your inputs separated by a space and the
operation.\n");
printf("Enter ! when you are done.\n");
while(1){
scanf("%c",&test); //take input of current character
printf("%c is user character\n", test);
if(test == '!'){break;} //terminates character
if(test == ' ' || test == '\n'){ //allows for space or newline
input
if(atleastone) //check if at least one previous digit scanned
push(num);
num = 0, atleastone = 0;
continue;
}
else if(test>='0' && test <='9'){
atleastone = 1;
num = num*10 + (test - '0');
continue;
}
if(top == NULL){
printf("This operation is invalid.\n");
break;
}
num1 = returnTopElement(); pop();
if(top == NULL){
printf("This operation is invalid.\n");
break;
}
num2 = returnTopElement(); pop();
if(test == '+');
push(num2+num1); //allows addition
if(test == '-');
push(num2-num1); //allows subtraction
if(test == '*');
push(num2*num1); //allows multiplication
if(test == '/');
push(num2/num1); //allows division
}
if(top == NULL)
return -1;
return returnTopElement();
}
int arrayInput(char** inputArray){
int i = 0;
int atleastone = 0;
int num = 0;
int num1;
int num2;
char test;
while((*inputArray)[i] != '\0'){
test = (*inputArray)[i];i++; //gets char input from function
if(test == '!'){break;} //terminate character
if(test == ' ' || test == '\n'){ //allows for space or newline
input
if(atleastone)
push(num);
num = 0, atleastone = 0;
continue;
}
else if(test >= '0' && test <= '9'){
atleastone = 1;
num = num*10 + (test- '0');
continue;
}
if(top == NULL){
printf("This operation is invalid.\n");
break;
}
num2 = returnTopElement(); pop();
if(test == '+');
push(num2+num1); //allows addition
if(test == '-');
push(num2-num1); //allows subtraction
if(test == '*');
push(num2*num1); //allows multiplication
if(test == '/');
push(num2/num1); //allows division
}
if(top == NULL)
return -1;
return returnTopElement();
}
int main(int argc, const char * argv[]) {
printf("Check oneByone function: \n");
int ans = oneByOne();
if(top == NULL){
printf("The input is invalid.\n");
}
else{
printf("The answer is %d\n", ans);
}
char * e = "23 45 + 3 *";
char ** expression = &e;
printf("\nChecking arrayInput function for %s \n",
*expression);
printf("The above expression is hardcoded in the code itself.
\n");
ans = arrayInput(expression);
if(top == NULL){
printf("The input expression is invalid. \n");
}
else{
printf("The answer of the input expression is %d\n", ans);
}
return 0;
}
class Solution {
public:
int evalRPN(vector &tokens) {
stack<int> nums;
int len = tokens.size();
for (int i = 0; i < len; i ++) {
string x = tokens[i];
if ((x == "+") || (x == "-") || (x == "*") || (x == "/")) {
// pop two numbers off the stack
int a = nums.top();
nums.pop();
int b = nums.top();
nums.pop();
// evaluate and push the result back
switch (x[0]) {
case '+': nums.push(a + b); break;
case '-': nums.push(b - a); break;
case '*': nums.push(b * a); break;
case '/': nums.push(b / a); break;
}
} else { // push a number into the stack
int n;
istringstream(x) >> n;
nums.push(n);
}
}
if (nums.size()) {
int xx = nums.top();
while (nums.size()) {
nums.pop();
}
return xx;
}
return 0; // error occurs
}
};
try this