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

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

    Time Limit: 1000MS Memory limit: 65536K

    题目描述

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

    输入

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

    输出

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

    示例输入

    59*684/-3*+#

    示例输出

    57

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<iostream>
     4 #include<algorithm>
     5 #include<stack>
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     stack<int>q;
    11     int i, t=0, f=0, len;
    12     char s[10000];
    13     scanf("%s", s);
    14     len = strlen(s);
    15     for(i=0; i<len; i++)
    16     {
    17         if(s[i]=='#')
    18         {
    19             printf("%d
    ", q.top());
    20             break;
    21         }
    22         if(s[i]>='0'  && s[i]<='9')
    23         {
    24             t = s[i]-'0';
    25             q.push(t);
    26         }
    27         else
    28         {
    29             f = q.top();
    30             q.pop();
    31             t = q.top();
    32             q.pop();
    33             if(s[i]=='+')
    34                 t = t+f;
    35             if(s[i]=='-')
    36                 t = t-f;
    37             if(s[i]=='*')
    38                 t = t*f;
    39             if(s[i]=='/')
    40                 t = t/f;
    41             q.push(t);
    42         }
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    User-agent大全
    获取https
    python 异常类型
    Git之生成ssh公钥
    Git 笔记
    iptables
    如何在CentOS 6.4上安装并使用OpenVZ?
    centos6.5 pptpd
    CentOS 6.x安装Metasploit
    CentOS 6.5下安装BeEF
  • 原文地址:https://www.cnblogs.com/6bing/p/4126093.html
Copyright © 2011-2022 走看看