From 37f9a3d138cab5829250a9ecb85da84d718af8aeee55cdedfe486c47c709b456 Mon Sep 17 00:00:00 2001 From: dxrknesss Date: Thu, 6 Feb 2025 12:54:34 +0200 Subject: [PATCH] first commit! --- .clangd | 2 + .gitignore | 3 + arrays.c | 21 ++++++ atof.c | 48 ++++++++++++ atoi_v2.c | 27 +++++++ fahrcelcdefine.c | 13 ++++ fahrcelcforloop.c | 9 +++ fahrenheittable.c | 18 +++++ filecopy.c | 17 +++++ helloworld.c | 7 ++ line_count.c | 10 +++ longest_line.c | 38 ++++++++++ longest_line_extern.c | 47 ++++++++++++ power.c | 15 ++++ register.c | 22 ++++++ segfault.c | 3 + shellsort.c | 38 ++++++++++ sqlite_ex.c | 170 ++++++++++++++++++++++++++++++++++++++++++ sqlite_ex.h | 45 +++++++++++ struct_beginning.c | 55 ++++++++++++++ typeconversions.c | 21 ++++++ typeslength.c | 25 +++++++ wc.c | 15 ++++ word_count.c | 24 ++++++ 24 files changed, 693 insertions(+) create mode 100644 .clangd create mode 100644 .gitignore create mode 100644 arrays.c create mode 100644 atof.c create mode 100644 atoi_v2.c create mode 100644 fahrcelcdefine.c create mode 100644 fahrcelcforloop.c create mode 100644 fahrenheittable.c create mode 100644 filecopy.c create mode 100644 helloworld.c create mode 100644 line_count.c create mode 100644 longest_line.c create mode 100644 longest_line_extern.c create mode 100644 power.c create mode 100644 register.c create mode 100644 segfault.c create mode 100644 shellsort.c create mode 100644 sqlite_ex.c create mode 100644 sqlite_ex.h create mode 100644 struct_beginning.c create mode 100644 typeconversions.c create mode 100644 typeslength.c create mode 100644 wc.c create mode 100644 word_count.c diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..1bc5d1d --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-xc, -Wall, -Wextra, -lc] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cec9558 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +* +!*.c +!*.h diff --git a/arrays.c b/arrays.c new file mode 100644 index 0000000..456eeaf --- /dev/null +++ b/arrays.c @@ -0,0 +1,21 @@ +#include + +int main() { + int ch, n_whitesp, n_other; + int n_digit[10] = {0}; /* initialize array with zeroes */ + + n_whitesp = n_other = 0; + + while ((ch = getchar()) != EOF) + if (ch >= '0' && ch <= '9') + ++n_digit[ch - '0']; + else if (ch == ' ' || ch == '\n' || ch == '\t') + ++n_whitesp; + else + ++n_other; + + printf("digits = "); + for (int i = 0; i < 10; ++i) + printf(" %d", n_digit[i]); + printf(", white space = %d, other = %d\n", n_whitesp, n_other); +} diff --git a/atof.c b/atof.c new file mode 100644 index 0000000..1e3f86e --- /dev/null +++ b/atof.c @@ -0,0 +1,48 @@ +#include +#include + +#define MAXLINE 100 + +int getlime(char s[], int lim) +{ + int ch, i; + for (i = 0; i < lim - 1 && (ch = getchar()) != EOF && ch != '\n'; ++i) + s[i] = ch; + if (ch == '\n') { + s[i] = ch; + ++i; + } + s[i] = '\0'; + return i; +} + +double atof(char s[]) { + double val, power; + int ind, sign; + + for (ind = 0; isspace(s[ind]); ++ind) /* skip whitespace */ + ; + sign = (s[ind] == '-') ? -1 : 1; + if (s[ind] == '+' || s[ind] == '-') + ++ind; + + for (val = 0.0; isdigit(s[ind]); ++ind) + val = 10 * val + (s[ind] - '0'); + if(s[ind] == '.') + ++ind; + for (power = 1.0; isdigit(s[ind]); ++ind) { + val = 10.0 * val + (s[ind] - '0'); + power *= 10; + } + return sign * val / power; +} + +int main() { + double sum = 0; + char line[MAXLINE]; + + while (getlime(line, MAXLINE) > 0) + printf("\t%g\n", sum += atof(line)); + + return 0; +} diff --git a/atoi_v2.c b/atoi_v2.c new file mode 100644 index 0000000..fd7ac9a --- /dev/null +++ b/atoi_v2.c @@ -0,0 +1,27 @@ +#include +#include + +int atoi(char string[]) { + int i, sign_ext, res; + + for (i = 0; isspace(string[i]); ++i) + ; + + sign_ext = string[i] == '-' ? -1 : 1; + if (string[i] == '-') + ++i; + + for (res = 0; isdigit(string[i]); ++i) { + res = res * 10 + (string[i] - '0'); + } + + return sign_ext * res; +} + +int main() { + printf("%d\n", atoi("123")); + printf("%d\n", atoi("-123")); + printf("%d\n", atoi("+123")); + printf("%d\n", atoi("0123")); + printf("%d\n", atoi("0123.1")); +} diff --git a/fahrcelcdefine.c b/fahrcelcdefine.c new file mode 100644 index 0000000..36caad5 --- /dev/null +++ b/fahrcelcdefine.c @@ -0,0 +1,13 @@ +#include + +#define LOWER 0 +#define UPPER 300 +#define STEP 20 + +int main() { + int fahr; + + for (fahr = LOWER; fahr <= UPPER; fahr += STEP) { + printf("%3d %6.1f\n", fahr, (5.0/9) * (fahr - 32)); + } +} diff --git a/fahrcelcforloop.c b/fahrcelcforloop.c new file mode 100644 index 0000000..48fe18f --- /dev/null +++ b/fahrcelcforloop.c @@ -0,0 +1,9 @@ +#include + +int main() { + int fahr; + + for (fahr = 0; fahr <= 300; fahr = fahr + 20) { + printf("%3d %6.1f\n", fahr, (5.0/9) * (fahr - 32)); + } +} diff --git a/fahrenheittable.c b/fahrenheittable.c new file mode 100644 index 0000000..d58bc4e --- /dev/null +++ b/fahrenheittable.c @@ -0,0 +1,18 @@ +#include + +int main() { + double fahr, celcius; + double lower, upper, step; + + lower = 0; + upper = 300; + step = 20; + + fahr = lower; + printf("%6c %8c\n", 'F', 'C'); + while (fahr <= upper) { + celcius = 5.0/9 * (fahr - 32); + printf("%6.2f %8.2f\n", fahr, celcius); + fahr = fahr + step; + } +} diff --git a/filecopy.c b/filecopy.c new file mode 100644 index 0000000..c9697c7 --- /dev/null +++ b/filecopy.c @@ -0,0 +1,17 @@ +#include + +int main() { + int character; + + /* + character = getchar(); + while (character != EOF) { + putchar(character); + character = getchar(); + } + * which can be rewritten as... + */ + + while ((character = getchar()) != EOF) + putchar(character); +} diff --git a/helloworld.c b/helloworld.c new file mode 100644 index 0000000..58dd9a2 --- /dev/null +++ b/helloworld.c @@ -0,0 +1,7 @@ +#include + +int main() { + printf("hello, "); + printf("world"); + printf("\n"); +} diff --git a/line_count.c b/line_count.c new file mode 100644 index 0000000..be33505 --- /dev/null +++ b/line_count.c @@ -0,0 +1,10 @@ +#include + +int main() { + int chars, lines, character; + + for (chars = 0, lines = 0; (character = getchar()) != EOF; ++chars) + if (character == '\n') + ++lines; + printf("\n%d lines were inputted\n", lines); +} diff --git a/longest_line.c b/longest_line.c new file mode 100644 index 0000000..8593b12 --- /dev/null +++ b/longest_line.c @@ -0,0 +1,38 @@ +#include +#define MAXLINE 1000 + +int getlime(char line[], int maxline); /* tasty lime! */ +void copy(char to[], char from[]); + +int main() { + int current_len, max_len = 0; + char current_line[MAXLINE]; + char longest[MAXLINE]; + + while ((current_len = getlime(current_line, MAXLINE)) > 0) + if(current_len > max_len) { + max_len = current_len; + copy(longest, current_line); + } + if (max_len > 0) /* there was some line */ + printf("%s", longest); + return 0; +} + +int getlime(char s[], int lim) { + int ch, i; + + for (i = 0; i < lim - 1 && (ch = getchar()) != EOF && ch != '\n'; ++i) + s[i] = ch; + if (ch == '\n') { + s[i] = ch; + ++i; + } + s[i] = '\0'; + return i; +} + +void copy(char to[], char from[]) { + for (int i = 0; (to[i] = from[i]) != '\0'; ++i) + ; +} diff --git a/longest_line_extern.c b/longest_line_extern.c new file mode 100644 index 0000000..77c6bd2 --- /dev/null +++ b/longest_line_extern.c @@ -0,0 +1,47 @@ +#include +#define MAXLINE 1000 + +int max_len; +char current_line[MAXLINE]; +char longest[MAXLINE]; + +int getlime(); +void copy(); + +int main() { + int current_len; + extern int max_len; + extern char longest[]; + + max_len = 0; + while ((current_len = getlime()) > 0) + if (current_len > max_len) { + max_len = current_len; + copy(); + } + if (max_len > 0) /* there was some line */ + printf("%s", longest); + + return 0; +} + +int getlime() { + int ch, i; + extern char current_line[]; + + for (i = 0; i < MAXLINE - 1 && (ch = getchar()) != EOF && ch != '\n'; ++i) + current_line[i] = ch; + if (ch == '\n') { + current_line[i] = ch; + ++i; + } + current_line[i] = '\0'; + return i; +} + +void copy() { + extern char current_line[], longest[]; + + for (int i = 0; (longest[i] = current_line[i]) != '\0'; ++i) + ; +} diff --git a/power.c b/power.c new file mode 100644 index 0000000..41cc63e --- /dev/null +++ b/power.c @@ -0,0 +1,15 @@ +#include + +int power(int m, int n); + +int main() { + for (int i = 0; i < 10; ++i) + printf("%d %d %d\n", i, power(2, i), power(-3, i)); +} + +int power(int base, int n) { + int p = 1; /* 0th power of any number */ + for (int i = 1; i <= n; ++i) + p *= base; + return p; +} diff --git a/register.c b/register.c new file mode 100644 index 0000000..2b0cb86 --- /dev/null +++ b/register.c @@ -0,0 +1,22 @@ +#include +#include + +int main() { + struct timespec t0, t1; + long int i = 0; + register long int x = 0; + + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0); + for (; i < 1000; ++i) + printf("%ld ", i + 1); + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1); + printf("\n%ldns took usual var", (t1.tv_sec - t0.tv_sec) * (long)1e9 + (t1.tv_nsec - t0.tv_nsec)); + printf("\n\n"); + + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t0); + for (; x < 1000; ++x) + printf("%ld ", x + 1); + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &t1); + + printf("\n%ldns took register var", (t1.tv_sec - t0.tv_sec) * (long)1e9 + (t1.tv_nsec - t0.tv_nsec)); +} diff --git a/segfault.c b/segfault.c new file mode 100644 index 0000000..7f4b0d0 --- /dev/null +++ b/segfault.c @@ -0,0 +1,3 @@ +int main() { + *(int*)0 = 0; +} diff --git a/shellsort.c b/shellsort.c new file mode 100644 index 0000000..f5b3de1 --- /dev/null +++ b/shellsort.c @@ -0,0 +1,38 @@ +#include +#include + +#define NUMBERS 1000000 + +void print_arr(int[]); +void shellsort(int[], int); + +void shellsort(int arr[], int n) { + int temp; + + for (int gap = n / 2; gap > 0; gap /= 2) + for (int i = gap; i < n; ++i) + for (int j = i - gap; j >= 0 && arr[j] > arr[j + gap]; j -= gap) { + temp = arr[j]; + arr[j] = arr[j + gap]; + arr[j + gap] = temp; + } +} + +int main() { + int ind, numbers[NUMBERS]; + for (ind = NUMBERS; ind > 0; --ind) { + numbers[ind] = rand(); + } + + shellsort(numbers, NUMBERS); + // print_arr(numbers); + return 0; +} + +void print_arr(int numbers[]) { + for (int ind = 0; ind < NUMBERS; ++ind) { + if (ind >= 10 && ind % 10 == 0) + printf("\n"); + printf("%d ", numbers[ind]); + } +} diff --git a/sqlite_ex.c b/sqlite_ex.c new file mode 100644 index 0000000..281f6ea --- /dev/null +++ b/sqlite_ex.c @@ -0,0 +1,170 @@ +#include "sqlite_ex.h" + +static char date_string[STR_SIZE]; +static meas *measurements; +static int meas_count; +static Datetime twnth_day; +static Cost costs; +static char *zErrMsg = NULL; +static sqlite3 *db; + +int main(int argc, char **argv) { + int respCode; + if ((respCode = sqlite3_open("communal.db", &db))) { + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); + sqlite3_close(db); + return 1; + } + + if (argc == 2 && !strcmp(argv[1], "-t")) { + sqlite_list_tables(db); + } else { + sqlite_exec_query(SELECT_COUNT_MEAS, count_measurements); + measurements = malloc(sizeof(meas) * meas_count); + + sqlite_exec_query(SELECT_MEASUREMENTS, parse_measurements); + measurements -= meas_count; // bring it back to the start of array + + sqlite_exec_query(SELECT_TWNTH_DAY, select_twnth_day); + + sqlite3_stmt *st; + respCode = sqlite3_prepare_v2(db, SELECT_CURRENT_COSTS, -1, &st, NULL); + if (respCode != SQLITE_OK) { + fprintf(stderr, "Prepared statement error: %d\n", respCode); + } + sqlite3_bind_int(st, 1, measurements[meas_count - 1].cost_id); + respCode = sqlite3_step(st); + if (respCode == SQLITE_ROW) { + int n_cols = sqlite3_column_count(st); + for (int col_ind = 0; col_ind < n_cols; ++col_ind) { + const char *col_name = sqlite3_column_name(st, col_ind); + if (!strcmp(col_name, "gas_cost")) { + costs.gas_c = sqlite3_column_double(st, col_ind); + } else if (!strcmp(col_name, "water_cost")) { + costs.water_c = sqlite3_column_double(st, col_ind); + } else if (!strcmp(col_name, "electricity_cost")) { + costs.electricity_c = sqlite3_column_double(st, col_ind); + } else if (!strcmp(col_name, "other_cost")) { + costs.housing_c = sqlite3_column_double(st, col_ind); + } + } + } + respCode = sqlite3_finalize(st); + if (respCode != SQLITE_OK) { + fprintf(stderr, "Prepared statement finalization error: %d\n", respCode); + sqlite3_free(zErrMsg); + } + + print_measurements(); + + for (int i = meas_count - 1; i >= 0; --i) { + Datetime *dt = measurements[i].datetime; + if (dates_same(dt, &twnth_day)) { + if (i == meas_count - 1) { + printf("No measurements were made since last time!\n"); + goto free; + } + double total = communal_difference(&measurements[meas_count - 1], + &measurements[i]); + strftime(date_string, STR_SIZE, "%x", + measurements[meas_count - 1].datetime); + printf("date of last measurement: %s\n", date_string); + printf("total: %f\n", total); + } + } + + free: + free_measurements(); + } + + sqlite3_close(db); + return 0; +} + +void sqlite_exec_query(char *query, void *callback) { + int respCode = sqlite3_exec(db, query, callback, 0, &zErrMsg); + if (respCode != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + } +} + +int count_measurements(void *_, int n_cols, char **row_vals, char **cols) { + meas_count = atoi(row_vals[0]); + return 0; +} + +int parse_measurements(void *_, int n_cols, char **row_vals, char **cols) { + meas measurement; + + for (int col = 0; col < n_cols; ++col) { + if (!strcmp(cols[col], "gas")) { + measurement.gas = atof(row_vals[col]); + } else if (!strcmp(cols[col], "water")) { + measurement.water = atof(row_vals[col]); + } else if (!strcmp(cols[col], "electricity")) { + measurement.electricity = atof(row_vals[col]); + } else if (!strcmp(cols[col], "date")) { + Datetime *dt = malloc(sizeof(Datetime)); + strptime(row_vals[col], "%F", dt); + measurement.datetime = dt; + } else if (!strcmp(cols[col], "costs")) { + measurement.cost_id = atoi(row_vals[col]); + } + } + *measurements++ = measurement; + + return 0; +} + +void sqlite_list_tables(sqlite3 *db) { + char *errMsg = NULL; + int respCode = sqlite3_exec(db, SELECT_ALL_TABLES, print_tables, 0, &errMsg); + if (respCode != SQLITE_OK) { + fprintf(stderr, "SQL error: %s\n", errMsg); + sqlite3_free(errMsg); + } +} + +int print_tables(void *_, int rows_n, char **rows, char **cols) { + for (int i = 0; i < rows_n; ++i) { + printf("%s = %s\n", cols[i], rows[i] ? rows[i] : "NULL"); + } + printf("\n"); + return 0; +} + +int select_twnth_day(void *_, int n_cols, char **row_vals, char **cols) { + strptime(row_vals[0], "%F", &twnth_day); + return 0; +} + +int dates_same(Datetime *dt1, Datetime *dt2) { + return dt1->tm_year == dt2->tm_year && dt1->tm_mon == dt2->tm_mon && + dt1->tm_mday == dt2->tm_mday; +} + +double communal_difference(meas *m_now, meas *m_twnth) { + double gas_diff = m_now->gas - m_twnth->gas; + double water_diff = m_now->water - m_twnth->water; + double electricity_diff = m_now->electricity - m_twnth->electricity; + + return gas_diff * costs.gas_c + water_diff * costs.water_c + + electricity_diff * costs.electricity_c + costs.housing_c; +} + +void print_measurements() { + for (int i = 0; i < meas_count; ++i) { + meas m = measurements[i]; + strftime(date_string, STR_SIZE, "%x", m.datetime); + printf("%-15s%11s\n%-15s%11f\n%-15s%11f\n%-15s%11f\n\n", + "date:", date_string, "gas:", m.gas, "water:", m.water, + "electricity:", m.electricity); + } +} + +void free_measurements() { + for (int i = 0; i < meas_count; ++i) + free(measurements[i].datetime); + free(measurements); +} diff --git a/sqlite_ex.h b/sqlite_ex.h new file mode 100644 index 0000000..5b244ef --- /dev/null +++ b/sqlite_ex.h @@ -0,0 +1,45 @@ +#define _GNU_SOURCE 1 + +#include +#include +#include +#include +#include + +#define SELECT_MEASUREMENTS "select * from measurements order by date;" +#define SELECT_COUNT_MEAS "select count(*) from measurements;" +#define SELECT_ALL_TABLES "select * from sqlite_schema where type = 'table';" +#define SELECT_TWNTH_DAY "select * from twnth_day;" +#define SELECT_CURRENT_COSTS "select * from costs where id = ?;" +#define STR_SIZE 11 + +typedef struct tm Datetime; + +typedef struct { + Datetime *datetime; + /* NOTE: probably should change to union type? + * (to hold both double and int representation, to count the integer cost) + */ + double gas; + double water; + double electricity; + int cost_id; +} meas; + +typedef struct { + double gas_c; + double water_c; + double electricity_c; + double housing_c; +} Cost; + +int print_tables(void *, int, char **, char **); +void print_measurements(); +void free_measurements(); +int dates_same(Datetime *, Datetime *); +double communal_difference(meas *, meas *); +int parse_measurements(void *, int, char **, char **); +int count_measurements(void *, int, char **, char **); +int select_twnth_day(void *, int, char **, char **); +void sqlite_list_tables(sqlite3 *db); +void sqlite_exec_query(char *query, void *callback); diff --git a/struct_beginning.c b/struct_beginning.c new file mode 100644 index 0000000..4c43b9a --- /dev/null +++ b/struct_beginning.c @@ -0,0 +1,55 @@ +#include +#include +struct point { + int x; + int y; +}; + +struct { + int x; + int y; +} point2; + +struct point3 pt; + +struct point3 { + int x; + int y; +}; + +struct rect { + struct point pt1; + struct point pt2; +}; + +struct point makepoint(int, int); +struct point *makepoint_p(int, int); + +int main() { + struct point p = {2, 3}; + printf("x: %d, y: %d\n", p.x, p.y); + + struct rect r1 = {{2, 3}, {3, 4}}; + printf("pt1.x: %d, pt1.y: %d, pt2.x: %d, pt2.y: %d\n", r1.pt1.x, r1.pt1.y, + r1.pt2.x, r1.pt2.y); + + struct point p2 = makepoint(3, 4); + printf("x: %d, y: %d\n", p2.x, p2.y); + + struct point *p3 = makepoint_p(3, 4); + printf("x: %d, y: %d\n", p3->x, p3->y); + free(p3); +} + +struct point makepoint(int x, int y) { + struct point p = {x, y}; + return p; +} + +struct point *makepoint_p(int x, int y) { + struct point *p = malloc(sizeof(struct point)); + p->x = x; + p->y = y; + + return p; +} diff --git a/typeconversions.c b/typeconversions.c new file mode 100644 index 0000000..c253d00 --- /dev/null +++ b/typeconversions.c @@ -0,0 +1,21 @@ +#include +#include + +int atoi(char s[]); + +int main() { + printf("%d\n", atoi("234")); +} + +int atoi(char string[]) { + int n = 0; + /* + * v1 + for (int i = 0; string[i] >= '0' && string[i] <= '9'; ++i) + n = 10 * n + (string[i] - '0'); + */ + for (int i = 0; isdigit(string[i]); ++i) + n = 10 * n + (string[i] - '0'); + + return n; +} diff --git a/typeslength.c b/typeslength.c new file mode 100644 index 0000000..23dfa52 --- /dev/null +++ b/typeslength.c @@ -0,0 +1,25 @@ +#include +#include + +enum boolean { NO, YES }; + +int main() { + printf("\nINTEGER TYPES:\n"); + printf("%-20s%ld bytes wide\n", "char: ", sizeof(char)); + printf("%-20s%ld bytes wide\n", "int: ", sizeof(int)); + printf("%-20s%ld bytes wide\n", "short int: ", sizeof(short int)); + printf("%-20s%ld bytes wide\n", "long: ", sizeof(long)); + printf("%-20s%ld bytes wide\n", "long int: ", sizeof(long int)); + printf("%-20s%ld bytes wide\n", "long long int: ", sizeof(long long int)); + + printf("\nFLOATING POINT TYPES:\n"); + printf("%-20s%ld bytes wide\n", "float: ", sizeof(float)); + printf("%-20s%ld bytes wide\n", "double: ", sizeof(double)); + printf("%-20s%ld bytes wide\n", "long double: ", sizeof(long double)); + +#if defined(__STDC_VERSION__) + printf("\n%ld\n", __STDC_VERSION__); +#endif + printf("\nseveral " "string " "constants.\n"); + return 0; +} diff --git a/wc.c b/wc.c new file mode 100644 index 0000000..a70c4c6 --- /dev/null +++ b/wc.c @@ -0,0 +1,15 @@ +#include + +int main() { + /* + long num_chars = 0; + + while (getchar() != EOF) + ++num_chars; + * or.. + */ + long num_chars; + + for (num_chars = 0; getchar() != EOF; ++num_chars); + printf("%ld\n", num_chars); +} diff --git a/word_count.c b/word_count.c new file mode 100644 index 0000000..3489599 --- /dev/null +++ b/word_count.c @@ -0,0 +1,24 @@ +#include + +#define IN 1 +#define OUT 0 + +int main() { + int character, lines, words, chars, state; + + state = OUT; + lines = words = chars = 0; + while ((character = getchar()) != EOF) { + ++chars; + if (character == '\n') + ++lines; + if (character == ' ' || character == '\n' || character == '\t') + state = OUT; + else if (state == OUT) { + state = IN; + ++words; + } + } + + printf("\n%d %d %d\n", lines, words, chars); +}