当前位置: 首页 > news >正文

栈------表达式求值

栈的应用---表达式求值

 

1.简单计算器

Problem Description
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
 

 

Input
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
 

 

Output
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
 

 

Sample Input
1 + 2 4 + 2 * 5 - 7 / 11 0
 

 

Sample Output
3.00 13.36
 

 

Source
浙大计算机研究生复试上机考试-2006年
#include <iostream>
#include <stack>
#include <string>
#include <cstdio>
using namespace std;

stack<double> opnd; //运算对象栈
stack<char> optr;   //运算符号栈
void f()            //取栈顶元素运算
{
    double tmp;
    char key=optr.top();
    optr.pop();
    double x=opnd.top();
    opnd.pop();
    double y=opnd.top();
    opnd.pop();
    switch(key)
    {
    case '+': tmp=x+y;  break;
    case '-': tmp=y-x;  break;
    case '*': tmp=x*y;  break;
    case '/': tmp=y/x;  break;
    }
    opnd.push(tmp);
}
int main()
{
    double a,ans=0;
    char t,b;
    while(scanf("%lf",&a)==1)
    {
        opnd.push(a);
        scanf("%c",&t);
        if(a==0&&t=='\n')           //结束循环
            break;
        while(scanf("%s %lf",&b,&a)==2)
        {
            char ch=getchar();      //读入数字后面的字符
            while(1)
            {
                if(optr.empty()||((b=='*'||b=='/')&&(optr.top()=='+'||optr.top()=='-')))
                {
                    optr.push(b);           //栈空或下一个运算符优先级更高,入栈
                    opnd.push(a);
                    break;
                }
                else                        //否则取栈顶元素运算然后运算符入栈
                     f();
            }
            if(ch=='\n')                 //输出结果
            {
                while(!optr.empty())
                {
                    f();
                }
                ans=opnd.top();
                printf("%.2lf\n",ans);
                while(!opnd.empty())    //多组测试数据,清空栈
                {
                    opnd.pop();
                }
                while(!optr.empty())
                {
                    optr.pop();
                }
                break;
            }
        }
    }
    return 0;
}

 这题至少整了两天,一开始是为了练一练STL,熟悉熟悉应用的,然后发现数据结构教科书上P87的例题写错了,只能进行10以内的加减乘除运算,除法还没法保持精度,然后这题一开始敲的时候就是整体将表达式读入,再处理的,没有注意到题中的每个符号之间都有一个空格,这能很好的简化题目,否则就得考虑字符串转化为数字的处理,手动模拟数字乘位权值;(这里,题目也没给清楚表达式里数字的范围,= =有想到java的大数浮点型)。最后终于AC了。。。

2.POJ2106 Boolean Expressions

Description

The objective of the program you are going to produce is to evaluate boolean expressions as the one shown next: 
Expression: ( V | V ) & F & ( F | V )

where V is for True, and F is for False. The expressions may include the following operators: ! for not , & for and, | for or , the use of parenthesis for operations grouping is also allowed. 

To perform the evaluation of an expression, it will be considered the priority of the operators, the not having the highest, and the or the lowest. The program must yield V or F , as the result for each expression in the input file. 

Input

The expressions are of a variable length, although will never exceed 100 symbols. Symbols may be separated by any number of spaces or no spaces at all, therefore, the total length of an expression, as a number of characters, is unknown. 

The number of expressions in the input file is variable and will never be greater than 20. Each expression is presented in a new line, as shown below. 

Output

For each test expression, print "Expression " followed by its sequence number, ": ", and the resulting value of the corresponding test expression. Separate the output for consecutive test expressions with a new line. 

Use the same format as that shown in the sample output shown below. 

Sample Input

( V | V ) & F & ( F| V)
!V | V & V & !F & (F | V ) & (!F | F | !V & V)
(F&F|V|!V&!F&!(F|F&V))

Sample Output

Expression 1: F
Expression 2: V
Expression 3: V

Source

