From 682bb557af31dd905d13d5f36ab88b1373a5dd6f Mon Sep 17 00:00:00 2001 From: bronku Date: Sun, 12 Oct 2025 23:00:27 +0200 Subject: [PATCH] record type --- TODO.md | 14 ++++++++++++++ record.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 TODO.md create mode 100644 record.h diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..3e4112b --- /dev/null +++ b/TODO.md @@ -0,0 +1,14 @@ +- [ ] file operations + - [ ] write to file (at index) + - [ ] read from file (at index) + - [ ] print_file (maybe just calling cat???) +- [ ] a tape type - representing a file? +- [x] a record type - x1,x2,x3,x4,x5 + - [x] comparing records +- [ ] natural merge sort +- [ ] configuration + - [ ] cmd args - man getopts +- [ ] file input +- [ ] keyboard input +- [ ] testing +- [ ] report diff --git a/record.h b/record.h new file mode 100644 index 0000000..1eec490 --- /dev/null +++ b/record.h @@ -0,0 +1,39 @@ +#pragma once +#include + +typedef struct { + int a[5]; + int x; +} record; + +static inline int read_record(FILE* stream, record* out) +{ + int result = fscanf(stream, "%d %d %d %d %d %d", &out->a[0], &out->a[1], &out->a[2], &out->a[3], &out->a[4], &out->x); + if (result == 6) { + return 0; + } + if (result == EOF) { + return EOF; + } + return -2; +} + +static inline int g(const record* x) +{ + int out = 0; + int x_n = 1; + for (int i = 0; i < 5; i++) { + out += x->a[i] * x_n; + x_n *= x->x; + } + return out; +} + +static inline int compare_records(const void* a, const void* b) +{ + const record* ra = (const record*)a; + const record* rb = (const record*)b; + const int ga = g(ra); + const int gb = g(rb); + return (ga > gb) - (ga < gb); +}