通过栈来实现数学表达式的计算

/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co.,     Ltd. 
File name:
Author:Jerey_Jobs    Version:0.1    Date: 
Description:
Funcion List:将数学算术表达式以栈的形式写入并读取计算
             只能运算整数,不能输入浮点型或负数 
*****************************************************/

#include <stdio.h>

int check(char ma[]);
float run(char ma[]);

int main()
{
    char ma[100];

    printf("请输入数学表达式:");
    gets(ma);
    int a = check(ma);
    if(a == -1)
    {
        printf("输入错误!\n");
    }
    if(a == 0)
    {
        printf("yes!\n");
        puts(ma);
        printf("结果为: %.7f\n",run(ma));
    }
    return 0;
}

//开始运算
float run(char ma[])
{
    float nu = 0.0;
    int flag;
    int i = 0;
    int num_top = -1;
    int op_top = -1;
    float num[100] = {0.0};//存放表达式中的数字
    char op[100] = {'\0'};//存放表达式中的运算符
    while(ma[i] != '\0')
    {
        //如果是数字的话,就直接进栈
        if(ma[i] >= '0' && ma[i] <= '9')
        {
            nu = ma[i] - '0';
            while(ma[i + 1] >= '0' && ma[i + 1] <= '9')//判断是几位数的数字,并转换成整形存储
            {
                nu = 10 * nu + (ma[i + 1] - '0');
                i++;
            }
            num_top += 1;
            num[num_top] = nu;
        }
        //如果是加减的话,因为加减的优先级最低,因此这里的只要遇到加减号,无论操作符栈中的是什么运算符都要运算
        else if(ma[i] == '+' || ma[i] == '-')
        {
            while(op_top != -1 && (op[op_top] == '+'|| op[op_top] == '-' || op[op_top] == '*' || op[op_top] == '/'))
            {
                if(op[op_top] == '+')
                {
                    nu = num[num_top] + num[num_top - 1];
                }
                else if(op[op_top] == '-')
                {
                    nu = num[num_top - 1] - num[num_top];
                }
                else if(op[op_top] == '*')
                {
                    nu = num[num_top] * num[num_top - 1];
                }
                else if(op[op_top] == '/')
                {
                    nu = num[num_top - 1] / num[num_top];
                }
                num[num_top] = 0;
                num_top -= 1;
                num[num_top] = nu;
                op[op_top] = '\0';
                op_top--;
            }
            //将当前运算符入栈
            op_top += 1;
            op[op_top] = ma[i];
        }
        //当前运算符为‘'('时,直接压入运算符栈
        else if(ma[i] == '(')
        {
            op_top += 1;
            op[op_top] = ma[i];
        }
        //当运算符为')'时,不停清除栈中的运算符直至左括号
        else if(ma[i] == ')')
        {
            while(op[op_top] != '(')
            {
                if(op[op_top] == '+')
                {
                    nu = num[num_top] + num[num_top - 1];
                }
                else if(op[op_top] == '-')
                {
                    nu = num[num_top - 1] - num[num_top];
                }
                else if(op[op_top] == '*')
                {
                    nu = num[num_top] * num[num_top - 1];
                }
                else if(op[op_top] == '/')
                {
                    nu = num[num_top - 1] / num[num_top];
                }
                //将当前数学结果压入数字栈,并将运算符栈的栈顶置空
                num[num_top] = 0;
                num_top -= 1;
                num[num_top] = nu;
                op[op_top] = '\0';
                op_top--;
            }
            //将遇到的左括号置空
            op[op_top] = '\0';
            op_top--;
        }
        //当前运算符是乘除的时候,因为优先级高于加减,因此要判断最上面的是否是乘除,如果是乘除就运算,否则的话直接入
        else if(ma[i] == '*' || ma[i] == '/')
        {
            while(op_top != -1 && (op[op_top] == '*' || op[op_top] == '/'))
            {
                if(op[op_top] == '*')
                {
                    nu = num[num_top] * num[num_top - 1];
                }
                else if(op[op_top] == '/')
                {
                    nu = num[num_top - 1] / num[num_top];
                }
                //将当前的数学结果入栈,并将运算符的栈顶置空
                num[num_top] = 0;
                num_top -= 1;
                num[num_top] = nu;
                op[op_top] = '\0';
                op_top--;
            }
            //将当前运算符压入栈
            op_top += 1;
            op[op_top] = ma[i];
        }
        i++;
    }
    //遍历结束后,当栈中不是空的时候继续运算,直到栈中为空即可
    while(op_top != -1)
    {
        if(op[op_top] == '+')
        {
            nu = num[num_top] + num[num_top - 1];
        }
        else if(op[op_top] == '-')
        {
            nu = num[num_top - 1] - num[num_top];
        }
        else if(op[op_top] == '*')
        {
            nu = num[num_top] * num[num_top - 1];
        }
        else if(op[op_top] == '/')
        {
            nu = num[num_top - 1] / num[num_top];
        }
        num[num_top] = 0;
        num_top -= 1;
        num[num_top] = nu;
        op[op_top] = '\0';
        op_top--;
    }
    //将最终结果压入栈,并返回最终结果
    nu = num[num_top];
    return nu;
}

