Mercurial > MadButterfly
annotate examples/calculator/main.c @ 90:bd29780bf266
Reduce redundant code
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 23 Aug 2008 22:23:44 +0800 |
parents | 90428161fc61 |
children | 3f619ae03678 |
rev | line source |
---|---|
88 | 1 #include <stdio.h> |
2 #include <mb_types.h> | |
3 #include <X_supp.h> | |
4 #include <shapes.h> | |
5 #include <tools.h> | |
6 #include "calculator_scr.h" | |
7 | |
8 typedef struct _ex_rt ex_rt_t; | |
9 struct _ex_rt { | |
10 X_MB_runtime_t *rt; | |
11 calculator_scr_t *code; | |
12 }; | |
13 | |
14 static struct { | |
15 int c; | |
16 int off; | |
17 } tgt_list[] = { | |
18 { 0, OFFSET(calculator_scr_t, but_0) }, | |
19 { 1, OFFSET(calculator_scr_t, but_1) }, | |
20 { 2, OFFSET(calculator_scr_t, but_2) }, | |
21 { 3, OFFSET(calculator_scr_t, but_3) }, | |
22 { 4, OFFSET(calculator_scr_t, but_4) }, | |
23 { 5, OFFSET(calculator_scr_t, but_5) }, | |
24 { 6, OFFSET(calculator_scr_t, but_6) }, | |
25 { 7, OFFSET(calculator_scr_t, but_7) }, | |
26 { 8, OFFSET(calculator_scr_t, but_8) }, | |
27 { 9, OFFSET(calculator_scr_t, but_9) }, | |
28 { '+', OFFSET(calculator_scr_t, but_add) }, | |
29 { '-', OFFSET(calculator_scr_t, but_minus) }, | |
30 { '*', OFFSET(calculator_scr_t, but_mul) }, | |
31 { '/', OFFSET(calculator_scr_t, but_div) }, | |
32 { '=', OFFSET(calculator_scr_t, but_eq) }, | |
33 { 'c', OFFSET(calculator_scr_t, but_clr) } | |
34 }; | |
35 | |
36 static int real_compute(int op, int v1, int v2) { | |
37 int r = v2; | |
38 | |
39 switch(op) { | |
40 case '+': | |
41 r = v1 + v2; | |
42 break; | |
43 case '-': | |
44 r = v1 - v2; | |
45 break; | |
46 case '*': | |
47 r = v1 * v2; | |
48 break; | |
49 case '/': | |
89
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
50 if(v2) |
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
51 r = v1 / v2; |
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
52 else |
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
53 r = v1; |
88 | 54 break; |
55 } | |
56 | |
57 return r; | |
58 } | |
59 | |
90 | 60 static void show_text(ex_rt_t *ex_rt, int num, const char *prefix) { |
88 | 61 char buf[20]; |
62 | |
90 | 63 sprintf(buf, "%s%d", prefix, num); |
88 | 64 sh_text_set_text(ex_rt->code->screen_text, buf); |
65 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text); | |
66 } | |
67 | |
68 static void compute(ex_rt_t *ex_rt, coord_t *tgt) { | |
69 int i; | |
70 coord_t **coord_p; | |
71 static int num = 0; | |
90 | 72 static int saved = 0, saved2 = 0; |
73 static int op = 0, op1 = 0; | |
88 | 74 |
75 for(i = 0; i < 16; i++) { | |
76 coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off); | |
77 if(*coord_p == (void *)tgt) | |
78 break; | |
79 } | |
80 if(i >= 16) return; | |
81 | |
82 if(i < 10) { | |
83 num = num * 10 + i; | |
90 | 84 show_text(ex_rt, num, ""); |
88 | 85 } else { |
86 switch(tgt_list[i].c) { | |
87 case 'c': | |
88 saved = num = 0; | |
90 | 89 show_text(ex_rt, 0, ""); |
88 | 90 break; |
91 | |
92 case '+': | |
93 case '-': | |
94 case '*': | |
95 case '/': | |
96 saved = real_compute(op, saved, num); | |
90 | 97 show_text(ex_rt, saved, "="); |
98 op = tgt_list[i].c; | |
99 saved2 = num; | |
88 | 100 num = 0; |
101 break; | |
102 | |
103 case '=': | |
90 | 104 saved = real_compute(op, saved, saved2); |
105 show_text(ex_rt, saved, "="); | |
88 | 106 num = 0; |
107 break; | |
108 } | |
109 } | |
110 rdman_redraw_changed(ex_rt->rt->rdman); | |
111 } | |
112 | |
113 static void buttons_handler(event_t *evt, void *arg) { | |
114 ex_rt_t *ex_rt = (ex_rt_t *)arg; | |
115 | |
116 switch(evt->type) { | |
117 case EVT_MOUSE_BUT_PRESS: | |
118 compute(ex_rt, (coord_t *)evt->cur_tgt); | |
119 break; | |
120 } | |
121 } | |
122 | |
123 static void setup_observers(ex_rt_t *ex_rt) { | |
124 calculator_scr_t *calculator_scr; | |
125 ob_factory_t *factory; | |
126 subject_t *subject; | |
90 | 127 coord_t *coord; |
128 int off; | |
129 int i; | |
88 | 130 |
131 calculator_scr = ex_rt->code; | |
132 factory = rdman_get_ob_factory(ex_rt->rt->rdman); | |
133 | |
90 | 134 for(i = 0; i < 16; i++) { |
135 off = tgt_list[i].off; | |
136 coord = *(coord_t **)((void *)calculator_scr + off); | |
137 subject = coord_get_mouse_event(coord); | |
138 subject_add_observer(factory, subject, buttons_handler, ex_rt); | |
139 } | |
88 | 140 } |
141 | |
142 int main(int argc, char * const argv[]) { | |
143 X_MB_runtime_t rt; | |
144 calculator_scr_t *calculator_scr; | |
145 ex_rt_t ex_rt; | |
146 int r; | |
147 | |
148 r = X_MB_init(":0.0", 300, 400, &rt); | |
149 | |
150 calculator_scr = calculator_scr_new(rt.rdman); | |
151 | |
152 ex_rt.rt = &rt; | |
153 ex_rt.code = calculator_scr; | |
154 setup_observers(&ex_rt); | |
155 | |
156 X_MB_handle_connection(&rt); | |
157 | |
158 calculator_scr_free(calculator_scr); | |
159 X_MB_destroy(&rt); | |
160 | |
161 return 0; | |
162 } |