后缀表达式

后缀表达式的计算和中缀表达式转后缀表达式


此处的运算用的是链表的表示方法


以下为三个会涉及到的头文件


//
//  Error.h
//  后缀表达式
//
//  Created by Kyle.Yang on 14/12/2.
//  Copyright (c) 2014年 Kyle.Yang. All rights reserved.
//

#ifndef ______Error_h
#define ______Error_h

#include <iostream>
using namespace std;

class Error
{
protected:
    string name;
public:
    Error(string tempname="Default_Name")
    {
        this->name=tempname;
        cout<<"Error:"<<name<<endl;
    }
    
    virtual ~Error() {}
};

#endif

//
//  Node.h
//  后缀表达式
//
//  Created by Kyle.Yang on 14/12/2.
//  Copyright (c) 2014年 Kyle.Yang. All rights reserved.
//

#ifndef ______Node_h
#define ______Node_h

#include <stdio.h>
#include <iostream>
using namespace std;

template <class ElemType>
struct Node
{
    //数据成员
    ElemType data;
    Node<ElemType> *next;
    
    //构造函数
    Node()
    {
        next=NULL;
    }
    
    Node(ElemType e,Node<ElemType> *link=NULL)
    {
        data=e;
        next=link;
    }
    
};

#endif

//
//  LinkStack.h
//  后缀表达式
//
//  Created by Kyle.Yang on 14/12/2.
//  Copyright (c) 2014年 Kyle.Yang. All rights reserved.
//

#ifndef ______LinkStack_h
#define ______LinkStack_h

#include "Node.h"
#include <stdio.h>
#include <iostream>
using namespace std;

template <class ElemType>
class LinkStack
{
protected:
    Node<ElemType> *top;
    
public:
    LinkStack()
    {
        top=NULL;
    }
    
    virtual ~LinkStack()
    {
        Clear();
    }
    
    void Clear()
    {
        Node<ElemType> *p;
        while(top!=NULL)
        {
            p=top;
            top=top->next;
            delete p;
        }
    }
    
    bool IsEmpty() const
    {
        if(top==NULL)
            return true;
        else
            return false;
    }
    
    void Push(const ElemType e)
    {
        Node<ElemType> *p=new Node<ElemType>(e,top);
        if(p==NULL)
            cout<<"OVER_FLAW";
        else
        {
            top=p;
            //cout<<"Success";
        }
    }
    
    void Top(ElemType &e) const
    {
        if(IsEmpty())
            cout<<"UNDER_FLAW";
        else
        {
            e=top->data;
            //cout<<"Success";
        }
    }
    
    void Pop(ElemType &e)
    {
        if(IsEmpty())
            cout<<"OVER_FLAW";
        else
        {
            Node<ElemType> *p=top;
            e=top->data;
            top=top->next;
            delete p;
            //cout<<"Success";
        }
    }
    
};

#endif

功能函数如下

//
//  function.cpp
//  后缀表达式
//
//  Created by Kyle.Yang on 14/12/2.
//  Copyright (c) 2014年 Kyle.Yang. All rights reserved.
//


#include "LinkStack.h"
#include "Error.h"
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;

bool IsOperator(char ch)
{
    if(ch=='#' || ch=='(' || ch=='^' || ch=='*' || ch=='/' || ch=='+' || ch=='-' || ch==')')
        return true;
    else
        return false;
}

int OperPrior(char op1,char op2)
{
    int prior=0;
    switch (op1) {
        case '+':
        case '-':
            if(op2=='+' || op2=='-' || op2==')' || op2=='#')
                prior=2;
            else
                prior=1;
            break;
        case '*':
        case '/':
        case '^':
            if(op2=='^' || op2=='(')
                prior=1;
            else
                prior=2;
            break;
        case '(':
            if(op2==')')
                prior=0;
            else if(op2=='#')
                prior=-1;
            else
                prior=1;
            break;
        case ')':
            if(op2=='(')
                prior=-1;
            else
                prior=2;
            break;
        case '#':
            if(op2==')')
                prior=-1;
            else if(op2=='#')
                prior=3;
            else
                prior=1;
            break;
    }
    
    return prior;
}

