Artificial Intelligence

读取题目要求格式的字符串是个问题。

 1 /* UVa 537 - Artificial Intelligence? */
2 # include <stdio.h>
3 # include <ctype.h>
4 # include <stdlib.h>
5
6 typedef struct {
7 double val;
8 int vis;
9 }phy;
10
11 phy U, I, P, x;
12 int T;
13 char tmp[50];
14
15 void getval(void);
16
17 int main()
18 {
19 int i, c, t, j;
20
21 scanf("%d", &T);
22 getchar();
23 for (i = 1; i <= T; ++i)
24 {
25 U.vis = I.vis = P.vis = 0;
26 printf("Problem #%d\n", i);
27
28 getval();
29
30 if (!U.vis) printf("U=%.2lfV\n", (P.val)/(I.val));
31 else if (!I.vis) printf("I=%.2lfA\n", (P.val)/(U.val));
32 else printf("P=%.2lfW\n", (U.val)*(I.val));
33
34 printf("\n");
35 }
36 return 0;
37 }
38
39 void getval(void)
40 {
41 int j, c, t;
42 phy x;
43
44 while ((c = getchar()) != EOF)
45 {
46 if (c == '\n') return ;
47 if (c == '=')
48 {
49 j = 0;
50 while (isdigit(c = getchar()) || c == '.')
51 tmp[j++] = c;
52 tmp[j] = '\0';
53
54 x.val = atof(tmp);
55 if (c == 'm') x.val /= 1000;
56 else if (c == 'k') x.val *= 1000;
57 else if (c == 'M') x.val *= 1000000;
58
59 x.vis = 1;
60 if (t == 'U') U = x;
61 else if (t == 'I') I = x;
62 else P = x;
63 }
64 t = c;
65 }
66 }

一次 AC,加油!

你可能感兴趣的:(int)