File: mathtest.c

package info (click to toggle)
allegro4.4 2:4.4.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 20,744 kB
  • sloc: ansic: 164,458; asm: 17,620; cpp: 3,848; objc: 1,687; sh: 1,107; python: 676; pascal: 179; makefile: 55; perl: 29; lisp: 1
file content (423 lines) | stat: -rw-r--r-- 10,442 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*         ______   ___    ___
 *        /\  _  \ /\_ \  /\_ \ 
 *        \ \ \L\ \\//\ \ \//\ \      __     __   _ __   ___ 
 *         \ \  __ \ \ \ \  \ \ \   /'__`\ /'_ `\/\`'__\/ __`\
 *          \ \ \/\ \ \_\ \_ \_\ \_/\  __//\ \L\ \ \ \//\ \L\ \
 *           \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
 *            \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
 *                                           /\____/
 *                                           \_/__/
 *
 *      Fixed point maths test program for the Allegro library.
 *
 *      By Shawn Hargreaves.
 *
 *      See readme.txt for copyright information.
 */


#include <stdio.h>
#include <string.h>

#include "allegro.h"



#define SIN    1
#define COS    2
#define TAN    3
#define ASIN   4
#define ACOS   5
#define ATAN   6
#define SQRT   7

#define ADD    1
#define SUB    2
#define MUL    3
#define DIV    4

char calc_str[80];

fixed calc_value;
int calc_operation;
int calc_clear_flag;

extern DIALOG calculator[];

#define CALC_STR  3



void redraw(DIALOG *d)
{
   object_message(calculator+CALC_STR, MSG_DRAW, 0);

   if (d)
      object_message(d, MSG_DRAW, 0);
}



void reset_calc(void)
{
   calc_value = 0;
   calc_operation = 0;
   calc_clear_flag = FALSE;
}



fixed get_calc_value(void)
{
   double d = atof(calc_str);
   return ftofix(d);
}



void set_calc_value(fixed v)
{
   int c, l;
   int has_dot;
   char b[80];
   double d = fixtof(v);

   sprintf(b, "%.8f", d);

   l = 0;
   has_dot = FALSE;

   for (c=0; b[c]; c++) {
      if ((b[c] >= '0') && (b[c] <= '9')) {
	 l++;
	 if (l > 8) {
	    b[c] = 0;
	    break;
	 }
      }
      else
	 if (b[c] == '.')
	    has_dot = TRUE;
   }

   if (has_dot) {
      for (c=c-1; c>=0; c--) {
	 if (b[c] == '0')
	    b[c] = 0;
	 else {
	    if (b[c] == '.') 
	       b[c] = 0;
	    break;
	 }
      }
   }

   if (errno)
      sprintf(calc_str, "-E- (%s)", b);
   else
      strcpy(calc_str, b);

   errno = 0;
}



int right_text_proc(int msg, DIALOG *d, int c)
{
   int len;

   if (msg == MSG_DRAW) {
      len = strlen(d->dp);
      textout_ex(screen, font, d->dp, d->x+d->w-len*8, d->y, d->fg, d->bg);
      rectfill(screen, d->x, d->y, d->x+d->w-len*8-1, d->y+d->h, d->bg); 
   }

   return D_O_K;
}



int input_proc(int msg, DIALOG *d, int c)
{
   int c1, c2;
   char *s = d->dp;
   int ret = d_button_proc(msg, d, c);

   if (ret == D_CLOSE) {
      if ((calc_clear_flag) && (s[0] != '+'))
	 strcpy(calc_str, "0");
      else if (strncmp(calc_str, "-E-", 3) == 0) {
	 redraw(d);
	 return D_O_K;
      }

      calc_clear_flag = FALSE;

      if (s[0] == '.') {
	 for (c1=0; calc_str[c1]; c1++) 
	    if (calc_str[c1] == '.')
	       break;

	 if (!calc_str[c1])
	    strcat(calc_str, ".");
      }
      else if (s[0] == '+') {
	 if (calc_str[0] == '-')
	    memmove(calc_str, calc_str+1, strlen(calc_str+1)+1);
	 else {
	    memmove(calc_str+1, calc_str, strlen(calc_str)+1);
	    calc_str[0] = '-';
	 }
      }
      else {
	 if (strcmp(calc_str, "0") == 0)
	    calc_str[0] = 0;
	 else if (strcmp(calc_str, "-0") == 0) 
	    strcpy(calc_str, "-");

	 c2 = 0;
	 for (c1=0; calc_str[c1]; c1++)
	    if ((calc_str[c1] >= '0') && (calc_str[c1] <= '9'))
	       c2++;

	 if (c2 < 8)
	    strcat(calc_str, s);
      }

      redraw(d);
      return D_O_K;
   }

   return ret;
}



