The program is a simple calculator that reads input from the keyboard in the postfix notation and performs calculation for integer numbers can someone help me find the problem in my code? here are the codes: main.c:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "calc.h" /* header file */
#define MAXOP 128
int main(int argc, char *argv[]) {
int type;
double op2;
char s[MAXOP];
while ((type = getop(s)) != EOF) {
switch(type) {
case NUMBER:
push(atoi(s));
break;
case '+':
push(pop() + pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '*':
push(pop() * pop());
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push(pop() / op2);
else {
printf("error : zero divisor\n");
exit(1);
}
break;
case '&':
push(pop() & pop());
break;
case '|':
push(pop() | pop());
case '^':
push(pop() ^ pop());
break;
case '~':
push(~pop());
break;
case '>':
push(pop() > pop());
break;
case '=':
push(pop() == pop());
break;
case '<':
push(pop() < pop());
break;
//return 1 if number is 0 otherwise 1
case '!':
push(!pop());
break;
case '?':
if(pop() > 0)
push(pop());
break;
case '\n':
printf("The answer is %d.\n", pop());
break;
default:
printf("error: unknown command %s\n", s);
exit(1);
}
}
}
stack.c
#include <stdio.h>
#include <stdlib.h>
#include "calc.h"
#define MAXVAL 1024 /* maximum depth of val stack */
static int val[MAXVAL]; /* value stack */
static unsigned stackTop = 0; /* next free stack position */
/* push: push i onto value stack */
void push(int i) {
/* If the stack has free position, store the value */
if (stackTop < MAXVAL) {
val[stackTop++] = i;
}
else {
fprintf(stderr, "error: stack full, can't push %d\n", i);
exit(1);
}
}
/* pop: pop and return top value from stack */
int pop(void) {
/* If the stack contains values, return the last one */
if (stackTop != 0) {
int x = val[--stackTop];
return x;
}
else {
fprintf(stderr, "error: stack empty\n");
exit(1);
}
}
clac.h:
#define NUMBER '0'
/* stack.c */
void push(int);
int pop(void);
/* getop.c */
int getop(char[]);
/* getch.c */
int getch(void);
void ungetch(int);
getch.c
*
* getch.c
*
* getch returns one character from stdin
* or from the buffer buf if it is not empty
* ungetch puts one character in the buffer buf
*/
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 1024
static char buf[BUFSIZE] ; /* buffer for ungetch */
static unsigned bufTop = 0; /* next free position in buf */
/* get a (possibly pushed back) character */
int getch(void) {
return (bufTop > 0) ? buf[--bufTop] : getchar();
}
/* push character back on input */
void ungetch(int c) {
if (bufTop >= BUFSIZE) {
fprintf(stderr, "ungetch: buffer full\n");
exit(1);
}
else
buf[bufTop++] = c;
}
getop.c
/*
* getop.c
*
* Gets next token: operator or numeric operand
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "calc.h"
int getop(char s[]) {
int i, c;
while ((s[0] = c = getch()) == ' ' || c == '\t')
;
s[1] = '\0';
if (!isdigit(c) && c != '.')
return c; /* not a number */
/* collect integer part in string s */
i = 0;
if (isdigit(c))
while (isdigit(s[++i] = c = getch()))
;
/* collect fractional part in string s */
if (c == '.')
while (isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if (c != EOF)
ungetch(c);
return NUMBER;
}
makefile
# Makefile for project 1
# format of entries;
# <target list>: <dependency list>
# <command list>
# make will look at the last-write dates of each file, and if the
# target file hasn't been updated since the source files were last
# modified, it will update the target file by executing the shell
# command. Note that the character before <command list> MUST be a
# tab, not spaces.
calc: main.o getop.o stack.o getch.o
gcc -o calc main.o getop.o stack.o getch.o
# note that if calc.h changes, main.c and getop.c must be recompiled
main.o: main.c calc.h
gcc -c main.c
getop.o: getop.c calc.h
gcc -c getop.c
stack.o: stack.c
gcc -c stack.c
getch.o: getch.c
gcc -c getch.c
# This tells make what to make if you just type 'make'; if this isn't
# there, the first rule in the file will be the default.
# .DEFAULT_GOAL := calc
# Here is a rule to clean up the directory when calc is final and you
# want to reduce the clutter in the listing. Note that clean doesn't
# need to depend on any file modification time, so the <dependency
# list> position is just left empty.
clean:
rm *.o
testPostfix.txt
65535 255 &
1024 255 |
255 255 ^
32767 ~
123 456 >
123 456 =
123 456 <
0 !
5 !
2 3 0 ?
2 3 11 ?
2 99 + 65535 1024 & 2048 1024 | 255 254 ^ ? 4 5 > ?
but when I execute my program, I got this output

