Given the binary file format:
The file contains a sequence of blocks representing fractions.
Each block contains three parts: the first byte represents positive/negative of the fraction; the second part is an int directly dumpped from memory to file and it represents the numerator; the third part is also a direct-dump int representing the denominator.
The three parts are saved in the file consecutively.
The first part: either a positive value or a negative value. A positive value represents non-negative fraction and a negative value represents a negative fraction.
The second and the third part are positive or zero. (assume denominator is always non-zero)
Implement the function that returns the largest fraction in the file. Read the program to figure out the proper signature of the required function.
main c:
/*
Don't modify this file. All the modifications will be discarded.
*/
#include
#include "frac.h"
#include "impl.h"
int main(int argc, char **argv) {
FILE *f;
Frac largest;
if (argc<2) {
f = fopen("3909_2019.dat", "rb");
/*
This loads the default test data.
There are 10 fractions and the largest is 3909/2019.
*/
} else {
f = fopen(argv[1], "rb");
}
largest = find_largest(f);
/* a negative fraction will be printed like -123/456 */
printf("%d/%d\n", largest.n, largest.d);
fclose(f);
return 0;
}
franc.h
#ifndef _frac_h
#define _frac_h
typedef struct _Frac {
int n;
int d;
} Frac;
#endif