Opravy
[BETON.git] / main.c
blob8b3f313c5145efa266dd9181a7c2699497eaaa7a
1 #include <gtk/gtk.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <limits.h>
5 #include "knihovna.h"
7 /* location of UI XML file relative to path in which program is running */
8 #define BUILDER_XML_FILE "kal.glade"
10 #define MAX_VALUE 9999
13 // struktura s ukazateli na jednotlive widgety v aplikaci
14 typedef struct
16 GtkWidget *window;
17 GtkWidget *button0;
18 GtkWidget *button1;
19 GtkWidget *button2;
20 GtkWidget *button3;
21 GtkWidget *button4;
22 GtkWidget *button5;
23 GtkWidget *button6;
24 GtkWidget *button7;
25 GtkWidget *button8;
26 GtkWidget *button9;
27 GtkWidget *button10;
28 GtkWidget *button11;
29 GtkWidget *button12;
30 GtkWidget *button13;
31 GtkWidget *button14;
32 GtkWidget *button15;
33 GtkWidget *button16;
34 GtkWidget *button17;
35 GtkWidget *button18;
36 GtkWidget *button19;
37 GtkLabel *label1;
38 } App;
40 //deklarace globalnich promenych pro vysledky kalkulacky
41 Tvysledek hodnota1;
43 Tvysledek hodnota2;
45 int operace=0; //udaj o tom, co se ma delat
46 int desetina=0; //pri hodnote 1 se už tvori desetina cisla
47 int cislic=0;
48 int omezeni=8; //omezeni kolik maximalne muze uzivatel zadat cislic na jedno cislo
50 * 0 - nic, nebylo zadano
51 * 1 - scitani
52 * 2 - odcitani
53 * 3 - nasobeni
54 * 4 - deleni
55 * 5 - umocneni
56 * 6 - logaritmus
57 * 7 - faktorial
60 // handler, ktery je v glade prirazen stisknuti tlacitka
61 void on_button0_clicked (GtkObject *object, App *app)
63 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
64 operace=1;
65 cislic=0;
66 desetina=0;
68 else{ //uz bylo vybrano a tak se to pouzije...
69 //zde by mela byt moznost scitani
71 /*int x;
72 x = atoi(gtk_entry_get_text(app->entry));
73 x++;
74 if(x > MAX_VALUE)
75 return;
77 char str[10];
78 snprintf(str,10,"%d",x);
79 gtk_entry_set_text(app->entry,str);*/
80 //app->label1->set_label("tlacitko 1");
81 //app->label1 = gtk_label_new("Cold was my");
82 //gtk_label_set_text(app->label1,"pokus");
85 void on_button1_clicked (GtkObject *object, App *app)
87 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
88 operace=2;
89 cislic=0;
90 desetina=0;
92 else{ //uz bylo vybrano a tak se to pouzije...
93 //zde by mela byt moznost scitani
96 void on_button2_clicked (GtkObject *object, App *app)
98 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
99 operace=3;
100 cislic=0;
101 desetina=0;
103 else{ //uz bylo vybrano a tak se to pouzije...
104 //zde by mela byt moznost scitani
107 void on_button3_clicked (GtkObject *object, App *app)
109 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
110 operace=4;
111 cislic=0;
112 desetina=0;
114 else{ //uz bylo vybrano a tak se to pouzije...
115 //zde by mela byt moznost scitani
118 void on_button4_clicked (GtkObject *object, App *app)
120 if ((operace==0)||(operace==8)){ //zatim je jen jedno cislo
121 operace=5;
122 cislic=0;
123 desetina=0;
125 else{ //uz bylo vybrano a tak se to pouzije...
126 //zde by mela byt moznost scitani
129 void on_button5_clicked (GtkObject *object, App *app)
131 hodnota1=logaritmus(hodnota1.result);
132 if (hodnota1.error!=0){
133 gtk_label_set_text(app->label1,"ERROR");
134 operace=0;
135 desetina=0;
136 hodnota1.result=0;
137 hodnota2.result=0;
138 cislic=0;
140 else{
141 char* x = malloc(sizeof(char)*15);
142 sprintf(x,"%.5f",hodnota1.result);
143 gtk_label_set_text(app->label1,x);
144 hodnota2.result=0;
145 operace=0; //uz nebude mozne zadavat hodnotu 1
146 cislic=8;
147 desetina=1;
148 free(x);
151 void on_button6_clicked (GtkObject *object, App *app)
153 hodnota1=faktorial(hodnota1.result);
154 if (hodnota1.error!=0){
155 gtk_label_set_text(app->label1,"ERROR");
156 operace=0;
157 desetina=0;
158 hodnota1.result=0;
159 hodnota2.result=0;
160 cislic=0;
162 else{
163 char* x = malloc(sizeof(char)*15);
164 sprintf(x,"%.5f",hodnota1.result);
165 gtk_label_set_text(app->label1,x);
166 hodnota2.result=0;
167 operace=0; //uz nebude mozne zadavat hodnotu 1
168 cislic=8;
169 desetina=1;
170 free(x);
173 void on_button7_clicked (GtkObject *object, App *app)
175 operace=0;
176 desetina=0;
177 hodnota1.result=0;
178 hodnota1.error=0;
179 hodnota2.result=0;
180 hodnota2.error=0;
181 cislic=0;
182 gtk_label_set_text(app->label1,"Kalkulacka BETON");
184 void on_button8_clicked (GtkObject *object, App *app)
186 if (cislic<omezeni){
187 cislic++;
188 char* x = malloc(sizeof(char)*15);
189 if (operace==0) {
190 if (desetina==0){
191 hodnota1.result=hodnota1.result * 10 + 1;
193 else{
194 hodnota1.result=hodnota1.result + (1.0/pow(10,desetina));
195 desetina++;
197 sprintf(x,"%.5f",hodnota1.result);
198 gtk_label_set_text(app->label1,x);
200 else{
201 if (desetina==0){
202 hodnota2.result=hodnota2.result * 10 + 1;
204 else{
205 hodnota2.result=hodnota2.result + (1.0/pow(10,desetina));
206 desetina++;
208 sprintf(x,"%.5f",hodnota2.result);
209 gtk_label_set_text(app->label1,x);
211 free(x);
214 void on_button9_clicked (GtkObject *object, App *app)
216 if (cislic<omezeni){
217 cislic++;
218 char* x = malloc(sizeof(char)*15);
219 if (operace==0) {
220 if (desetina==0){
221 hodnota1.result=hodnota1.result * 10 + 2;
223 else{
224 hodnota1.result=hodnota1.result + (2.0/pow(10,desetina));
225 desetina++;
227 sprintf(x,"%.5f",hodnota1.result);
228 gtk_label_set_text(app->label1,x);
230 else{
231 if (desetina==0){
232 hodnota2.result=hodnota2.result * 10 + 2;
234 else{
235 hodnota2.result=hodnota2.result + (2.0/pow(10,desetina));
236 desetina++;
238 sprintf(x,"%.5f",hodnota2.result);
239 gtk_label_set_text(app->label1,x);
241 free(x);
244 void on_button10_clicked (GtkObject *object, App *app)
246 if (cislic<omezeni){
247 cislic++;
248 char* x = malloc(sizeof(char)*15);
249 if (operace==0) {
250 if (desetina==0){
251 hodnota1.result=hodnota1.result * 10 + 3;
253 else{
254 hodnota1.result=hodnota1.result + (3.0/pow(10,desetina));
255 desetina++;
257 sprintf(x,"%.5f",hodnota1.result);
258 gtk_label_set_text(app->label1,x);
260 else{
261 if (desetina==0){
262 hodnota2.result=hodnota2.result * 10 + 3;
264 else{
265 hodnota2.result=hodnota2.result + (3.0/pow(10,desetina));
266 desetina++;
268 sprintf(x,"%.5f",hodnota2.result);
269 gtk_label_set_text(app->label1,x);
271 free(x);
274 void on_button11_clicked (GtkObject *object, App *app)
276 if (cislic<omezeni){
277 cislic++;
278 char* x = malloc(sizeof(char)*15);
279 if (operace==0) {
280 if (desetina==0){
281 hodnota1.result=hodnota1.result * 10 + 4;
283 else{
284 hodnota1.result=hodnota1.result + (4.0/pow(10,desetina));
285 desetina++;
287 sprintf(x,"%.5f",hodnota1.result);
288 gtk_label_set_text(app->label1,x);
290 else{
291 if (desetina==0){
292 hodnota2.result=hodnota2.result * 10 + 4;
294 else{
295 hodnota2.result=hodnota2.result + (4.0/pow(10,desetina));
296 desetina++;
298 sprintf(x,"%.5f",hodnota2.result);
299 gtk_label_set_text(app->label1,x);
301 free(x);
304 void on_button12_clicked (GtkObject *object, App *app)
306 if (cislic<omezeni){
307 cislic++;
308 char* x = malloc(sizeof(char)*15);
309 if (operace==0) {
310 if (desetina==0){
311 hodnota1.result=hodnota1.result * 10 + 5;
313 else{
314 hodnota1.result=hodnota1.result + (5.0/pow(10,desetina));
315 desetina++;
317 sprintf(x,"%.5f",hodnota1.result);
318 gtk_label_set_text(app->label1,x);
320 else{
321 if (desetina==0){
322 hodnota2.result=hodnota2.result * 10 + 5;
324 else{
325 hodnota2.result=hodnota2.result + (5.0/pow(10,desetina));
326 desetina++;
328 sprintf(x,"%.5f",hodnota2.result);
329 gtk_label_set_text(app->label1,x);
331 free(x);
334 void on_button13_clicked (GtkObject *object, App *app)
336 if (cislic<omezeni){
337 cislic++;
338 char* x = malloc(sizeof(char)*15);
339 if (operace==0) {
340 if (desetina==0){
341 hodnota1.result=hodnota1.result * 10 + 6;
343 else{
344 hodnota1.result=hodnota1.result + (6.0/pow(10,desetina));
345 desetina++;
347 sprintf(x,"%.5f",hodnota1.result);
348 gtk_label_set_text(app->label1,x);
350 else{
351 if (desetina==0){
352 hodnota2.result=hodnota2.result * 10 + 6;
354 else{
355 hodnota2.result=hodnota2.result + (6.0/pow(10,desetina));
356 desetina++;
358 sprintf(x,"%.5f",hodnota2.result);
359 gtk_label_set_text(app->label1,x);
361 free(x);
364 void on_button14_clicked (GtkObject *object, App *app)
366 if (cislic<omezeni){
367 cislic++;
368 char* x = malloc(sizeof(char)*15);
369 if (operace==0) {
370 if (desetina==0){
371 hodnota1.result=hodnota1.result * 10 + 7;
373 else{
374 hodnota1.result=hodnota1.result + (7.0/pow(10,desetina));
375 desetina++;
377 sprintf(x,"%.5f",hodnota1.result);
378 gtk_label_set_text(app->label1,x);
380 else{
381 if (desetina==0){
382 hodnota2.result=hodnota2.result * 10 + 7;
384 else{
385 hodnota2.result=hodnota2.result + (7.0/pow(10,desetina));
386 desetina++;
388 sprintf(x,"%.5f",hodnota2.result);
389 gtk_label_set_text(app->label1,x);
391 free(x);
394 void on_button15_clicked (GtkObject *object, App *app)
396 if (cislic<omezeni){
397 cislic++;
398 char* x = malloc(sizeof(char)*15);
399 if (operace==0) {
400 if (desetina==0){
401 hodnota1.result=hodnota1.result * 10 + 8;
403 else{
404 hodnota1.result=hodnota1.result + (8.0/pow(10,desetina));
405 desetina++;
407 sprintf(x,"%.5f",hodnota1.result);
408 gtk_label_set_text(app->label1,x);
410 else{
411 if (desetina==0){
412 hodnota2.result=hodnota2.result * 10 + 8;
414 else{
415 hodnota2.result=hodnota2.result + (8.0/pow(10,desetina));
416 desetina++;
418 sprintf(x,"%.5f",hodnota2.result);
419 gtk_label_set_text(app->label1,x);
421 free(x);
424 void on_button16_clicked (GtkObject *object, App *app)
426 if (cislic<omezeni){
427 cislic++;
428 char* x = malloc(sizeof(char)*15);
429 if (operace==0) {
430 if (desetina==0){
431 hodnota1.result=hodnota1.result * 10 + 0;
433 else{
434 hodnota1.result=hodnota1.result + (0.0/pow(10,desetina));
435 desetina++;
437 sprintf(x,"%.5f",hodnota1.result);
438 gtk_label_set_text(app->label1,x);
440 else{
441 if (desetina==0){
442 hodnota2.result=hodnota2.result * 10 + 0;
444 else{
445 hodnota2.result=hodnota2.result + (0.0/pow(10,desetina));
446 desetina++;
448 sprintf(x,"%.5f",hodnota2.result);
449 gtk_label_set_text(app->label1,x);
451 free(x);
454 void on_button17_clicked (GtkObject *object, App *app)
456 if (desetina==0){
457 desetina=1;
458 //mozna jeste neco
460 else{ //vypsani chyby pokud se vickrat klikne na desetinu
464 void on_button18_clicked (GtkObject *object, App *app)
466 if (cislic<omezeni){
467 cislic++;
468 char* x = malloc(sizeof(char)*15);
469 if (operace==0) {
470 if (desetina==0){
471 hodnota1.result=hodnota1.result * 10 + 9;
473 else{
474 hodnota1.result=hodnota1.result + (9.0/pow(10,desetina));
475 desetina++;
477 sprintf(x,"%.5f",hodnota1.result);
478 gtk_label_set_text(app->label1,x);
480 else{
481 if (desetina==0){
482 hodnota2.result=hodnota2.result * 10 + 9;
484 else{
485 hodnota2.result=hodnota2.result + (9.0/pow(10,desetina));
486 desetina++;
488 sprintf(x,"%.5f",hodnota2.result);
489 gtk_label_set_text(app->label1,x);
491 free(x);
494 void on_button19_clicked (GtkObject *object, App *app)
496 /* * 0 - nic, nebylo zadano
497 * 1 - scitani
498 * 2 - odcitani
499 * 3 - nasobeni
500 * 4 - deleni
501 * 5 - umocneni
502 * 6 - logaritmus
503 * 7 - faktorial
504 * 8 - rovnase - hodnota pro zmenu
506 char* x = malloc(sizeof(char)*15);
507 switch (operace){
508 case 1:
509 hodnota1=scitani(hodnota1.result, hodnota2.result);
510 sprintf(x,"%.5f",hodnota1.result);
511 gtk_label_set_text(app->label1,x);
512 hodnota2.result=0;
513 break;
515 case 2:
516 hodnota1=odcitani(hodnota1.result, hodnota2.result);
517 sprintf(x,"%.5f",hodnota1.result);
518 gtk_label_set_text(app->label1,x);
519 hodnota2.result=0;
520 break;
522 case 3:
523 hodnota1=nasobeni(hodnota1.result, hodnota2.result);
524 sprintf(x,"%.5f",hodnota1.result);
525 gtk_label_set_text(app->label1,x);
526 hodnota2.result=0;
527 break;
529 case 4:
530 hodnota1=deleni(hodnota1.result, hodnota2.result);
531 if (hodnota1.error!=0){
532 gtk_label_set_text(app->label1,"ERROR");
533 operace=0;
534 desetina=0;
535 hodnota1.result=0;
536 hodnota2.result=0;
537 cislic=0;
539 else{
540 sprintf(x,"%.5f",hodnota1.result);
541 gtk_label_set_text(app->label1,x);
542 hodnota2.result=0;
544 break;
546 case 5:
547 hodnota1=umocneni(hodnota1.result, hodnota2.result);
548 sprintf(x,"%.5f",hodnota1.result);
549 gtk_label_set_text(app->label1,x);
550 hodnota2.result=0;
551 break;
554 free(x);
555 operace=8;
559 // ukonci hlavni smycku gtk pri zavreni okna
560 void on_window_destroy (GtkObject *object, gpointer user_data)
562 gtk_main_quit ();
566 int main (int argc, char *argv[])
568 hodnota1.result=0;
569 hodnota1.error=0;
570 hodnota2.result=0;
571 hodnota2.error=0;
572 App *app;
573 GtkBuilder *builder;
575 // alokuje pamet pro app
576 app = g_slice_new(App);
578 gtk_init (&argc, &argv);
580 // nacte GUI z XML
581 builder = gtk_builder_new ();
582 gtk_builder_add_from_file (builder, BUILDER_XML_FILE, NULL);
584 // priradi do struktury jednotlive widgety
585 app->window = GTK_WIDGET (gtk_builder_get_object (builder, "window1"));
586 app->button0 = GTK_WIDGET (gtk_builder_get_object (builder, "button0"));
587 app->button1 = GTK_WIDGET (gtk_builder_get_object (builder, "button1"));
588 app->button2 = GTK_WIDGET (gtk_builder_get_object (builder, "button2"));
589 app->button3 = GTK_WIDGET (gtk_builder_get_object (builder, "button3"));
590 app->button4 = GTK_WIDGET (gtk_builder_get_object (builder, "button4"));
591 app->button5 = GTK_WIDGET (gtk_builder_get_object (builder, "button5"));
592 app->button6 = GTK_WIDGET (gtk_builder_get_object (builder, "button6"));
593 app->button7 = GTK_WIDGET (gtk_builder_get_object (builder, "button7"));
594 app->button8 = GTK_WIDGET (gtk_builder_get_object (builder, "button8"));
595 app->button9 = GTK_WIDGET (gtk_builder_get_object (builder, "button9"));
596 app->button10 = GTK_WIDGET (gtk_builder_get_object (builder, "button10"));
597 app->button11 = GTK_WIDGET (gtk_builder_get_object (builder, "button11"));
598 app->button12 = GTK_WIDGET (gtk_builder_get_object (builder, "button12"));
599 app->button13 = GTK_WIDGET (gtk_builder_get_object (builder, "button13"));
600 app->button14 = GTK_WIDGET (gtk_builder_get_object (builder, "button14"));
601 app->button15 = GTK_WIDGET (gtk_builder_get_object (builder, "button15"));
602 app->button16 = GTK_WIDGET (gtk_builder_get_object (builder, "button16"));
603 app->button17 = GTK_WIDGET (gtk_builder_get_object (builder, "button17"));
604 app->button18 = GTK_WIDGET (gtk_builder_get_object (builder, "button18"));
605 app->button19 = GTK_WIDGET (gtk_builder_get_object (builder, "button19"));
606 app->label1 = GTK_LABEL (gtk_builder_get_object (builder, "label1"));
608 gtk_builder_connect_signals (builder, app);
610 g_object_unref (G_OBJECT (builder));
612 gtk_widget_show (app->window);
614 //hlavni smycka gtk
615 gtk_main ();
617 return 0;