1 条题解

  • 0
    @ 2025-4-12 21:56:16

    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

    信息

    ID
    2411
    时间
    3000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者