Posledni stabilni verze by Michal Rysavy.
[BETON.git] / main.c
blob68147369cb0523e3ebda80372828393ee174f323
1 /**
2 * @file main.c
4 * @author Michal Rysavy
5 * @brief Hlavni logika kalkulacky a volani GTK+
6 */
7 #include <gtk/gtk.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <limits.h>
11 #include "knihovna.h"
13 /* location of UI XML file relative to path in which program is running */
14 #define BUILDER_XML_FILE "kal.glade"
16 /**
17 * struktura s ukazateli na jednotlive widgety v aplikaci
19 typedef struct
21 GtkWidget *window;
22 GtkWidget *button0;
23 GtkWidget *button1;
24 GtkWidget *button2;
25 GtkWidget *button3;
26 GtkWidget *button4;
27 GtkWidget *button5;
28 GtkWidget *button6;
29 GtkWidget *button7;
30 GtkWidget *button8;
31 GtkWidget *button9;
32 GtkWidget *button10;
33 GtkWidget *button11;
34 GtkWidget *button12;
35 GtkWidget *button13;
36 GtkWidget *button14;
37 GtkWidget *button15;
38 GtkWidget *button16;
39 GtkWidget *button17;
40 GtkWidget *button18;
41 GtkWidget *button19;
42 GtkLabel *label1;
43 } App;
45 /** deklarace globalnich promenych pro vysledky kalkulacky
47 Tvysledek hodnota1;
48 Tvysledek hodnota2;
50 /**
51 * udaj o tom, co se ma delat
52 * 0 - nic, nebylo zadano
53 * 1 - scitani
54 * 2 - odcitani
55 * 3 - nasobeni
56 * 4 - deleni
57 * 5 - umocneni
58 * 6 - logaritmus
59 * 7 - faktorial
61 int operace=0;
62 int desetina=0; /**< pri hodnote 1 se už tvori desetina cisla */
63 int cislic=0;
64 int omezeni=8; /**< omezeni kolik maximalne muze uzivatel zadat cislic na jedno cislo */
66 /**
67 * handler, ktery je v glade prirazen stisknuti tlacitka +
69 void on_button0_clicked (GtkObject *object, App *app)
71 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
72 operace=1;
73 cislic=0;
74 desetina=0;
76 else{ //uz bylo vybrano a tak se to pouzije...
77 //zde by mela byt moznost scitani
81 /**
82 * handler, ktery je v glade prirazen stisknuti tlacitka -
84 void on_button1_clicked (GtkObject *object, App *app)
86 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
87 operace=2;
88 cislic=0;
89 desetina=0;
91 else{ //uz bylo vybrano a tak se to pouzije...
92 //zde by mela byt moznost scitani
96 /**
97 * handler, ktery je v glade prirazen stisknuti tlacitka *
99 void on_button2_clicked (GtkObject *object, App *app)
101 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
102 operace=3;
103 cislic=0;
104 desetina=0;
106 else{ //uz bylo vybrano a tak se to pouzije...
107 //zde by mela byt moznost scitani
112 * handler, ktery je v glade prirazen stisknuti tlacitka :
114 void on_button3_clicked (GtkObject *object, App *app)
116 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
117 operace=4;
118 cislic=0;
119 desetina=0;
121 else{ //uz bylo vybrano a tak se to pouzije...
122 //zde by mela byt moznost scitani
127 * handler, ktery je v glade prirazen stisknuti tlacitka umocneni
129 void on_button4_clicked (GtkObject *object, App *app)
131 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
132 operace=5;
133 cislic=0;
134 desetina=0;
136 else{ //uz bylo vybrano a tak se to pouzije...
137 //zde by mela byt moznost scitani
142 * handler, ktery je v glade prirazen stisknuti tlacitka log a rovnou vypocita
144 void on_button5_clicked (GtkObject *object, App *app)
146 hodnota1=logaritmus(hodnota1.result);
147 if (hodnota1.error!=0){
148 gtk_label_set_text(app->label1,"ERROR");
149 operace=0;
150 desetina=0;
151 hodnota1.result=0;
152 hodnota2.result=0;
153 cislic=0;
155 else{
156 char* x = malloc(sizeof(char)*15);
157 sprintf(x,"%.5f",hodnota1.result);
158 gtk_label_set_text(app->label1,x);
159 hodnota2.result=0;
160 operace=0; //uz nebude mozne zadavat hodnotu 1
161 cislic=8;
162 desetina=1;
163 free(x);
168 * handler, ktery je v glade prirazen stisknuti tlacitka faktorial a rovnou vypocita
170 void on_button6_clicked (GtkObject *object, App *app)
172 hodnota1=faktorial(hodnota1.result);
173 if (hodnota1.error!=0){
174 gtk_label_set_text(app->label1,"ERROR");
175 operace=0;
176 desetina=0;
177 hodnota1.result=0;
178 hodnota2.result=0;
179 cislic=0;
181 else{
182 char* x = malloc(sizeof(char)*15);
183 sprintf(x,"%.5f",hodnota1.result);
184 gtk_label_set_text(app->label1,x);
185 hodnota2.result=0;
186 operace=0; //uz nebude mozne zadavat hodnotu 1
187 cislic=8;
188 desetina=1;
189 free(x);
193 * handler, ktery vynuluje kalkulacku
195 void on_button7_clicked (GtkObject *object, App *app)
197 operace=0;
198 desetina=0;
199 hodnota1.result=0;
200 hodnota1.error=0;
201 hodnota2.result=0;
202 hodnota2.error=0;
203 cislic=0;
204 gtk_label_set_text(app->label1,"Kalkulacka BETON");
208 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
210 void on_button8_clicked (GtkObject *object, App *app)
212 if (cislic<omezeni){
213 cislic++;
214 char* x = malloc(sizeof(char)*15);
215 if (operace==0) {
216 if (desetina==0){
217 hodnota1.result=hodnota1.result * 10 + 1;
219 else{
220 hodnota1.result=hodnota1.result + (1.0/pow(10,desetina));
221 desetina++;
223 sprintf(x,"%.5f",hodnota1.result);
224 gtk_label_set_text(app->label1,x);
226 else{
227 if (desetina==0){
228 hodnota2.result=hodnota2.result * 10 + 1;
230 else{
231 hodnota2.result=hodnota2.result + (1.0/pow(10,desetina));
232 desetina++;
234 sprintf(x,"%.5f",hodnota2.result);
235 gtk_label_set_text(app->label1,x);
237 free(x);
241 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
243 void on_button9_clicked (GtkObject *object, App *app)
245 if (cislic<omezeni){
246 cislic++;
247 char* x = malloc(sizeof(char)*15);
248 if (operace==0) {
249 if (desetina==0){
250 hodnota1.result=hodnota1.result * 10 + 2;
252 else{
253 hodnota1.result=hodnota1.result + (2.0/pow(10,desetina));
254 desetina++;
256 sprintf(x,"%.5f",hodnota1.result);
257 gtk_label_set_text(app->label1,x);
259 else{
260 if (desetina==0){
261 hodnota2.result=hodnota2.result * 10 + 2;
263 else{
264 hodnota2.result=hodnota2.result + (2.0/pow(10,desetina));
265 desetina++;
267 sprintf(x,"%.5f",hodnota2.result);
268 gtk_label_set_text(app->label1,x);
270 free(x);
274 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
276 void on_button10_clicked (GtkObject *object, App *app)
278 if (cislic<omezeni){
279 cislic++;
280 char* x = malloc(sizeof(char)*15);
281 if (operace==0) {
282 if (desetina==0){
283 hodnota1.result=hodnota1.result * 10 + 3;
285 else{
286 hodnota1.result=hodnota1.result + (3.0/pow(10,desetina));
287 desetina++;
289 sprintf(x,"%.5f",hodnota1.result);
290 gtk_label_set_text(app->label1,x);
292 else{
293 if (desetina==0){
294 hodnota2.result=hodnota2.result * 10 + 3;
296 else{
297 hodnota2.result=hodnota2.result + (3.0/pow(10,desetina));
298 desetina++;
300 sprintf(x,"%.5f",hodnota2.result);
301 gtk_label_set_text(app->label1,x);
303 free(x);
307 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
309 void on_button11_clicked (GtkObject *object, App *app)
311 if (cislic<omezeni){
312 cislic++;
313 char* x = malloc(sizeof(char)*15);
314 if (operace==0) {
315 if (desetina==0){
316 hodnota1.result=hodnota1.result * 10 + 4;
318 else{
319 hodnota1.result=hodnota1.result + (4.0/pow(10,desetina));
320 desetina++;
322 sprintf(x,"%.5f",hodnota1.result);
323 gtk_label_set_text(app->label1,x);
325 else{
326 if (desetina==0){
327 hodnota2.result=hodnota2.result * 10 + 4;
329 else{
330 hodnota2.result=hodnota2.result + (4.0/pow(10,desetina));
331 desetina++;
333 sprintf(x,"%.5f",hodnota2.result);
334 gtk_label_set_text(app->label1,x);
336 free(x);
340 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
342 void on_button12_clicked (GtkObject *object, App *app)
344 if (cislic<omezeni){
345 cislic++;
346 char* x = malloc(sizeof(char)*15);
347 if (operace==0) {
348 if (desetina==0){
349 hodnota1.result=hodnota1.result * 10 + 5;
351 else{
352 hodnota1.result=hodnota1.result + (5.0/pow(10,desetina));
353 desetina++;
355 sprintf(x,"%.5f",hodnota1.result);
356 gtk_label_set_text(app->label1,x);
358 else{
359 if (desetina==0){
360 hodnota2.result=hodnota2.result * 10 + 5;
362 else{
363 hodnota2.result=hodnota2.result + (5.0/pow(10,desetina));
364 desetina++;
366 sprintf(x,"%.5f",hodnota2.result);
367 gtk_label_set_text(app->label1,x);
369 free(x);
373 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
375 void on_button13_clicked (GtkObject *object, App *app)
377 if (cislic<omezeni){
378 cislic++;
379 char* x = malloc(sizeof(char)*15);
380 if (operace==0) {
381 if (desetina==0){
382 hodnota1.result=hodnota1.result * 10 + 6;
384 else{
385 hodnota1.result=hodnota1.result + (6.0/pow(10,desetina));
386 desetina++;
388 sprintf(x,"%.5f",hodnota1.result);
389 gtk_label_set_text(app->label1,x);
391 else{
392 if (desetina==0){
393 hodnota2.result=hodnota2.result * 10 + 6;
395 else{
396 hodnota2.result=hodnota2.result + (6.0/pow(10,desetina));
397 desetina++;
399 sprintf(x,"%.5f",hodnota2.result);
400 gtk_label_set_text(app->label1,x);
402 free(x);
406 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
408 void on_button14_clicked (GtkObject *object, App *app)
410 if (cislic<omezeni){
411 cislic++;
412 char* x = malloc(sizeof(char)*15);
413 if (operace==0) {
414 if (desetina==0){
415 hodnota1.result=hodnota1.result * 10 + 7;
417 else{
418 hodnota1.result=hodnota1.result + (7.0/pow(10,desetina));
419 desetina++;
421 sprintf(x,"%.5f",hodnota1.result);
422 gtk_label_set_text(app->label1,x);
424 else{
425 if (desetina==0){
426 hodnota2.result=hodnota2.result * 10 + 7;
428 else{
429 hodnota2.result=hodnota2.result + (7.0/pow(10,desetina));
430 desetina++;
432 sprintf(x,"%.5f",hodnota2.result);
433 gtk_label_set_text(app->label1,x);
435 free(x);
439 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
441 void on_button15_clicked (GtkObject *object, App *app)
443 if (cislic<omezeni){
444 cislic++;
445 char* x = malloc(sizeof(char)*15);
446 if (operace==0) {
447 if (desetina==0){
448 hodnota1.result=hodnota1.result * 10 + 8;
450 else{
451 hodnota1.result=hodnota1.result + (8.0/pow(10,desetina));
452 desetina++;
454 sprintf(x,"%.5f",hodnota1.result);
455 gtk_label_set_text(app->label1,x);
457 else{
458 if (desetina==0){
459 hodnota2.result=hodnota2.result * 10 + 8;
461 else{
462 hodnota2.result=hodnota2.result + (8.0/pow(10,desetina));
463 desetina++;
465 sprintf(x,"%.5f",hodnota2.result);
466 gtk_label_set_text(app->label1,x);
468 free(x);
472 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
474 void on_button16_clicked (GtkObject *object, App *app)
476 if (cislic<omezeni){
477 cislic++;
478 char* x = malloc(sizeof(char)*15);
479 if (operace==0) {
480 if (desetina==0){
481 hodnota1.result=hodnota1.result * 10 + 0;
483 else{
484 hodnota1.result=hodnota1.result + (0.0/pow(10,desetina));
485 desetina++;
487 sprintf(x,"%.5f",hodnota1.result);
488 gtk_label_set_text(app->label1,x);
490 else{
491 if (desetina==0){
492 hodnota2.result=hodnota2.result * 10 + 0;
494 else{
495 hodnota2.result=hodnota2.result + (0.0/pow(10,desetina));
496 desetina++;
498 sprintf(x,"%.5f",hodnota2.result);
499 gtk_label_set_text(app->label1,x);
501 free(x);
505 * Nastavi desetinnou carku a pristi cislice jsou desetinna
507 void on_button17_clicked (GtkObject *object, App *app)
509 if (desetina==0){
510 desetina=1;
511 //mozna jeste neco
513 else{ //vypsani chyby pokud se vickrat klikne na desetinu
518 * Priradi cislici do hodnoty 1 nebo 2 v zavislosti na stavu kalkulacky dle operace a desetinne carky
520 void on_button18_clicked (GtkObject *object, App *app)
522 if (cislic<omezeni){
523 cislic++;
524 char* x = malloc(sizeof(char)*15);
525 if (operace==0) {
526 if (desetina==0){
527 hodnota1.result=hodnota1.result * 10 + 9;
529 else{
530 hodnota1.result=hodnota1.result + (9.0/pow(10,desetina));
531 desetina++;
533 sprintf(x,"%.5f",hodnota1.result);
534 gtk_label_set_text(app->label1,x);
536 else{
537 if (desetina==0){
538 hodnota2.result=hodnota2.result * 10 + 9;
540 else{
541 hodnota2.result=hodnota2.result + (9.0/pow(10,desetina));
542 desetina++;
544 sprintf(x,"%.5f",hodnota2.result);
545 gtk_label_set_text(app->label1,x);
547 free(x);
551 * Podle typu operace zavola prislusnou funkci z nasi uchvatne knihovny
553 void on_button19_clicked (GtkObject *object, App *app)
555 /* * 0 - nic, nebylo zadano
556 * 1 - scitani
557 * 2 - odcitani
558 * 3 - nasobeni
559 * 4 - deleni
560 * 5 - umocneni
561 * 6 - logaritmus
562 * 7 - faktorial
563 * 8 - rovnase - hodnota pro zmenu
565 char* x = malloc(sizeof(char)*15);
566 switch (operace){
567 case 1:
568 hodnota1=scitani(hodnota1.result, hodnota2.result);
569 sprintf(x,"%.5f",hodnota1.result);
570 gtk_label_set_text(app->label1,x);
571 hodnota2.result=0;
572 break;
574 case 2:
575 hodnota1=odcitani(hodnota1.result, hodnota2.result);
576 sprintf(x,"%.5f",hodnota1.result);
577 gtk_label_set_text(app->label1,x);
578 hodnota2.result=0;
579 break;
581 case 3:
582 hodnota1=nasobeni(hodnota1.result, hodnota2.result);
583 sprintf(x,"%.5f",hodnota1.result);
584 gtk_label_set_text(app->label1,x);
585 hodnota2.result=0;
586 break;
588 case 4:
589 hodnota1=deleni(hodnota1.result, hodnota2.result);
590 if (hodnota1.error!=0){
591 gtk_label_set_text(app->label1,"ERROR");
592 operace=0;
593 desetina=0;
594 hodnota1.result=0;
595 hodnota2.result=0;
596 cislic=0;
598 else{
599 sprintf(x,"%.5f",hodnota1.result);
600 gtk_label_set_text(app->label1,x);
601 hodnota2.result=0;
603 break;
605 case 5:
606 hodnota1=umocneni(hodnota1.result, hodnota2.result);
607 sprintf(x,"%.5f",hodnota1.result);
608 gtk_label_set_text(app->label1,x);
609 hodnota2.result=0;
610 break;
613 free(x);
614 operace=8;
619 * ukonci hlavni smycku gtk pri zavreni okna
621 void on_window_destroy (GtkObject *object, gpointer user_data)
623 gtk_main_quit ();
627 * Vynuluje globalni promenne a nastavi GTK a vytovori GUI.
629 int main (int argc, char *argv[])
631 hodnota1.result=0;
632 hodnota1.error=0;
633 hodnota2.result=0;
634 hodnota2.error=0;
635 App *app;
636 GtkBuilder *builder;
638 // alokuje pamet pro app
639 app = g_slice_new(App);
641 gtk_init (&argc, &argv);
643 // nacte GUI z XML
644 builder = gtk_builder_new ();
645 gtk_builder_add_from_file (builder, BUILDER_XML_FILE, NULL);
647 // priradi do struktury jednotlive widgety
648 app->window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
649 app->button0 = GTK_WIDGET (gtk_builder_get_object (builder, "button0"));
650 app->button1 = GTK_WIDGET (gtk_builder_get_object (builder, "button1"));
651 app->button2 = GTK_WIDGET (gtk_builder_get_object (builder, "button2"));
652 app->button3 = GTK_WIDGET (gtk_builder_get_object (builder, "button3"));
653 app->button4 = GTK_WIDGET (gtk_builder_get_object (builder, "button4"));
654 app->button5 = GTK_WIDGET (gtk_builder_get_object (builder, "button5"));
655 app->button6 = GTK_WIDGET (gtk_builder_get_object (builder, "button6"));
656 app->button7 = GTK_WIDGET (gtk_builder_get_object (builder, "button7"));
657 app->button8 = GTK_WIDGET (gtk_builder_get_object (builder, "button8"));
658 app->button9 = GTK_WIDGET (gtk_builder_get_object (builder, "button9"));
659 app->button10 = GTK_WIDGET (gtk_builder_get_object (builder, "button10"));
660 app->button11 = GTK_WIDGET (gtk_builder_get_object (builder, "button11"));
661 app->button12 = GTK_WIDGET (gtk_builder_get_object (builder, "button12"));
662 app->button13 = GTK_WIDGET (gtk_builder_get_object (builder, "button13"));
663 app->button14 = GTK_WIDGET (gtk_builder_get_object (builder, "button14"));
664 app->button15 = GTK_WIDGET (gtk_builder_get_object (builder, "button15"));
665 app->button16 = GTK_WIDGET (gtk_builder_get_object (builder, "button16"));
666 app->button17 = GTK_WIDGET (gtk_builder_get_object (builder, "button17"));
667 app->button18 = GTK_WIDGET (gtk_builder_get_object (builder, "button18"));
668 app->button19 = GTK_WIDGET (gtk_builder_get_object (builder, "button19"));
669 app->label1 = GTK_LABEL (gtk_builder_get_object (builder, "label1"));
671 gtk_builder_connect_signals (builder, app);
673 g_object_unref (G_OBJECT (builder));
675 gtk_widget_show (app->window);
677 //hlavni smycka gtk
678 gtk_main ();
680 return 0;