题目:再一句话里面有P,I,U中的2个已知量,求第三个未知量。(P=I*U)
分析:字符串。利用'='定位已知量,然后将'='后面的的数字和单位分别读入处理。
说明:注意单位有m,k,M的前缀,以及小数点的处理;每组输出后面有一个空行。
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
char Satz[1000];
int main()
{
int n;
while ( ~scanf("%d",&n) ) {
getchar();
for ( int t = 1 ; t <= n ; ++ t ) {
gets(Satz);
double pp = 0.0,ii = 0.0,uu = 0.0;
int P = 0,I = 0,U = 0;
for ( int j,i = 1 ; Satz[i] ; ++ i ) {
if ( Satz[i] == '=' ) {
double value = 0.0;
int flag = 0;
for ( j = i+1 ; Satz[j] ; ++ j ) {
if ( Satz[j] == '.' ) {
flag = 10;continue;
}
if ( Satz[j] < '0' || Satz[j] > '9')
break;
if ( flag ) {
value += (Satz[j]-'0'+0.0)/flag;
flag *= 10;
}else {
value *= 10;
value += (Satz[j]-'0'+0.0);
}
}
if ( Satz[j] == 'm' ) value *= 0.001;
if ( Satz[j] == 'k' ) value *= 1000;
if ( Satz[j] == 'M' ) value *= 1000000;
if ( Satz[i-1] == 'P' ) {
pp = value;P = 1;
}
if ( Satz[i-1] == 'U' ) {
uu = value;U = 1;
}
if ( Satz[i-1] == 'I' ) {
ii = value;I = 1;
}
}
}
printf("Problem #%d\n",t);
if ( P && U ) printf("I=%.2lfA\n\n",pp/uu);
if ( P && I ) printf("U=%.2lfV\n\n",pp/ii);
if ( I && U ) printf("P=%.2lfW\n\n",ii*uu);
}
}
return 0;
}