• 逆波兰计算器


    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <math.h>
    
    #define IncreSize 10
    #define InitSize 10
    
    typedef int status;
    typedef int Elemtype;
    
    typedef struct sStack
    {
        Elemtype *base;
        Elemtype *top;
        int StackSize;
    }sqStack;
    
    void InitStack(sqStack *s)
    {
        s->base = (Elemtype *)malloc(sizeof(int)*InitSize);
        if(!s->base)
            exit(0);
        else
        {
            s->top = s->base;
            s->StackSize = InitSize;
            //return OK;
        }
    }
    
    void Push(sqStack *s, Elemtype e)
    {
        if(s->top - s->base >= s->StackSize)
        {
            s->base = (Elemtype *)realloc(s->base,(s->StackSize + IncreSize)*sizeof(Elemtype));  //这里申请的大一些的空间;
            if(!s->base)
                exit (0);
        }
        *(s->top) = e;
        s->top++;
        //return OK;
    }
    
    void Pop(sqStack *s, Elemtype *e)
    {
        if(s->top == s->base)
            exit(0);
        *e = *--(s->top);
       // return OK;
    }
    
    int StackLen(sqStack s)
    {
        return(s.top - s.base);   //注意这里的结果是栈中的数据个数;
    }
    
    //输入数字,然后根据计算法则进行求解。
    int main()
    {
        char c;
        char str[20];         //设立一个缓冲区;
        int i = 0;
        int d,e,f;
        sqStack s;
        InitStack(&s);
        scanf("%c", &c);
        while(c != '#')
        {
            while( isdigit(c) || c == '.')
            {
                str[i++] = c;
                str[i] = '';
                if(i >= 10)
                {
                    printf("过界
    ");
                    return -1;
                }
                scanf("%c", &c);
                if(c == ' ')
                {
                    d = atof(str);
                    Push(&s,d);
                    i = 0;
                    break;
                }
            }
            switch(c)
            {
                case '+' :
                    Pop(&s, &e);
                    Pop(&s, &f);
                    Push(&s,e+f);
                    break;
                case '-':
                    Pop(&s, &e);
                    Pop(&s, &f);
                    Push(&s,f-e);
                    break;
                case '*':
                    Pop(&s, &e);
                    Pop(&s, &f);
                    Push(&s,e*f);
                    break;
                case '/':
                    Pop(&s, &e);
                    Pop(&s, &f);
                    if(f == 0)
                        exit(0);
                    Push(&s,e/f);
                    break;
            }
            scanf("%c",&c);
        }
        Pop(&s,&e);
        printf("%d",e);
        return 0;
    }

    这个程序还是有些地方刚刚开始细节没有考虑清楚。

  • 相关阅读:
    display:inline、block、inline-block的区别
    CSS选择器优先级总结
    bootstarp模板01
    Vue深度学习(6)- 组件
    在Ubuntu下安装mongodb
    Ajax高级应用---Comet
    ubuntu安装
    linux使用
    跨浏览器的CORS
    防止伪造跨站请求
  • 原文地址:https://www.cnblogs.com/wit-lq/p/4253236.html
Copyright © 2020-2023  润新知