The C Programming Language 练习题4-6

题目
给计算器程序增加处理变量的命令(提供26 个具有单个英文字母变量名的变量很容易)。增加一个变量存放最近打印的值。

题目分析
由于判断条件太多,导致程序非常复杂,实现二次赋值有些麻烦,终于还是想明白了。中间加了很多打印便于分析,就不去除了,下一题还要用:)

代码实现

#include <stdio.h>
#include <stdlib.h>  /* for atof() */
#include <math.h>
#include <string.h>
#include <ctype.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[]);
double varname[26];
double varval[26];
double val[MAXVAL]; /* value stack */
int sp = 0; /* next free stack position */

int main()
{
    int type, lastinput, i, m, n;
    double op2, vtemp;
    char s[MAXOP];

    vtemp = 0.0;
    for (i = 0; i < 26; i++)
        {
            varval[i] = 0.0;
            varname[i] = 'a';
        }

    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':
            if(lastinput == '=')
                {
                    printf("\t%.8g\n", val[0]);
                    vtemp = val[0];
                }
            else
                {
                vtemp = pop();
                printf("\t%.8g\n", vtemp);
                }
            break;
        case NAME:
            mathfnc(s);
            break;
        case '=':
            if(isupper(lastinput))
                {
                    if (sp > 1)
                        pop();
                    m = pop();
                    n = lastinput - 'A';
                    varval[n] = m;
                    printf("varval[%d]=%f\tm=%d\tval[%d]=%f\n", n, varval[n], m, sp, val[sp]);
                }
            else
                printf("error: no var\n");
            break;
        default:
            if(isupper(type) && (varname[type - 'A'] == 'a'))/* 如果变量没有被赋值 */
                varname[type - 'A'] = type;
            else if(isupper(type) && (varname[type - 'A'] != 'a'))/* 如果变量已经被赋值 */
                push(varval[type - 'A']);
            else if (type == 'v')
                push(vtemp);
            else
                printf("error: unknown command %s\n", s);
            break;
        }
        lastinput = type;
    }
    return 0;
}

void mathfnc(char s[])
{
    double op4;

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




/*push: push f onto value stack */
void push(double f)
{
    if (sp < MAXVAL)
        {
            val[sp++] = f;
            printf("val[%d]=%f\t\n", 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))
        {
            if (c == 'v')
            return 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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值