| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 317 人关注过本帖
标题:新手用C++编写的用栈求解算数表达式的程序,望朋友帮忙看一下,多谢
只看楼主 加入收藏
任随我心
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2012-9-16
收藏
 问题点数:0 回复次数:0 
新手用C++编写的用栈求解算数表达式的程序,望朋友帮忙看一下,多谢
#include<iostream>

#include<cstring>
#include<string>
#include<stack>
#include<cmath>
#include<cstdlib>
#include<cctype>
using namespace std;
     char OP[7]={'+','-','*','/','(',')','#'};
            char Priority[7][7]={{'>','>','<','<','<','>','>'},{'>','>','<','<','<','>','>'},
            {'>','>','>','>','<','>','>'},{'>','>','>','>','<','>','>'},
            {'<','<','<','<','<','=','@'},{'>','>','>','>','@','>','>'},
            {'<','<','<','<','<','@','='}};
            int Isoperator(char c)
            {
             for(int i=0;i!=7;++i)
             if(c==OP[i])  return 1;
             return 0;
                }
            char Precede(char c1,char c2)
                {
                    int m,n;
                    for(int i=0;i!=7;i++)
                    {if(c1==OP[i]) m=i;
                    if(c2==OP[i]) n=i;}
                    return Priority[m][n];
                    }
            float Operate (float a,char theta,float b)
                    {
                        switch (theta)
                        {
                        case '+':
                              return a+b;
                        case '-':return a-b;
                        case '*':return a*b;
                        case '/':
                        if(b==0) cout<<"the number b divided is zero"<<endl;
                        else return a/b;
                        }
                    }
        float EvaluateExpression(){
            stack <char> OPTR;
            OPTR.push('#');
            stack <float>  OPND;float sum;
            char c;
            cin>>c;
            while(c!='#'||OPTR.top()!='#')
            {
                if(!Isoperator(c))
                {
                    sum=0;
                    while(!Isoperator(c))
                    {
                        sum=10*sum+(c-'0');
                        cin>>c;
                        if(!Isoperator(c)) continue;//如果C还是数字的话,继续输入字符
                        else break;//否则退出循环

                    }

                    OPND.push(sum);//将数字输入的数字压入栈中

                }
                else
                switch(Precede(OPTR.top(),c))
                {
                    case '<':
                    OPTR.push(c);
                    cin>>c;
                    break;
                    case '=':
                    OPTR.pop();
                    cin>>c;
                    break;
                    case '>':
                    char theta=OPTR.top();
                    OPTR.pop();
                   float b=OPND.top();
                   OPTR.pop();
                  float a=OPND.top();
                  OPTR.pop();
                  OPND.push(Operate(a,theta,b));
                  break;
                    }

                }
                return OPND.top();
                }
                int main()
                {

                   cout<<EvaluateExpression()<<endl;
                    return 0;
                    }
搜索更多相关主题的帖子: 表达式 include 
2012-09-16 10:53
快速回复:新手用C++编写的用栈求解算数表达式的程序,望朋友帮忙看一下,多谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024187 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved