Created
December 15, 2017 00:23
-
-
Save Pipeliner/a2732318e7a0a8b258aeed92812d5ffb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
struct longNumber { | |
int length; | |
unsigned char *digits; | |
}; | |
longNumber multiply(longNumber a, longNumber b) { | |
longNumber result; | |
result.length = a.length + b.length; | |
result.digits = new unsigned char[result.length]; | |
return result; | |
} | |
void checkMultiplication(longNumber a, longNumber b, longNumber expectedResult) { | |
longNumber actualResult = multiply(a, b); | |
if (actualResult.length != expectedResult.length) { | |
std::cout << "Wrong multiplication result: length mismatch" << std::endl; | |
return; | |
} | |
for (int i = 0; i < actualResult.length; i++) { | |
if (actualResult.digits[i] != expectedResult.digits[i]) { | |
std::cout << "Wrong multiplication result: digit mismatch" << std::endl; | |
return; | |
} | |
} | |
std::cout << "Correct multiplication result" << std::endl; | |
} | |
int main() { | |
longNumber one; | |
one.length = 1; | |
one.digits = new unsigned char[one.length]; | |
one.digits[0] = 1; | |
longNumber one2byte; // = {2, {0, 1}}; | |
one2byte.length = 2; | |
one2byte.digits = new unsigned char[one2byte.length]; | |
one2byte.digits[0] = 0; | |
one2byte.digits[1] = 1; | |
checkMultiplication(one, one, one2byte); // one * one == one2byte | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment