1 solutions
-
0
C :
#include<stdio.h> #include<stdlib.h> char a[505]; double fun() { if(scanf("%s",a)==EOF) return -65535; if(a[0]=='+') return fun()+fun(); if(a[0]=='-') return fun()-fun(); if(a[0]=='*') return fun()*fun(); if(a[0]=='/') return fun()/fun(); return atof(a); } int main() { while(1){ double jieguo=fun(); if(jieguo>-65534) printf("%.2lf\n",jieguo); else break; } return 0; }C++ :
#include <cstdio> #include <cstdlib> #include <stack> #include <cstring> using namespace std; double val(char s, double a, double b){ switch(s){ case '+': return a+b; case '-': return a-b; case '*': return a*b; case '/': return a/b; } } int main(){ int i,dot,len; char s[1200]; double m,x,y; stack<double> a; while(gets(s)){ for(i=strlen(s)-1;i>=0;i--){ while(s[i-1]!=' '&&i>0) i--; if(s[i]<='9'&&s[i]>='0') a.push(atof(&s[i])); else{ x=a.top();a.pop(); y=a.top();a.pop(); a.push(val(s[i],x,y)); } } printf("%.2f\n",a.top()); a.pop(); } return 0; }
- 1
Information
- ID
- 19018
- Time
- 3000ms
- Memory
- 128MiB
- Difficulty
- (None)
- Tags
- # Submissions
- 0
- Accepted
- 0
- Uploaded By