54 lines
984 B
C
54 lines
984 B
C
#include <stdio.h>
|
|
#include "int20.h"
|
|
|
|
struct int20 create20(char val[]) {
|
|
// int20 deklarieren
|
|
struct int20 num;
|
|
|
|
// Länge des übergebenen Strings herausfinden. Maximal 20
|
|
int maxIndex = 1;
|
|
for (; maxIndex < 20 && val[maxIndex] != '\0'; maxIndex++);
|
|
|
|
// num.digits mit den übergebenen Ziffern auffüllen, bzw mit '0'
|
|
for (int i = 0; i < 20; i++) {
|
|
if (i < maxIndex) {
|
|
num.digits[i] = val[maxIndex - i - 1];
|
|
} else {
|
|
num.digits[i] = '0';
|
|
}
|
|
}
|
|
|
|
return num;
|
|
}
|
|
|
|
struct int20 add20(struct int20 a, struct int20 b) {
|
|
struct int20 c;
|
|
|
|
int carry = 0;
|
|
for (int i = 0; i < 20; i++) {
|
|
|
|
char cd = a.digits[i] + b.digits[i] - '0' + carry;
|
|
carry = 0;
|
|
|
|
if (cd > '9') {
|
|
cd -= 10;
|
|
carry = 1;
|
|
}
|
|
|
|
c.digits[i] = cd;
|
|
}
|
|
if (carry != 0) {
|
|
printf("INT20 OVERFLOW\n");
|
|
}
|
|
return c;
|
|
}
|
|
|
|
|
|
void print20(struct int20 num) {
|
|
int i = 19;
|
|
for (; i >= 0 && num.digits[i] == '0'; i--);
|
|
if (i < 0) printf("0");
|
|
for (; i >= 0; i--) {
|
|
printf("%c", num.digits[i]);
|
|
}
|
|
} |