Skip to content

Commit

Permalink
Added basic integer calculator functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
the-grue authored Dec 10, 2020
1 parent 3b55ee5 commit cfd2883
Showing 1 changed file with 105 additions and 10 deletions.
115 changes: 105 additions & 10 deletions modules/src/calc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,116 @@
#include <string.h>
#include <ctype.h>

#define MAXDIGIT 10
#define NUL_ACTION 0x0
#define ADD_ACTION 0x1
#define SUB_ACTION 0x2
#define MUL_ACTION 0x4
#define DIV_ACTION 0x8
#define EQU_ACTION 0x10

uint32_t w = 302;
uint32_t h = 350;
char buf[256];
long int display = 0;
long int accumulate = 0;
uint32_t action = NUL_ACTION;
uint32_t numdigits = 0;
char buf[MAXDIGIT];
char dispbuf[MAXDIGIT];
button_t* text_field;

void num_clicked(button_t* btn) {
// uint32_t n = strlen(buf);
void clear_buffer(void) {
for (int counter = 0; counter < MAXDIGIT; counter++)
buf[counter] = 0;
buf[0] = '0';
numdigits = 0;
accumulate = 0;
}

void update_buffer(void) {
for (int counter = 0; counter < MAXDIGIT; counter++)
dispbuf[counter] = 0;

if (display)
itoa(display, dispbuf, 10);
else
dispbuf[0] = '0';
}

void clear_vars(void) {
clear_buffer();
display = 0;
action = NUL_ACTION;
}

void take_action(void) {
switch (action) {
case ADD_ACTION:
display = display + accumulate;
break;
case SUB_ACTION:
display = display - accumulate;
break;
case MUL_ACTION:
display = display * accumulate;
break;
case DIV_ACTION:
if (accumulate == 0) {
printf("Division by zero!\n");
clear_vars();
}
else if (display == 0)
display = 0;
else
display = display / accumulate;
break;
case EQU_ACTION:
break;
default:
display = accumulate;
break;
}
update_buffer();
clear_buffer();
}

void num_clicked(button_t* btn) {
switch (btn->text[0]) {
case '=': // TODO: do stuff
case '=':
take_action();
action = EQU_ACTION;
break;
case 'C':
buf[0] = 0;
clear_vars();
update_buffer();
break;
case '+':
take_action();
action = ADD_ACTION;
break;
case '-':
take_action();
action = SUB_ACTION;
break;
case '*':
take_action();
action = MUL_ACTION;
break;
case '/':
take_action();
action = DIV_ACTION;
break;
default:
// strcpy(buf+n, btn->text);
if (numdigits < (MAXDIGIT - 2)) {
buf[numdigits++] = btn->text[0];
accumulate = strtol(buf, NULL, 10);
strcpy(dispbuf, buf);
}
else
printf("Numeric Overflow!\n");
break;
}

// button_set_text(text_field, buf);
strcpy(text_field->text, dispbuf);
}

int main() {
Expand All @@ -36,7 +127,7 @@ int main() {
vbox_t* main_vb = vbox_new();
ui_set_root(app, (widget_t*) main_vb);

button_t* text_field = button_new("TODO");
text_field = button_new(" ");
text_field->widget.flags |= UI_EXPAND_HORIZONTAL;
vbox_add(main_vb, (widget_t*) text_field);

Expand Down Expand Up @@ -116,6 +207,10 @@ int main() {
vbox_add(actions_vb, (widget_t*) action_btn);
}

clear_vars();
update_buffer();
strcpy(text_field->text, dispbuf);

while (true) {
wm_event_t event = snow_get_event(win);

Expand All @@ -130,4 +225,4 @@ int main() {
}

return 0;
}
}

0 comments on commit cfd2883

Please sign in to comment.