-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchar.cpp
218 lines (183 loc) · 6.73 KB
/
char.cpp
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
/*************************
* char.cpp
* editor: amo
*************************/
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bitset>
#include <cmath>
#include <vector>
#include <char.h>
#include <algorithm> // std::for_each
#include <stack>
using namespace amo;
using std::hex;
/* -------------------------------------------------------------------- */
/* my define macro */
/* -------------------------------------------------------------------- */
/* -------------------------------------------------------------------- */
/* global variables */
/* -------------------------------------------------------------------- */
// Allocating and initializing Class's static data member
Char* Char::instance = 0;
/* -------------------------------------------------------------------- */
/* implements */
/* -------------------------------------------------------------------- */
amo::Char::Char() {
std::cout << "[Char::Char()]:" << this << std::endl;
}
amo::Char::~Char() {
std::cout << "[Char::~Char()]:" << this << std::endl;
//delete Char::instance;
}
Char& amo::Char::getInstance() {
if (instance == nullptr) {
std::cout << "[Char::getInstance()]: going to 'instance = new Char()':" << &instance << std::endl;
instance = new Char();
}
//std::cout << "[Char::getInstance()]: returns:" << instance << std::endl;
return *instance;
}
void amo::Char::trim(const char exp[], int& lo, int& hi) {
std::cout << "[Char::trim()]: exp:" << exp << std::endl;
int i = hi-lo+1;
int index = lo;
while (0<i--) std::cout << "[Char::trim()]: char[" << index++ << "]:" << exp[index] << std::endl;
while (lo<=hi && exp[lo]!='(' && exp[lo]!=')') lo++;
while (hi>=lo && exp[hi]!='(' && exp[hi]!=')') hi--;
}
int amo::Char::divide(const char exp[], int lo, int hi) {
std::cout << "[Char::divide()]: lo:" << lo << " and hi:" << hi << std::endl;
int count = 1;
int mi = lo;
while (0!=count && ++mi<hi) {
std::cout << "[Char::divide()]: while=loop, exp[" << mi << "]:" << exp[mi] << std::endl;
if (exp[mi] == '(') {
std::cout << "[Char::divide()]: going to count++ to be " << count+1 << std::endl;
count++;
}
if (exp[mi] == ')') {
std::cout << "[Char::divide()]: going to count-- to be " << count-1 << std::endl;
count--;
}
}
std::cout << "[Char::divide()]: returns:" << mi << std::endl;
return mi;
}
bool amo::Char::paren(const char exp[], int begin, int end){
std::cout << CYAN << "[Char::paren()]: begin:" << begin << " and end:" << end << WHITE << std::endl;
int lo=begin, hi=end, mi;
trim(exp, lo, hi);
std::cout << YELLOW << "[Char::paren()]: trimmed lo:" << lo << " and hi:" << hi << WHITE << std::endl;
if (lo > hi) {
std::cout << YELLOW << "[Char::paren()]: lo > hi and returns true" << WHITE << std::endl;
return true;
}
if (exp[lo] != '(') {
std::cout << YELLOW << "[Char::paren()]: exp[lo] != '(':" << exp[lo] << " and returns false" << WHITE << std::endl;
return false;
}
if (exp[hi] != ')') {
std::cout << YELLOW << "[Char::paren()]: exp[hi] != ')':" << exp[hi] << " and returns false" << WHITE << std::endl;
return false;
}
mi = divide(exp, lo, hi);
std::cout << YELLOW << "[Char::paren()]: divided index:" << mi << WHITE << std::endl;
if (mi > hi) return false;
std::cout << CYAN << "[Char::paren()]: going to recursion" << WHITE << std::endl;
bool ret = paren(exp, lo+1, mi-1) && paren(exp, mi+1, hi);
std::cout << YELLOW << "[Char::paren()]: returns:" << (bool)ret << WHITE << std::endl;
return ret;
}
bool amo::Char::paren(const char exp[], int len) {
amo::Stack<char> stack;
for (int i=0; i<len; i++) {
if (exp[i] == '(' || exp[i] == '[' || exp[i] == '{') {
stack.push(exp[i]);
continue;
}
else if (exp[i] == ')' && stack.pop()!= '(') {
std::cout << YELLOW << "[Char::paren()]: not () and returns false" << WHITE << std::endl;
return false;
}
else if (exp[i] == '}' && stack.pop()!= '{') {
std::cout << YELLOW << "[Char::paren()]: not {} and returns false" << WHITE << std::endl;
return false;
}
else if (exp[i] == ']' && stack.pop()!= '[') {
std::cout << YELLOW << "[Char::paren()]: not [] and returns false" << WHITE << std::endl;
return false;
}
else continue;
}
std::cout << YELLOW << "[Char::paren()]: returns true" << WHITE << std::endl;
return true;
}
#if 0 //HB at bottom
void amo::Char::convert(amo::Stack<char>& stack, int n, int base) {
static char digit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
if (n > 0) {
convert(stack, n/base, base);
std::cout << "[Char::convert()]: going to push:" << digit[n%base] << std::endl;
stack.push(digit[n%base]);
} else std::cout << "[Char::convert()]: return and n:" << n << std::endl;
}
#else
void amo::Char::convert(amo::Stack<char>& stack, int n, int base) {
static char digit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int rmd;
while ((rmd=n%base)>=0 && n!=0) {
stack.push(digit[rmd]);
n = n/base;
}
}
#endif
void amo::Char::convert(char dest[], int n, int base) { //like atoi()
char* ori = dest;
amo::Stack<char> stack;
convert(stack, n, base);
while(!stack.empty()) { //LB at bottom
*(dest++) = stack.pop();
}
*(dest++) = '\0';
dest = ori;
std::cout << "[Char::convert(char*,int,int)]: dest:" << dest << std::endl;
}
void amo::Char::resolve(char* p, amo::Stack<float>& stack) {
float factor = 10;
float fraction = 0.1f;
stack.push(0);
while (isdigit(*p)) {
std::cout << std::fixed << std::setprecision(4) << "[Char::resolve()]: integer resolving:" << *p-'0' << std::endl;
stack.push(stack.pop()*factor+(*p-'0'));
p++;
}
if ('.' != *p) return;
else p++;
while (isdigit(*p)) {
std::cout << "[Char::resolve()]: fraction resolving:" << *p-'0' << ", fraction:" << fraction << ", top:" << stack.top() << std::endl;
stack.push(stack.pop()+(*p-'0')*fraction);
fraction /= 10;
p++;
}
std::cout << "[Char::resolve()]: top of stack:" << stack.top() << std::endl;
std::cout.unsetf( std::ios::fixed );
}
void amo::Char::append(char* exp, int opnd) {
int n = strlen(exp);
int len;
char opnds[64];
convert(opnds, opnd, 10);
len = strlen(opnds);
//exp = (char*) realloc(exp, sizeof(char)*(n+len+1));
strcat(exp, opnds);
}
void amo::Char::append(char* exp, char optr) {
int n = strlen(exp);
//exp = (char*) realloc(exp, sizeof(char)*(n+2));
sprintf(exp+n, "%c", optr);
*(exp+n+1) = '\0';
}