P1022 [NOIP2000 普及组] 计算器的改良

 

>>>string -> cin

>>> char-> scanf

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------debug\n");
using namespace std;

int k,b,x,f=1,now=1,r;
char ch,cc;
double ans;

int main()
{
    //ios::sync_with_stdio(false); cin.tie(0);
    
    while(scanf("%c",&ch)&&ch!='\n'&&ch!='\r')//
    {
        
        if(ch=='-'){ b+=f*now*x; f=-1,x=0;r=0;}
        if(ch=='+'){ b+=f*now*x; f=1,x=0;r=0;}
        if(ch=='='){ b+=f*now*x; now=-1,x=0;r=0;f=1;}
        if(ch>='a'&&ch<='z')//
        {
            cc=ch;
            if(r)//avoid  0x----
            {
                k+=f*now*x;
                x=0;
            }
            else k+=f*now,x=0;
            r=0;
        }
        if(ch>='0'&&ch<='9') x=x*10+ch-'0',r=1;//!
    }
    
    b+=f*now*x;
    
    ans=1.0*(b*(-1.0))/k;//
//    ddd
//cout<<b*(-1.0)<<endl<<k<<endl;
    if(ans==-0.000) ans=0;
    printf("%c=%.3lf\n",cc,ans);
    
    return 0;
}
View Code

 

 
posted @ 2023-07-31 12:17  JMXZ  阅读(10)  评论(0)    收藏  举报