comparison examples/calculator/main.c @ 92:3f619ae03678

Improve calcuator example program.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 24 Aug 2008 00:18:59 +0800
parents bd29780bf266
children c3c41f88e776
comparison
equal deleted inserted replaced
91:a0d1ecdaa6a5 92:3f619ae03678
32 { '=', OFFSET(calculator_scr_t, but_eq) }, 32 { '=', OFFSET(calculator_scr_t, but_eq) },
33 { 'c', OFFSET(calculator_scr_t, but_clr) } 33 { 'c', OFFSET(calculator_scr_t, but_clr) }
34 }; 34 };
35 35
36 static int real_compute(int op, int v1, int v2) { 36 static int real_compute(int op, int v1, int v2) {
37 int r = v2; 37 int r = v1;
38 38
39 switch(op) { 39 switch(op) {
40 case '+': 40 case '+':
41 r = v1 + v2; 41 r = v1 + v2;
42 break; 42 break;
50 if(v2) 50 if(v2)
51 r = v1 / v2; 51 r = v1 / v2;
52 else 52 else
53 r = v1; 53 r = v1;
54 break; 54 break;
55 case 'n':
56 r = v2;
57 break;
58 case 'N':
59 break;
55 } 60 }
56 61
57 return r; 62 return r;
58 } 63 }
59 64
60 static void show_text(ex_rt_t *ex_rt, int num, const char *prefix) { 65 static void show_text(ex_rt_t *ex_rt, int num, int saved, int op,
66 const char *suffix) {
61 char buf[20]; 67 char buf[20];
62 68
63 sprintf(buf, "%s%d", prefix, num); 69 sprintf(buf, "%d%s", num, suffix);
64 sh_text_set_text(ex_rt->code->screen_text, buf); 70 sh_text_set_text(ex_rt->code->screen_text, buf);
65 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text); 71 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text);
72
73 if(op == 'n')
74 sprintf(buf, "None");
75 else
76 sprintf(buf, "%d%c", saved, op);
77 sh_text_set_text(ex_rt->code->saved_text, buf);
78 rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->saved_text);
66 } 79 }
67 80
68 static void compute(ex_rt_t *ex_rt, coord_t *tgt) { 81 static void compute(ex_rt_t *ex_rt, coord_t *tgt) {
69 int i; 82 int i;
70 coord_t **coord_p; 83 coord_t **coord_p;
84 static int valid_num = 0;
85 static int factor = 1;
71 static int num = 0; 86 static int num = 0;
72 static int saved = 0, saved2 = 0; 87 static int op = 'n';
73 static int op = 0, op1 = 0; 88 static int saved = 0;
89 char buf[2] = " ";
74 90
75 for(i = 0; i < 16; i++) { 91 for(i = 0; i < 16; i++) {
76 coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off); 92 coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off);
77 if(*coord_p == (void *)tgt) 93 if(*coord_p == (void *)tgt)
78 break; 94 break;
79 } 95 }
80 if(i >= 16) return; 96 if(i >= 16) return;
81 97
82 if(i < 10) { 98 if(i < 10) {
83 num = num * 10 + i; 99 num = num * 10 + i;
84 show_text(ex_rt, num, ""); 100 show_text(ex_rt, num * factor, saved, op, "");
101 valid_num = 1;
85 } else { 102 } else {
86 switch(tgt_list[i].c) { 103 switch(tgt_list[i].c) {
87 case 'c': 104 case 'c':
88 saved = num = 0; 105 saved = num = 0;
89 show_text(ex_rt, 0, ""); 106 factor = 1;
107 valid_num = 0;
108 op = 'n';
109 show_text(ex_rt, 0, saved, op, "");
90 break; 110 break;
91 111
112 case '-':
113 if(!valid_num) {
114 factor *= -1;
115 valid_num = 1;
116 break;
117 }
92 case '+': 118 case '+':
93 case '-':
94 case '*': 119 case '*':
95 case '/': 120 case '/':
96 saved = real_compute(op, saved, num); 121 saved = real_compute(op, saved, num * factor);
97 show_text(ex_rt, saved, "="); 122 buf[0] = tgt_list[i].c;
123 show_text(ex_rt, saved, saved, 'n', buf);
98 op = tgt_list[i].c; 124 op = tgt_list[i].c;
99 saved2 = num;
100 num = 0; 125 num = 0;
126 factor = 1;
127 valid_num = 0;
101 break; 128 break;
102 129
103 case '=': 130 case '=':
104 saved = real_compute(op, saved, saved2); 131 saved = real_compute(op, saved, num * factor);
105 show_text(ex_rt, saved, "="); 132 show_text(ex_rt, saved, 0, 'n', "");
106 num = 0; 133 num = 0;
134 op = 'N';
107 break; 135 break;
108 } 136 }
109 } 137 }
110 rdman_redraw_changed(ex_rt->rt->rdman); 138 rdman_redraw_changed(ex_rt->rt->rdman);
111 } 139 }