fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define FILENAME "book-6.dat"
  5.  
  6. int goukei(int kurikoshikin, FILE *fp);
  7.  
  8. int main(void)
  9. {
  10. FILE *fp;
  11. int kurikoshikin;
  12. char YesOrNo = 'n';
  13. fp = fopen(FILENAME, "r");
  14.  
  15. if (fp == NULL) {
  16. fprintf(stderr, "%s is not found!\n", FILENAME);
  17. exit(1);
  18. }
  19. do {
  20. printf("Input kurikoshikin > ");
  21. scanf("%d%*c", &kurikoshikin);
  22. printf("OK? ('y' or 'n') > ");
  23. scanf("%c%*c", &YesOrNo);
  24. } while (YesOrNo != 'y');
  25.  
  26. printf("zandaka = %d\n" , goukei(kurikoshikin, fp));
  27. fclose(fp);
  28.  
  29. return 0;
  30. }
  31.  
  32. int goukei(int kurikoshikin, FILE *fp)
  33. {
  34. int sum = 0,tanka,suryou;
  35. char syouhin[50];
  36. char data[60];
  37. int in_out_flag;
  38.  
  39. sum = kurikoshikin;
  40.  
  41. while (fscanf(fp, "%s%d%s%d%d", data, &in_out_flag, syouhin, &tanka, &suryou) == 5) {
  42. if (in_out_flag == 0) {
  43. sum += tanka * suryou;
  44. } else {
  45. sum -= tanka * suryou;
  46. }
  47. }
  48. return sum;
  49. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty