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.
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.
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.
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
20 points: The two binary numbers can be read to memory entirely, and the lengths of the two numbers are the same.
20 points: The two binary numbers can be read to memory entirely, and the lengths of the two numbers may be different.
60 points: The two binary numbers cannot be read to memory entirely, and the lengths of the two numbers may be different.
Input Format
The input contains only one test case. There are three strings in a line. The first and the second strings are the filenames of the input files and the third string is the name of the output file.
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.
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:
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.
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
Input Format
The input contains only one test case. There are three strings in a line. The first and the second strings are the filenames of the input files and the third string is the name of the output file.
Output Format
Write the sum to an output binary file.
Sample Input 1
Sample Output 1
Sample Input 2
Sample Output 2