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 onP-U-IType 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 formI =XA,U =XVOrP =XW,
WhereXIs a real number.
Directly before the unit (A,VOrW) One of the prefixesM(Milli ),K(Kilo) andM(Mega) may also occur. To summarize it: Data fields adhere to
Following grammar:
DataField ::= Concept '=' RealNumber [Prefix] UnitConcept ::= '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.
- EitherPAndU,PAndI, OrUAndIWill be given.
Output
For each test case, print three lines:
- A line saying''Problem #K"WhereKIs 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
3If the voltage is U=200V and the current is I=4.5A, which power is generated?A light-bulb yields P=100W and the voltage is U=220V. Compute the current, please.bla bla bla lightning strike I=2A bla bla bla P=2.5MW bla bla voltage?
Sample Output
Problem #1P=900.00WProblem #2I=0.45AProblem #3U=1250000.00V
Miguel A. Revilla
2017-01-11
This question is quite easy, just careless WA once, read the wrong question, think m is also a 10 3 release, in fact, it is 10-3 release. The AC code is as follows:
# Include <stdio. h> # include <stdlib. h> # include <string. h> # define MAXN150double foo (char s [], int I) {double f; char t [MAXN]; int j = I, p = 0; while (s [j]! = 'W' & s [j]! = 'V' & s [j]! = 'A')/* Find the Unit */+ + j;/* printf ("\ nj = % d Unit: % c \ n", j, s [j]); */while (I <j & (isdigit (s [I]) | s [I] = '. ') t [p ++] = s [I ++]; t [p] =' \ 0';/* printf ("% s \ n ", t); */f = atof (t);/* printf ("% f \ n", f); */if (I <j) {if (s [I] = 'k') f = f * 10.0*10.0*10.0; else if (s [I] = 'M ') f = f * 10.0*10.0*10.0*10.0*10.0; else if (s [I] = 'M') f = f/10.0/10.0/10.0 ;} /* printf ("% f \ n", f); */return f;} int main (void) {int n, count = 0; char s [MAXN]; /* freopen ("data. in "," r ", stdin); */scanf (" % d ", & n); getchar (); while (n --) {int I, j, t [2]; double P, I, U; count ++; printf ("Problem # % d \ n", count); fgets (s, MAXN, stdin ); for (I = 0, j = 0; I <strlen (s); ++ I) if (s [I] = ') t [j ++] = I;/* printf ("% c \ n", s [t [0]-1], s [t [1]-1]); */P = I = U = 0; switch (s [t [0]-1]) {case 'p ': P = foo (s, t [0] + 1); break; case 'I': I = foo (s, t [0] + 1); break; case 'U': U = foo (s, t [0] + 1); break;} switch (s [t [1]-1]) {case 'p ': P = foo (s, t [1] + 1); break; case 'I': I = foo (s, t [1] + 1); break; case 'U': U = foo (s, t [1] + 1); break ;} /* printf ("P = % f I = % f U = % f \ n", P, I, U); */if (P = 0) printf ("P = %. 2fW \ n ", I * U); else if (I = 0) printf (" I = %. 2fA \ n ", P/U); elseprintf (" U = %. 2fV \ n ", P/I); putchar ('\ n');} return 0 ;}