C //练习 7-5 改写第4章中的后缀计算器程序,用scanf函数和(或)sscanf函数实现输入以及数的转换。

发布时间:2024年01月17日

C程序设计语言 (第二版) 练习 7-5

练习 7-5 改写第4章中的后缀计算器程序,用scanf函数和(或)sscanf函数实现输入以及数的转换。

注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010

?

代码块:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>

#define MAXOP 100
#define MAXVARS 26
#define MAXLINE 1000
#define NUMBER 'N'
#define FUNCTION 'F'
#define VARIABLE 'V'

void callFunc(char funcName[]);
void duplicate(void);
void swap(void);
void printTopOfStack(void);
void push(double d);
void pushv(char f[]);
double pop(void);
char* popv(void);

double vars[MAXVARS];

int main() {
    int i;

    double op2;

    double a = 0;
    char s[MAXOP], c, e = '\0';

    printf("all variables are set to zero by default\n");
    for (i = 0; i < MAXVARS; ++i)
        vars[i] = 0;

    while (scanf("%s%c", s, &e) == 2) {
        if (sscanf(s, " %lf", &a) == 1) {
            push(a);
        } else if (sscanf(s, " %s", s) == 1) {

            if (strlen(s) > 1) {
                callFunc(s);
                printTopOfStack();
                continue;
            }

            c = s[0];

            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                pushv(s);
                continue;
            }

            switch (c) {
                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 division\n");
                    break;
                case '%':
                    op2 = pop();
                    if (op2 != 0.0) {
                        int r = fmod(pop(), op2);
                        push((r * op2 < 0) ? r + op2 : r);
                    } else
                        printf("error: cannot mod by zero");
                    break;
                default:
                    printf("error: unknown command type: \"%c\", inpt: \"%s\"\n",
                           c, s);
                    break;
            }

            printTopOfStack();
        }
    }
    return 0;
}

void callFunc(char funcName[]) {
    double op2;
    char vop2[MAXOP];

    if (strcmp(funcName, "sin") == 0)
        push(sin(pop()));
    else if (strcmp(funcName, "cos") == 0)
        push(cos(pop()));
    else if (strcmp(funcName, "tan") == 0)
        push(tan(pop()));
    else if (strcmp(funcName, "exp") == 0)
        push(exp(pop()));
    else if (strcmp(funcName, "log") == 0)
        push(log(pop()));
    else if (strcmp(funcName, "pow") == 0) {
        op2 = pop();
        push(pow(pop(), op2));
    }
    else if (strcmp(funcName, "sqrt") == 0)
        push(sqrt(pop()));
    else if (strcmp(funcName, "floor") == 0)
        push(floor(pop()));
    else if (strcmp(funcName, "ceil") == 0)
        push(ceil(pop()));
    else if (strcmp(funcName, "abs") == 0)
        push(fabs(pop()));
    else if (strcmp(funcName, "top") == 0)
        ;
    else if (strcmp(funcName, "swap") == 0)
        swap();
    else if (strcmp(funcName, "duplicate") == 0)
        duplicate();
    else if (strcmp(funcName, "set") == 0) {
        strcpy(vop2, popv());
        op2 = pop();

        if (!isalpha(vop2[0])) {
            printf("error: second argument must be variable\n");
        } else {
            int index = vop2[0] - 'a';
            vars[index] = op2;

            printf("set var \"%c\" to \"%g\", pushing \"%c\" onto stack\n",
                   vop2[0], op2, vop2[0]);

            pushv(vop2);
        }
    } else
        printf("error: unknown function %s\n", funcName);
}

#define MAXVAL 100

int sp = 0;
char val[MAXOP][MAXVAL];

void printTopOfStack(void) {
    if (sp < 1) {
        printf("not enough elements\n");
        return;
    }

    printf("\t%s\n", val[sp - 1]);
}

void swap(void){
    if (sp < 2){
        printf("not enough elements\n");
        return;
    }

    char temp[MAXOP];
    strcpy(temp, val[sp - 1]);
    strcpy(val[sp - 1], val[sp - 2]);
    strcpy(val[sp - 2], temp);
    printf("swapped\n\n");
}

void duplicate(void) {
    int j, t = sp;
    for (j = 0; j < t; j++)
        pushv(val[j]);
    printf("duplicated\n\n");
}

void push(double d) {
    char temp[MAXOP];
    if (sp < MAXVAL) {
        sprintf(temp, "%g", d);
        pushv(temp);
    } else
        printf("error: stack full, can't push %g\n", d);
}

void pushv(char f[]) {
    if (sp < MAXVAL)
        strcpy(val[sp++], f);
    else
        printf("error: stack full, can't push %s\n", f);
}

double pop(void) {
    char r[MAXOP];
    if (sp > 0) {
        strcpy(r, val[--sp]);
        if (isalpha(r[0]))
            return vars[r[0] - 'a'];
        else
            return atof(r);
    } else {
        printf("error: stack empty\n");
        return 0.0;
    }
}

char* popv(void) {
    if (sp > 0)
        return val[--sp];
    else {
        printf("error: stack empty\n");
        return "0.0";
    }
}
文章来源:https://blog.csdn.net/navicheung/article/details/135646406
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。