The C Programming Language 练习题4-5

时间:2021-08-10 00:14:20

题目
给计算器程序增加访问sin、exp 与pow 等库函数的操作。有关这些库函数的详细信息,参见附录B.4节中的头文件

#include <stdio.h>
#include <stdlib.h> /* for atof() */
#include <math.h>
#include <string.h>

#define MAXOP 100
#define NUMBER '0'
#define MAXVAL 100 /* maximum depth of val stack */
#define NAME 'a'

int getop(char[]);
void push(double);
double pop(void);
void mathfnc(char s[]);


int main()
{
    int type, i;
    double op2;
    char s[MAXOP], matchcmd[MAXOP];

    i = 0;
    while ((type = getop(s)) != EOF)
    {
        switch (type)
        {
        case NUMBER:
            push(atof(s));
            break;
        case '+':
            push(pop() + pop());
            break;
        case '*':
            push(pop() * pop());
            break;
        case '-':
            op2 = pop();
            push(pop() - op2);
            break;
        case '/':
            op2 = pop();
            if (op2 != 0.0)
                push(pop() / op2);
            else
                printf("error: zero divisor\n");
            break;
        case '%':
            push((int)pop() % (int)pop());
            break;
        case '\n':
            printf("\t%.8g\n", pop());
            break;
        case NAME:
            mathfnc(s);
            break;
        default:
            printf("error: unknown command %s\n", matchcmd);
            break;
        }
    }
    return 0;
}

void mathfnc(char matchcmd[])
{
    double op4, op5;

    if(!strcmp(matchcmd, "sin"))
        {
            op4 = sin(pop());
            push(op4);
        }
    else if (!strcmp(matchcmd, "exp"))
        {
            op4 = exp(pop());
            push(op4);
        }
    else if(!strcmp(matchcmd, "pow"))
        {
            op4 = pop();
            op4 = pow(pop(), op4);
            push(op4);
        }
    else
        printf("error: command unknown.");
}

int sp = 0; /* next free stack position */
double val[MAXVAL]; /* value stack */

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

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

#include <ctype.h>

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

/* getop: get next character or numeric operand */
int getop(char s[])
{
    int i, c, d;

    while ((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '\0';
    if (!isdigit(c) && c != '.' && c != '-' && !islower(c))
        return c;   /*not a number */
    if ((c == '-' || c == '+') && !isdigit(d = getch()))
        {
            ungetch(d);
            return c;
        }
    i = 0;
    if(islower(c))
        {
        while(islower(s[++i] = c = getch()))
            ;
        s[i] = '\0';
        if(c != EOF)
            ungetch(c);
        return NAME;
        }
    if (isdigit(c))/* a number without '+' and '-' */
        {
            while(isdigit(s[++i] = c = getch()))
        ;
        }
        else /* a number with '+' and '-' */
        {
            s[++i] = d;
            while (isdigit(s[++i] = c = getch()))
                ;
        }
    if (c == '.')   /*collect fraction part */
        while (isdigit(s[++i] = c = getch()))
        ;
    s[i] = '\0';
    if (c != EOF)
        ungetch(c);
    return NUMBER;
}

#define BUFSIZE 1000

char buf[BUFSIZE];  /*buffer for ungetch */
int bufp = 0;       /*next free position in buf */

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

void ungetch(int c) /*push character back on input */
{
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}