-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht3.c
48 lines (40 loc) · 1.02 KB
/
t3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
const double XRate_US2EUR = 0.5;
const double XRate_US2RMB = 7.0;
int main (void)
{
double usd = 0, result = 0;
char selection = '\0';
int r = 0; // RetVal
puts ("Please input the amount of US dollars:");
r = scanf ("%lf", &usd);
if (r != 1) {
puts ("Illegal number of US dollars.");
return 255;
}
// this stdin very dirty
while ((r = getchar()) && r != '\n' && r != EOF) {}
puts ("Convert to Euro(E) or RMB(R)?");
r = scanf ("%c", &selection);
if (r != 1) {
puts ("Illegal type of conversion.");
return 255;
}
switch (selection) {
case 'E': // fall through
case 'e':
result = usd * XRate_US2EUR;
break;
case 'R': // fall through
case 'r':
result = usd * XRate_US2RMB;
break;
default:
// Not EUR nor RMB
puts ("Wrong type of conversion, exiting.");
return 1;
break;
}
printf ("%g US dollars converts to %g %s.\n", usd, result, (selection == 'E' || selection == 'e') ? "euro" : "yuan");
return 0;
}