int unary_operator(int msg, DIALOG *d, int c)
{
   fixed x;
   int ret = d_button_proc(msg, d, c);

   if (ret == D_CLOSE) {
      x = get_calc_value();

      switch (d->d1) {

	 case SIN:
	    x = fixsin(x);
	    break;

	 case COS:
	    x = fixcos(x);
	    break;

	 case TAN:
	    x = fixtan(x);
	    break;

	 case ASIN:
	    x = fixasin(x);
	    break;

	 case ACOS:
	    x = fixacos(x);
	    break;

	 case ATAN:
	    x = fixatan(x);
	    break;

	 case SQRT:
	    x = fixsqrt(x);
	    break;
      }

      set_calc_value(x);
      calc_clear_flag = TRUE;

      redraw(d);
      return D_O_K;
   }

   return ret;
}



int work_out(void)
{
   fixed x;

   x = get_calc_value();

   switch (calc_operation) {

      case ADD:
	 x = fixadd(calc_value, x);
	 break;

      case SUB:
	 x = fixsub(calc_value, x);
	 break;

      case MUL:
	 x = fixmul(calc_value, x);
	 break;

      case DIV:
	 x = fixdiv(calc_value, x);
	 break;
   }

   set_calc_value(x);
   reset_calc();
   calc_clear_flag = TRUE;

   redraw(NULL);
   return D_O_K;
}



int equals_proc(int msg, DIALOG *d, int c)
{
   int ret = d_button_proc(msg, d, c);

   if (ret == D_CLOSE) { 
      ret = work_out();
      redraw(d);
   }

   return ret;
}



int binary_operator(int msg, DIALOG *d, int c)
{
   int ret = d_button_proc(msg, d, c);

   if (ret == D_CLOSE) {
      work_out();

      calc_value = get_calc_value();
      calc_operation = d->d1;
      calc_clear_flag = TRUE;

      redraw(d);
      return D_O_K;
   }

   return ret;
}



int clearer(void)
{
   reset_calc();
   strcpy(calc_str, "0");
   redraw(NULL);
   return D_O_K;
}



int clear_proc(int msg, DIALOG *d, int c)
{
   int ret = d_button_proc(msg, d, c);

   if (ret == D_CLOSE) {
      ret = clearer();
      redraw(d);
   }

   return ret;
}



