-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculadora.cpp
More file actions
793 lines (695 loc) · 21 KB
/
Copy pathcalculadora.cpp
File metadata and controls
793 lines (695 loc) · 21 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//
// This is example code from Chapter 7.2 "Input and output" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//
/*
Statement :
Declaration
Expression
Print
pow
let -> #
Quit
surt
Print :
;
Quit:
quit
surt
Calculation Statement
Expression :
Term
Expression + Term
Expression - Term
Term :
Primary
Term * Primary
Term / Primary
Term % Primary
Term ! Primary
Primary :
Number
( Expression )
- Primary
+ Primary
sqrt Primary
Number :
Floating-point-literal
-------------------------------------------------------------
-> Input comes from cin through the Token_stream called td <-
-------------------------------------------------------------
*/
#include<iostream>
#include<stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <cmath>
#include <string>
#include <sstream>
#include<std_lib_facilities.h>
using namespace std;
#define debug 1
//------------------------------------------------------------------------------
//class Variable;
class Symbol_Table{
public:
string constant;
string name;
double value;
double val;
double get_value(string s);
void set_value(string s, double d);
bool is_declared(string var);
double define_name(string var, double val);
double define_name_const(string var, double val);
/*Variable*/
Symbol_Table(string nm, double val)
:name(nm), value(val){ }
/*Variable*/
Symbol_Table(string constat1, string nm, double val)
:constant(constat1), name(nm), value(val){ }
};
/*#####################Cambiat per Symbol_Table########################*/
/*class Variable
{
public:
string constant;
string name;
double value;
Variable(string nm, double val)
:name(nm), value(val){ }
Variable(string constat1, string nm, double val)
:constant(constat1), name(nm), value(val){ }
};
*/
class Token {
public:
char kind; // what kind of token
double value; // for numbers: a value
string name;
Token(char ch) // make a Token from a char
:kind{ch} {}
Token(char ch, double val) // make a Token from a char and a double
:kind{ch}, value{val} {}
Token(char ch, string n)
:kind{ch}, name{n} {}
};
//------------------------------------------------------------------------------
class Token_stream {
public:
Token_stream(); // make a Token_stream that reads from cin
Token get(); // get a Token (get() is defined elsewhere)
void putback(Token t); // put a Token back
void ignore(char c);
// void putback2(Token t, Token t2);
// void clean_up_mess();
private:
bool full{false}; // is there a Token in the buffer?
Token buffer; // here is where we keep a Token put back using putback()
};
//------------------------------------------------------------------------------
const char number ='8'; //global!! //8 <- és número
const char quit = 'e';
const char print = ';';
const string prompt = "|=>";
const char result = '='; //used to indicate that what follows is a result
//vector<Variable> var_table; //cambiat per Symbol_Table
vector<Symbol_Table> var_table;
Token_stream ts; // provides get() and putback()
// The constructor just sets full to indicate that the buffer is empty:
Token_stream::Token_stream()
:full(false), buffer(0) // no Token in buffer
{
}
void Token_stream::ignore(char c)
//c representa el tipus de Token
{
//primera mirada al buffer?:
if(full && c==buffer.kind){
full=false;
return;
}
full=false;
//ara busca introduccions
char ch=0;
while(cin>>ch){
//cout << " c=" << endl;
if(ch==c) return;
}
}
void clean_up_mess()
{
ts.ignore(print); //pagina 216-217 del llibre, just al cambiar de pàgina
}
//------------------------------------------------------------------------------
double get_value(string s)// return the Value of a variable named s
{
cout << "s = " << s<< endl;
for(const /*Variable*/ Symbol_Table& v: var_table)
if(v.name == s) return v.value;
error("get: undefined variable ",s);//, s);
}
void set_value(string s, double d)
// set the Variable named s to d
{
for (/*Variable*/Symbol_Table& v : var_table)
if (v.name == s) {
if(debug==1)cout << "Estic dins del setter Variable 2" << endl;
v.value = d;
return;
}
error("set: undefined variable ",s);//, s);
}
void set_value(string c, string s, double d)
// set the Variable named s to d
{
for (/*Variable*/Symbol_Table& v : var_table)
if (v.name == s) {
if(debug==1)cout << "Estic dins del setter Variable 3" << endl;
v.value = d;
return;
}
error("set: undefined variable ",s);//, s);
}
bool is_declared(string var)
// is var already in var_table ?
{
for (const /*Variable*/Symbol_Table& v : var_table)
if (v.name == var) return true;
return false;
}
double define_name(string var, double val)
// add (var,val) to var_table
{
if (is_declared(var)){
//error(var," declared twice");
char c='1';
do{
cout << "Var name already exists, do you want overwrite value ?(s,S,n,N)" ;
cin >> c;
}while(c!='s' && c!='n' && c!='S' && c!='N');
if(c!='s' && c!='S'){
error(var," declared twice");
}
else{
var_table.push_back(/*Variable*/Symbol_Table(var,val)); //falta el setter ... pagina 218
}
}
else{
var_table.push_back(/*Variable*/Symbol_Table(var,val));
}
return val;
}
double define_name_const(string var, double val)
// add (var,val) to var_table
{
if (is_declared(var)){
error(var," declared twice");
}
else{
var_table.push_back(/*Variable*/Symbol_Table(var,val)); //falta el setter ... pagina 218
}
return val;
}
// The putback() member function puts its argument back into the Token_stream's buffer:
void Token_stream::putback(Token t)
{
if (full) {
error("putback() into a full buffer\nNO HI HA RES AL BUFFER??\n");
//return (-3);
}
buffer = t; // copy t to buffer
full = true; // buffer is now full
}
//------------------------------------------------------------------------------
const char name = 'a';
const char let = '#';
const string decllet = "#";
const char arrel = 'r';
const string declsqrt = "sqrt";
const char pows='p';
const string declpow = "pow";
const char surt='s';
const string declsurt="surt";
const char quit1='q';
const string declquit="quit";
const char exit1='e';
const string declexit="exit";
const char ajuda='h';
const string declajuda="ajuda";
const string declhelp="help";
const char constat='c';
const string declconstat1="declara";
// name token
// declaration token
// declaration keyword
Token Token_stream::get() //buscar espais i retorns de carro '\n' amb la llibreria isspace(ch) -> cert si espai
//lectura de dades des del teclat i composició del Token
{
if (full) { // do we already have a Token ready?
// remove token from buffer
full=false;
return buffer;
}
char ch;
cin >> ch; // note that >> skips whitespace (spsmace, newline, tab, etc.)
// cout << " ch=" << ch << endl;
switch (ch) {
case quit:
case print: // for "print"
case result:
case '%':
case '!':
case '{':
case '}':
case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
case ',':
case '#':
//case '_':
return Token(ch); // let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
// cout << "ch = " << ch << endl;
cin.putback(ch); // put digit back into the input stream
double val;
cin >> val; // read a floating-point number
// cout << "val = " << val << endl << " number = " << number << endl;
return Token(number,val); // let '8' represent "a number"
}
default:
if(isalpha(ch)){ //si es let, val l, entro aquí.
string s;
//if(debug==1)cout << "s(abans) = " << s << endl;
s += ch;
//if(debug==1)cout << "s(després) = " << s << endl;
while(cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch=='_')){ // comparar aquí si ch == _ (underscore), no ?
s+=ch;
/* if(debug==1){
cout << endl << "carrega" << endl;
cout << endl << " ch = " << ch << endl << " s = " << s << endl << "decllet = " << decllet << endl << " s = " << s << endl;
cout << "s = " << s << endl;
}
*/
}
cin.putback(ch);
//cout << "ch = " << ch;
if(s==declsurt){
if(debug==1)cout << "SURT?" << endl;
return Token{surt};
}
if(s==declquit){
if(debug==1)cout << "QUIT?" << endl;
return Token{quit1};
}
if(s==declexit){
if(debug==1)cout << "EXIT?" << endl;
return Token{exit1};
}
if(s==decllet){//let
if(debug==1) {
cout << endl << "decllet" << endl;
cout << endl << "surto token{let}" << endl<< " let = " << let << endl << " ch =" << ch << endl << " s = " << s << endl << "decllet=" << decllet << endl;
}
return Token{let};
}
if(s==declsqrt){//arrel
/*if(debug==1){
cout << endl << "declsqrt" << endl;
cout << "faig arrel ?(get)" << endl;
}
*/
return Token{arrel};
}
if(s==declpow) {//pow
if(debug==1){
/*cout << endl << "declpow" << endl;
cout << "faig la potència?" << endl;
cout << "falta llegir la segona variable per fer l'exponencial, com ho faig ?" << endl;
*/
}
return Token{pows};
//float i;
//llegir
//expression
//o
//primary
//o
//term
//com ho faig ? com ho puc fer?
//return (0); //Token{pows};
}
if (s==declconstat1) {
if(debug==1)cout << endl << " Declara VARIABLE? (crear clase i metodes pk no es pugui sobreescriure?)" << endl;
return Token{constat};
}
if(s==declajuda || s==declhelp) {
if(debug==1) cout << endl << endl << endl << endl << endl << "AJUDA ?" << endl << endl << endl << endl;
return Token{ajuda};
}
return Token{name,s};
}
//cout << "ch =" << ch << endl;
error("Bad token");
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
double expression(); // declaration so that primary() can call expression()
//------------------------------------------------------------------------------
// deal with numbers and parentheses
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '{':
{
double d = expression();
t = ts.get();
if (t.kind != '}') error("'}' expected");
return d;
}
case'(': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
return d;
}
case number: // we use '8' to represent a number
return t.value; // return the number's value
case '-':
return - primary();
case '+':case '=':
return primary();
case 'r':
double valor;
double resultat;
valor = primary();
if(valor <0) {
cout << " D'aquest valor, " << valor << ", NO es pot calcular l'arrel REAL" << endl;
return (0);
}
else {
//if(debug==1)cout << " valor = " << valor;
resultat=sqrt(valor);
//cout << endl << " El resultat de l'arrel de "<< valor << " és " ; // << resultat << endl;
return (resultat);
}
case 'a': //si 'a', recull valor!!!!
if(debug==1)cout << "get_value(t.kind) = " << get_value(t.name) << endl;
return get_value(t.name);
/*case 'p': // forçar pow(x,i) i integer ? :w
*
double valore;
double valorp;
valorp=primary();
valore=expression();
cout << endl << "fa la potencia ara ? (primary)ÉS EL BOO??, t.value =" << t.value << "valorp = " << valorp << endl;
*/
default:
//if(debug==1)cout << "t.value = " << t.value << endl << "t.kind =" << t.kind << endl;
error("primary expected");
}
}
//------------------------------------------------------------------------------
int factorial(int left)
{
// if(debug)cout << " dins de factorial ?\n" ;
if (left!=1 && left!=0){
// if(debug) cout << "\n factorial de " << left << " * factorial(" << left-1 << ") = " << left*factorial(left-1) ;
return( left* factorial(left-1));
}
else {
//if(debug) cout << "left = " << left << "\n";
if(left==0) return(1);
//if(debug) cout << "\nfactorial de 1 " << endl;
return(1);
}
}
double sqrt1( double val){ //arrel
double prova;
if(val >1){
prova =sqrt(val);
if(debug==1)cout << "prova = " << prova << endl;
}
else{
cout << "\n Error!!!!!!!!!!!!! valor no vàlid" << endl;
// cout << "\n val = " << val << "\n sqrt(val) = " << sqrt(val) << endl;
}
return(1);
}
// deal with *, /, and %
double term()
{
double left = primary();
double valor;
Token t = ts.get(); // get the next token from token stream
while(true) {
switch (t.kind) {
case '!':
// if(debug)cout << "\nfactorial ?\n left = " << factorial((int)left) << "\n";
valor=left;
left=factorial(left);
cout << "El factorial de " << (int)valor << " és " << (int)left << "\nIntro > ";
t = ts.get();
break;
case '*':
left *= primary();
t = ts.get();
break;
case '/':
{
double d = primary();
if (d == 0) error("divide by zero");
left /= d;
t = ts.get();
break;
}
case '%':
{
//pagina 209 del llibre pdf, mirar narrow_cast
//int i1 = narrow_cast<int>(left); //no incloc narrow_cast, mirar-ho bé ... com fer-ho
//int i2 = narrow_cast<int>(primary());
try{
//pàgina 208 del llibre pdf pregunta 7. What does narrow_cast do?
int i1 = narrow_cast<int>(left);
int i2 = narrow_cast<int>(primary());
if (i2 == 0) error("%: divide by zero");
left = i1 % i2;
t = ts.get();
break;
/* double d = primary();
if(d==0) error("divide by zero");
// if(i2==0) error("% : divide by zero");
left = fmod(left,d);//i1 % i2;
t = ts.get();
break;
*/
}
catch (exception& e){
cout << "Aquí hi ha un error ? pot ser ? e.what = " << e.what() << endl;
}
}
default:
ts.putback(t); // put t back into the token stream
return left;
}
}
}
//------------------------------------------------------------------------------
// deal with + and -
double expression()
{
double left = term(); // read and evaluate a Term
Token t = ts.get(); // get the next token from token stream
while(true) {
switch(t.kind) {
case '+':
left += term(); // evaluate Term and add
t = ts.get();
break;
case '-':
left -= term(); // evaluate Term and subtract or change sign
t = ts.get();
break;
default:
ts.putback(t); // put t back into the token stream
return left; // finally: no more + or -: return the answer
}
}
}
double define_name(string var, double val);
double print_ajuda()
{
cout << endl<< endl << endl << endl << endl << "Funció ajuda" << endl << endl << endl << endl << endl << endl<< endl;
cout << "per declara una variable escriure : # variable = valor ;" << endl << " Per exemple # x = 3 ;" << endl;
cout << "per calcular l'arrel d'un nombre escriure : sqrt(valor);" << endl << " Per exemple sqrt(25);" << endl;
cout << "per sortir del programa has d'escriure : quit ó exit" << endl;
return (0);
}
double calcul_pow()
{
try{
Token t= ts.get();//pow
Token t2 = ts.get(); //valor1
double d=t2.value; //Read first value
Token t3 = ts.get(); //coma
//Token t4 = ts.get(); //valor2 //forçar integer
//float i=(int)t4.value;
try{
Token t4=ts.get(); //valor2 //forçar integer ?
int i= (int)t4.value;
if(debug==1)cout << "i = " << i << endl << " d=" << d << endl;
for (int j=1; j<i; j++) d=d*t2.value;
Token t5 = ts.get();//)
return(d);
}
catch(exception& e){
cout << "no és un enter ?" << endl;
}
return(0);
}
catch(exception& e){
cout << "Error de conversió" << endl;
return(0);
}
}
double declaration()
// assume we have seen "let”
// handle : name = expression
// declare a variable called "name ” with the initial value "expression”
{
Token t = ts.get();
//cout << "DEBUGt.kind = " << t.kind << endl << " name =" << name << endl; //kind value i name
cout << "t.kind = " << t.kind << endl << "t.value = " << t.value << endl << "t.name = " << t.name << endl;
if (t.kind != name) error ("name expected in declaration");
string var_name = t.name;
Token t2 = ts.get();
if ((t2.kind != '=')&&(t2.kind != '_')) error("_ or = missing in declaration of ", var_name);//aqui és on es llegeix el '_', és aquí on l'he de permetre.
//cout << "si?" << endl;
double d = expression();
cout << " d= " << d << endl;
define_name(var_name,d);
if(debug==1) cout << "var_name = " << var_name << "\n d =" << d << endl;
set_value(var_name,d);
return d;
}
double declaration_constants()
// assume we have seen "let”
// handle : name = expression
// declare a variable called "name ” with the initial value "expression”
{
Token t = ts.get();
cout << "t.kind = " << t.kind << endl << " constat = " <<constat << endl; //<< " t.value = " << t.value << "t.name = " << t.name;
//if( t.kind != constat ) error ("constant expected in declaration");
Token t1 = ts.get();
//cout << "DEBUGt.kind = " << t.kind << endl << " name =" << name << endl; //kind value i name
cout << "t1.kind = " << t1.kind << endl ; //<< "t1.value = " << t1.value << endl << "t1.name = " << t1.name << endl;
//if (t.kind != constat || t1.kind != name) error ("name expected in declaration or constant expected in declaration");
string var_name = t1.name;
Token t2 = ts.get();
if ((t2.kind != '=')&&(t2.kind != '_')) error("_ or = missing in declaration of ", var_name);//aqui és on es llegeix el '_', és aquí on l'he de permetre.
//cout << "si?" << endl;
double d = expression();
cout << " d= " << d << endl;
define_name_const(var_name,d);
if(debug==1) cout << "var_name = " << var_name << "\n d =" << d << endl;
set_value(var_name,d);
return d;
}
double statement()
{
Token t = ts.get();
switch (t.kind) {
case constat:
return declaration_constants();
case let:
return declaration();
case pows:
// cout << "statement pow" << endl << "t.kind = "<< t.kind << endl;
return calcul_pow();
// cout << "ts.putback(t) = " << ts.putback(t) << endl;
// return (ts.putback(t));
case ajuda:
return print_ajuda();
default:
//cout << "passa ?" << endl ;
ts.putback(t);
return expression();
}
}
void calculate()
{
while (cin)
try{
cout << prompt; // print prompt
Token t = ts.get();
while(t.kind == print) t = ts.get();
if(t.kind == quit1){
// keep_window_open();
// return (0);
if(debug==1)cout << "AQUI ara hauria de sortir per \"QUIT\"" << endl;
return;
}
if(t.kind== surt){
if(debug==1)cout << "AQUI ara hauria de sortir per \"SURT\"" << endl;
return;
}
if(t.kind== exit1){
if(debug==1)cout << "AQUI ara hauria de sortir per \"EXIT\"" << endl;
return;
}
if(debug==1)cout << "t.kind = " << t.kind << endl;
ts.putback(t);
cout << result << statement() << endl; //expression() << endl;
}
catch(exception& e){ //si hi ha error d'introducció, surt aquí ...
// cout << endl << "surto aqui, oi, pel let ??" << endl ;
cerr << e.what() << endl;
clean_up_mess();
}
}
//------------------------------------------------------------------------------
int main()
try
{
//predefined names :
define_name("pi", 3.1415926535);
define_name("ne", 2.7182818284);
define_name("k", 1000);
if(debug==1)cout << "\npàgina 226 del pdf, TEMA 7, Exercises 5"<< endl;
if(debug==1)cout << "Exercises" << endl << "5. Modify Token_stream::get() to return Token(print) when it sees a newline. This implies looking for whitespace characters and treating newline ('\n') specially. You might find the standard library function isspace(ch), which returns true if ch is a whitespace character, useful." << endl;
/*
*
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cplusplus.com/doc/tutorial/templates/
http://www.cplusplus.com/doc/tutorial/classes2/
http://www.cplusplus.com/doc/tutorial/inheritance/
http://www.cplusplus.com/doc/tutorial/polymorphism/
*/
calculate();
if(debug==1)cout << "Fora calculate" << endl;
keep_window_open();
return 0;
}
catch (exception& e){
cerr << "error: " << e.what() << endl;
keep_window_open("~~");
return (1);
}
catch (...) {
cerr << "Oops: unknown exception!\n";
keep_window_open("~~");
return (2);
}