首页 > 其他 > 详细

南阳oj 前缀式计算 题目218

时间:2015-08-06 11:09:26      阅读:230      评论:0      收藏:0      [点我收藏+]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stack>
#define N 1000
using namespace std;
char s[N],b[3];
stack<double>num;
stack<char>st;
int main()
{
 while(gets(s))
 {
   int k=strlen(s);
   double a1,a2;
   char s1[10],s2[10];
   for(int i=k-1;i>=0;i--)
   {
   //if(s[i]==‘ ‘) continue;
   if(s[i]>=‘0‘&&s[i]<=‘9‘||s[i]==‘.‘)//倒着来
   {
    int l=0;
    while(s[i]!=‘ ‘)
    {
     st.push(s[i]);
     i--;
    }
    while(!st.empty())
    {
     b[l]=st.top();
     st.pop();
     l++;
    }
    b[l]=‘\0‘;
    num.push(atof(b));
   }
   else
   {
    switch(s[i])
    {
     case ‘+‘ :
      a1=num.top();
      num.pop();
      a2=num.top();
      num.pop();
      num.push(a2+a1);
      break;
     case ‘-‘ :
      a1=num.top();
      num.pop();
      a2=num.top();
      num.pop();
      num.push(a1-a2);//顺序不能错
      break;
     case ‘*‘ :
      a1=num.top();
      num.pop();
      a2=num.top();
      num.pop();
      num.push(a2*a1);
      break;
     case ‘/‘ :
      a1=num.top();
      num.pop();
      a2=num.top();
      num.pop();
      num.push(a1/a2);//顺序不能错
      break;
    }
   }
  }
  printf("%0.2lf\n",num.top());
  num.pop();
   }
 return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

南阳oj 前缀式计算 题目218

原文:http://blog.csdn.net/yueloveme/article/details/47312879

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!