C语言用栈来实现加减乘除表达式的计算

阅读: 评论:0

C语言用栈来实现加减乘除表达式的计算

C语言用栈来实现加减乘除表达式的计算

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define STACK_INIT_SIZE 20  //初始化空间大小
#define STACKINCREASE 10  //每次追加的空间大小
#define ARRAY_SIZE 30  //数组的大小

typedef char Elemtype;
typedef double SElemtype;

struct Stack  //存放输入数据的字符串类栈
{
    Elemtype *bore;
    Elemtype *top;
    int stacksize;
};

struct COUCOUE //存放计算结果的栈
{
    SElemtype *sbore;
    SElemtype *stop;
    int Cstacksize;
};

void initstack(Stack *s) //初始化字符串栈
{
    s->bore = (Elemtype *)malloc(STACK_INIT_SIZE * sizeof(Elemtype));
    if (!s->bore)
    {
        printf("无法分配空间!07n");
        exit(0);
    }
    s->top = s->bore;
    s->stacksize = STACK_INIT_SIZE;
}

void Sinitstack(COUCOUE *s)  //初始化计算栈
{
    s->sbore = (SElemtype *)malloc(STACK_INIT_SIZE * sizeof(SElemtype));
    if (!s->sbore)
    {
        printf("无法分配空间!07n");
        exit(0);
    }
    s->stop = s->sbore;
    s->Cstacksize = STACK_INIT_SIZE;
}


void push(Stack *s,Elemtype e)  //字符串栈的入栈操作
{
    if (s->top - s->bore > STACK_INIT_SIZE)
    {
        s->bore = (Elemtype *)realloc(s->bore, (STACK_INIT_SIZE + STACKINCREASE) * sizeof(Elemtype));
        s->stacksize += STACKINCREASE;
    }
    *(s->top)= e;
    s->top++;
}

void Spush(COUCOUE *s, SElemtype e)   //操作数栈的入栈操作
{
    if (s->stop - s->sbore > STACK_INIT_SIZE)
    {
        s->sbore = (SElemtype *)realloc(s->sbore, (STACK_INIT_SIZE + STACKINCREASE) * sizeof(SElemtype));
        s->Cstacksize += STACKINCREASE;
    }
    *(s->stop) = e;
    s->stop++;
}

bool pop(Stack *s,Elemtype *e)  //字符串栈的出栈操作
{
    if (s->top == s->bore)
    {
        return false;
    }
    *e = *--(s->top);
    return true;
}

bool Spop(COUCOUE *s, SElemtype *e)   //操作数栈的出栈操作
{
    if (s->stop == s->sbore)
    {
        return false;
    }
    *e = *--(s->stop);
    return true;
}

int LenthStack(Stack s)
{
    return (s.top - s.bore);
}

int SLenthStack(COUCOUE s)
{
    return (s.stop - s.sbore);
}

int main()
{
    Stack s;
    COUCOUE conclu;
    char c,h;
    int i = 0,j=0,t=0;
    double d,g,a;
    char str[ARRAY_SIZE] = { 0 };      //用来存放输入的表达式
    char arr[ARRAY_SIZE] = { '' };   //用来作为字符串数据的缓冲区
    initstack(&s);
    Sinitstack(&conclu);
    printf("请输入要计算的加减乘除表达式(以#作为结束标志!):");
    scanf("%c", &c);
    while ('#' != c)
    {
        while(isdigit(c)||c=='.')
        {
            str[i++] = c;
            scanf("%c", &c);
            if (!isdigit(c)&& c!='.')
            {
                str[i++] = ' ';
            }
        }
        if (')' == c)
        {
            pop(&s, &h);
            while ('(' != h)
            {
                str[i++] = h;
                str[i++] = ' ';
                pop(&s, &h);
            }
        }
        else if ('+' == c || '-' == c)   //关键步骤,和栈顶元素比较优先级,加减是优先级最低的
        {                                //所以如果不是‘(’的话就输出栈顶元素
            if (!LenthStack(s))
            {
                push(&s, c);
            }
            else
            {
                do
                {
                    pop(&s, &h);
                    if ('(' == h)
                    {
                        push(&s, h);
                    }
                    else
                    {
                        str[i++] = h;
                        str[i++] = ' ';
                    }
                } while (LenthStack(s) && '(' != h);
                push(&s, c);
            }
        }
        else if ('*' == c || '/' == c || '(' == c)
        {
            push(&s, c);
        }
        else if ('#' == c)
            break;
        else
        {
            printf("输入的格式有误!");
        }
        scanf("%c",&c);
    }
    while (LenthStack(s))
    {
        pop(&s, &h);
        str[i++] = h;
        str[i++] = ' ';
    }

    printf("后缀表达式为:");
    printf("%sn", str);

    while (str[j])   //开始计算结果
    {
        if (str[j] == ' ')
        {
            j++;
            continue;
        }
        if(isdigit(str[j])||str[j]=='.')
        {
            arr[t++] = str[j];
            arr[t] = '';   //将前面保留的数字清零!!!!
            if (str[j + 1] == ' ')
            {
                d = atof(arr);
                Spush(&conclu, d);
                t = 0;
            }
        }

        switch (str[j])
        {
        case '+':Spop(&conclu, &g);
            Spop(&conclu, &a);
            Spush(&conclu,a+g);
            break;
        case '-':Spop(&conclu, &g);
            Spop(&conclu, &a);
            Spush(&conclu, a - g);
            break;
        case '*':Spop(&conclu, &g);
            Spop(&conclu, &a);
            Spush(&conclu, a*g);
            break;
        case '/':Spop(&conclu, &g);
            Spop(&conclu, &a);
            if (0 == g)
            {
                printf("除数不能为零!07n");
                return -1;
            }
            else
                Spush(&conclu,a/g);
            break;
        }
        j++;
    }
    Spop(&conclu, &d);
    printf("计算的结果为:");
    printf("%lf", d);
    getchar();
    getchar();
    return 0;
}

本文发布于:2024-02-05 04:38:25,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170724332963103.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23