double Operate(double first,char ch,double second)
{
    switch(ch){
        case '+':
            return first+second;
            break;
        case '-':
            return first-second;
            break;
        case '*':
            return first*second;
            break;
        case '/':
            return first/second;
            break;
        case '^':
            return pow(first,second);
            break;
    }
    return 0;
}

void InfixInToPostfix()
{
    LinkStack<char> optr;
    char ch;
    char priorChar;
    char op='#';
    double operand;
    int operandCount=0;
    
    optr.Push('#');
    priorChar='#';
    
    getchar();
    cout<<"输入中缀表达式,以‘#’号结束:";
    ch=getchar();
    while(op!='#' || ch!='#')
    {
        if(isdigit(ch) || ch=='.')
        {
            if(priorChar=='0' || priorChar==')')
                throw Error("两个操作数之间缺少运算符!");
            cin.putback(ch);
            cin>>operand;
            cout<<operand<<" ";
            operandCount++;
            priorChar='0';
            ch=getchar();
        }
        else if(!IsOperator(ch))
        {
            throw Error("表达式中有非法符号!");
        }
        else
        {
            if(ch=='(' && (priorChar=='0' || priorChar == ')'))
                throw Error("'('前缺少操作符!");
            while(OperPrior(op,ch)==2)
            {
                if(operandCount<2)
                    throw Error("缺少操作数!");
                operandCount--;
                optr.Pop(op);
                cout<<op<<" ";
                if(optr.IsEmpty())
                    throw Error("缺少操作符!");
                optr.Top(op);
            }
            switch (OperPrior(op,ch)) {
                case -1:
                    throw Error("括号不匹配!");
                    break;
                case 0:
                    optr.Pop(op);
                    if(optr.IsEmpty())
                        throw Error("缺少操作符!");
                    priorChar=ch;
                    ch=getchar();
                    break;
                case 1:
                    optr.Push(ch);
                    op=ch;
                    priorChar=ch;
                    ch=getchar();
                    break;
            }
        }
    }
    if(operandCount!=1)
        throw Error("缺少操作数!");
    cout<<endl;
};

void PostfixExpressionCalculation()
{
    LinkStack<double> opnd;
    char ch;
    double operand,first,second;
    getchar();
    cout<<"输入后缀表达式,以‘#’号结束:";
    ch=getchar();
    while(ch!='#')
    {
        if(isdigit(ch) || ch=='.')
        {
            cin.putback(ch);
            cin>>operand;
            opnd.Push(operand);
        }
        else if((!IsOperator(ch) || ch=='(' || ch==')') && (ch!=' '))
        {
            throw Error("表达式中有非法符号!");
        }
        else if(ch!=' ')
        {
            if(opnd.IsEmpty())
                throw Error("缺少操作数!");
            opnd.Pop(second);
            if(opnd.IsEmpty())
                throw Error("缺少操作数!");
            opnd.Pop(first);
            opnd.Push(Operate(first,ch,second));
        }
        ch=getchar();
    }
    if(opnd.IsEmpty())
        throw Error("缺少操作数!");
    opnd.Pop(operand);
    if(!opnd.IsEmpty())
        throw Error("缺少操作符!");
    cout<<"表达式的结果为:"<<operand<<endl;
};

void system()
{
    int choice;
    
    cout<<"后缀表达式 beta V1.0"<<endl;
    cout<<"1.后缀表达式的计算 2.中缀表达式转后缀表达式 0:退出系统"<<endl;
    cout<<"有问题请不要联系...版权所有...翻录...也不追究..."<<endl;
    cout<<"请键入你的选择:";
    cin>>choice;
    while(choice!=0)
    {
        if(choice==1)
            PostfixExpressionCalculation();
        else if(choice==2)
            InfixInToPostfix();
        else
            cout<<"输入错误"<<endl;
        
        cout<<"请键入你的选择:";
        cin>>choice;
    }
};

主函数如下

//
//  main.cpp
//  后缀表达式
//
//  Created by Kyle.Yang on 14/12/2.
//  Copyright (c) 2014年 Kyle.Yang. All rights reserved.
//

#include <iostream>
using namespace std;

void system();

int main(void)
{
    system();
    
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值