PTA Evaluate Postfix Expression (10 分)

6-1 Evaluate Postfix Expression_DS_pta_C++_ PTA 6-1 Evaluate Postfix Expression 立即下载
  • Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /.

Format of functions:

ElementType EvalPostfix( char *expr );
  • where expr points to a string that stores the postfix expression. It is guaranteed that there is exactly one space between any two operators or operands. The function EvalPostfix is supposed to return the value of the expression. If it is not a legal postfix expression, EvalPostfix must return a special value Infinity which is defined by the judge program.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>

typedef double ElementType;
#define Infinity 1e8
#define Max_Expr 30   /* max size of expression */

ElementType EvalPostfix( char *expr );

int main()
{
    ElementType v;
    char expr[Max_Expr];
    gets(expr);
    v = EvalPostfix( expr );
    if ( v < Infinity )
        printf("%f\n", v);
    else
        printf("ERROR\n");
    return 0;
}

/* Your function will be put here */

Sample Input 1:

11 -2 5.5 * + 23 7 / -
结尾无空行

Sample Output 1:

-3.285714
结尾无空行

Sample Input 2:

11 -2 5.5 * + 23 0 / -

Sample Output 2:

ERROR

Sample Input 3:

11 -2 5.5 * + 23 7 / - *

Sample Output 3:

ERROR

Special thanks to Sirou Zhu (朱思柔) for providing a valuable test case.


AC:

ElementType EvalPostfix(char *expr)
{
    ElementType stack[Max_Expr];
    char num[Max_Expr];
    int i = 0, j = 0;
    int top = -1;
    ElementType x, y;
    while (expr[i] != '\0') /* '\0' 是字符串的结束符 */
    {
        while (expr[i] == ' ')
            i++;
        if (isdigit(expr[i]) || expr[i] == '.') /* isdigit()判断一个字符是否是十进制数字 */
        {
            num[j++] = expr[i];
            if (expr[i + 1] == ' ' || expr[i + 1] == '\0')
            {
                num[j] = '\0';
                stack[++top] = atof(num); /* atof()将字符串转换为double(双精度浮点数) */
                j = 0;
            }
        }
        else
        {
            if (expr[i] == '-' || expr[i] == '+')
            {
                if (isdigit(expr[i + 1]))
                {
                    num[j++] = expr[i];
                }
                else if (expr[i + 1] == ' ' || expr[i + 1] == '\0')
                {
                    if (top)
                    {
                        x = stack[top--];
                        y = stack[top];
                        if (expr[i] == '+')
                            stack[top] = y + x;
                        else
                            stack[top] = y - x;
                    }
                    else
                        return Infinity;
                }
            }
            else if (expr[i] == '*' || expr[i] == '/')
            {
                if (top)
                {
                    x = stack[top--];
                    y = stack[top];
                    if (expr[i] == '*')
                        stack[top] = y * x;
                    else
                    {
                        if (x)
                            stack[top] = y / x;
                        else
                            return Infinity;
                    }
                }
                else
                    return Infinity;
            }
        }
        i++;
    }
    if (!top)
        return stack[top];
    else
        return Infinity;
}

上面引用 isdigit() 函数时缺少了 <ctype.h>头文件!

ElementType EvalPostfix( char *expr ) {
    char t[10];
    double s[100],flag;
    int c = 0,k = 0;
    while(expr[k]) {
        int j = 0;
        while(expr[k] && expr[k] != ' ') t[j ++] = expr[k ++];
        t[j] = 0;
        if(t[0] >= '0' && t[0] <= '9' || t[1] && (t[0] == '-' || t[0] == '+')) {
            s[c] = 0;
            int i = 0;
            flag = 1;
            if(t[i] == '-') {
                flag = -1;
                i ++;
            }
            if(t[i] == '+') i ++;
            while(t[i] && t[i] != '.') {
                s[c] = s[c] * 10 + t[i ++] - '0';
            }
            if(t[i] == '.') {
                i ++;
                double d = 0.1;
                while(t[i]) {
                    s[c] += (t[i ++] - '0') * d;
                    d *= 0.1;
                }
            }
            s[c] *= flag;
            c ++;
        }
        else {
            if(c < 2) return Infinity;
            switch(t[0]) {
                case '+':s[c - 2] += s[c - 1];break;
                case '-':s[c - 2] -= s[c - 1];break;
                case '*':s[c - 2] *= s[c - 1];break;
                case '/': if(s[c - 1] == 0) return Infinity;
                          s[c - 2] /= s[c - 1];break;
            }
            c --;
        }
        while(expr[k] && expr[k] == ' ') k ++;
    }
    if(c > 1) return Infinity;
    return s[0];
}

