add changes to sqlite_ex and some new files

This commit is contained in:
2025-02-27 21:56:12 +02:00
parent 37f9a3d138
commit 547e562f69
6 changed files with 159 additions and 7 deletions

View File

@ -16,8 +16,38 @@ int main(int argc, char **argv) {
return 1;
}
if (argc == 2 && !strcmp(argv[1], "-t")) {
sqlite_list_tables(db);
if (argc == 2) {
if (!strcmp(argv[1], "-t")) {
sqlite_list_tables(db);
} else if (!strcmp(argv[1], "-i")) {
char *ms[] = {"gas", "water", "electricity"};
float ms_num[] = {0, 0, 0};
for (int i = 0; i < 3; ++i) {
printf("Enter %s measurement: ", ms[i]);
scanf("%f", &ms_num[i]);
}
sqlite3_stmt *st;
respCode = sqlite3_prepare_v2(db, INSERT_MEASUREMENT, -1, &st, NULL);
if (respCode != SQLITE_OK) {
fprintf(stderr, "Prepared statement error: %d\n", respCode);
}
sqlite3_bind_double(st, 1, ms_num[0]);
sqlite3_bind_double(st, 2, ms_num[1]);
sqlite3_bind_double(st, 3, ms_num[2]);
respCode = sqlite3_step(st);
if (respCode != SQLITE_DONE) {
fprintf(stderr, "Error while executing statement: %d", respCode);
}
respCode = sqlite3_finalize(st);
if (respCode != SQLITE_OK) {
fprintf(stderr, "Prepared statement finalization error: %d\n",
respCode);
sqlite3_free(zErrMsg);
}
}
} else {
sqlite_exec_query(SELECT_COUNT_MEAS, count_measurements);
measurements = malloc(sizeof(meas) * meas_count);
@ -145,12 +175,28 @@ int dates_same(Datetime *dt1, Datetime *dt2) {
}
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;
double gas_diff = (int)m_now->gas - (int)m_twnth->gas;
double water_diff = (int)m_now->water - (int)m_twnth->water;
double electricity_diff = (int)m_now->electricity - (int)m_twnth->electricity;
return gas_diff * costs.gas_c + water_diff * costs.water_c +
electricity_diff * costs.electricity_c + costs.housing_c;
double gas_total = gas_diff * costs.gas_c;
double water_total = water_diff * costs.water_c;
double electricity_total = electricity_diff * costs.electricity_c;
printf(CALCULATION_FORMAT, "gas", (int)m_now->gas, (int)m_twnth->gas,
gas_diff, costs.gas_c, gas_total);
printf(CALCULATION_FORMAT, "water", (int)m_now->water, (int)m_twnth->water,
water_diff, costs.water_c, water_total);
printf(CALCULATION_FORMAT "\n", "electricity", (int)m_now->electricity,
(int)m_twnth->electricity, electricity_diff, costs.electricity_c,
electricity_total);
double grand_total =
gas_total + water_total + electricity_total + costs.housing_c;
printf("%.2f + %.2f + %.2f + %.2f = %.2f\n", gas_total, water_total,
electricity_total, costs.housing_c, grand_total);
return grand_total;
}
void print_measurements() {