02-3
来源:互联网 发布:淘宝网站营销策划方案 编辑:程序博客网 时间:2024/06/05 17:25
哈哈哈哈哈~竟然一次过!!!
不过样例确实给了很多提示
参考了别人代码
http://www.patest.cn/contests/mooc-ds/02-3
/* title:02-3 问题分类: 新思路: 1.倒着看,遇到数字进栈,运算符出操作数2个 2.对于数字,就当做无小数点,直到遇到小数点,除以该权值,重置权值,再算 3.数字正负问题 */#include<stdio.h>#include<string.h>double ss[40];int top=-1;void push(double x){ss[++top]=x;}bool flag;//除数为0 double pop(){if(top!=-1){return ss[top--];}else{flag=1;}}double com(char c){double a=pop();double b=pop();if(c=='+'){return (a+b);}else if(c=='-'){return (a-b);}else if(c=='*'){return (a*b);}else if(c=='/'){if(b==0){//printf("ERROR\n");flag=1;}else{return (a/b);}}}int main(){freopen("in.txt","r",stdin); char s[40]; while(gets(s)){int len=strlen(s);//输入有空行 if(len==0){continue;}for(int i=len-1;i>=0;){if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/'){double c=com(s[i]);if(flag==1){printf("ERROR\n");break;}push(c);i--;}else if(s[i]==' '){i--;}else{double quan=1;double x=0;while(s[i]!=' '&&i>=0){if(s[i]=='.'){x/=quan;quan=1;}else if(s[i]=='+'||s[i]=='-'){if(s[i]=='-'){x=0-x;}}else{x+=(quan*(s[i]-'0'));quan*=10;}i--; }push(x);x=0;quan=1;}}if(!flag){double res=0;int count=0;while(top!=-1){res=pop();count++;}if(count!=1){printf("ERROR\n");}else{printf("%.1lf\n",res);}} } return 0;}
0 0
- 02-3
- Chapter 3-02
- 02课后作业3
- 2011-5-3 02:43
- 2011-5-3 02:13
- 3-02 U-Boot命令
- 2017-02-21 Hibernate(3)
- 《EJB 3 in Action》读书笔记02
- phpbb 3 源码解析02 --common.php
- 8/27/2011 3:02:21 AM
- VMware8.02安装MacOSX Lion 10.7.3
- 关于_WIN32_WINNT 2012-3-21 09:02
- Notes on <Papervision 3D Essentials> - 02
- 3d相册弹出效果02
- 02-线性结构3 Pop Sequence
- Vert.x 3学习笔记---02
- 02-线性结构3 Pop Sequence
- 02-线性结构3 Pop Sequence
- ECShop二次开发之首页导航栏管理
- Vector和LinkedList
- RaspberryPi 树莓派 设置无线USB网卡连接wifi
- u Calculate e
- 【BZOJ1266】【AHOI2006】上学路线route 最短路建图转最小割
- 02-3
- 【HDU5159】【BestCoderRound26.1002】Card
- Hi3516D 数据手册
- RabbitMQ 在php中的应用
- 关于scanf/printf
- POJ 3094/ ZOJ 2812: Quicksum 【.get()】
- 读刘未鹏《我在南大的七年》
- ZOJ 2478: Encoding 【string】
- ZOJ 1295: Reverse Text - cin.getline()