Korea University (KU) and Yonsei University (YU) compete five games every year.
ID: 3905806 • Letter: K
Question
Korea University (KU) and Yonsei University (YU) compete five games every year. In 1993. KU won 2 games and lost 3 games such that KU lost the year. But in 1994, KU won 3 games. tied 1 game, and lost 1 game such that it won the year. From 1993 to 1995, KU won two years and lost just one year. KU is superior than YU in this period. ??? ??? ??? ?? result.txt ??? ???? ??. The results are kept in the file result.txt as follows. 1993 2 0 3 1994 3 1 1 1995 3 1 1 ??? ???? ??-1 ? ??? ?? ????. ??? ???? ?? ?? ???? -1 ? ????. ? ??? ?? ??? ??? ???? ???? ??? ??? ??? ????. ? ??? ?? ???? ? ? ? ??????, ????, ????? ??? ?? ????? ?? ????? ?????. The number -1 at the last line indicates that there are no more records. Therefore, the file without any record will have -1 as its content. This file stores every record in the same format. We assume that there is no format error in the file. Read this file and print the total number of years in which KU won, draw, or lost as shown below. ./a.out Total win: 2 Total draw: 0 Total loss: 1Explanation / Answer
#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int year, win, tie, loss, total_win = 0, total_loss = 0, total_draw = 0;
fp = fopen("Games.txt", "r");
if (!fp)
{
printf("Not able to open file Games.txt ");
return -1;
}
while (fscanf(fp, "%d", &year) != EOF)
{
if (year != -1)
{
fscanf(fp, "%d%d%d", &win, &tie, &loss);
if (win > loss)
{
total_win++;
}
else
{
if(win < loss)
total_loss++;
}
if (total_draw <= 0)
{
if (tie)
{
total_draw+=tie;
}
}
else
{
if (tie)
{
total_draw -= tie;
}
}
}
}
printf("Total win:%d ", total_win);
printf("Total draw:%d ", total_draw);
printf("Total loss:%d ", total_loss);
return 0;
}
--------------------------------------------------------------------------------------------
/*output: with input file as
Gemes.txt
1993 2 0 3
1994 3 1 1
1995 3 1 1
Total win:2
Total Draw:0
Total loss:1
output2: with file has input
Gemes.txt
1993 2 0 3
1994 3 1 1
1995 3 1 1
1996 2 2 1
-1
Total win:3
Total draw:2
Total loss:1
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.