Advertisement
cd62131

Shopping

Aug 5th, 2014
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void) {
  4.   const char *data_file = "shopping.dat";
  5.   int money_in_hand, price;
  6.   char line[BUFSIZ]; /* fgets で使用する行バッファ */
  7.   FILE *in = fopen(data_file, "r");
  8.   if (!in) {
  9.     fprintf(stderr, "open error: %s", data_file);
  10.     exit(1);
  11.   }
  12.   fgets(line, BUFSIZ, in); /* 一行目は所持金 */
  13.   sscanf(line, "%d", &money_in_hand);
  14.   while (fgets(line, BUFSIZ, in)) {
  15.     sscanf(line, "%d", &price);
  16.     money_in_hand -= price;
  17.     if (money_in_hand < 0) {
  18.       puts("足りません。");
  19.       fclose(in);
  20.       exit(1);
  21.     }
  22.   }
  23.   puts("足ります。");
  24.   fclose(in);
  25.   return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement