Write a function that evaluates an expression in RPN that is given as a string.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_STACK_SIZE 100
// Stack structure
typedef struct {
int top;
double items[MAX_STACK_SIZE];
} Stack;
// Function to initialize the stack
void initialize(Stack *s) {
s->top = -1;
}
// Function to push an item onto the stack
void push(Stack *s, double value) {
if (s->top == MAX_STACK_SIZE - 1) {
printf("Stack Overflow\n");
exit(EXIT_FAILURE);
} else {
s->top++;
s->items[s->top] = value;
}
}
// Function to pop an item from the stack
double pop(Stack *s) {
if (s->top == -1) {
printf("Stack Underflow\n");
exit(EXIT_FAILURE);
} else {
double value = s->items[s->top];
s->top--;
return value;
}
}
// Function to evaluate RPN expression Reverse Polish Notation
double evaluateRPN(char *expression) {
Stack stack;
initialize(&stack);
char *ptr = expression;
while (*ptr != '\0') {
if (isdigit(*ptr)) {
// Convert character to integer and push onto stack
push(&stack, atof(ptr));
// Move pointer to the end of the number
____ __________ __________ ___ _____ ________ _________ ______ ____.
_________ _________ ____ __________ _________ _________.
___ _______ ________ __________ __________ _________.
___ ___ ___ ______ __________.
_________ ____ _______ _____ _________.
______ ____ ___ _________ _______ ________ ____ _____ _________ ______ ______ ________.
_________ ___ _____ __________ ____ _______ _____ _____.
______ _________ ____ _____ _________ __________ ____ ___.
__________ _______ ______ ____ __________ __________ __________ __________ __________.
______ __________ ____ ___ _______ _________ ________ ___ __________.
__________ _________ _____ _____ ____.
_________ _________ _______ _______ ___ ____ _________ ___ _______ ____ ___ _________.
_________ __________ ____ _____ ______ __________ ________.
_____ ___ __________ _________ ___.
___ ___ ___ _______ ______ ____.
___ _____ ___ _______ __________ _____.
______ ___ _______ ______ __________ ___.
______ ___ ____ _______ _________.
___ _____ ________ __________ _______ ________.
_________ ______ ____ __________ ___ ________ ____.
______ ___ _________ ___ ___ __________ __________ ____ _____ ____ ________.
________ ________ ______ ________ __________ ____ _______.
______ ____ ________ __________ _______ _______ _________ ________ _______ _______ _______ __________.
___ ___ _____ _________ ______ ___ ________ _______ ____ _____ __________ _________.
____ ____ ___ _____ ______ ___ ______ ______ ____ ______ ________.
______ ______ ____ ______ _______ ___ _______ ______ _____ _________ ___ _______.
__________ ___ ________ ____ ___ ____ _____ ________.
________ ____ _____ ________ ___ ______ ___ ____ ____ ______ ____ __________.
__________ _______ _________ __________ ________ ___.
________ ________ _______ ________ _______ ________ _____ ____ ______ ___ __________ ________.
______ _____ _________ _____ ____ ___ ___ ________.
_________ _____ __________ __________ ______.
___ ______ __________ ____ _____ _____.
____ ___ ________ _____ ________ _______ _________ ________ _______ ______ _______.
__________ ________ _________ ______ ___ ____ _____ ____.
_____ _______ _________ __________ ______ ________ _____ ______.
________ ______ ________ ________ _________.
________ ____ ________ __________ _________.
_________ ______ ______ ____ _____ ______ ______ ____.
___ ____ _______ ___ _______ ______ ___ __________ _________ ___ ______ ________.
_____ _______ ______ _____ _______ ___.
_____ _____ _________ ______ ________ ___ ____ _____ _______.
__________ _________ _______ ____ _______ ________ __________ ____ _____ ______ ____ ________.
________ _____ _____ ____ ______ _____ ______ _____ ___.
_______ _______ ________ ____ ___ ___ __________ ______ _______ _______.
_________ ____ _________ ____ ______ ______ _____ ______ _________ _____.
_______ _________ _______ ________ _________ ______ ________ ________ ____ ___ ____.
____ ____ ___ _____ _____ __________ ________ ___.
__________ __________ ________ ___ ________ ________ ___ ______.
__________ __________ __________ ___ _____ ______.
__________ ________ ___ ___ _________ _____ ________ ______.
_________.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required