数据结构:栈的应用之中缀转后缀C++

            最近刚学数据结构,写了一个中缀转后缀的程序,虽然勉强达到了目的,但是感觉写的很繁琐。希望各位大神能够帮忙指出我程序中的问题,代码风格、语法错误、实现算法等方面的问题都可以指出,谢谢!


头文件:

#include <iostream>
#include <string>


using namespace std;


typedef struct
{
    char elements[100];
    int top;
} body;


class stack
{
public :
    body S;
    stack()
    {
        S.top = -1;
    }


    bool Empty()
    {
        if(S.top<0)
            return true;
        else
            return false;
    }


    char Pop()
    {
        char x;
        if(Empty())
            x = ' ';
        else
        {
            x = S.elements[S.top];
            S.top--;
        }
        return x;
    }


    void Push(char x)
    {
        if(S.top == 99)
            cout << "the stack is already full";
        else
        {
            S.top = S.top +1;
            S.elements[S.top] = x;
        }
    }
};


正文:

#include <iostream>
#include <string>
#include "STACK.h"
#include <stdlib.h>
#include <string.h>


using namespace std;


void operate(string lines, stack &pre, stack &post);//将中缀转为后缀
int priority(char op);//比较运算符的优先级
void caculate(stack &post);//将字符串转换为数字
float expression(float x, float y, char z);//运算


int main()
{
    string lines;//将输入存储为字符串
    stack pre ;//用于存储运算符
    stack post ;//存储后缀表达式
    cout << "请输入算数中缀表达式:" ;
    cin >> lines;
    cout << "后缀表达式为:";
    operate(lines, pre, post);
    cout << endl;
    caculate(post);
    return 0;
}


void operate(string lines, stack &pre, stack &post)
{
    int len = lines.length();
    char x;
    for (int i=0; i<len; i++)
    {
        //分为两大类,若是数字或是小数点,则直接进入到post栈里
        //否则先进入到pre中处理后在进入到post栈中
        //用空格进行操作数的分离,和输出的排版
        if ((lines[i]>='0' && lines[i]<='9') || lines[i]=='.')
        {
            post.Push(lines[i]);
            cout << lines[i];
        }
        else
        {
            post.Push(' ');//将数字分开,以便后面进行多位数的区分
            if (lines[i] == '(')
                pre.Push(lines[i]);
            else if (lines[i] == ')')
            {
                cout << ' ';
                x = pre.Pop();
                while (x != '(')    //将栈中'('前的所有优先级比
                {                   //此时的运算符大的弹出
                    post.Push(x);   //再将此时的值压入
                    cout << x;
                    x = pre.Pop();
                }
            }
            else if (lines[i]=='+' || lines[i]=='-' || lines[i]=='*' || lines[i]=='/')
            {
                cout << ' ';
                if (!pre.Empty())
                {
                    x = pre.Pop();
                    while (priority(lines[i])<=priority(x))//比较优先级
                    {
                        post.Push(x);
                        cout << x;
                        cout << ' ';
                        if(pre.Empty())
                            break;
                        x = pre.Pop();
                    }
                    if (priority(x)<priority(lines[i]))
                        pre.Push(x);
                }
                pre.Push(lines[i]);
                cout << ' ';
            }
        }
    }
    while (!pre.Empty())//将pre中未出栈的操作符弹出
    {
        x = pre.Pop();
        post.Push(x);
        cout << ' ';
        cout << x;
        cout << ' ';
    }
}


int priority(char op)//优先级比较函数
{
    switch (op)
    {
    case '+':
    case '-':
        return 1;
    case '*':
    case '/':
        return 2;
    case '(':
        return 0;
    default:
        break;
    }
}


void caculate(stack &post)
{
    char z, m[15];
    float x, y;
    int n, i=0, j=0;
    float value[100];


    while(i <= post.S.top)
    {
        if((post.S.elements[i]>='0' && post.S.elements[i]<='9') || post.S.elements[i]=='.')
        {
            n = 0;
            memset(m,0,15);//清空m中残存的干扰值;
            while((post.S.elements[i]>='0' && post.S.elements[i]<='9') || post.S.elements[i]=='.')
            {//将字符型操作数分开存储在value中
                m[n++] = post.S.elements[i];
                i++;
            }
            value[j++]  = atof(m);//字符串型数字转换为float型;
        }
        if(post.S.elements[i] == ' ')
            i++;
        else
        {
            y = value[--j];
            x = value[--j];
            z = post.S.elements[i];
            value[j++] = expression(x,y,z);
            i++;
        }
    }
    cout << " the result is : " << value[--j] << endl;
}


float expression(float x, float y, char z)//返回单次计算值
{
    if(z == '+')
        return(x+y);
    else if(z == '-')
        return(x-y);
    else if(z == '*')
        return(x*y);
    else if(z == '/')
        return(x/y);
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值