#include <stdio.h>
#include <stdlib.h>
#define FILENAME "book-6.dat"
int goukei(int kurikoshikin, FILE *fp);
int main(void)
{
FILE *fp;
int kurikoshikin;
char YesOrNo = 'n';
fp
= fopen(FILENAME
, "r");
if (fp == NULL) {
fprintf(stderr
, "%s is not found!\n", FILENAME
); }
do {
printf("Input kurikoshikin > "); scanf("%d%*c", &kurikoshikin
); printf("OK? ('y' or 'n') > "); scanf("%c%*c", &YesOrNo
); } while (YesOrNo != 'y');
printf("zandaka = %d\n" , goukei
(kurikoshikin
, fp
));
return 0;
}
int goukei(int kurikoshikin, FILE *fp)
{
int sum = 0,tanka,suryou;
char syouhin[50];
char data[60];
int in_out_flag;
sum = kurikoshikin;
while (fscanf(fp
, "%s%d%s%d%d", data
, &in_out_flag
, syouhin
, &tanka
, &suryou
) == 5) { if (in_out_flag == 0) {
sum += tanka * suryou;
} else {
sum -= tanka * suryou;
}
}
return sum;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCiNkZWZpbmUgRklMRU5BTUUgImJvb2stNi5kYXQiCgppbnQgZ291a2VpKGludCBrdXJpa29zaGlraW4sIEZJTEUgKmZwKTsKCmludCBtYWluKHZvaWQpCnsKCUZJTEUgKmZwOwoJaW50IGt1cmlrb3NoaWtpbjsKCWNoYXIgWWVzT3JObyA9ICduJzsKCWZwID0gZm9wZW4oRklMRU5BTUUsICJyIik7CgoJaWYgKGZwID09IE5VTEwpIHsKCQlmcHJpbnRmKHN0ZGVyciwgIiVzIGlzIG5vdCBmb3VuZCFcbiIsIEZJTEVOQU1FKTsKCQlleGl0KDEpOwoJfQoJZG8gewoJCXByaW50ZigiSW5wdXQga3VyaWtvc2hpa2luID4gIik7CgkJc2NhbmYoIiVkJSpjIiwgJmt1cmlrb3NoaWtpbik7CgkJcHJpbnRmKCJPSz8gKCd5JyBvciAnbicpID4gIik7CgkJc2NhbmYoIiVjJSpjIiwgJlllc09yTm8pOwoJfSB3aGlsZSAo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