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
原创粉丝点击