C语言使用栈实现整数加减乘除运算(不包含括号)


#include <stdio.h>
#include <stdlib.h>
#define max_size 10
typedef int type;

// 使用栈来实现整数的加减乘除
typedef struct{
    type *data;
    type top; // 栈顶指针
} Stack;

Stack init_Stack(){
    // 初始栈
    Stack s;
    s.data = (type*)malloc(sizeof(type)*max_size);
    s.top = 0;
    return s;
}


int push(Stack *s, type elem){
    // 将elem元素压入栈
    if(s->top == max_size){
        printf("栈满,压栈失败\n");
        return 0;
    }
    s->data[s->top] = elem;
    s->top ++;
    return 1;   // 压栈成功
}

int pop(Stack *s){
    // 将栈顶元素弹出
    if(s->top == 0){
        printf("栈空,弹栈失败\n");
        return 0;
    }
    s->top --;
    return 1;
}

int size(Stack *s){
    // 返回栈中元素个数
    return s->top;
}

int is_empty(Stack *s){
    // 判断栈是否为空
    if(s->top == 0){
        return 1;
    }
    return 0;
}

int top(Stack *s){
    //返回栈顶元素
    if(s->top == 0){
        printf("栈空,返回失败\n");
        return 0;
    }
    return s->data[s->top-1];
}

void del(Stack *s){
    free(s->data);  // 释放栈空间
}



int main()
{
    Stack op_s = init_Stack();  // 操作符栈
    Stack nm_s = init_Stack();  // 数字栈
    char ch[100];
    scanf("%s", ch);
    int i = 0;
    char tc;
    int lx = 0; // 判断是否是连续的多位数字
    int number; // 保持栈中取出阿里的数字
    int t_num,next_num;
    while(1){
        tc = ch[i];
        if(tc != '\0'){
            if(tc <= '9' && tc >= '0'){
                if(lx == 1){    //连续的数字出现,表示多位数,比如11,将栈顶元素取出*10+当前数字
                    number = top(&nm_s);    //压入栈顶
                    pop(&nm_s);
                    push(&nm_s, number*10+(int)(tc-'0'));
                }else{
                    lx = 1;
                    // 为数字字符
                    push(&nm_s, (int)(tc-'0')); //  如果前面是运算符或者是第一个数字字符
                                                //,将其直接压入数字栈中
                }
                i++;

            }else if(tc == '+' || tc == '-'){   //+-,将操作符压入操作符栈中,最后处理+-
                push(&op_s, (int)tc);
                lx = 0;
                i++;
            }else if(tc == '*'){        // */ 要注意这种情况 12*14+1,要将操作符后的整个数字14取出来,再做运算
                t_num = top(&nm_s); //得到栈顶元素
                pop(&nm_s); //弹出栈顶

                next_num = 0;   // 取出操作符后的所有数字字符组成一个数
                while(1){
                    i ++;
                    if(ch[i] <= '9' && ch[i] >= '0'){
                        next_num = next_num*10 + (int)(ch[i] - '0');
                    }else{
                        break;
                    }
                }
                push(&nm_s, t_num * next_num);  // 将*结果,压入数字栈中,除法一致
                lx = 0;
            }else if(tc == '/'){
                t_num = top(&nm_s); //得到栈顶元素
                pop(&nm_s); //弹出栈顶
                next_num = 0;
                while(1){
                    i ++;
                    if(ch[i] <= '9' && ch[i] >= '0'){
                        next_num = next_num*10 + (int)(ch[i] - '0');
                    }else{
                        break;
                    }
                }
                push(&nm_s, t_num / next_num);
                lx = 0;
            }else if(tc == '='){    // 遇到=时表示运算结束,处理+-,将操作符栈中的一个操作符
                int n1,n2;          // 和数字栈中的两个数字取出,进行运算,并将结果压入栈中
                while(!is_empty(&op_s)){    // 当操作符栈中为空时,表示运算结束,运算结果在数字栈顶
                    tc = (char)top(&op_s);
                    pop(&op_s);
                    n1 = top(&nm_s);
                    pop(&nm_s);
                    n2 = top(&nm_s);
                    pop(&nm_s);
                    if(tc == '+'){
                        push(&nm_s, n2+n1);
                    }else{
                        push(&nm_s, n2 - n1);
                    }
                }
                break;
            }

        }else{
            break;
        }
    }
    int ans = top(&nm_s);   // 取出结果
    printf("%d\n", ans);

    return 0;
}


运算结果:
在这里插入图片描述

posted @ 2020-12-13 18:39  没尾巴的刺刺鱼  阅读(429)  评论(0编辑  收藏  举报