pangfengliu / programmingtasks

programming tasks from my courses
67 stars 17 forks source link

File Addition #356

Open littlehug opened 6 years ago

littlehug commented 6 years ago

Write a program to add two big numbers from two binary files.

Task Description

We are given two binary files, and each file contains a big binary number. The task is to add these two numbers together and write the sum to an output binary file. The first byte of a file has the most significant bits of the number it represents.

Let us illustrate the task with two examples. In the first example, we assume that the two binary files 0.bin and 1.bin contain two binary numbers as the following.

/* Binary File 0.bin */
00001101 01011110 11110101 01100010 01001110 01101100 00011011 01010101 10010101 11001111
/* Binary File 1.bin */
00001100 11100001 10000110 00010011 11001010 11100001 00101010 00101000 10001011 11100010

The two numbers are both 10 bytes. The file position indicator starts from MSB (the most significant bit), so we need to call fseek to reposition the indicator to the last byte first, then we read the two bytes to unsigned character variables and add them. Note that we need to add the numbers from the least significant bits, so we should process the files from the end towards the beginning. Note that there may be a carry after addition, if so, we need to consider it for the next addition. After addition, we also need to call fseek to change the indicator of the output file to the corresponding position and write the sum of the two bytes to the output file. After that, we call fseek to reposition the indicators of input files to the next to last bytes and do addition again. We repeat this process until each byte is calculated.

Note that it is guaranteed that the MSB of the number will not produce a carry when we do addition, so the file length of the output file will be the same as the length of the longer input file.

The sum is as follows:

/*Binary File 0.out*/
00011010 01000000 01111011 01110110 00011001 01001101 01000101 01111110 00100001 10110001

In the second example, we assume that we are given two binary files 2.bin and 3.bin as the following. There are 9 and 5 bytes in these two files respectively. Note that in this example the lengths of the two files are different.

/*Binary File 2.bin*/
00001110 00010111 00011001 01001010 01001111 01100010 00110111 10010110 10000100
/*Binary File 3.bin*/
01111000 10111101 01101111 00001010 00110100
/*Binary File 1.out*/
00001110 00010111 00011001 01001010 11001000 00011111 10100110 10100000 10111000

Again note that it is guaranteed that the MSB of the number will not produce a carry when we do addition, so the file length of the output file will be the same as the length of the longer input file.

Subtask

Sample Input 1

0.bin 1.bin 0.out

Sample Output 1

00011010 01000000 01111011 01110110 00011001 01001101 01000101 01111110 00100001 10110001

Sample Input 2

2.bin 3.bin 1.out

Sample Output 2

00001110 00010111 00011001 01001010 11001000 00011111 10100110 10100000 10111000