//检查表达式的合法性,-1为不合法,0为合法
int check(char ma[])
{
    int i = 0;
    int lc,rc;
    lc = 1;//连续的'('的个数
    rc = 1;//连续的')'的个数
    int count;//未配对的'('的个数
    count = 0;
    while(ma[i] != '\0')
    {
        if((ma[i] < '0' || ma[i] > '9') && (ma[i] != '+') && (ma[i] != '-') && (ma[i] != '*') && (ma[i] != '/') && (ma[i] != '(') && (ma[i] != ')') )
        {
            return -1;
        }
        i++;
    }
//到此上面检查的是每个字符是否合法,即012356789+- * / ( )

/*
'('要么是第一个字符,否则的话,前一个必须是'('或者是运算符,不是数字,否则不合法;必须有下一个字符,且下一个字符必须是数字或者是'(',否则不合法
'+'、'-'、'*'、'/',运算符的前一个字符必须是数字或者是')',否则不合法;必须有下一个字符,且下一个字符必须是数字或者是'(',否则不合法
')'的前一个必须是数字或者是')',否则不合法;不一定有下一个字符,如果有,就必须是运算符或者是')'
数字:要么是表达式的第一个字符,如果不是,那么前一个字符,只要不是')',其他合法的字符都可以;不一定有下一个字符,如果有,只要不是')',其他合法字符都可以
*/
    i = 0;
    while(ma[i] != '\0')
    {
        if(i == 0)//对第一个字符进行判断
        {
            if(ma[i] != '('&& (ma[i] < '0' || ma[i] > '9'))
            {
                printf("1\n");
                return -1;
            }
            else
            {
                if(ma[i] == '(')
                {
                    if((ma[i + 1] < '0' || ma[i + 1] > '9') && ma[i + 1] != '(')
                    {
                        printf("2\n");
                        return -1;
                    }
                    count++;
                    if(ma[i + 1] == '(')
                    {
                        lc++;
                    }
                }
                else if(ma[i] >= '0' && ma[i] <= '9')
                {
                    if(ma[i + 1] != '\0')
                    {
                        if(ma[i + 1] != '+' && ma[i + 1] != '-' && ma[i + 1] != '*' && ma[i + 1] != '/' && (ma[i + 1] < '0' || ma[i + 1] > '9'))
                        {
                            printf("3\n");
                            return -1;
                        }
                    }
                }
            }
        }
        else//对剩余的表达式字符进行判断
        {
            if(ma[i] == '+' ||ma[i] == '-' || ma[i] == '*' || ma[i] == '/')//为+ - * / 时
            {
                //当运算符字符的前面不是右括号或者数字的话,返回-1
                if(ma[i - 1] != ')' && (ma[i - 1] < '0' || ma[i - 1] > '9'))
                {
                    printf("4\n");
                    return -1;
                }
                if(ma[i + 1] != '\0')
                {
                    if(ma[i + 1] != '(' && (ma[i + 1] < '0' || ma[i + 1] > '9'))//当运算符字符的下一个不是数字或左括号时,返回-1
                    {
                        printf("5\n");
                        return -1;
                    }
                }
            }
            else if(ma[i] == '(')//为左括号时
            {
                //前一个不是左括号或者+、-、*、/时,返回-1
                if(ma[i - 1] != '(' && ma[i - 1] != '+' && ma[i - 1] != '-' && ma[i- 1] != '*' && ma[i- 1] != '/')
                {
                    printf("6\n");
                    return -1;
                }
                if(ma[i + 1] != '\0')
                {
                    //下一个不是(,也不是数字时,返回-1
                    if(ma[i + 1] != '(' && (ma[i + 1] < '0' || ma[i + 1] > '9'))
                    {
                        printf("7\n");
                        return -1;
                    }
                    //遇到连续的(时,标识lc记录连续的个数
                    if(ma[i + 1] == '(')
                    {
                        lc++;
                    }
                }
                count++;
            }
            else if(ma[i] == ')')
            {
                if(ma[i - 1] != ')'&& (ma[i - 1] < '0' || ma[i - 1] > '9'))
                {
                    printf("8\n");
                    return -1;
                }
                if(ma[i + 1] != '\0')
                {
                    if(ma[i + 1] != ')' && ma[i + 1] != '+' && ma[i + 1] != '-' && ma[i + 1] != '*' && ma[i + 1] != '/')
                    {
                        printf("9\n");
                        return -1;
                    }
                    if(ma[i + 1] == ')')
                    {
                        rc++;
                    }
                    count--;
                }
                else
                {
                    count--;
                }
            }
            else if(ma[i] >= '0' && ma[i] <= '9')
            {
                if(ma[i - 1] == ')')
                {
                    printf("10\n");
                    return -1;
                }
                if(ma[i + 1] != '\0')
                {
                    if(ma[i + 1] == '(')
                    {
                        printf("11\n");
                        return -1;
                    }
                }
            }
            //判断未配对的'('的个数
            if(count < 0)
            {
                        printf("12\n");
                return -1;
            }
            else if(count == 0)
            {
                //是否有连续的'('或者')',有的话就不合法;没有的话就均置为1
                if(lc > 1 && rc > 1)
                {
                        printf("13\n");
                    return -1;
                }
                else
                {
                    lc = rc = 1;
                }
            }
        }
        i++;
    }
    //如果未配对的'('数量不为0,表达式就不合法
    if(count != 0)
    {
                        printf("14444\n");
        return -1;
    }
    return 0;

}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值