pemdas-calculator/gtk-calc.c

84 lines
2.3 KiB
C

#include <gtk/gtk.h>
#include "calc.h"
/*
Tesses Calculator a PEMDAS calculator (with mod)
Copyright (C) 2023 Mike Nolan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
GtkWidget* number;
void truncatezeros(char* c)
{
size_t pos = strlen(c) - 1;
while(pos >= 0 && c[pos] == '0' )
{
c[pos] = 0;
pos--;
if(pos >= 0 && c[pos] == '.')
{
c[pos] = 0;
}
}
}
void calculate_answer(GtkWidget* calculate,gpointer data)
{
const char* text= gtk_entry_get_text(GTK_ENTRY(number));
bool error=false;
double val=calc(text,&error);
char myNumber[1000];
if(error)
{
snprintf(myNumber,1000,"error");
}
else
{
snprintf(myNumber,1000,"%f",val);
truncatezeros(myNumber);
}
gtk_entry_set_text(GTK_ENTRY(number),myNumber);
}
int
main (int argc,
char **argv)
{
GtkWidget* window;
GtkWidget* grid;
GtkWidget* calculate;
gtk_init(&argc,&argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);
grid = gtk_grid_new();
gtk_container_add(GTK_CONTAINER(window),grid);
number = gtk_entry_new();
gtk_grid_attach(GTK_GRID(grid),number,0,0,1,1);
calculate = gtk_button_new_with_label("Calculate");
gtk_widget_set_hexpand (calculate, TRUE);
gtk_widget_set_halign (calculate, GTK_ALIGN_CENTER);
g_signal_connect(calculate,"clicked",G_CALLBACK(calculate_answer),NULL);
gtk_grid_attach(GTK_GRID(grid),calculate,0,1,1,1);
gtk_window_set_title(GTK_WINDOW(window),"Tesses Calculator");
gtk_window_set_default_size(GTK_WINDOW(window),320,-1);
gtk_widget_show_all(window);
gtk_main();
return 0;
}