Browse Source

Implemented addition of two infints

long_to_char
Cameron Weinfurt 3 years ago
parent
commit
de52e0b99c
  1. 28
      infint.c
  2. 1
      infint.h
  3. 21
      infint_four_func.c
  4. 2
      main.c

28
infint.c

@ -129,12 +129,40 @@ void test_div() {
assert(ii->size == 1);
}
void test_add2() {
infint_t *ii1, *ii2;
ii1 = infint_new(4);
ii2 = infint_new(2);
ii2->data[0] = 75;
ii2->data[1] = 2;
ii2->size = 2;
infint_add2(ii1, ii2);
assert(ii1->data[0] == 75);
assert(ii1->data[1] == 2);
assert(ii1->size == 2);
infint_add2(ii1, ii2);
assert(ii1->data[0] == 50);
assert(ii1->data[1] == 5);
assert(ii1->size == 2);
ii2->data[1] = 94;
infint_add2(ii1, ii2);
assert(ii1->data[0] == 25);
assert(ii1->data[1] == 0);
assert(ii1->data[2] == 1);
assert(ii1->size == 3);
}
void test_infint() {
test_utils();
test_add();
test_sub();
test_mult();
test_div();
test_add2();
}
#endif

1
infint.h

@ -25,6 +25,7 @@ char infint_less_than(infint_t *ii, uint8_t n);
// Four functions
void infint_add(infint_t *ii, uint8_t n);
void infint_add2(infint_t *ii1, infint_t *ii2);
void infint_sub(infint_t *ii, uint8_t n);
void infint_mult(infint_t *ii, uint8_t n);
uint8_t infint_div(infint_t *ii, uint8_t n);

21
infint_four_func.c

@ -63,3 +63,24 @@ uint8_t infint_div(infint_t *ii, uint8_t n) {
while (ii->data[ii->size - 1] == 0) ii->size -= 1;
return acc / 100;
}
void infint_add2(infint_t *ii1, infint_t *ii2) {
if(ii1->sign ^ ii2->sign) {
// Sub2 required first.
} else {
uint8_t acc = 0;
infint_grow_to(ii1, ii2->size);
for(size_t i = 0; i < ii2->size; ++i) {
acc += ii1->data[i];
acc += ii2->data[i];
ii1->data[i] = acc % 100;
acc /= 100;
}
for(size_t i = ii2->size; acc > 0; ++i) {
infint_grow_to(ii1, i + 1);
acc += ii1->data[i];
ii1->data[i] = acc % 100;
acc /= 100;
}
}
}

2
main.c

@ -10,7 +10,7 @@
int main() {
test_infint();
printf("\n**********************\n| All tests passed |\n**********************\n");
printf("**********************\n| All tests passed |\n**********************\n");
}
#else

Loading…
Cancel
Save