changeset 92:3f619ae03678

Improve calcuator example program.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 24 Aug 2008 00:18:59 +0800
parents a0d1ecdaa6a5
children c3c41f88e776
files examples/calculator/calculator_scr.svg examples/calculator/main.c src/timertool.c
diffstat 3 files changed, 55 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/examples/calculator/calculator_scr.svg	Sat Aug 23 22:26:46 2008 +0800
+++ b/examples/calculator/calculator_scr.svg	Sun Aug 24 00:18:59 2008 +0800
@@ -100,7 +100,7 @@
      width="300px"
      height="400px"
      inkscape:window-width="822"
-     inkscape:window-height="600"
+     inkscape:window-height="609"
      inkscape:window-x="192"
      inkscape:window-y="23" />
   <metadata
@@ -502,11 +502,21 @@
        xml:space="preserve"
        style="font-size:28px;font-style:normal;font-weight:normal;fill:#008000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans"
        x="36.939316"
-       y="67.546173"
+       y="61.546173"
        id="screen_text_u"><tspan
          sodipodi:role="line"
          id="screen_text"
          x="36.939316"
-         y="67.546173">0</tspan></text>
+         y="61.546173">0</tspan></text>
+    <text
+       xml:space="preserve"
+       style="font-size:14px;font-style:normal;font-weight:normal;fill:#008000;fill-opacity:1;stroke:none;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1;font-family:Bitstream Vera Sans Mono"
+       x="71.767807"
+       y="78.100266"
+       id="text2238"><tspan
+         sodipodi:role="line"
+         id="saved_text"
+         x="71.767807"
+         y="78.100266">None</tspan></text>
   </g>
 </svg>
--- a/examples/calculator/main.c	Sat Aug 23 22:26:46 2008 +0800
+++ b/examples/calculator/main.c	Sun Aug 24 00:18:59 2008 +0800
@@ -34,7 +34,7 @@
 };
 
 static int real_compute(int op, int v1, int v2) {
-    int r = v2;
+    int r = v1;
 
     switch(op) {
     case '+':
@@ -52,25 +52,41 @@
 	else
 	    r = v1;
 	break;
+    case 'n':
+	r = v2;
+	break;
+    case 'N':
+	break;
     }
 
     return r;
 }
 
-static void show_text(ex_rt_t *ex_rt, int num, const char *prefix) {
+static void show_text(ex_rt_t *ex_rt, int num, int saved, int op,
+		      const char *suffix) {
     char buf[20];
 
-    sprintf(buf, "%s%d", prefix, num);
+    sprintf(buf, "%d%s", num, suffix);
     sh_text_set_text(ex_rt->code->screen_text, buf);
     rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->screen_text);
+
+    if(op == 'n')
+	sprintf(buf, "None");
+    else
+	sprintf(buf, "%d%c", saved, op);
+    sh_text_set_text(ex_rt->code->saved_text, buf);
+    rdman_shape_changed(ex_rt->rt->rdman, ex_rt->code->saved_text);
 }
 
 static void compute(ex_rt_t *ex_rt, coord_t *tgt) {
     int i;
     coord_t **coord_p;
+    static int valid_num = 0;
+    static int factor = 1;
     static int num = 0;
-    static int saved = 0, saved2 = 0;
-    static int op = 0, op1 = 0;
+    static int op = 'n';
+    static int saved = 0;
+    char buf[2] = " ";
 
     for(i = 0; i < 16; i++) {
 	coord_p = (coord_t **)((void *)ex_rt->code + tgt_list[i].off);
@@ -81,29 +97,41 @@
 
     if(i < 10) {
 	num = num * 10 + i;
-	show_text(ex_rt, num, "");
+	show_text(ex_rt, num * factor, saved, op, "");
+	valid_num = 1;
     } else {
 	switch(tgt_list[i].c) {
 	case 'c':
 	    saved = num = 0;
-	    show_text(ex_rt, 0, "");
+	    factor = 1;
+	    valid_num = 0;
+	    op = 'n';
+	    show_text(ex_rt, 0, saved, op, "");
 	    break;
 
+	case '-':
+	    if(!valid_num) {
+		factor *= -1;
+		valid_num = 1;
+		break;
+	    }
 	case '+':
-	case '-':
 	case '*':
 	case '/':
-	    saved = real_compute(op, saved, num);
-	    show_text(ex_rt, saved, "=");
+	    saved = real_compute(op, saved, num * factor);
+	    buf[0] = tgt_list[i].c;
+	    show_text(ex_rt, saved, saved, 'n', buf);
 	    op = tgt_list[i].c;
-	    saved2 = num;
 	    num = 0;
+	    factor = 1;
+	    valid_num = 0;
 	    break;
 
 	case '=':
-	    saved = real_compute(op, saved, saved2);
-	    show_text(ex_rt, saved, "=");
+	    saved = real_compute(op, saved, num * factor);
+	    show_text(ex_rt, saved, 0, 'n', "");
 	    num = 0;
+	    op = 'N';
 	    break;
 	}
     }
--- a/src/timertool.c	Sat Aug 23 22:26:46 2008 +0800
+++ b/src/timertool.c	Sun Aug 24 00:18:59 2008 +0800
@@ -1,3 +1,4 @@
+#include <stdio.h>
 #include <sys/time.h>
 #ifdef __FreeBSD__
 #include <machine/cpufunc.h>