From: Jakob Haufe Date: Sat, 4 Nov 2017 23:47:54 +0000 (+0100) Subject: Fix format_euros() X-Git-Tag: rgb2rv17^2 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=3cffefc69ba92ddfdea7353b130f39a7414a703b;p=c128-kasse Fix format_euros() This fixes two related issues: - Total amount reaching 327,67 EUR does no longer result in negativ numbers - Total amount less than 0 EUR gets calculated and displayed correctly (but not across reloads yet) fixes #36 --- diff --git a/include/config.h b/include/config.h index 4dd7fcc..2e2bcd8 100644 --- a/include/config.h +++ b/include/config.h @@ -9,8 +9,8 @@ #define CREDITS_PER_PAGE 10 /* Eingenommes Geld in Cent */ -extern unsigned long int money; -extern unsigned long int items_sold; +extern int32_t money; +extern int32_t items_sold; extern BYTE printer_port; /* Datenstruktur der verkauften Einträge */ diff --git a/include/general.h b/include/general.h index 4b7c99b..0aec044 100644 --- a/include/general.h +++ b/include/general.h @@ -19,7 +19,7 @@ int16_t cget_number(int16_t default_val); void cget_return(void); uint8_t cget_nickname(char *buf, uint8_t len); char retry_or_quit(void); -char *format_euro(char *s, int maxlen, int cent); +char *format_euro(char *s, int maxlen, int32_t cent); void c128_perror(BYTE, char *); extern BYTE _oserror; @@ -66,9 +66,9 @@ extern BYTE _oserror; #define VIDEOMODE (((*(BYTE *)0xD7) == 0x80) ? 80 : 40) -/* "999,99€" */ -#define EUR_FORMAT "%3d,%02d" EURSYM -#define EUR_FORMAT_MINLEN 7 +/* "-999,99€" */ +#define EUR_FORMAT "%3ld,%02lu" EURSYM +#define EUR_FORMAT_MINLEN 8 /* because there is no macro expansion when stringifying, we need to use two * levels of macros to stringify the value of a macro (for example diff --git a/src/config.c b/src/config.c index 6d447a7..0e235f3 100644 --- a/src/config.c +++ b/src/config.c @@ -23,8 +23,8 @@ */ unsigned char __fastcall__ _sysremove(const char *name); -unsigned long int money = 0; -unsigned long int items_sold = 0; +int32_t money = 0; +int32_t items_sold = 0; BYTE printer_port = 4; static bool items_exists = false; static bool credits_exists = false; @@ -88,7 +88,8 @@ void load_items(void) { cbm_load("items", (BYTE)8, &status); for (c = 0; c < status.num_items; c++) { items_sold += status.status[c].times_sold; - money += (status.status[c].price * status.status[c].times_sold); + money += (((int32_t)status.status[c].price) * + ((int32_t)status.status[c].times_sold)); } } else memset(&status, 0, sizeof(struct status_array_t)); diff --git a/src/general.c b/src/general.c index 537c833..10d715d 100644 --- a/src/general.c +++ b/src/general.c @@ -215,8 +215,11 @@ char retry_or_quit(void) { return *c; } -char *format_euro(char *s, int maxlen, int cent) { - if (snprintf(s, maxlen, EUR_FORMAT, cent / 100, cent % 100) > maxlen) +char *format_euro(char *s, int maxlen, int32_t cent) { + int32_t euros, cents; + euros = cent / 100; + cents = abs(cent % 100); + if (snprintf(s, maxlen, EUR_FORMAT, euros, cents) > maxlen) return NULL; return s; } diff --git a/src/kasse.c b/src/kasse.c index d5426c2..f063a97 100644 --- a/src/kasse.c +++ b/src/kasse.c @@ -46,7 +46,7 @@ static void print_screen(void) { clrscr(); if (format_euro(profit, sizeof(profit), money) == NULL) { cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money); - profit[0]='\0'; + profit[0] = '\0'; } textcolor(TC_CYAN); cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r, mxf) " GV "\r\n"); @@ -109,8 +109,8 @@ static void print_screen(void) { * Prints a line and logs it to file. Every line can be at max 80 characters. * */ -static void print_log(char *name, int item_price, int einheiten, char *nickname, - char *rest) { +static void print_log(char *name, int32_t item_price, int16_t einheiten, + char *nickname, char *rest) { char *time = get_time(); uint8_t n; char price[EUR_FORMAT_MINLEN + 1]; @@ -135,9 +135,9 @@ static void print_log(char *name, int item_price, int einheiten, char *nickname, "%8s - " /* Eintragname (= Getränk) -- 9-stellig + 3 */ "%-" xstr(MAX_ITEM_NAME_LENGTH) "s - " - /* Preis (in Cents) -- 7-stellig + 3 */ + /* Preis (in Cents) -- 8-stellig + 3 */ "%" xstr(EUR_FORMAT_MINLEN) "s - " - /* restguthaben (7-stellig) + 3 */ + /* restguthaben (8-stellig) + 3 */ "%" xstr(EUR_FORMAT_MINLEN) "s - " /* Anzahl -- 2-stellig + 3 */ "%2d - " @@ -158,10 +158,10 @@ static void print_log(char *name, int item_price, int einheiten, char *nickname, } /* dialog which is called for each bought item */ -static signed int buy(char *name, unsigned int price) { +static signed int buy(char *name, int32_t price) { BYTE matches = 0; BYTE c, nickname_len; - int einheiten; + int16_t einheiten; char nickname[NICKNAME_MAX_LEN + 1]; char rest[EUR_FORMAT_MINLEN + 1]; struct credits_t *credit; @@ -186,7 +186,7 @@ static signed int buy(char *name, unsigned int price) { * to NULL if no such credit could be found */ credit = find_credit(nickname); if (credit != NULL) { - while ((signed int)credit->credit < ((signed int)price * einheiten)) { + while ((int32_t)credit->credit < (price * einheiten)) { if (format_euro(rest, sizeof(rest), credit->credit) == NULL) { cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);