按照题意去模拟就行了。
注意逻辑。
代码:
#include <stdio.h>
#include <math.h>
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int ncase;
scanf("%d", &ncase);
getchar();
for (int k = 1; k <= ncase; k++)
{
printf("Problem #%d\n", k);
char s[1024];
fgets(s, sizeof(s), stdin);
double U = 0, I = 0, P = 0;
//bool end = false;
for (int i = 0; s[i] != '\0'; i++)
{
if ((s[i] == 'U' || s[i] == 'I' || s[i] == 'P') && s[i + 1] == '=')
{
int fu = 0;
double shu = 0;
int j;
for (j = i + 2; s[j] != '\0'; j++)
{
if (s[j] == 'A' || s[j] == 'V' || s[j] == 'W')
break;
else if (s[j] == 'm')
shu *= 0.001;
else if (s[j] == 'k')
shu *= 1000;
else if (s[j] == 'M')
shu *= 1000000;
else if (s[j] == '.')
fu = j;
else
{
if (fu == 0)
shu = shu * 10 + (s[j] - '0');
else
shu += (s[j] - '0') * pow(10, fu - j);
}
}
if (s[i] == 'U')
U = shu;
else if (s[i] == 'I')
I = shu;
else
P = shu;
/*if (!end)
{
end = true;
i = j;
}
else
break;*/
}
}
if (U == 0)
printf("U=%.2lfV\n\n",P/I);
else if (P == 0)
printf("P=%.2lfW\n\n",I*U);
else
printf("I=%.2lfA\n\n",P/U);
}
return 0;
}