México and Central America 2004
#include <iostream>
#include <stack>
#include <string>
#include <cstdio>
using namespace std;
int main()
{
    string s;
    int kase=1;
    //freopen("Atext.in","r",stdin);
    while(getline(cin,s)){
        int x,y,c;
        stack<int> val;
        stack<char> op;
        for(int i=0;i<s.size();i++){
            switch(s[i])
            {
            case '(':
                op.push(s[i]);
                break;
            case '!':       //注意如:!!!F,一开始没处理好RE了;
                if(!op.empty()&&op.top()=='!'){
                    op.pop();//注意!为单目运算符,一开始没处理好!
                }else
                    op.push(s[i]);
                break;
            case '|':
            case '&':
            case ')':
                while(!op.empty()){
                    if(op.top()=='!'){
                    x=val.top();
                    val.pop();
                    val.push((!x));
                    op.pop();
                    }else if(op.top()=='|')
                    {
                        x=val.top();
                        val.pop();
                        y=val.top();
                        val.pop();
                        val.push((x|y));
                        op.pop();
                    }else if(op.top()=='&')
                    {
                        x=val.top();
                        val.pop();
                        y=val.top();
                        val.pop();
                        val.push((x&y));
                        op.pop();
                    }else
                        break;
                }
                if(s[i]==')')
                    op.pop();
                else
                    op.push(s[i]);
                break;
            case 'V':
                val.push(1);
                break;
            case 'F':
                val.push(0);
                break;
            default :
                break;
            }
        }
        while(!op.empty()){
                if(op.top()=='!'){
                x=val.top();
                val.pop();
                val.push((!x));
                op.pop();
            }else if(op.top()=='|')
            {
                x=val.top();
                val.pop();
                y=val.top();
                val.pop();
                val.push((x|y));
                op.pop();
            }else if(op.top()=='&')
            {
                x=val.top();
                val.pop();
                y=val.top();
                val.pop();
                val.push((x&y));
                op.pop();
            }
        }
       // cout << val.size() << endl;
        //cout << op.size() << endl;
        printf("Expression %d: ",kase++);
        if(val.top()==1)
            printf("V\n");
        else
            printf("F\n");
    }
    return 0;
}
//新运算符比栈顶元素优先级高,入栈否则栈顶元素出栈运算,
//直至栈顶元素运算符优先级低于要入栈的运算符或栈空;

 

转载于:https://www.cnblogs.com/Cloud-king/p/8453703.html

相关文章:

  • UFPS入门: Unity FPS 教程
  • .NET Core 2.1路线图
  • 进程状态
  • linux运维面试精选
  • 链栈的实现
  • mysql字符集乱码
  • JavaWeb项目架构之NFS文件服务器
  • 应该怎么开始学习? study is a journey!
  • 17、文件IO详解及实例
  • TiDB 在 Ping++ 金融聚合支付业务中的实践
  • Apache(httpd)源码包安装
  • 重新认识下数组的concat方法
  • 基于Socket+Zookeeper的简单RPC框架
  • 算法学习之路|升序排序
  • vue:响应原理
  • 《用数据讲故事》作者Cole N. Knaflic:消除一切无效的图表
  • angular2 简述
  • ES6--对象的扩展
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • Github访问慢解决办法
  • mac修复ab及siege安装
  • magento2项目上线注意事项
  • Mithril.js 入门介绍
  • python学习笔记-类对象的信息
  • Redis学习笔记 - pipline(流水线、管道)
  • 百度贴吧爬虫node+vue baidu_tieba_crawler
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 开源SQL-on-Hadoop系统一览
  • 如何使用 OAuth 2.0 将 LinkedIn 集成入 iOS 应用
  • 设计模式 开闭原则
  • 使用 QuickBI 搭建酷炫可视化分析
  • 适配iPhoneX、iPhoneXs、iPhoneXs Max、iPhoneXr 屏幕尺寸及安全区域
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • 自定义函数
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • ​LeetCode解法汇总2304. 网格中的最小路径代价
  • ​linux启动进程的方式
  • ​MySQL主从复制一致性检测
  • #微信小程序(布局、渲染层基础知识)
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • $.ajax()方法详解
  • $NOIp2018$劝退记
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (11)MATLAB PCA+SVM 人脸识别
  • (C语言)共用体union的用法举例
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (二)linux使用docker容器运行mysql
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (附源码)小程序儿童艺术培训机构教育管理小程序 毕业设计 201740
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (一)硬件制作--从零开始自制linux掌上电脑(F1C200S) <嵌入式项目>
  • (转) ns2/nam与nam实现相关的文件
  • (转)IIS6 ASP 0251超过响应缓冲区限制错误的解决方法