fork download
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAXN 500+2
  4.  
  5. char line1[MAXN];
  6. double num[2] = {0.0, 0.0};
  7. char unit[2] = {'0', '0'};
  8. char prefix[2] = {'0', '0'};
  9.  
  10. void dataProcess(char unit1, double num1, char unit2, double num2)
  11. {
  12. if(unit1 == 'W' && unit2 == 'V')
  13. printf("I=%.2lfA\n", (1.0 * num1) / (1.0 * num2));
  14. else if(unit1 == 'V' && unit2 == 'W')
  15. printf("I=%.2lfA\n", (1.0 * num2) / (1.0 * num1));
  16. else if(unit1 == 'W' && unit2 == 'A')
  17. printf("U=%.2lfV\n", (1.0 * num1) / (1.0 * num2));
  18. else if(unit1 == 'A' && unit2 == 'W')
  19. printf("U=%.2lfV\n", (1.0 * num2) / (1.0 * num1));
  20. else
  21. printf("P=%.2lfW\n", 1.0 * num1 * num2);
  22. }
  23.  
  24. double conversion(double num, char prefix)
  25. {
  26. switch(prefix){
  27. case 'm':
  28. num *= 0.001;
  29. case 'k':
  30. num *= 1000;
  31. case 'M':
  32. num *= 1000000;
  33. }
  34. return num;
  35. }
  36.  
  37. int main()
  38. {
  39. int nTest = 0;
  40. int i = 0, j = 1;
  41. scanf("%d", &nTest);
  42. getchar();
  43. while(nTest--){
  44. i = 0;
  45. memset(num, 0.0, sizeof(num));
  46. memset(unit, 0, sizeof(unit));
  47. memset(prefix, 0.0, sizeof(prefix));
  48.  
  49. fgets(line1, MAXN, stdin);
  50.  
  51. char * pch;
  52. pch = strpbrk (line1, "=");
  53. while (pch != NULL)
  54. {
  55. if(unit[i] == 'm'|'k'|'M'){
  56. sscanf(pch+1, "%lf %[mkM] %[VAW]", &num[i], &prefix[i], &unit[i]);
  57. num[i] = conversion(num[i], prefix[i]);
  58. }
  59. pch = strpbrk (pch+1,"=");
  60. i++;
  61. }
  62.  
  63. printf("Problem #%d\n", j++);
  64. dataProcess(unit[0], num[0], unit[1], num[1]);
  65. if(nTest)
  66. printf("\n");
  67.  
  68. }
  69.  
  70. return 0;
  71. }
  72.  
Success #stdin #stdout 0s 2900KB
stdin
3
If 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?
stdout
Problem #1
P=900.00W

Problem #2
P=22000.00W

Problem #3
P=5000000.00W