-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblatt06_a.cpp
More file actions
390 lines (343 loc) · 8.91 KB
/
blatt06_a.cpp
File metadata and controls
390 lines (343 loc) · 8.91 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Programmentwurf zu Aufgabe 6a, WS 2015/2015
// Programmieren II fuer Mathematiker
#include <iostream>
#include <iomanip>
#include <list>
#include <sstream>
using namespace std;
class Monom
{
public:
long coeff;
unsigned deg;
Monom (long coeff = 0, unsigned deg = 0) : coeff(coeff), deg(deg) { }
Monom operator-() const { return Monom(+coeff, deg); }
Monom operator-() const { return Monom(-coeff, deg); }
friend unsigned grad(Monom const & m) { return m.deg; }
friend long wert(Monom const & m) { return m.coeff; }
friend bool operator< (Monom m1, Monom m2) { return m1.deg < m2.deg ? true : m1.coeff < m2.coeff; }
friend bool operator==(Monom m1, Monom m2) { return m1.deg == m2.deg && m1.coeff == m2.coeff; }
template <typename T, T zero = T(), T one = T(1)> inline
T operator()(T const & value)
{
if (deg == 0) return one;
if (value == zero) return zero;
unsigned hiBit = ~((~0u) >> 1u);
while ((hiBit & deg) == 0) hiBit >>= 1u;
// hiBit maskiert erstes gesetztes Bit von deg (ist != 0, da deg != 0);
T erg = value;
while (hiBit >>= 1u)
{
erg *= erg;
if (hiBit & deg) erg *= value;
}
return erg;
}
friend ostream & operator<<(ostream & os, Monom const & m)
{
return os << m.coeff << "x^" << m.deg;
}
};
class Polynom
{
private:
typedef list<Monom> mlist;
mlist coeff;
public:
Polynom() { }
Polynom(long coeff = 0, unsigned deg = 0) : coeff(coeff == 0 ? 0 : 1, Monom(coeff, deg)) { }
Polynom(Monom monom) : coeff(wert(monom) == 0 ? 0 : 1, monom) { }
Polynom operator-() const
{
Polynom ret = *this;
for (auto it = ret.coeff.begin();
it != ret.coeff.end();
++it)
{
it->coeff = -(it->coeff);
}
return ret;
}
friend Polynom operator+(Polynom const & pol1, Polynom const & pol2)
{
Polynom ret = pol1;
auto it2 = pol2.coeff.begin();
for (auto it1 = ret.coeff.begin();
it1 != ret.coeff.end() && it2 != pol2.coeff.end();
++it1)
{
if (it1->deg > it2->deg)
{
auto it2range = it2;
while (++it2range != pol2.coeff.end() && it1->deg > it2range->deg);
ret.coeff.insert(it1, it2, it2range);
it2 = it2range;
}
else if (it1->deg == it2->deg)
{
it1->coeff += it2->coeff;
++it2;
}
}
// now it1 == ret.coeff.end() or it2 == pol2.coeff.end()
// second one doesn't actually matter at all:
// assuming second implies the following command to do nothing,
// so we only care for first one. Then it1 == ret.coeff.end()
ret.coeff.insert(ret.coeff.end() , it2, pol2.coeff.end());
return ret;
}
Polynom & operator+=(Polynom const & pol) { return *this = *this + pol; }
friend
Polynom operator-(Polynom const & pol1, Polynom const & pol2) { return pol1 + (-pol2); }
Polynom & operator-=(Polynom const & pol) { return *this = *this - pol; }
Polynom & operator*=(Monom const & mon)
{
for (auto it = coeff.begin();
it != coeff.end();
++it)
{
it->coeff *= mon.coeff;
it->deg += mon.deg;
}
return *this;
}
Polynom operator*(Monom const & mon) const
{
cout << "Mult Polynom " << *this << " and Monom " << mon << endl;
Polynom ret = *this;
ret *= mon;
return ret;
}
Polynom & operator*=(Polynom const & pol)
{
cout << "Mult Polynom " << *this << " with Polynom " << pol << endl;
Polynom summe;
for (auto it = pol.coeff.begin();
it != pol.coeff.end();
++it)
{
summe += *this *= *it;
}
return *this = summe;
}
friend
Polynom operator*(Polynom const & pol1, Polynom const & pol2)
{
cout << "Mult Polynom " << pol2 << " and Polynom " << pol2 << endl;
Polynom ret = pol1;
ret *= pol2;
return ret;
}
friend
ostream & operator<<(ostream & os, Polynom const & pol)
{
auto it = pol.coeff.begin();
switch (it->deg)
{
case 0: os << it->coeff; break;
case 1: os << it->coeff << 'x'; break;
default: os << it->coeff << "x^" << it->deg;
}
while (++it != pol.coeff.end())
{
switch (it->deg)
{
case 0: os << " + " << it->coeff; break;
case 1: os << " + " << it->coeff << 'x'; break;
default: os << " + " << it->coeff << "x^" << it->deg;
}
}
return os;
}
};
const bool debug = true;
class Ausdruck
{
private:
string str;
string::iterator pos;
inline
void debugMsg(string msg)
{
if (debug)
cout << " " << msg << endl
<< str << endl
<< string(pos - str.begin(), ' ') << '^' << endl;
}
template <typename T> inline
void debugMsg(string msg, T value)
{
if (debug)
cout << " " << msg << ": " << value << endl
<< str << endl
<< string(pos - str.begin(), ' ') << '^' << endl;
}
inline void inc()
{
if (pos++ == str.end()) fehler();
}
// Wandelt einen Ausdruck der Form
// Ausdruck ::= Term ([+|-] Term)*
// in einen gültiges Polynom um.
Polynom WertAusdruck()
{
debugMsg("WertAusdruck: enter");
Polynom summe;
switch (*pos)
{
case '-': inc(); summe = -WertTerm(); break;
case '+': inc(); // NO break;
default : summe = WertTerm();
}
for(;;)
{
debugMsg("WertAusdruck: loop");
debugMsg("WertAusdruck: Partial value", summe);
switch (*pos)
{
case '+': inc(); summe += WertTerm(); break;
case '-': inc(); summe -= WertTerm(); break;
default: goto exit_for;
}
}
exit_for:
debugMsg("WertAusdruck: exit");
return summe;
}
// Wandelt einen Term der Form
// Term ::= Faktor ([*|/] Faktor)*
// in einen gültiges Polynom um.
Polynom WertTerm()
{
debugMsg("WertTerm: enter");
Polynom produkt = WertFaktor();
for (;;)
{
debugMsg("WertTerm: loop");
debugMsg("WertTerm: Partial value", produkt);
switch(*pos)
{
case '*': inc(); produkt *= WertFaktor();
cout << produkt << endl;
break;
//case '/': inc(); produkt /= WertFaktor(); break;
default: goto exit_for;
}
}
exit_for:
debugMsg("WertTerm: exit");
return produkt;
}
// Wandelt eine ganze Zahl der Form
// Ganzzahl ::= [0-9][0-9]*
// in eine gültige ganze Zahl um.
unsigned WertGanzzahl()
{
debugMsg("WertGanzzahl: enter");
string substr(pos, str.end());
istringstream iss(substr);
istringstream::pos_type anf = iss.tellg();
unsigned value;
iss >> value;
if (iss.eof())
{
pos += substr.size();
}
else
{
pos += iss.tellg() - anf;
}
debugMsg("WertGanzzahl: Partial value", value);
debugMsg("WertGanzzahl: exit");
return value;
}
// Wandelt ein TeilMonom der Form
// TeilMonom ::= (^Ganzzahl)?
// in einen gültiges Monom um.
// Falls (^Ganzzahl) nicht angegeben wird, wird x^1 zurückgegeben.
Monom WertMonom()
{
debugMsg("WertMonom: enter");
unsigned deg = 1;
if (*pos == '^')
{
inc();
deg = WertGanzzahl();
}
auto ret = Monom(1, deg);
debugMsg("WertMonom: Partial value", ret);
debugMsg("WertMonom: exit");
return ret;
}
Polynom WertFaktor()
{
debugMsg("WertFaktor: enter");
Polynom wert;
if (*pos == '(')
{
debugMsg("WertFaktor: enter section parenthesis");
inc();
wert = WertAusdruck();
debugMsg("WertFaktor: Partial value", wert);
if (*pos != ')') fehler();
inc();
wert *= WertMonom();
debugMsg("WertFaktor: exit section parenthesis");
}
else if ('0' <= *pos && *pos <= '9')
{
debugMsg("WertFaktor: enter section integer number");
unsigned value = WertGanzzahl();
Monom m = WertMonom();
wert = Monom(m(value), 0);
if (*pos == 'x')
{
inc();
wert *= WertMonom();
}
debugMsg("WertFaktor: exit section integer number");
}
else if (*pos == 'x')
{
debugMsg("WertFaktor: enter section variable");
inc();
wert = WertMonom();
debugMsg("WertFaktor: exit section variable");
}
else
{
debugMsg("WertFaktor: no valid section");
fehler();
}
debugMsg("WertFaktor: Partial value", wert);
debugMsg("WertFaktor: exit");
return wert;
}
void fehler()
{
cout << "Fehler:" << endl
<< str << endl
<< string(pos - str.begin(), ' ') << '^' << endl;
exit(1);
}
public:
Ausdruck(string s)
: str(s),
pos(str.begin())
{ }
Polynom auswerten()
{
static Polynom value = WertAusdruck();
return value;
}
};
int main(void)
{
cout << "Produnkt von polynomen falsch!"
return 0;
string s;
cout << "Arithm. Ausdruck: ";
cin >> s;
cout << Ausdruck(s).auswerten() << endl;
return 0;
}