- include <stdio.h>
include <stdlib.h>
include <MLV/MLV_all.h>
include "grid.c"
include "expression.c"
- define WIDTH 320
define HEIGHT 400
define MAX_EXPR 64
void draw(grid g, char *expr)
{
MLV_clear_window(MLV_COLOR_WHITE);
drawGrid(g);
MLV_draw_text(10, 32, expr, MLV_COLOR_BLACK);
MLV_actualise_window();
}
void update(grid g)
{
int mouseX = 0;
int mouseY = 0;
char expr[MAX_EXPR];
int i = 0;
int op = 0;
int r;
button currentButton;
MLV_Event e;
MLV_Button_state buttonState;
draw(g, "");
do
{
e = MLV_get_event(NULL, NULL, NULL, NULL, NULL, &mouseX, &mouseY, NULL, &buttonState);
if(e == MLV_MOUSE_BUTTON && buttonState == MLV_PRESSED)
{
currentButton = getButtonByClick(mouseX, mouseY, g);
if(i < MAX_EXPR)
{
switch(op)
{
case 0:
{
if(currentButton.label == '+' || currentButton.label == '-' || currentButton.label == 'x' || currentButton.label == '/')
{
op = 1;
}
expr[i] = currentButton.label;
i++;
break;
}
case 1:
{
if(currentButton.label != '+' && currentButton.label != '-' && currentButton.label != 'x' && currentButton.label != '/')
{
expr[i] = currentButton.label;
i++;
}
if(currentButton.label == '=')
{
r = getExpressionResult(parseExpression(expr));
sprintf(expr, "%d", r);
op = 0;
i = strlen(expr);
printf("EXPR : %s\n", expr);
}
break;
}
default:
{
break;
}
}
}
}
draw(g, expr);
}
while(1);
}
int main()
{
const char *title = "[Calculatrice]";
grid g = createGrid(4, 4, 80, "1234567890+-/x=C");
MLV_create_window(title, "", WIDTH, HEIGHT);
update(g);
MLV_free_window();
exit(EXIT_SUCCESS);
}