Mercurial > MadButterfly
annotate examples/calculator/main.c @ 121:76ba6fd61c7d
More bug of insert sort.
elms[i] is over wrote by elms[i-1]. Save it to a local variable at
start of loop iteration for elms[i].
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 14 Sep 2008 23:40:57 +0800 |
parents | 565b55508c8d |
children | 17e97e92b76e |
rev | line source |
---|---|
88 | 1 #include <stdio.h> |
108 | 2 #include <mb/mb.h> |
88 | 3 #include "calculator_scr.h" |
4 | |
94 | 5 typedef struct _calc_data calc_data_t; |
6 struct _calc_data { | |
88 | 7 X_MB_runtime_t *rt; |
8 calculator_scr_t *code; | |
9 }; | |
10 | |
11 static struct { | |
12 int c; | |
13 int off; | |
14 } tgt_list[] = { | |
15 { 0, OFFSET(calculator_scr_t, but_0) }, | |
16 { 1, OFFSET(calculator_scr_t, but_1) }, | |
17 { 2, OFFSET(calculator_scr_t, but_2) }, | |
18 { 3, OFFSET(calculator_scr_t, but_3) }, | |
19 { 4, OFFSET(calculator_scr_t, but_4) }, | |
20 { 5, OFFSET(calculator_scr_t, but_5) }, | |
21 { 6, OFFSET(calculator_scr_t, but_6) }, | |
22 { 7, OFFSET(calculator_scr_t, but_7) }, | |
23 { 8, OFFSET(calculator_scr_t, but_8) }, | |
24 { 9, OFFSET(calculator_scr_t, but_9) }, | |
25 { '+', OFFSET(calculator_scr_t, but_add) }, | |
26 { '-', OFFSET(calculator_scr_t, but_minus) }, | |
27 { '*', OFFSET(calculator_scr_t, but_mul) }, | |
28 { '/', OFFSET(calculator_scr_t, but_div) }, | |
29 { '=', OFFSET(calculator_scr_t, but_eq) }, | |
30 { 'c', OFFSET(calculator_scr_t, but_clr) } | |
31 }; | |
32 | |
33 static int real_compute(int op, int v1, int v2) { | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
34 int r = v1; |
88 | 35 |
36 switch(op) { | |
37 case '+': | |
38 r = v1 + v2; | |
39 break; | |
40 case '-': | |
41 r = v1 - v2; | |
42 break; | |
43 case '*': | |
44 r = v1 * v2; | |
45 break; | |
46 case '/': | |
93 | 47 r = v1; |
89
90428161fc61
Prevent divide by zero error
Thinker K.F. Li <thinker@branda.to>
parents:
88
diff
changeset
|
48 if(v2) |
93 | 49 r /= v2; |
88 | 50 break; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
51 case 'n': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
52 r = v2; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
53 break; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
54 case 'N': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
55 break; |
88 | 56 } |
57 | |
58 return r; | |
59 } | |
60 | |
94 | 61 static void show_text(calc_data_t *calc_data, int num, int saved, int op, |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
62 const char *suffix) { |
88 | 63 char buf[20]; |
64 | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
65 sprintf(buf, "%d%s", num, suffix); |
94 | 66 sh_text_set_text(calc_data->code->screen_text, buf); |
67 rdman_shape_changed(calc_data->rt->rdman, calc_data->code->screen_text); | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
68 |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
69 if(op == 'n') |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
70 sprintf(buf, "None"); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
71 else |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
72 sprintf(buf, "%d%c", saved, op); |
94 | 73 sh_text_set_text(calc_data->code->saved_text, buf); |
74 rdman_shape_changed(calc_data->rt->rdman, calc_data->code->saved_text); | |
88 | 75 } |
76 | |
94 | 77 static void compute(calc_data_t *calc_data, coord_t *tgt) { |
88 | 78 int i; |
79 coord_t **coord_p; | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
80 static int valid_num = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
81 static int factor = 1; |
88 | 82 static int num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
83 static int op = 'n'; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
84 static int saved = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
85 char buf[2] = " "; |
88 | 86 |
87 for(i = 0; i < 16; i++) { | |
94 | 88 coord_p = (coord_t **)((void *)calc_data->code + tgt_list[i].off); |
88 | 89 if(*coord_p == (void *)tgt) |
90 break; | |
91 } | |
92 if(i >= 16) return; | |
93 | |
94 if(i < 10) { | |
95 num = num * 10 + i; | |
94 | 96 show_text(calc_data, num * factor, saved, op, ""); |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
97 valid_num = 1; |
88 | 98 } else { |
99 switch(tgt_list[i].c) { | |
100 case 'c': | |
101 saved = num = 0; | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
102 factor = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
103 valid_num = 0; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
104 op = 'n'; |
94 | 105 show_text(calc_data, 0, saved, op, ""); |
88 | 106 break; |
107 | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
108 case '-': |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
109 if(!valid_num) { |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
110 factor *= -1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
111 valid_num = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
112 break; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
113 } |
88 | 114 case '+': |
115 case '*': | |
116 case '/': | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
117 saved = real_compute(op, saved, num * factor); |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
118 buf[0] = tgt_list[i].c; |
94 | 119 show_text(calc_data, saved, saved, 'n', buf); |
90 | 120 op = tgt_list[i].c; |
88 | 121 num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
122 factor = 1; |
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
123 valid_num = 0; |
88 | 124 break; |
125 | |
126 case '=': | |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
127 saved = real_compute(op, saved, num * factor); |
94 | 128 show_text(calc_data, saved, 0, 'n', ""); |
88 | 129 num = 0; |
92
3f619ae03678
Improve calcuator example program.
Thinker K.F. Li <thinker@branda.to>
parents:
90
diff
changeset
|
130 op = 'N'; |
88 | 131 break; |
132 } | |
133 } | |
94 | 134 rdman_redraw_changed(calc_data->rt->rdman); |
88 | 135 } |
136 | |
137 static void buttons_handler(event_t *evt, void *arg) { | |
94 | 138 calc_data_t *calc_data = (calc_data_t *)arg; |
88 | 139 |
140 switch(evt->type) { | |
141 case EVT_MOUSE_BUT_PRESS: | |
94 | 142 compute(calc_data, (coord_t *)evt->cur_tgt); |
88 | 143 break; |
144 } | |
145 } | |
146 | |
94 | 147 static void setup_observers(calc_data_t *calc_data) { |
88 | 148 calculator_scr_t *calculator_scr; |
149 ob_factory_t *factory; | |
150 subject_t *subject; | |
90 | 151 coord_t *coord; |
152 int off; | |
153 int i; | |
88 | 154 |
94 | 155 calculator_scr = calc_data->code; |
156 factory = rdman_get_ob_factory(calc_data->rt->rdman); | |
88 | 157 |
90 | 158 for(i = 0; i < 16; i++) { |
159 off = tgt_list[i].off; | |
93 | 160 coord = OFF2TYPE(calculator_scr, off, coord_t *); |
90 | 161 subject = coord_get_mouse_event(coord); |
94 | 162 subject_add_observer(factory, subject, buttons_handler, calc_data); |
90 | 163 } |
88 | 164 } |
165 | |
166 int main(int argc, char * const argv[]) { | |
167 X_MB_runtime_t rt; | |
168 calculator_scr_t *calculator_scr; | |
94 | 169 calc_data_t calc_data; |
88 | 170 int r; |
171 | |
172 r = X_MB_init(":0.0", 300, 400, &rt); | |
173 | |
174 calculator_scr = calculator_scr_new(rt.rdman); | |
175 | |
94 | 176 calc_data.rt = &rt; |
177 calc_data.code = calculator_scr; | |
178 setup_observers(&calc_data); | |
88 | 179 |
180 X_MB_handle_connection(&rt); | |
181 | |
182 calculator_scr_free(calculator_scr); | |
183 X_MB_destroy(&rt); | |
184 | |
185 return 0; | |
186 } |