#include <iostream>
#include <stdio.h>
#include <string.h>
char stack[100];
int top = -1;
void printArray(int array[], int size) {
int i;
for ( i = 0; i < size; i++)
{
printf("%d,", array[i]);
}
printf("\n");
}
int isempty() {
if(top == -1)
return 1;
else
return 0;
}
int isfull() {
if(top == 99)
return 1;
else
return 0;
}
int peek() {
return stack[top];
}
int pop() {
int data;
if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}else {
printf("Could not retrieve data, Stack is empty.\n");
}
}
int push(int data) {
if(!isfull()) {
top = top + 1;
printf("push %d, top is %d\n", data, top);
stack[top] = data;
//printf("push stack[top] = data, top is %d\n", top);
}else {
printf("Could not insert data, Stack is full, top is %d.\n",
top);
}
}
int eval(int op1, int op2, char operate) {
switch (operate) {
case '*': return op2 * op1;
case '/': return op2 / op1;
case '+': return op2 + op1;
case '-': return op2 - op1;
default : return 0;
}
}
int processexpr(char expr[])
{
// write this function
int i = 0;
char ch;
int val;
int size = strlen(expr);
while (expr[i]!='\0') {
ch = expr[i];
if(ch=='=')
return val;
if (isdigit(ch)) {
push(ch-'0');
}
else {
int op1 = peek();
pop();
int op2 = peek();
pop();
val = eval(op1, op2, ch);
push(val);
}
i++;
}
return val;
}
int main() {
char expr[100] = "31+=";
int x = processexpr(expr);
printf("%s %d\n", expr, x);
// make your own postfix expression to test
// and print out the result
}
===================================
See Output
Thanks
(Using C++) Create a function that will solve integer postfix equations. (must implement own stack, can’t...
By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand. Input A B C * + D E /...
This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...
Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...
I need assistance with this code. Is there any way I can create this stack class (dealing with infix to postfix then postfix evaluation) without utilizing <stdio.h> and <math.h>? ____________________________________________________________________________________________ C++ Program: #include <iostream> #include <string> #include <stdio.h> #include <math.h> using namespace std; //Stack class class STACK { private: char *str; int N; public: //Constructor STACK(int maxN) { str = new char[maxN]; N = -1; } //Function that checks for empty int empty() { return (N == -1); } //Push...
Using Windows API perform following exercise: Implement your own version of the ReadString procedure, using stack parameters. Pass it a pointer to a string and an integer, indicating the maximum number of characters to be entered. Return a count (in EAX) of the number of characters actually entered. The procedure must input a string from the console and insert a null byte at the end of the string (in the position occupied by 0Dh). See Section 11.1.4 for details on...
Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...
Java please 5. Using the following postfix expressions, use a class) to stack (or the method we used in (12 pts-6 eaeh) solve them producing a final result. (a) 69 5 725 7*+-18 25 5,2025 20%+ (b) 2 3+ 5-6 78+ 45 5 % 12 3/++ -
Python: using a stack, implement a function that takes in an arithmetic expression, and evaluates it, supported operations are + and -, which have same precedence. Note: one way to do it is to scan the entire expr pushing numbers onto val-stack and operations on op-stack until tokens in expr are done. Then, use a loop to pop two numbers from val-stack and one operation from op-stack, evaluate, and push results back to val-stack, until op-stack is empty.
You must implement the following methods using Java's Stack Object. /** * Computes the factorial of n * @param n-integer value greater or equal to 0 * @return n! */ public static int factorial( int n ) { } /** * Computes the nth term of the Fibonacci sequence * @param n -nth term to find * @return -the nth term */ public static int fibonacci( int n ) {} /** * Find the min value using the comparable interface...
Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...