1 | /* |
---|
2 | * conv_f8_utils.cpp |
---|
3 | * L-systemToF1 |
---|
4 | * |
---|
5 | * Created by Maciej Wajcht on 08-09-21. |
---|
6 | * Copyright 2008 __MyCompanyName__. All rights reserved. |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | #include "conv_f8_utils.h" |
---|
11 | |
---|
12 | using namespace std; |
---|
13 | |
---|
14 | bool containsChars(const SString s, char c[]) { |
---|
15 | const char* str=(const char*)s; |
---|
16 | for (int i = 0; i < s.len(); i++) { |
---|
17 | for (int j = 0; j < strlen(c); j++) { |
---|
18 | if (str[i] == c[j]) { |
---|
19 | return true; |
---|
20 | } |
---|
21 | } |
---|
22 | } |
---|
23 | return false; |
---|
24 | } |
---|
25 | |
---|
26 | bool containsChar(std::vector<char> tab, char c) { |
---|
27 | for (int i = 0; i < tab.size(); i++) { |
---|
28 | if (tab[i] == c) { |
---|
29 | return true; |
---|
30 | } |
---|
31 | } |
---|
32 | return false; |
---|
33 | } |
---|
34 | |
---|
35 | int reverseFindInSString(const SString s, char c, int index) { |
---|
36 | if (index > 0) { |
---|
37 | index = (s.len() - 1 > index) ? index : s.len() - 1; //min(index, s.len() - 1) |
---|
38 | } else { |
---|
39 | index = s.len() - 1; |
---|
40 | } |
---|
41 | |
---|
42 | const char* str=(const char*)s; |
---|
43 | for (int i = index; i >= 0; i--) { |
---|
44 | if (str[i] == c) { |
---|
45 | return i; |
---|
46 | } |
---|
47 | } |
---|
48 | return -1; |
---|
49 | } |
---|
50 | |
---|
51 | SString trimSString(const SString s) { |
---|
52 | SString result = ""; |
---|
53 | for (int i = 0; i < s.len(); i++) { |
---|
54 | char c = s.charAt(i); |
---|
55 | if (c != ' ' && c != '\t') { |
---|
56 | result += c; |
---|
57 | } |
---|
58 | } |
---|
59 | return result; |
---|
60 | } |
---|
61 | |
---|
62 | double parseDouble(const SString &s) { |
---|
63 | const char* str = s; |
---|
64 | std::istringstream i(str); |
---|
65 | double x; |
---|
66 | if (!(i >> x)) |
---|
67 | throw BadConversion("parseDouble(\"" + string(str) + "\")"); |
---|
68 | return x; |
---|
69 | } |
---|
70 | |
---|
71 | int parseInt(const SString &s) { |
---|
72 | const char* str = s; |
---|
73 | std::istringstream i(str); |
---|
74 | int x; |
---|
75 | if (!(i >> x)) |
---|
76 | throw BadConversion("parseInt(\"" + string(str) + "\")"); |
---|
77 | return x; |
---|
78 | } |
---|
79 | |
---|
80 | SString stringToSString(string str) { |
---|
81 | return SString(str.c_str()); |
---|
82 | } |
---|
83 | |
---|
84 | string sstringToString(SString sstr) { |
---|
85 | const char* tmp = sstr; |
---|
86 | return string(tmp); |
---|
87 | } |
---|
88 | |
---|
89 | SString convertReversePolishNotationToNatural(const SString &s) { |
---|
90 | SString result = ""; |
---|
91 | SString tok = ""; |
---|
92 | int pos = 0; |
---|
93 | stack<SString> stck; |
---|
94 | |
---|
95 | while (s.getNextToken(pos, tok, ';')) { |
---|
96 | if (tok.len() == 1 && (tok[0] == '+' || tok[0] == '-' || tok[0] == '*' || tok[0] == '/')) { |
---|
97 | SString s1, s2; |
---|
98 | if (!stck.empty()) { |
---|
99 | s1 = stck.top(); |
---|
100 | stck.pop(); |
---|
101 | } else { |
---|
102 | throw ParseExpressionException("Not enough elements on stack: " + sstringToString(s)); |
---|
103 | } |
---|
104 | if (!stck.empty()) { |
---|
105 | s2 = stck.top(); |
---|
106 | stck.pop(); |
---|
107 | } else { |
---|
108 | throw ParseExpressionException("Not enough elements on stack: " + sstringToString(s)); |
---|
109 | } |
---|
110 | switch (tok[0]) { |
---|
111 | case '+': stck.push(s2 + "+" + s1); break; |
---|
112 | case '-': stck.push(s2 + "-" + s1); break; |
---|
113 | case '*': stck.push(s2 + "*" + s1); break; |
---|
114 | case '/': stck.push(s2 + "/" + s1); break; |
---|
115 | } |
---|
116 | } else if (tok.len() > 0) { |
---|
117 | stck.push(tok); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | result = stck.top(); |
---|
122 | |
---|
123 | return result; |
---|
124 | } |
---|
125 | |
---|
126 | double parseExpression(const SString &s) { |
---|
127 | double result = 0; |
---|
128 | SString tok = ""; |
---|
129 | int pos = 0; |
---|
130 | stack<double> stck; |
---|
131 | |
---|
132 | while (s.getNextToken(pos, tok, ';')) { |
---|
133 | if (tok.len() == 1 && (tok[0] == '+' || tok[0] == '-' || tok[0] == '*' || tok[0] == '/')) { |
---|
134 | double n1, n2; |
---|
135 | if (!stck.empty()) { |
---|
136 | n1 = stck.top(); |
---|
137 | stck.pop(); |
---|
138 | } else { |
---|
139 | throw ParseExpressionException("Not enough elements on stack: " + sstringToString(s)); |
---|
140 | } |
---|
141 | if (!stck.empty()) { |
---|
142 | n2 = stck.top(); |
---|
143 | stck.pop(); |
---|
144 | } else { |
---|
145 | throw ParseExpressionException("Not enough elements on stack: " + sstringToString(s)); |
---|
146 | } |
---|
147 | switch (tok[0]) { |
---|
148 | case '+': stck.push(n2 + n1); break; |
---|
149 | case '-': stck.push(n2 - n1); break; |
---|
150 | case '*': stck.push(n2 * n1); break; |
---|
151 | case '/': |
---|
152 | if (n1 == 0) { |
---|
153 | throw ParseExpressionException("Division by zero"); |
---|
154 | } |
---|
155 | stck.push(n2 / n1); |
---|
156 | break; |
---|
157 | } |
---|
158 | } else if (tok.len() > 0) { |
---|
159 | stck.push(parseDouble(tok)); |
---|
160 | }/* else { |
---|
161 | throw ParseExpressionException("Unrecognized element in input string '" + sstringToString(tmp) + "'"); |
---|
162 | }*/ |
---|
163 | } |
---|
164 | |
---|
165 | result = stck.top(); |
---|
166 | |
---|
167 | return result; |
---|
168 | } |
---|