Question: I need help with this C programming Assignment. Copy these files calc.h, getch.c, getop.c, main.c, stack.c, and Makefile. The objective of this assignment is to

I need help with this C programming Assignment.

Copy these files calc.h, getch.c, getop.c, main.c, stack.c, and Makefile. The objective of this assignment is to let you practice with managing multiple files and using the make utility. The standard way to write an expression, such at a + b * c is called the infix notation. The same expression in the postfix notation is a b c * +. You can convert between infix and postfix Read the provided code calc.h, getch.c, getop.c, main.c, and stack.c. The program implements a simple calculator that reads input from keyboard in the postfix notation, and performs calculation for floating-point numbers. You can create the executable calc by entering the command make. Run the program a few times to become familiar with it. Your tasks in this asignment is to convert the code from floating-point calculation to integer calculation mostly replacing double with int. The original code uses atof() to convert ASCII to a floating-point number. You can use atoi() to convert ASCII to integer. Additionally, you add functionality to the calculator by impementing the following operations. Four bitwise operations: AND (&), OR (|), XOR (^), and NOT (). Note that AND, OR, and XOR are binary operators, but NOT is a unary operator. For example, a b & is the bitwise AND of a and b; a is the bitwise NOT of a. Three comparison operations: GREATER-THAN (>), EQUAL-TO (=), and LESS-THAN (<). The resulting values are either zero or one. Note that EQUAL-TO is denoted =, rather than == as in C. One logical operation NOT (!). It returns zero for a non-zero number, and returns one for zero. For example, 0 ! is 1, and 5 ! is 0. One ternary operation ?: The usual ternary expression a ? b : c in C will be written as c b a ?. If a is non-zero, the value of the expression is b, otherwise it is c.

calc.h

#define NUMBER '0'

void push(double); double pop(void);

int getop(char[]);

int getch(void); void ungetch(int);

----------------------------------------

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

#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 "); exit(1); } else buf[bufTop++] = c; }

----------------------------------------

getop.c

gets next token: operator or numeric operand */

#include #include #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; }

--------------------------------

main.c

postfix calculator */

#include #include #include #include "calc.h"

#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(atof(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 "); exit(1); }

-----------------------------------------------------

stack.c

stack routines */

#include #include "calc.h"

#define MAXVAL 1024 /* maximum depth of val stack */

static double val[MAXVAL]; /* value stack */ static unsigned stackTop = 0; /* next free stack position */

/* push: push f onto value stack */ void push(double f) { if (stackTop < MAXVAL) val[stackTop++] = f; else { fprintf(stderr, "error: stack full, can't push %lf ", f); exit(1); } }

/* pop: pop and return top value from stack */ double pop(void) { if (stackTop > 0) return val[--stackTop]; else { fprintf(stderr, "error: stack empty "); exit(1); } return 0.0; }

---------------------------

Make

: #

# 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 MUST be a # tab, not spaces.

cc = icc CFLAGS = -ansi -Wall

calc: main.o getop.o stack.o getch.o $(cc) $(CFLAGS) -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 $(cc) $(CFLAGS) -c -o main.o main.c

getop.o: getop.c calc.h $(cc) $(CFLAGS) -c -o getop.o getop.c

stack.o: stack.c $(cc) $(CFLAGS) -c -o stack.o stack.c

getch.o: getch.c $(cc) $(CFLAGS) -c -o getch.o 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: calcit

# 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 position is just left empty.

clean: rm *.o

That's all the files for this assignment

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!