Attempted to separate the snippets of code to small functions and each set of functions have its own file. Also made some improvements by using pools instead of constantly mallocing space.
This commit is contained in:
15 files changed
+561
-289
No files matched your search
@@ -0,0 +1,38 @@
|
||||
#include "io.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int read_int128_triplet(const char* prompt, int128_t* a, int128_t* b, int128_t* c) {
|
||||
char sa[256], sb[256], sc[256];
|
||||
|
||||
if (printf("%s ", prompt) < 0) return -1;
|
||||
|
||||
if (scanf("%255s %255s %255s", sa, sb, sc) != 3) return -2;
|
||||
|
||||
*a = str_to_int128(sa);
|
||||
*b = str_to_int128(sb);
|
||||
*c = str_to_int128(sc);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int128_t str_to_int128(const char* s) {
|
||||
int sign = (*s == '-') ? (s++, -1) : 1;
|
||||
int128_t v = 0;
|
||||
while (*s >= '0' && *s <= '9') v = v * 10 + (*s++ - '0');
|
||||
return sign * v;
|
||||
}
|
||||
|
||||
void print_int128(int128_t n) {
|
||||
if (n == 0) { printf("0"); return; }
|
||||
char buf[40];
|
||||
int i = 0;
|
||||
int neg = n < 0;
|
||||
if (neg) n = -n;
|
||||
while (n > 0) {
|
||||
buf[i++] = '0' + n % 10;
|
||||
n /= 10;
|
||||
}
|
||||
if (neg) putchar('-');
|
||||
while (i--) putchar(buf[i]);
|
||||
}
|
||||
Reference in new issue
Block a user