Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- #define N 1000
- //char num1[N], num2[N];
- struct BigInteger {
- int mas[N];
- int size;
- bool sign;
- BigInteger(){
- }
- BigInteger(int a) {
- size = a;
- sign = true;
- setZero();
- }
- BigInteger(int digits[], int sizee) {
- for (int i = 0; i <= sizee; i++) {
- mas[i] = digits[i];
- }
- size = sizee;
- }
- BigInteger(string str) {
- size = str.size() - 1;
- setZero();
- for (int i = 0; i <= size; i++) {
- mas[i] = (str.at(size - i) - '0');
- }
- }
- void setZero() {
- for (int i = 0; i < N; i++) {
- mas[i] = 0;
- }
- }
- bool compareNum(BigInteger num) {
- int maxSize = max(size, num.size);
- for (int i = maxSize; i >= 0; i--) {
- if (mas[i] > num.mas[i]) {
- return true;
- }
- if (num.mas[i] > mas[i]) return false;
- }
- return true;
- }
- BigInteger getSum(BigInteger num) {
- int sizeRes;
- int maxSize = max(size, num.size);
- BigInteger res(0);
- for (int i = 0; i <= maxSize; i++) {
- int x = mas[i] + num.mas[i];
- res.mas[i] += x;
- res.mas[i + 1] = res.mas[i]/ 10;
- res.mas[i] %= 10;
- sizeRes = i + (res.mas[i + 1] ? 1 : 0);
- }
- res.size = sizeRes;
- return res;
- }
- void print() {
- if (sign == false) cout << "-";
- for (int i = size; i >= 0; i--) {
- cout << mas[i];
- }
- }
- BigInteger getSubtraction(BigInteger num) {
- int x;
- BigInteger res(0);
- int maxSize = max(size, num.size);
- bool maxNum = compareNum(num);
- if (maxNum == false) res.sign = false;
- for (int i = 0; i <= maxSize; i++) {
- if (maxNum == true) {
- x = mas[i] - num.mas[i];
- }
- else x = num.mas[i] - mas[i];
- if (x < 0) {
- res.mas[i] += 10 + x;
- res.mas[i + 1] -= 1;
- }
- if (x == 0) {
- if (res.mas[i] < 0) {
- res.mas[i] = 10 + res.mas[i];
- res.mas[i + 1] -= 1;
- }
- }
- if (x > 0) {
- res.mas[i] += x;
- }
- }
- int sizeRes = maxSize;
- int i = maxSize;
- while (i > 0) {
- if (res.mas[i] == 0) {
- sizeRes--;
- }
- if (res.mas[i] != 0) break;
- i--;
- }
- res.size = sizeRes;
- return res ;
- }
- };
- int main() {
- string str1, str2;
- cin >> str1 >> str2;
- BigInteger a(str1);
- BigInteger b(str2);
- BigInteger c = a.getSubtraction(b);
- c.print();
- return 0;
- }
Add Comment
Please, Sign In to add comment