Physics teachers in high school often think that problems given as text are more demanding than pure computations. After all, the pupils have to read and understand the problem first!
So they don't state a problem like ''U = 10 V, I = 5A, P =? "But rather like ''you have an electrical circuit that contains a battery with a voltage of U = 10 V and a light-bulb. there's an electrical current of I = 5A through the bulb. which power is generated in the bulb? ".
However, half of the pupils just don't pay attention to the text anyway. they just extract from the text what is given: U = 10 V, I = 5A. then they think: ''which formulae do I know? Ah yes, P = U * I. Therefore P = 10 V * 5A = 500 W. Finished ."
OK, this doesn' t always work, so these pupils are usually not the top scorers in physics tests. but at least this simple algorithm is usually good enough to pass the class. (Sad but true .)
Today we will check if a computer can pass a high school physics test. we will concentrate on the P-U-I type problems first. that means, problems in which two of power, voltage and current are given and the third is wanted.
Your job is to write a program that reads such a text problem and solves it according to the simple algorithm given abve.
Input
The first line of the input file will contain in the number of test cases.
Each test case will consist of one line containing exactly two data fields and some additional arbitrary words. A data field will be of the form I = xA, U = xV or P = xW, where x is a real number.
Directly before the unit (A, V or W) one of the prefixes m (milli), k (kilo) and M (Mega) may also occur. to summarize it: Data fields adhere to the following grammar:
DataField: = Concept '= 'realnumber [Prefix] Unit
Concept: = 'P' | 'U' | 'I'
Prefix: = 'M' | 'K' | 'M'
Unit: = 'W' | 'V' | 'A'
Additional assertions:
The equal sign ('=') will never occur in an other context than within a data field.
There is no whitespace (tabs, blanks) inside a data field.
Either P and U, P and I, or U and I will be given.
Output
For each test case, print three lines:
A line saying ''Problem # k" where k is the number of the test case
A line giving the solution (voltage, power or current, dependent on what was given), written without a prefix and with two decimal places as shown in the sample output
A blank line
Sample Input
3
If the voltage is U = 200 V and the current is I = 4.5A, which power is generated?
A light-bulb yields P = 100 W and the voltage is U = 220 V. Compute the current, please.
Bla lightning strike I = 2A bla P = 2.5 MW bla voltage?
Sample Output
Problem #1
P = 900.00 W
Problem #2
I = 0.45A
Problem #3
U = 1250000.00 V
# Include <stdio. h>
# Include <string. h>
# Define N 1000
Char s [N];
Char f1 [] = "UIP ";
Char f2 [] = "mkM ";
Char f3 [] = "VAW ";
Double sum [3];
Double r [3] = {0.001, 1000.00, 1000000.00 };
Char pre;
Int main ()
{
Int t, j, flag, I, k;
Int count = 1;
Double res;
Scanf ("% d", & t );
Getchar ();
While (t --)
{
Memset (sum, 0, sizeof (sum ));
Gets (s );
For (I = 0; I <strlen (s); I ++)
{
If (s [I + 1] = ')
{
For (j = 0; f1 [j]! = S [I]; j ++)
{
;
}
Sscanf (& s [I] + 2, "% lf % c", & sum [j], & pre );
For (k = 0; k <3; k ++)
{
If (pre = f2 [k])
{
Sum [j] * = r [k];
}
}
}
}
For (I = 0; I <3; I ++)
{
If (sum [I] = 0)
{
Flag = I;
}
}
If (flag = 0)
{
Res = sum [2]/sum [1];
}
Else if (flag = 1)
{
Res = sum [2]/sum [0];
}
Else
{
Res = sum [0] * sum [1];
}
Printf ("Problem # % d \ n % c = %. 2lf % c \ n", count ++, f1 [flag], res, f3 [flag]);
} Return 0;
}
The sscanf () here uses a lot of clever code