zoukankan      html  css  js  c++  java
  • 前缀表达式-怎样用空格分隔一个字符串

     1 #include<iostream>
     2 #include<cstdlib>
     3 #include<stack>
     4 #include<string>
     5 #include<sstream> 
     6 using namespace std;
     7 stack<double>s;
     8 char a[100][20];
     9 int main(){
    10     string str;
    11     getline(cin, str);
    12     istringstream iss(str);
    13     int i = 0;
    14     while(!iss.eof()){
    15         iss>>a[i++];
    16     }
    17     i--;
    18     for(;i>=0;i--){
    19         if(atof(a[i])!=0)
    20             s.push(atof(a[i]));
    21         else{
    22             string si = a[i];
    23             double d1 = s.top();
    24             s.pop();
    25             double d2 = s.top();
    26             s.pop();
    27             if(si=="*")d1*=d2;
    28             else if(si=="+")d1+=d2;
    29             else if(si=="-")d1-=d2;
    30             else if(si=="/")d1/=d2;
    31             s.push(d1);
    32         }
    33     }
    34     printf("%f
    ",s.top());
    35     return 0;
    36 }

    备注:主要就是利用标黄的那几行!

    非常好用!

  • 相关阅读:
    struts2文件上传下载
    struts2自定义拦截器
    JSP_Servlet 解决中文乱码登录问题
    ajax提交form表单
    sql语句大全
    spring
    struts2
    jsp_servlet
    jsp_servlet2
    数据库
  • 原文地址:https://www.cnblogs.com/fangziyuan/p/12151791.html
Copyright © 2011-2022 走看看