add changes to sqlite_ex and some new files
This commit is contained in:
27
cat_v1.c
Normal file
27
cat_v1.c
Normal file
@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
|
||||
void filecopy(FILE *, FILE *);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
FILE *file_p;
|
||||
|
||||
if (argc == 1)
|
||||
filecopy(stdin, stdout);
|
||||
else
|
||||
while (--argc > 0)
|
||||
if ((file_p = fopen(*++argv, "r")) == NULL) {
|
||||
printf("cat: can't open: %s\n", *argv);
|
||||
return 1;
|
||||
} else {
|
||||
filecopy(file_p, stdout);
|
||||
fclose(file_p);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void filecopy(FILE* in_file_p, FILE* out_file_p) {
|
||||
int ch;
|
||||
|
||||
while((ch = getc(in_file_p)) != EOF)
|
||||
putc(ch, out_file_p);
|
||||
}
|
33
cat_v2.c
Normal file
33
cat_v2.c
Normal file
@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void filecopy(FILE *, FILE *);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
FILE *file_p;
|
||||
char *prog_name = argv[0];
|
||||
|
||||
if (argc == 1)
|
||||
filecopy(stdin, stdout);
|
||||
else
|
||||
while (--argc > 0)
|
||||
if ((file_p = fopen(*++argv, "r")) == NULL) {
|
||||
fprintf(stderr, "%s: can't open: %s\n", prog_name, *argv);
|
||||
exit(1);
|
||||
} else {
|
||||
filecopy(file_p, stdout);
|
||||
fclose(file_p);
|
||||
}
|
||||
if (ferror(stdout)) {
|
||||
fprintf(stderr, "%s: error writing stdout\n", prog_name);
|
||||
exit(2);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void filecopy(FILE *in_file_p, FILE *out_file_p) {
|
||||
int ch;
|
||||
|
||||
while ((ch = getc(in_file_p)) != EOF)
|
||||
putc(ch, out_file_p);
|
||||
}
|
35
minprintf.c
Normal file
35
minprintf.c
Normal file
@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
void minprintf(char *fmt, ...) {
|
||||
va_list arg_pointer;
|
||||
char *p, *sval;
|
||||
int ival;
|
||||
double dval;
|
||||
|
||||
va_start(arg_pointer, fmt);
|
||||
for (p = fmt; *p; p++) {
|
||||
if (*p != '%') {
|
||||
putchar(*p);
|
||||
continue;
|
||||
}
|
||||
switch(*++p) {
|
||||
case 'd':
|
||||
ival = va_arg(arg_pointer, int);
|
||||
printf("%d", ival);
|
||||
break;
|
||||
case 'f':
|
||||
dval = va_arg(arg_pointer, double);
|
||||
printf("%f", dval);
|
||||
break;
|
||||
case 's':
|
||||
for(sval = va_arg(arg_pointer, char*); *sval; sval++)
|
||||
putchar(*sval);
|
||||
break;
|
||||
default:
|
||||
putchar(*p);
|
||||
break;
|
||||
}
|
||||
}
|
||||
va_end(arg_pointer);
|
||||
}
|
9
rudimentary_calc.c
Normal file
9
rudimentary_calc.c
Normal file
@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
double sum = 0, val;
|
||||
|
||||
while(scanf("%lf", &val) == 1)
|
||||
printf("\t%.2f\n", sum += val);
|
||||
return 0;
|
||||
}
|
60
sqlite_ex.c
60
sqlite_ex.c
@ -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() {
|
||||
|
@ -11,6 +11,8 @@
|
||||
#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 INSERT_MEASUREMENT "insert into measurements (gas, water, electricity, costs) values (?, ?, ?, 1);"
|
||||
#define CALCULATION_FORMAT "%s: %d - %d = %f * %f = %f\n"
|
||||
#define STR_SIZE 11
|
||||
|
||||
typedef struct tm Datetime;
|
||||
|
Reference in New Issue
Block a user