UVA 537 (暑假-字符串(1) -D - Artificial Intelligence?)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
int kong,i,j,n,p,len,t;
char st[1000],st1[1000];
double I,U,P,sum;
scanf("%d",&n);
gets(st);
for (p=1;p<=n;p++)
{
gets(st);
I=-1;
U=-1;
P=-1;
len=strlen(st); kong=0;
for (i=0;i<=len;i++)
{ 
if ((st[i]=='=') && ( st[i-1]=='U')) kong=1;
            if ((st[i]=='=') && ( st[i-1]=='I')) kong=2;
            if ((st[i]=='=') && ( st[i-1]=='P')) kong=3;
            if (kong!=0) 
{   for (t=0;t<1000;t++) st1[t]=0;
t=-1; 
     for (j=i+1;j<=len;j++)
{
 if (((st[j]>='0') && (st[j]<='9')) ||(st[j]=='.')) { t++; st1[t]=st[j];}
 else break;
 }
 sum=atof(st1);
 if (st[j]=='m') sum=sum*0.001;
 if (st[j]=='k') sum=sum*1000;
 if (st[j]=='M') sum=sum*1000000;
 if (kong==1) U=sum;
 if (kong==2) I=sum;
 if (kong==3) P=sum;
 kong=0;
}
}
if (P==-1)
{
printf("Problem #%d\n",p);
printf("P=%.2fW\n",U*I);
printf("\n");
}
if (I==-1)
{
printf("Problem #%d\n",p);
printf("I=%.2fA\n",P/U);
printf("\n");
}
if (U==-1)
{
printf("Problem #%d\n",p);
printf("U=%.2fV\n",P/I);
printf("\n");
}
}
}

你可能感兴趣的:(UVA 537 (暑假-字符串(1) -D - Artificial Intelligence?))