feat: Big num for state representation

This commit is contained in:
snsd0805 2023-06-02 16:52:59 +08:00
parent 821bc5727f
commit 7fcadce548
Signed by: snsd0805
GPG Key ID: 569349933C77A854
2 changed files with 48 additions and 0 deletions

41
bignum.c Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdbool.h>
#include "bignum.h"
struct BigNum long_to_BigNum(long long num) {
struct BigNum ans;
int temp;
for (int i=BIGNUM_LEN-1; i>=0; i--) {
temp = num % 10;
num /= 10;
ans.num[i] = (char)(temp + 48);
}
return ans;
}
struct BigNum add(struct BigNum a, struct BigNum b) {
struct BigNum ans;
bool carry;
short s;
for (short i=BIGNUM_LEN-1; i>=0; i--) {
s = (a.num[i]-48) + (b.num[i]-48) + carry;
carry = s / 10;
s %= 10;
ans.num[i] = (char)(s+48);
}
return ans;
}
struct BigNum mul(struct BigNum a, int b) {
struct BigNum ans;
short s, carry;
for (short i=BIGNUM_LEN-1; i>=0; i--) {
s = (a.num[i]-48) * b + carry;
carry = s / 10;
s %= 10;
ans.num[i] = (char)(s+48);
}
return ans;
}

7
bignum.h Normal file
View File

@ -0,0 +1,7 @@
#include "constant.h"
struct BigNum {
char num[BIGNUM_LEN];
};
struct BigNum long_to_BigNum(long long num);
struct BigNum add(struct BigNum a, struct BigNum b);
struct BigNum mul(struct BigNum a, int b);