Added a way to print out instead of stdout but to a file (everything about the process but errors (those still go to stderr) and made it so now someone can actually use echo -e "" | ./bin/stergios (prior this was impossible to do due to readline quirks)
Stergios Upload Compile Checker / Explore-Gitea-Actions (push) Successful in 7s

This commit is contained in:
metallaxis committed 2026-07-05 05:31:32 +03:00
1 parent 030b73597f
commit 49467332de
10 files changed
+191 -63

No files matched your search

+64 -26
View File
@@ -8,8 +8,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
int128_t LIMIT = 0;
@@ -30,7 +28,7 @@ bool is_new_root(int128_t a, int128_t b, int128_t c) {
cur = cur->next;
}
GlobalEntry* e = malloc(sizeof(GlobalEntry));
GlobalEntry* e = malloc(sizeof(*e));
if (!e) return true; /* Fallback to running the solver if malloc fails */
e->a = a; e->b = b; e->c = c;
e->next = global_root_table[h];
@@ -60,7 +58,8 @@ int main() {
int128_t a0 = 10, b0 = 6, c0 = 0, sum = 0;
/* File Specific Settings */
FILE* file = NULL;
FILE* f_input = NULL;
FILE* f_output = stdout;
char* input = NULL;
size_t size = 0;
@@ -68,39 +67,66 @@ int main() {
if (printf("1: manual\n") < 0) return 1;
if (printf("2: auto\n") < 0) return 1;
if (printf("3: Read file\n") < 0) return 1;
if (printf("Choose what mode you want to enter: ") < 0) return 1;
if (scanf(" %i", &mode) != 1) {
fprintf(stderr, "Input error.\n");
return 1;
}
char* line = prompt_line("Choose what mode you want to enter: ");
if (!line) {
fprintf(stderr, "Input error.\n");
return 1;
}
mode = atoi(line);
free(line);
if (mode != 1 && mode != 2 && mode != 3) mode = 0;
}
if (mode == 3) {
char* file_path = NULL;
while (!file_path || *file_path == '\0') {
file_path = readline("Enter file path: ");
file_path = prompt_line("Enter the file from which to take data from: ");
}
int i = 0;
while (file_path[i] != '\0') i++;
if (file_path[i-1] == ' ') file_path[i-1] = '\0';
if (!(file = fopen(file_path, "r"))) {
if (!(f_input = fopen(file_path, "r"))) {
fprintf(stderr, "File not found.\n");
free(file_path);
file_path = NULL;
return 1;
}
free(file_path);
file_path = NULL;
}
if (mode == 2 || mode == 3) {
char* file_path = NULL;
while (!file_path) {
file_path = prompt_line("Enter the file from which write data to (leave empty for console): ");
}
if (*file_path != '\0') {
int i = 0;
while (file_path[i] != '\0') i++;
if (file_path[i-1] == ' ') file_path[i-1] = '\0';
if (!(f_output = fopen(file_path, "w"))) {
fprintf(stderr, "File not found.\n");
free(file_path);
file_path = NULL;
return 1;
}
free(file_path);
file_path = NULL;
}
}
while (print_mode == 0) {
if (printf("1: Print full?\n") < 0) return 1;
if (printf("2: Print minimal?\n") < 0) return 1;
if (printf("Choose what mode you want to enter: ") < 0) return 1;
if (scanf(" %i", &print_mode) != 1) {
fprintf(stderr, "Input error.\n");
return 1;
}
char* line = prompt_line("Choose what mode you want to enter: ");
if (!line) {
fprintf(stderr, "Input error.\n");
return 1;
}
print_mode = atoi(line);
free(line);
if (print_mode != 1 && print_mode != 2) print_mode = 0;
}
@@ -123,23 +149,23 @@ int main() {
int128_t greater = max2(b, c);
LIMIT = a >= greater ? a * greater : b * c;
solve(&sum, a, b, c, print_mode);
solve(&sum, a, b, c, print_mode, f_output);
} else if (mode == 2) {
clear_global_table(); /* Reset unique root tracker */
int32_t p = 7;
int32_t exponents[3] = {2*p + 2, p, p};
int128_t values[3] = {4 * power(a0, p) * power(b0, p), power(a0, p), power(b0, p)};
generate_factor_pairs(&sum, 0, exponents, values, 1, print_mode);
generate_factor_pairs(&sum, 0, exponents, values, 1, print_mode, f_output);
printf("Total sum is: ");
print_int128(sum); printf("\n");
fprintf(f_output, "Total sum is: ");
fprint_int128(f_output, sum); fprintf(f_output, "\n");
} else if (mode == 3) {
clear_global_table(); /* Reset unique root tracker */
if (read_line(file, &input, &size)) return 1;
if (read_line(f_input, &input, &size)) return 1;
int p = atoi(input);
a0 = power(a0, p);
b0 = power(b0, p);
while (!read_line(file, &input, &size)) {
while (!read_line(f_input, &input, &size)) {
c0 = str_to_int128(input);
int128_t gcd = gcd3(a0, b0, c0);
int128_t ra = a0 / gcd, rb = b0 / gcd, rc = c0 / gcd;
@@ -147,13 +173,25 @@ int main() {
if (is_new_root(ra, rb, rc)) {
int128_t greater = max2(rb, rc);
LIMIT = ra >= greater ? ra * greater / 2 : rb * rc / 2;
solve(&sum, ra, rb, rc, print_mode);
solve(&sum, ra, rb, rc, print_mode, f_output);
}
}
printf("Total sum is: ");
print_int128(sum); printf("\n");
fclose(file);
fprintf(f_output, "Total sum is: ");
fprint_int128(f_output, sum); fprintf(f_output, "\n");
if (f_input) {
fclose(f_input);
f_input = NULL;
}
if (f_output && f_output != stdin) {
fclose(f_output);
f_output = NULL;
}
}
if (input) {
free(input);
input = NULL;
}
queue_destroy();