此方法转自【链接】

使用 DEM 模拟金属烧结过程 了解离散元素法 (DEM) 如何彻底改变烧结工艺,提高制造业的效率和精度。 阅读详情

相关推荐

一分钟学会MATLAB-数据清洗(含完整代码)

数据清洗是数据分析过程中的一个重要步骤,目的是提高数据的质量,以便进行更有效的分析。% 删除超出阈值的行。% 用线性插值填充缺失值。% 删除包含缺失值的行。% 找到缺失值的位置。% 可视化数据,识别异常值。缺失值是数据清洗中常见的问题。通过以上步骤,你可以有效地清洗数据,使其准备好进行后续的分析和建模。

2402_87132195的博客 3325

Evaluate Postfix Expression_后缀表达式求解

6-2 Evaluate Postfix Expression(25 分) Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /. Format of functions:ElementType EvalPostfix( c

NITjianbagengmu的博客 2924

麒麟信安3.3-6C电网版虚拟机安装避坑指南:从ISO下载到SSH配置全流程

本文提供麒麟信安3.3-6C电网版虚拟机的详细安装与配置指南。针对电力行业仿真测试需求,重点解析从ISO镜像获取、虚拟机环境准备、安装过程中的三大“雷区”(如多管理员密码逻辑、分区方案、网络配置)到首次启动后的网络、SSH及root策略配置的全流程避坑要点,帮助技术人员高效、安全地完成部署。

rainy的博客 863

6-1 Evaluate Postfix Expression (25 分)

6-1 Evaluate Postfix Expression (25 分) Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /. Format of functions: ElementType EvalPostf...

CodeMonkey 4031

用Cadence IC618仿真双平衡吉尔伯特混频器:从原理图到后仿的完整避坑指南

本文详细介绍了在Cadence IC618环境中设计双平衡吉尔伯特混频器的全流程,包括原理图设计、仿真优化和版图实现。通过实战案例分享,重点解析了设计中的关键技巧和常见问题解决方案,帮助工程师高效完成高性能混频器设计,特别适合射频前端开发人员参考。

weixin_42656416的博客 169

酒店前台pms管理系统

对于酒店经营者来说,选择一款优秀的管理系统,是提升运营效率、优化客人体验的关键。PMS酒店管理系统凭借其强大的房态管理、实时数据监控、OTA直联、无人值守等功能,已经成为许多酒店提升管理水平的得力助手。无论是大中型酒店,还是小型民宿,PMS系统都能根据不同需求提供量身定制的解决方案,帮助酒店实现精细化管理、提高运营效率。如果你也在为酒店管理中的繁琐琐事而困扰,不妨尝试一下PMS酒店管理系统,感受科技带来的便利与高效,让你的酒店运营更上一层楼!

2409_89897808的博客 2494

Evaluate Postfix Expression

4-5 Evaluate Postfix Expression   (25分) Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /. Format of functions: ElementType E

不忘初心 6319

[XJTUSE DATABASE]——第三章 关系数据库标准语言SQL

新年了,来个大的,写了好几天,将近9000字,希望对大家有所帮助

weixin_47692652的博客 3129

6-5 Evaluate Postfix Expression(25 分)

6-5 Evaluate Postfix Expression(25 分)Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /.Format of functions:ElementType EvalPostfix( char...

友人A 1948

STM32F103C8T6 驱动2位共阳数码管(基于 74HC595 和 PNP 三极管)

整体设计思路是通过 STM32F103C8T6 控制 74HC595 和 PNP 三极管,实现共阳数码管的显示功能。最初的电路设计中,三极管发射极接 5V 电源,这导致了数码管两位显示相同数字的问题。数码管能够稳定显示 0-99的数字。

qq_40289410的博客 2228

《备考信息系统项目管理师》 - 专栏介绍和目录

本专栏会持续详细梳理软考高级信息系统项目管理师(高项)的考点、所有历年真题(包括综合知识、案例分析、论文)等,有论文专项,预测模拟题等内容。需要的可以先订阅下哦。

数据知道的博客 2万+

6-5 Evaluate Postfix Expression (25 分)

ElementType EvalPostfix( char *expr ){ double s1[10],t; int top1=-1,top2=-1,l=strlen(expr),neg=0; char s2[10]; for(int i=0;i<l;){ if(expr[i]=='-'&&expr[i+1]!=' '&&i!=l-1){ neg=1; i++;

Jay98的博客 482

2026年寒武纪数字IC笔试试卷带答案

本文摘要: 该数字IC设计测试卷包含单选题(30分)、波形分析题(15分)、多选题(30分)三类客观题,覆盖时序约束、半导体工艺、状态机、数制转换、同步复位等核心知识点。重点考察:1)跨时钟域处理(如异步FIFO、格雷码应用);2)时序分析(建立/保持时间、时钟约束指令);3)低功耗设计技术(时钟门控、DVFS);4)寒武纪MLU架构特性(张量核、混合精度计算)。解析强调工程实践要点,如异步复位需同步释放、建立时间违例与频率的关联性等。试题难度梯度明显,既包含基础概念辨析(如Moore/Mealy状态机),

Open 230

PTA Evaluate Postfix Expression (15 分)

3-4 Evaluate Postfix Expression (15 分) Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /. Format of functions: ElementType EvalPostfix( char *expr ); where expr points to a string that stores

haiziche的博客 913

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Data Structures and Algorithms (English) - 6-5 Evaluate Postfix Expression(25 分)

题目链接:点击打开链接 题目大意:略。 解题思路: 注意1:末尾判断不是'\n',而是'\000'。 注意2:手动建栈。 AC 代码 int h,t; ElementType sk[10000]; void fpop() { t--; } ElementType ftop() { return sk[t]; } int fempty() { ...

883

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

LeetCode–Evaluate Reverse Polish Notation

LeetCode–Evaluate Reverse Polish Notation (LeetCode - 评价逆波兰表示法,即后缀表达式) By X Wang (LeetCode: 一个在线编程网站,里面有很多大公司面试的题目)       The problem: 问题 Evaluate the value of an arithmetic expression in R

zgljl2012的专栏 1340

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

6-5 Evaluate Postfix Expression

Evaluate Postfix Expression(25 分) Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /. Format of functions: ElementType EvalPostfix( ...

lannister_awalys_pay的博客 692

6-2 Evaluate Postfix Expression (25 分)

题目: Write a program to evaluate a postfix expression. You only have to handle four kinds of operators: +, -, x, and /. Format of functions: ElementType EvalPostfix( char *expr ); where expr points to a string that stores the postfix expression. It is guara

weixin_51154479的博客 827

PAT(甲级)2019年秋季考试 7-3 Postfix Expression (25分)

Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators. Input Specification: Each input file contai...

一只小羊 828

PTA 1130 Infix Expression (25 分) 1162 Postfix Expression (25 分)

这两个题,一开始用一样的做法做,发现不行,最后发现是自己误解了,总结一下。 这种表达就两个规则。 左右子树均在: 括号+dfs(左)+当前值+dfs(右)+括号(中序就这样,后序当前值就放两dfs后边,这个是一致的); 只有一棵子树: 括号+当前值+dfs(那颗子树)+括号(这里不论什么序,都这样,这点误解了); 空的子树不用遍历,这样写方便记忆,空的也能遍历原因看下面代码。 PTA 1130 Infix Expression (25 分) 针对 中序表达 首先根据***普遍规则***,一个式子就能表示

qq_51791303的博客 255

Evaluate a Cubic Bézier on GPU

Evaluate a Cubic Bézier on GPU I’ve made an application as an example for this thread on how to compute/evaluate a Cubic Bézier Curve using a Geometry Shader. The formula is pretty straightfo

wanglang3081的专栏 4377

Postfix重要数据结构

binhash.c实现了哈希表的基本增删查改等功能,其binhash_hash函数完成哈希值的计算。 BINHASH_INFO结构体实现哈希表节点 BINHASH结构体表示哈希表本身 HTABLE结构体实现了哈希表双向链表(qmgr模块所使用的一系列主要结构体都用HTABLE来存放。)/util/binhash.h typedef struct BINHASH_INFO {

哪吒要结婚的博客 954

PAT (Advanced Level)1130 Infix Expression (25分)

Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators. Input Specification: Each input file contains one test case. For each case, the first line gives a

zy的博客 265

LeetCode //C - 1320. Minimum Distance to Type a Word Using Two Fingers

This problem involves finding the minimum total distance to type a given word using two fingers on a keyboard layout where letters are arranged in a grid. The solution uses dynamic programming to track the positions of the two fingers after each keystroke,

Made in Code 558
上一篇: PTA 两个有序链表序列的合并 (10 分)
下一篇: PTA 另类堆栈 (10 分)
小白兔奶糖ovo
博客等级 码龄6年 5312粉丝 223原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值