#include /* Money program */ int main () { double DeutscheMarken, DMRate, /* German currency variables */ Dollars, /* US currency */ Pounds, LBRate, /* British currency */ Francs, FFRate, /* French currency */ Rate, Amount, bucks; /* input variables etc. */ FILE *f; /* file pointer */ char unit, command; int selling; /* true if selling, false if buying */ /* read the rates from the file */ f = fopen ("money-info", "r"); while (!feof (f)) { fscanf (f, "%c %lf %lf\n", &unit, &Rate, &Amount); printf ("%c %lf %lf\n", unit, Rate, Amount); switch (unit) { case 'U': Dollars = Amount; break; case 'D': DMRate = Rate; DeutscheMarken = Amount; break; case 'B': Pounds = Amount; LBRate = Rate; break; case 'F': Francs = Amount; FFRate = Rate; break; default: printf ("huh?\n"); break; } } fclose (f); /* process transactions while not end of file */ while (!feof (stdin)) { scanf ("%c %c %lf", &command, &unit, &Amount); if (command == 'b') selling = 0; else selling = 1; switch (unit) { case 'D': bucks = DMRate * Amount; if (selling) { if (Dollars < bucks) printf ("not enough dollars, sorry.\n"); else { printf ("customer selling %0.2lf German Marks, here is $%0.2lf\n", Amount, bucks); Dollars -= bucks; DeutscheMarken += Amount; } } else { if (DeutscheMarken - Amount < 0) { printf ("not enough German Marks, sorry.\n"); } else { printf ("customer buying %0.2lf German Marks, that will be $%0.2lf, please.\n", Amount, bucks); DeutscheMarken -= Amount; Dollars += bucks; } } break; case 'F': bucks = FFRate * Amount; if (selling) { if (Dollars < bucks) printf ("not enough dollars, sorry.\n"); else { printf ("customer selling %0.2lf French Francs, here is $%0.2lf\n", Amount, bucks); Dollars -= bucks; Francs += Amount; } } else { if (Francs - Amount < 0) { printf ("not enough French Francs, sorry.\n"); } else { printf ("customer buying %0.2lf French Francs, that will be $%0.2lf, please.\n", Amount, bucks); Francs -= Amount; Dollars += bucks; } } break; case 'B': bucks = LBRate * Amount; if (selling) { if (Dollars < bucks) printf ("not enough dollars, sorry.\n"); else { printf ("customer selling %0.2lf British Pounds, here is $%0.2lf\n", Amount, bucks); Dollars -= bucks; Pounds += Amount; } } else { if (Pounds - Amount < 0) { printf ("not enough British Pounds, sorry.\n"); } else { printf ("customer buying %0.2lf British Pounds, that will be $%0.2lf, please.\n", Amount, bucks); Pounds -= Amount; Dollars += bucks; } } break; default: printf ("sorry, we don't sell that.\n"); break; } scanf ("\n"); } /* print ending balances */ printf ("Ending Balances:\n"); printf ("Dollars:\t%0.2lf\n", Dollars); printf ("German Marks:\t%0.2lf\n", DeutscheMarken); printf ("French Francs:\t%0.2lf\n", Francs); printf ("British Pounds:\t%0.2lf\n", Pounds); exit (0); }