File: simpleCalc.c

package info (click to toggle)
wcalc 2.4-1.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,916 kB
  • sloc: ansic: 7,089; objc: 2,002; sh: 890; lex: 816; yacc: 641; makefile: 103
file content (157 lines) | stat: -rw-r--r-- 3,969 bytes parent folder | download | duplicates (2)
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
#include "simpleCalc.h"
#include "calculator.h"
#include "number.h"
#include "string_manip.h"
#include <ctype.h>		       // for isdigit
#include <string.h>		       // for strcmp/strlen/stpcpy/strdup
#include <stdlib.h>		       // for calloc
#include <stdio.h>
#ifdef MEMWATCH
#include "memwatch.h"
#endif

static char append = 1;
static Number cur_number;
static Number prev_number;
static unsigned char operationPending = 0;

void simpleCalcInit();

void simpleCalcEval(const unsigned char op)
{
    Dprintf("op: %c\n", op);
    switch (op) {
	case '+':
	    simple_exp(prev_number, prev_number, wplus, cur_number);
	    break;
	case '-':
	    simple_exp(prev_number, prev_number, wminus, cur_number);
	    break;
	case '*':
	    simple_exp(prev_number, prev_number, wmult, cur_number);
	    break;
	case '/':
	    simple_exp(prev_number, prev_number, wdiv, cur_number);
	    break;
	default:
	    num_set(prev_number, cur_number);
	    break;
    }
}

char *simpleCalc(const unsigned char input, const char *expStr)
{
    unsigned int expStrLen = strlen(expStr);
    simpleCalcInit();
    Dprintf("simpleCalc: %c, %s\n", input, expStr);
    Dprintf(" ~ cur: %f, prev: %f\n", num_get_d(cur_number),
	    num_get_d(prev_number));
    switch (input) {
	case '+': case '-': case '*': case '/':
	    {
		char * expdup = strdup(expStr);
		// Americanize the numbers
		if (conf.in_thou_delimiter != 0) {
		    strstrip(conf.in_thou_delimiter, expdup);
		} else {
		    strstrip(conf.thou_delimiter, expdup);
		}
		if (conf.in_dec_delimiter != 0 && conf.in_dec_delimiter != '.') {
		    strswap(conf.in_dec_delimiter, '.', expdup);
		} else if (conf.dec_delimiter != '.') {
		    strswap(conf.dec_delimiter, '.', expdup);
		}
		// ... and now, depending on the state...
		if (operationPending) {
		    num_set_str(cur_number, expdup, 10);
		    free(expdup);

		    simpleCalcEval(operationPending);
		    operationPending = input;
		    append = 0;
		    set_prettyanswer(prev_number);
		    return strdup(pretty_answer);
		} else {
		    num_set_str(prev_number, expdup, 10);
		    free(expdup);

		    operationPending = input;
		    append = 0;
		    return strdup(expStr);
		}
	    }
	    break;
	case '=':
	    append = 0;
	    {
		char * expdup = strdup(expStr);
		// Americanize the numbers
		if (conf.in_thou_delimiter != 0) {
		    strstrip(conf.in_thou_delimiter, expdup);
		} else {
		    strstrip(conf.thou_delimiter, expdup);
		}
		if (conf.in_dec_delimiter != 0 && conf.in_dec_delimiter != '.') {
		    strswap(conf.in_dec_delimiter, '.', expdup);
		} else if (conf.dec_delimiter != '.') {
		    strswap(conf.dec_delimiter, '.', expdup);
		}
		// ... and now, depending on the state...
		if (! operationPending) {
		    num_set_str(prev_number, expdup, 10);
		    free(expdup);
		} else {
		    num_set_str(cur_number, expdup, 10);
		    free(expdup);
		    simpleCalcEval(operationPending);
		    operationPending = 0;
		}
	    }
	    set_prettyanswer(prev_number);
	    return NULL; // This should cause it to go through the displayAnswer logic
	    break;
	default:
	    if (input == conf.dec_delimiter || isdigit(input)) {
		char *newStr;
		if (append && (strcmp("0",expStr) || input == conf.dec_delimiter)) {
		    newStr = malloc(expStrLen + 2);
		    snprintf(newStr, expStrLen + 2, "%s%c", expStr, input);
		} else {
		    newStr = malloc(2);
		    snprintf(newStr, 2, "%c", input);
		    append = 1;
		}
		return newStr;
	    }
    }
    return NULL;
}

void simpleClearEntry()
{
    simpleCalcInit();
    num_set_ui(cur_number, 0);
}

void simpleCalcInit()
{
    static int initialized = 0;

    if (!initialized) {
	num_init(cur_number);
	num_init(prev_number);
	num_init(last_answer);
	num_init_set_ui(cur_number, 0);
	num_init_set_ui(last_answer, 0);
	initialized = 1;
    }
}

void simpleClearAll()
{
    simpleCalcInit();
    num_set_ui(cur_number, 0);
    num_set_ui(last_answer, 0);
    num_set_ui(prev_number, 0);
    operationPending = 0;
}