DIALOG calculator[] =
{
   /* (dialog proc)     (x)   (y)   (w)   (h)   (fg)  (bg)  (key) (flags)  (d1)     (d2)           (dp)        (dp2) (dp3) */
   { d_shadow_box_proc, 0,    0,    193,  173,  255,  8,    0,    0,       0,       0,             NULL,       NULL, NULL  },
   { d_box_proc,        8,    10,   177,  21,   16,   255,  0,    0,       0,       0,             NULL,       NULL, NULL  },
   { d_box_proc,        10,   12,   173,  17,   255,  16,   0,    0,       0,       0,             NULL,       NULL, NULL  },
   { right_text_proc,   24,   16,   144,  8,    255,  16,   0,    0,       0,       0,             calc_str,   NULL, NULL  },

   { unary_operator,    16,   48,   49,   13,   16,   255,  0,    D_EXIT,  SIN,     0,             "sin",      NULL, NULL  },
   { unary_operator,    72,   48,   49,   13,   16,   255,  0,    D_EXIT,  COS,     0,             "cos",      NULL, NULL  },
   { unary_operator,    128,  48,   49,   13,   16,   255,  0,    D_EXIT,  TAN,     0,             "tan",      NULL, NULL  },

   { unary_operator,    16,   64,   49,   13,   16,   255,  0,    D_EXIT,  ASIN,    0,             "asin",     NULL, NULL  },
   { unary_operator,    72,   64,   49,   13,   16,   255,  0,    D_EXIT,  ACOS,    0,             "acos",     NULL, NULL  },
   { unary_operator,    128,  64,   49,   13,   16,   255,  0,    D_EXIT,  ATAN,    0,             "atan",     NULL, NULL  },

   { input_proc,        8,    88,   33,   13,   16,   255,  '7',  D_EXIT,  0,       0,             "7",        NULL, NULL  },
   { input_proc,        44,   88,   33,   13,   16,   255,  '8',  D_EXIT,  0,       0,             "8",        NULL, NULL  },
   { input_proc,        80,   88,   33,   13,   16,   255,  '9',  D_EXIT,  0,       0,             "9",        NULL, NULL  },
   { binary_operator,   116,  88,   33,   13,   16,   255,  '/',  D_EXIT,  DIV,     0,             "/",        NULL, NULL  },
   { clear_proc,        152,  88,   33,   13,   16,   255,  'c',  D_EXIT,  0,       0,             "C",        NULL, NULL  },
   { d_keyboard_proc,   0,    0,    0,    0,    0,    0,    0,    0,       KEY_DEL, KEY_BACKSPACE, clearer,    NULL, NULL  },

   { input_proc,        8,    108,  33,   13,   16,   255,  '4',  D_EXIT,  0,       0,             "4",        NULL, NULL  },
   { input_proc,        44,   108,  33,   13,   16,   255,  '5',  D_EXIT,  0,       0,             "5",        NULL, NULL  },
   { input_proc,        80,   108,  33,   13,   16,   255,  '6',  D_EXIT,  0,       0,             "6",        NULL, NULL  },
   { binary_operator,   116,  108,  33,   13,   16,   255,  '*',  D_EXIT,  MUL,     0,             "*",        NULL, NULL  },
   { d_button_proc,     152,  108,  33,   13,   16,   255,  27,   D_EXIT,  0,       0,             "off",      NULL, NULL  },

   { input_proc,        8,    128,  33,   13,   16,   255,  '1',  D_EXIT,  0,       0,             "1",        NULL, NULL  },
   { input_proc,        44,   128,  33,   13,   16,   255,  '2',  D_EXIT,  0,       0,             "2",        NULL, NULL  },
   { input_proc,        80,   128,  33,   13,   16,   255,  '3',  D_EXIT,  0,       0,             "3",        NULL, NULL  },
   { binary_operator,   116,  128,  33,   13,   16,   255,  '-',  D_EXIT,  SUB,     0,             "-",        NULL, NULL  },
   { unary_operator,    152,  128,  33,   13,   16,   255,  0,    D_EXIT,  SQRT,    0,             "sqr",      NULL, NULL  },

   { input_proc,        8,    148,  33,   13,   16,   255,  '0',  D_EXIT,  0,       0,             "0",        NULL, NULL  },
   { input_proc,        44,   148,  33,   13,   16,   255,  '.',  D_EXIT,  0,       0,             ".",        NULL, NULL  },
   { input_proc,        80,   148,  33,   13,   16,   255,  0,    D_EXIT,  0,       0,             "+/-",      NULL, NULL  },
   { binary_operator,   116,  148,  33,   13,   16,   255,  '+',  D_EXIT,  ADD,     0,             "+",        NULL, NULL  },
   { equals_proc,       152,  148,  33,   13,   16,   255,  '=',  D_EXIT,  0,       0,             "=",        NULL, NULL  },
   { d_keyboard_proc,   0,    0,    0,    0,    0,    0,    13,   0,       0,       0,             work_out,   NULL, NULL  },

   { d_yield_proc,      0,    0,    0,    0,    0,    0,    0,    0,       0,       0,             NULL,       NULL, NULL  },
   { NULL,              0,    0,    0,    0,    0,    0,    0,    0,       0,       0,             NULL,       NULL, NULL  }
};



int main(void)
{
   int i;
   
   if (allegro_init() != 0)
      return 1;
   install_mouse();
   install_keyboard();
   install_timer();

   if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0) {
      if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
	 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
	 allegro_message("Error setting graphics mode\n%s\n", allegro_error);
	 return 1;
      }
   }

   textout_ex(screen, font, "Angles are binary, 0-255", 0, 0, palette_color[8], 0);
   reset_calc();
   strcpy(calc_str, "0");
   errno = 0;

   /* we set up colors to match screen color depth (in case it changed) */
   for (i = 0; calculator[i].proc; i++) {
      calculator[i].fg = palette_color[calculator[i].fg];
      calculator[i].bg = palette_color[calculator[i].bg];
   }

   centre_dialog(calculator);
   do_dialog(calculator, -1);

   return 0;
}

END_OF_MAIN()