-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.c
97 lines (87 loc) · 2.75 KB
/
calc.c
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
/* calc.c */
#include <stdio.h>
#include <stdlib.h>
#include "forms.h"
#include "perms.h"
#include "rpn.h"
double compute(int size, unsigned int form, double_string *ds, counter *ops) {
rpn_stack s = malloc(sizeof(rpn_stack));
unsigned int shift = form;
int ops_p = 0;
double v = 0.0;
int i = 0;
for(i = 0 ; i < size; i++) {
if((shift & 0x01) == 1) {
rpn_push(&s, ds->value);
ds = ds->next;
} else {
switch(ops->values[ops_p++]) {
case 0 : v = rpn_calc(&s, '+'); break;
case 1 : v = rpn_calc(&s, '-'); break;
case 2 : v = rpn_calc(&s, '*'); break;
case 3 : v = rpn_calc(&s, '/'); break;
}
}
shift = shift >> 1;
}
rpn_destroy(s);
return v;
}
double compute_verbose(int size, unsigned int form, double_string *ds, counter *ops) {
rpn_stack s = malloc(sizeof(rpn_stack));
unsigned int shift = form;
int ops_p = 0;
double v = 0.0;
double p1, p2;
int op_last = 0;
int i = 0;
for(i = 0 ; i < size; i++) {
if((shift & 0x01) == 1) {
rpn_push(&s, ds->value);
ds = ds->next;
op_last = 0;
} else {
switch(ops->values[ops_p++]) {
case 0 : p1 = s->value;
p2 = s->p->value;
v = rpn_calc(&s, '+');
if(op_last == 0)
printf("%.2f + %.2f = %.2f\n", p1, p2, v);
else
printf(" + %.2f = %.2f\n", p2, v);
op_last = 1;
break;
case 1 : p1 = s->value;
p2 = s->p->value;
v = rpn_calc(&s, '-');
if(op_last == 0)
printf("%.2f - %.2f = %.2f\n", p1, p2, v);
else
printf(" - %.2f = %.2f\n", p2, v);
op_last = 1;
break;
case 2 : p1 = s->value;
p2 = s->p->value;
v = rpn_calc(&s, '*');
if(op_last == 0)
printf("%.2f * %.2f = %.2f\n", p1, p2, v);
else
printf(" * %.2f = %.2f\n", p2, v);
op_last = 1;
break;
case 3 : p1 = s->value;
p2 = s->p->value;
v = rpn_calc(&s, '/');
if(op_last == 0)
printf("%.2f / %.2f = %.2f\n", p1, p2, v);
else
printf(" / %.2f = %.2f\n", p2, v);
op_last = 1;
break;
}
}
shift = shift >> 1;
}
rpn_destroy(s);
return v;
}