zoukankan      html  css  js  c++  java
  • 数据结构实验之栈三:后缀式求值

                                                                               数据结构实验之栈三:后缀式求值

    Description

    对于一个基于二元运算符的后缀表示式(基本操作数都是一位正整数),求其代表的算术表达式的值。

    Input

    输入一个算术表达式的后缀式字符串,以‘#’作为结束标志。

    Output

    求该后缀式所对应的算术表达式的值,并输出之。

    Sample Input

    59*684/-3*+#

    Sample Output

    57

    Hint

    基本操作数都是一位正整数!

    #include<stdio.h>
    int main(){
        int shi[1000];
        char str;
        int front=0;
        while(scanf("%c", &str), str != '#') {
            if('0'<=str && str<='9')
                shi[++front] = str-48;
            else {
                switch(str){
                    case '+':
                            shi[front-1] = shi[front-1] + shi[front];
                            break;
                    case '-':
                            shi[front-1] = shi[front-1] - shi[front];
                            break;
                    case '*':
                            shi[front-1] = shi[front-1] * shi[front];
                            break;
                    case '/':
                            shi[front-1] = shi[front-1] / shi[front];
                            break;
                }
                front--;
            }
        }
        printf("%d
    ", shi[front]);
    return 0;
    }


  • 相关阅读:
    CentOS 7
    CentOS
    CentOS 7
    CentOS 7
    Linux目录结构说明
    CentOS 7
    CentOS 7
    Linux——工具参考篇
    Linux工具进阶
    Selenium——UI自动化测试(2)——How to Download & Install Selenium WebDriver (待续)
  • 原文地址:https://www.cnblogs.com/Genesis2018/p/9079906.html
Copyright © 2011-2022 走看看