K & R C Programs Exercise 4-6.

K and R C, Solution to Exercise 4-6:
K and R C Programs Exercises provides the solution to all the exercises in the C Programming Language (2nd Edition). You can learn and solve K&R C Programs Exercise.
C Program to Add commands for handling variables.(It’s easy to provide twenty-six variables with single-letter names.). Add a variable for the most recently printed value.

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAXOP 100
#define NUMBER 0
#define IDENTIFIER 1
#define ENDSTRING 2
#define TRUE 1
#define FALSE 0
#define MAX_ID_LEN 32
#define MAXVARS 30


struct varType{
char name[MAX_ID_LEN];
double val;
};


int Getop(char s[]);
void push(double val);
double pop(void);
void showTop(void);
void duplicate(void);
void swapItems(void);
void clearStacks(struct varType var[]);
void dealWithName(char s[], struct varType var[]);
void dealWithVar(char s[], struct varType var[]);

int pos = 0;
struct varType last;

int main(void)
{
int type;
double op2;
char s[MAXOP];
struct varType var[MAXVARS];

clearStacks(var);

while((type = Getop(s)) != EOF)
{
switch(type)
{
case NUMBER:
push(atof(s));
break;
case IDENTIFIER:
dealWithName(s, var);
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop()- op2);
break;
case '/':
op2 = pop();
if(op2)
push(pop() / op2);
else
printf("nError: division by zero!");
break;
case '%':
op2 = pop();
if(op2)
push(fmod(pop(), op2));
else
printf("nError: division by zero!");
break;
case '?':
showTop();
break;
case '#':
duplicate();
break;
case '~':
swapItems();
break;
case '!':
clearStacks(var);
break;
case 'n':
printf("nt%.8gn", pop());
break;
case ENDSTRING:
break;
case '=':
pop();
var[pos].val = pop();
last.val = var[pos].val;
push(last.val);
break;
case '<':
printf("The last variable used was: %s (value == %g)n",
last.name, last.val);
break;

default:
printf("nError: unknown command %s.n", s);
break;
}
}
return EXIT_SUCCESS;
}

#define MAXVAL 100

int sp = 0;
double val[MAXVAL];

/* push: push f onto stack. */
void push(double f)
{
if(sp < MAXVAL)
val[sp++] = f;
else
printf("nError: stack full can't push %gn", f);
}

/*pop: pop and return top value from stack.*/
double pop(void)
{
if(sp > 0)
{
return val[--sp];
}
else
{
printf("nError: stack emptyn");
return 0.0;
}
}

void showTop(void)
{
if(sp > 0)
printf("Top of stack contains: %8gn", val[sp-1]);
else
printf("The stack is empty!n");
}


void duplicate(void)
{
double temp = pop();

push(temp);
push(temp);
}

void swapItems(void)
{
double item1 = pop();
double item2 = pop();

push(item1);
push(item2);
}


void clearStacks(struct varType var[])
{
int i;
sp = 0;

for( i = 0; i < MAXVARS; ++i)
{
var[i].name[0] = '';
var[i].val = 0.0;
}
}

void dealWithName(char s[], struct varType var[])
{
double op2;

if(!strcmp(s, "sin"))
push(sin(pop()));
else if(!strcmp(s, "cos"))
push(cos(pop()));
else if (!strcmp(s, "exp"))
push(exp(pop()));
else if(!strcmp(s, "pow"))
{
op2 = pop();
push(pow(pop(), op2));
}

else
{
dealWithVar(s, var);
}
}


void dealWithVar(char s[], struct varType var[])
{
int i = 0;

while(var[i].name[0] != '' && i < MAXVARS-1)
{
if(!strcmp(s, var[i].name))
{
strcpy(last.name, s);
last.val = var[i].val;
push(var[i].val);
pos = i;
return;
}
i++;
}

/* variable name not found so add it */
strcpy(var[i].name, s);
/* And save it to the last variable */
strcpy(last.name, s);
push(var[i].val);
pos = i;
}

int getch(void);
void unGetch(int);

/* Getop: get next operator or numeric operand. */
int Getop(char s[])
{
int i = 0;
int c;
int next;

/* Skip whitespace */
while((s[0] = c = getch()) == ' ' || c == 't')
{
;
}
s[1] = '';

if(isalpha(c))
{
i = 0;
while(isalpha(s[i++] = c ))
{
c = getch();
}
s[i - 1] = '';
if(c != EOF)
unGetch(c);
return IDENTIFIER;
}

/* Not a number but may contain a unary minus. */
if(!isdigit(c) && c != '.' && c != '-')
{
/* 4-6 Deal with assigning a variable. */
if('=' == c && 'n' == (next = getch()))
{
unGetch('');
return c;
}
if('' == c)
return ENDSTRING;

return c;
}

if(c == '-')
{
next = getch();
if(!isdigit(next) && next != '.')
{
return c;
}
c = next;
}
else
{
c = getch();
}

while(isdigit(s[++i] = c))
{
c = getch();
}
if(c == '.') /* Collect fraction part. */
{
while(isdigit(s[++i] = c = getch()))
;
}
s[i] = '';
if(c != EOF)
unGetch(c);
return NUMBER;
}

#define BUFSIZE 100

int buf[BUFSIZE];
int bufp = 0;

/* Getch: get a ( possibly pushed back) character. */
int getch(void)
{
return (bufp > 0) ? buf[--bufp]: getchar();
}

/* unGetch: push character back on input. */
void unGetch(int c)
{
if(bufp >= BUFSIZE)
printf("nUnGetch: too many charactersn");
else
buf[bufp++] = c;
}



Read more Similar C Programs
C Basic
C Strings
K and R C Programs Exercise

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link
(c) www.c-program-example.com

One comment on “K & R C Programs Exercise 4-6.

Leave a Reply