UVa OJ 537

1、scanf不能读\n,要用getchar()消去\n。

2、ch是ASCII码,需要减去‘0’才能正常使用。

3、使用强制类型转换。

 

 

#include <stdio.h>
#include <ctype.h>
int n,ch,pre,i;
double P=0.0,I=0.0,U=0.0;
void trans(int p);
int main(void)
{
    scanf("%d",&n);getchar();
    for(i=1;i<=n;i++)
    {
        pre=getchar();
        while((ch=getchar())!='\n')
        {
            if(ch=='=')
                 trans(pre);
            pre=ch;
        }
        printf("Problem #%d\n",i);
        if(P&&I)
        printf("U=%.2lfV\n\n",P/I);
        if(P&&U)
        printf("I=%.2lfA\n\n",P/U);
        if(I&&U)
        printf("P=%.2lfW\n\n",I*U);
        P=0.0;I=0.0;U=0.0;
    }
    return 0;
}
void trans(int p)
{
    double temp=0.0;
    int ch,count=10;
    while(isdigit(ch=getchar()))
    {
         temp=temp*10+ch-'0';
    }

    if(ch=='.')
        while(isdigit(ch=getchar()))
        {
           temp+=(double)(ch-'0')/count;
           count*=10;
        }
    if(ch=='m') temp/=1000;
    if(ch=='k') temp*=1000;
    if(ch=='M') temp*=1000000;
    switch(p)
    {
        case 'P':P=temp;break;
        case 'I':I=temp;break;
        case 'U':U=temp;break;
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值