Question
Here is the code provided by chegg but not working. Please, provide a good code or fix that one.
#include<stdio.h>
#include<conio.h>
#define LEN 25
void push(int *stack , int* top ,int node)
{
printf(" pushing : %d ", node);
stack[*top] = node;
(*top)++;
}
int pop(int *stack , int* top)
{
int temp;
(*top)--;
temp = stack[*top];
printf(" popping : %d",temp);
return temp;
}
void calc(int* numstack , int* numtop, int* opstack , int* optop){
int num1,num2,ans;
char sign;
num1 = pop(numstack,numtop);
num1 = pop(numstack,numtop);
switch(pop(opstack,optop)){
case '+' : ans = num1 + num2; sign = '+' ; break;
case '-' : ans = num1 - num2; sign = '-' ; break;
case '*' : ans = num1 * num2; sign = '*' ; break;
case '/' : ans = num1 / num2; sign = '/' ; break;
}
printf("%d %c %d = %d ",num1,sign,num2,ans);
push(numstack,numtop,ans);
while(getchar()!=' '){}
}
int main(void)
{
int optop = 0 , numtop = 0, j=0 , numstack[50] , opstack[30];
char str[102], c;
clrscr();
printf("enter string to be evaluated (100 chars max): ");
fgets(str , 100 , stdin);
while(str[j]!=' '){ j++; }
str[j] = '';
printf(" string : %s Length : %d",str,j);
j = 0;
while( (c=str[j]) !=''){
printf(" reading : %c ",str[j]);
if(c=='('){ j++; continue; }
else if(c=='+'||c=='-'||c=='*'||c=='/') push(opstack,&optop,c);
else if(c>=48 && c <=57) push(numstack,&numtop,(c-48));
else if(c==')') calc(numstack , &numtop, opstack , &optop);
else printf(" %c is invalid.. ",c);
j++;
}
printf(" answer is : %g",pop(numstack,&numtop));
getch();
return 0;
}
3. Create a calculator. This is a textual calculator not a GUI. a. Your calculator should begin by displaying the functions your calculator is able to perform b. The user should then be able to insert a formula for which you calculate and display the answer. c. The following functions need to be handled by your calculator: . Sin ii. Cos iii. Tan iv. XA2 V. XAY vi. Square root vii. Addition viii. Subtraction ix. Multiplication x. Division xi. Parentheses d. The user should be able to enter a formula like: 2 (3+4)-21/7 Your calculator should honor the order of operations and give the correct answer of 11.
Explanation / Answer
/** * Alexander Bernal * Project 3: SUPER-DUPER JAVA CALCULATOR */ import java.util.*; import java.util.Stack; import java.lang.String; import java.util.ArrayList; import java.lang.StringBuilder; import java.util.HashSet; import java.lang.Exception; import java.lang.Math; public class InfixEvaluator { public static class SyntaxErrorException extends Exception { /** * Construct a SyntaxErrorException with the specified message. * * @param message The message */ SyntaxErrorException(String message) { super(message); } } /** * This is the stack of operands: * i.e. (doubles/parentheses/brackets/curly braces) */ private static Stack operandStack = new Stack(); /** * This is the operator stack * i.e. (+-/*%^) */ private static Stack operatorStack = new Stack(); /** * These are the possible operators */ private static final String OPERATORS = "+-/*%^()[]{}"; private static final String BRACES = "()[]{}"; private static final String NONBRACES = "+-/*%^"; private static final int[] PRECEDENCE = {1, 1, 2, 2, 2, -1, -1, -1, -1, -1, -1}; /** * This is an ArrayList of all the discrete * things (operators/operands) making up an input. * This is really just getting rid of the spaces, * and dividing up the "stuff" into manageable pieces. */ static ArrayList input = new ArrayList(); public static ArrayList inputCleaner(String postfix) { StringBuilder sb = new StringBuilder(); String noSpaces = postfix.replace(" ", ""); try { for (int i = 0; i = '0' && c