-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrixCalc.cpp
837 lines (746 loc) · 22.9 KB
/
matrixCalc.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
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
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/*---------------------------------------------------------------------------*\
* matrixCalc.cpp *
* A calculator with support for matrices. *
* *
* Written by: Colin Hamilton, Tufts University *
* Last Modified: May 8, 2014 *
* *
* Calculator uses RPN notation. Objects (numbers or matrices) are stored *
* on a stack, and operators may be used on them. Numbers may be entered *
* as literals. Matrices can be created with the "m" command. *
* Numbers are stored as fraction, for minimal loss of accuracy. *
* Use the -h option (or read the description() function) for more *
* detailed explanations of how to use the calculator. *
\*---------------------------------------------------------------------------*/
#include<iostream>
#include<climits>
#include "fraction.h"
#include "matrix.h"
using namespace std;
void info()
{
cout << "-----------------------------------------------" << endl;
cout << "Matrix Calculator v1.0" << endl
<< " Written by Colin Hamilton, Tufts University" << endl
<< "March 12, 2014" << endl
<< "Last Update: March 14, 2014" << endl;
cout << "-----------------------------------------------" << endl;
}
void description()
{
cout << "-----------------------------------------------" << endl;
cout << "This calculator handles integers, fractions, and matrices." << endl;
cout << "It utilizes Reverse Polish Notation, which means that" << endl
<< "operations are written after their operands. For example," << endl
<< "\"2+3\" would be written \"2 3 +\"." << endl;
cout << "When numbers or matrices are entered, they are put on" << endl
<< "\"the stack,\" where they are stored until they are used," << endl
<< "with the most recent entries on the top of the stack." << endl;
cout << "Operations really operate on the stack - unary operands" << endl
<< "like 'c' (change sign) operate on the top entry, while" << endl
<< "binary operands like '+' work on the top two entires." << endl;
cout << "-----------------------------------------------" << endl;
}
/* Global variables determine modes in which to run the calculator. */
bool PROMPT = true;
bool DECIMAL = false;
/* Different types of objects allowed on stack */
enum nodetype {NUMBER, MATRIX};
/* List structure is used for the stack. */
typedef struct Node {
nodetype type;
Matrix mdata;
Fraction fdata;
Node *rest;
} *List;
/* For arithmetic operations, whose operands come from the stack */
typedef bool (*StackOp)(List);
/* Functions that deal with input/output. *
*/
void runCalc(List *stack);
void processCommand(List *stack, char command);
void matrixOperate(List *stack);
void help(string state);
void instructions();
void options();
Fraction readFraction();
void readDecimal(List *stack, bool add);
/* Different types of operations. Deal with the stack and error-checking; *
* the functions passed should leave their answer on the stack. *
*/
void binary(StackOp operation, List *stack);
void unary(StackOp operation, List *stack);
void matrixOp(Matrix (*operation)(Matrix), List stack);
/* Binary arithmetic functions. *
*/
bool add(List stack);
bool subtract(List stack);
bool multiply(List stack);
bool divide(List stack);
bool power(List stack);
/* Unary arithmetic functions. *
*/
bool changeSign(List stack);
bool factorial(List stack);
bool root(List stack);
bool inverse(List stack);
bool transpose(List stack);
void determinant(List *stack);
/* Marix operations. *
*/
Matrix swap(Matrix m);
Matrix multRow(Matrix m);
Matrix addRow(Matrix m);
Matrix reduce(Matrix m);
/* Create a matrix. *
*/
void newMatrix(List *stack);
void identity(List *stack);
/* Stack operations. *
*/
void printStack(List stack);
bool makeDecimal(List stack);
void empty(List *stack);
void pop(List *stack);
void swap(List *stack);
void duplicate(List *stack);
/* Errors and prompts. *
*/
void tooFew();
void error(string message);
void prompt(string message);
int main(int argc, char *argv[])
{
List stack = NULL;
cout.precision(9);
for (int i = 1; i < argc; i++) {
switch (argv[i][0]) {
case 'h': help("begin"); break;
case 'd': DECIMAL = true; break;
case 'f': DECIMAL = false; break;
case 'p': PROMPT = true; break;
case 'e': PROMPT = false; break;
default:
cout << "Unknown option: " << argv[i] << endl;
}
}
runCalc(&stack);
empty(&stack);
return 0;
}
/* Argument specifies what will happen when the function returns; eg. *
* "quit" or "return to". (Should be a verb with "the program" as D.O.). *
*/
void help(string state)
{
char command = '\0';
do {
cout << "Enter 'h' for help on using the program." << endl;
cout << "Enter 'c' for a list of commands that can be used." << endl;
cout << "Enter 'i' for information on the program." << endl;
cout << "Enter 'r' to " << state << " the program." << endl;
cout << "Enter 'q' to quit the program." << endl;
cin >> command;
switch (command) {
case 'c': instructions(); break;
case 'h': description(); break;
case 'i': info(); break;
case 'q': case 'r': break;
}
} while (command != 'r' && command != 'q');
if (command == 'q') {
cin.putback(command);
}
}
void runCalc(List *stack)
{
char command ='\0';
cout << "For help using this calculator, enter 'h' " << endl;
do {
processCommand(stack, command);
cin.get(command);
} while (command != 'q');
}
/* Two possibilities for a command: *
* 1) It is a digit, in which case the number should be read and added to *
* the stack. *
* 2) It is alphabetic, in which case the appropriate function should be *
* called. *
* For explanations of which commands do what, see instructions() *
*/
void processCommand(List *stack, char command)
{
if (isdigit(command)) {
cin.putback(command);
List temp = new Node;
long long input;
cin >> input;
temp->type = NUMBER;
temp->fdata = input;
temp->rest = *stack;
*stack = temp;
if (cin.peek() == '.') { /* cin left off at first non-digit */
cin.get(command);
readDecimal(stack, true);
}
return;
}
switch (command) {
case '\0': break;
case '\n': printStack(*stack); break;
case '.': readDecimal(stack, false); break;
case '+': binary(add, stack); break;
case '-': binary(subtract, stack); break;
case '*': binary(multiply, stack); break;
case '/': binary(divide, stack); break;
case '^': binary(power, stack); break;
case '!': unary(factorial, stack); break;
case '|': determinant(stack); break;
case 'c': unary(changeSign, stack); break;
case 'd': duplicate(stack); break;
case 'h': help("return to"); break;
case 'i': unary(inverse, stack); break;
case 'm': matrixOperate(stack); break;
case 'o': options(); break;
case 'p': pop(stack); break;
case 'r': unary(root, stack); break;
case 's': swap(stack); break;
case 't': unary(transpose, stack); break;
case 'z': empty(stack); break;
default:
if (!isspace(command)) {
cout << "Unknown command: " << command << endl;
}
}
}
/* Describes the above commands. */
void instructions()
{
cout << "-----------------------------------------------" << endl;
cout << "'+': Add the top two entires on the stack." << endl;
cout << "'-': Subtract the top entry from the entry below it." << endl;
cout << "'*': Multiply the top two entries on the stack." << endl
<< " For matrices, A * B is calculated if A is below B." << endl;
cout << "'/': Divide the second entry on the stack by the top entry."<< endl;
cout << "'^': Raises the second entry to the power of the top entry."<< endl;
cout << "'!': Takes the factorial of the top number on the stack." << endl;
cout << "'c': Changes the sign of the top entry on the stack." << endl;
cout << "'d': Duplicates the top entry on the stack." << endl;
cout << "'h': Opens the help screen." << endl;
cout << "'o': Opens the options screen." << endl;
cout << "'p': Pops the top entry off of the stack." << endl;
cout << "'r': Take the sqare root of the top entry." << endl;
cout << "'s': Swaps the top two entries on the stack." << endl;
cout << "'z': \"Zeroes,\" or empties, the stack." << endl;
cout << "'m': Opens the matrix operation screen, which allows the" << endl
<< " creation of matrices, or the modification of the top" << endl
<< " entry on the stack, if it is a matrix." << endl;
cout << "From the matrix operation screen, the following commands are "
"allowed" << endl;
cout << "'a': Add a multiple of one row to another." << endl;
cout << "'i': Create an identity matrix of a particular size." << endl;
cout << "'m': Multiply a row by a certain factor." << endl;
cout << "'n': Create a new matrix, to push onto the stack." << endl;
cout << "'s': Swap two rows of a matrix." << endl;
cout << "'r': Return to the calculator." << endl;
cout << "From any screen, you may type 'q' to quit the calculator." << endl;
cout << "-----------------------------------------------" << endl;
}
/* The "matrix operation screen/mode." The part of the program that allows *
* for the creation of matrices and row operations on them. *
*/
void matrixOperate(List *stack)
{
char command;
do {
cin.get(command);
switch (command) {
case '\n':
if (*stack != NULL && (*stack)->type == MATRIX) {
prompt("Operating on matrix: ");
(*stack)->mdata.print(cout, " ");
} else {
prompt("No matrix on top of stack. Create a new one with 'm' or"
" 'i'\n");
} break;
case 'a': matrixOp(addRow, *stack); break;
case 'e': matrixOp(reduce, *stack); break;
case 'i': identity(stack); break;
case 'm': matrixOp(multRow, *stack); break;
case 'n': newMatrix(stack); break;
case 's': matrixOp(swap, *stack); break;
case 'r': case 'q': break;
default:
cout << "Unknown command: " << command << endl;
}
} while (command != 'r' && command != 'q');
if (command == 'q') {
cin.putback(command);
}
}
/* Creates a new matrix based on the user's input. User supplies size of *
* the matrix, and values of each entry. *
* cin.fail() cases are in case the user types, eg, "q" to quit. *
* (but also just as a general safety check) *
*/
void newMatrix(List *stack)
{
List temp = new Node;
temp->type = MATRIX;
int row, col;
row = col = 0;
prompt("Rows? ");
cin >> row;
prompt("Cols? ");
cin >> col;
if (cin.fail()) {
delete temp;
cin.clear();
cout << endl;
return;
}
temp->mdata = Matrix(row, col);
prompt("Please enter the entries in the matrix.\n");
for (int i = 0; i < row; i++) {
cout << "Row " << i+1 << " ";
for (int j = 0; j < col; j++) {
temp->mdata.set(i, j, readFraction());
if (cin.fail()) {
delete temp;
cin.clear();
return;
}
}
}
temp->rest = (*stack);
(*stack) = temp;
}
void identity(List *stack)
{
int size;
List temp = new Node;
temp->type = MATRIX;
prompt("What size identity matrix? ");
cin >> size;
temp->mdata = identityMatrix(size);
temp->rest = *stack;
*stack = temp;
}
/* All binary operations remove their operand from the stack and push their *
* answer. The workaround used here is indistinguishable from that. *
* Binary operations leave their answer as the *second* thing on the stack. *
* This allows binaryOp() to remove the top object. *
*/
void binary(StackOp operation, List *stack)
{
if (*stack == NULL || (*stack)->rest == NULL) {
tooFew();
return;
}
if(!operation(*stack)) return;
List temp = (*stack)->rest;
delete *stack;
*stack = temp;
}
bool add(List stack)
{
List temp = stack->rest;
if (stack->type == MATRIX && temp->type == MATRIX) {
if (stack->mdata.getRows() == temp->mdata.getRows() &&
stack->mdata.getCols() == temp->mdata.getCols()) {
temp->mdata += stack->mdata;
} else {
error("Matrices have incompatible sizes.");
return false;
}
} else if (stack->type == NUMBER && temp->type == NUMBER) {
temp->fdata += stack->fdata;
} else {
error("Mismatched types.");
return false;
}
return true;
}
bool subtract(List stack)
{
List temp = stack->rest;
if (stack->type == MATRIX && temp->type == MATRIX) {
if (stack->mdata.getRows() == temp->mdata.getRows() &&
stack->mdata.getCols() == temp->mdata.getCols()) {
temp->mdata -= stack->mdata;
} else {
error("Matrices have incompatible sizes.");
return false;
}
} else if (stack->type == NUMBER && temp->type == NUMBER) {
temp->fdata -= stack->fdata;
} else {
error("Mismatched types.");
return false;
}
return true;
}
bool multiply(List stack)
{
List temp = stack->rest;
if (stack->type == MATRIX) {
if (temp->type == NUMBER) {
stack->mdata *= temp->fdata;
stack->rest = temp->rest;
*temp = *stack;
stack->rest = temp;
} else {
if (temp->mdata.getCols() == stack->mdata.getRows()) {
temp->mdata *= stack->mdata;
} else {
error("Incompatible matrix sizes.");
return false;
}
}
} else if (stack->type == NUMBER) {
if (temp->type == MATRIX) {
temp->mdata *= stack->fdata;
} else if (temp->type == NUMBER) {
temp->fdata *= stack->fdata;
}
}
return true;
}
bool power(List stack)
{
if (stack->type == MATRIX || stack->fdata.getDenominator() != 1) {
error("Exponents must be integers.");
return false;
}
Fraction exp = stack->fdata;
if (stack->rest->type == NUMBER) {
List temp = stack->rest;
Fraction base = temp->fdata;
Fraction result = 1;
while (exp > 0) {
result *= base;
exp -= 1;
}
temp->fdata = result;
} else {
return false;
}
return true;
}
bool divide(List stack)
{
List temp = stack->rest;
if (stack->type == MATRIX) {
error("Division by a matrix is undefined.");
return false;
}
if (temp->type == MATRIX) {
temp->mdata *= stack->fdata.reciprocal();
} else if (temp->type == NUMBER) {
if (stack->fdata == 0) {
error("Division by zero is undefined.");
return false;
}
temp->fdata /= stack->fdata;
}
return true;
}
/* Unary Operations leave their answer as the top thing on the stack. *
* In so doing, they destroy the former object on top of the stack. *
*/
void unary(StackOp operation, List *stack)
{
if (*stack == NULL) {
tooFew();
return;
}
operation(*stack);
}
bool changeSign(List stack)
{
if (stack->type == MATRIX) {
stack->mdata = -(stack->mdata);
} else if (stack->type == NUMBER) {
stack->fdata = -(stack->fdata);
}
return true;
}
bool factorial(List stack)
{
if (stack->type != NUMBER || stack->fdata.isNegative() ||
stack->fdata.getDenominator() != 1) {
error("Factorial is only defined for nonnegative integers.");
return false;
}
unsigned long long result = 1;
unsigned base = stack->fdata.getNumerator();
for (unsigned i = 2; i <= base; i++) {
result *= i;
}
stack->fdata = result;
return true;
}
bool root(List stack)
{
if (stack->type == MATRIX) {
stack->mdata.reduce();
return true;
}
if (stack->fdata.isNegative()) {
error("Square roots of negative numbers are imaginary.");
return false;
}
stack->fdata = stack->fdata.sqroot();
return true;
}
bool inverse(List stack)
{
if (stack->type == MATRIX) {
error("No inverse for matrices has yet been implemented.");
return false;
} else {
stack->fdata = stack->fdata.reciprocal();
}
return true;
}
bool transpose(List stack)
{
if (stack->type != MATRIX) {
error("Transpose is only defined for matrices.");
return false;
} else {
stack->mdata = transpose(stack->mdata);
return true;
}
}
/* Operation for "|" operator. This is "absolute value" for a number, *
* and "determinant" for a matrix. *
* The former changes the top value on the stack (if it is negative). *
* The latter does not change or delete the matrix. It simply pushes the *
* number found on top of the matrix. *
* These divergent operations make this function unique. *
*/
void determinant(List *stack)
{
if (*stack == NULL) {
tooFew();
return;
}
if ((*stack)->type == NUMBER && (*stack)->fdata.isNegative()) {
(*stack)->fdata = -((*stack)->fdata);
} else {
if ((*stack)->mdata.getRows() != (*stack)->mdata.getCols()) {
error("Determinants can only be found for square matrices.");
} else {
Fraction result = (*stack)->mdata.determinant();
List temp = new Node;
temp->type = NUMBER;
temp->fdata = result;
temp->rest = *stack;
*stack = temp;
}
}
}
/* Matrix operations return a modified matrix. *
*/
void matrixOp(Matrix (*operation)(Matrix), List stack)
{
if (stack == NULL || stack->type != MATRIX) {
error("Need a matrix on the stack for that operation.");
return;
}
stack->mdata = operation(stack->mdata);
}
Matrix swap(Matrix m)
{
int r1, r2;
prompt("Which rows do you want to swap? ");
cin >> r1 >> r2;
m.switchRows(r1 - 1, r2 - 1);
return m;
}
Matrix multRow(Matrix m)
{
int row;
prompt("Multiply which row? ");
cin >> row;
prompt("By what factor? ");
m.multiplyRow(row - 1, readFraction());
return m;
}
Matrix addRow(Matrix m)
{
int r1, r2;
prompt("Add a multiple of which row? ");
cin >> r1;
prompt("To what other row? ");
cin >> r2;
prompt("By what factor? ");
m.addRow(r1 - 1, readFraction(), r2 - 1);
return m;
}
Matrix reduce(Matrix m)
{
m.reduce();
return m;
}
/* Reads in a number, accounting for the fact that it may have been input *
* as a fraction, with "/" seperating numerator and denominator. *
*/
Fraction readFraction()
{
long long num, den = 1;
char next;
cin >> num;
next = cin.peek();
while (isspace(next) && next != '\n') {
cin.get(next);
next = cin.peek();
}
if (next == '/') {
cin.get(next);
cin >> den;
}
return Fraction(num, den);
}
/* Reads a decimal from standard input and converts it to a fraction. *
* The function expects a decimal point to have been the last thing read in.*
* If the "add" bool is set, the number will be added to the top number on *
* the stack. Otherwise, the number is pushed onto the stack. *
*/
void readDecimal(List *stack, bool add)
{
long long num = 0, den = 1;
long long max = LLONG_MAX / 10;
char next;
cin.get(next);
while (isdigit(next)) {
if (den < max) {
num *= 10;
den *= 10;
num += next - '0';
}
cin.get(next);
}
cin.putback(next);
Fraction toAdd(num, den);
if (!add || *stack == NULL || (*stack)->type == MATRIX) {
List temp = new Node;
temp->type = NUMBER;
temp->fdata = toAdd;
temp->rest = *stack;
*stack = temp;
} else {
(*stack)->fdata += toAdd;
}
}
bool makeDecimal(List stack)
{
if (stack == NULL || stack->type != NUMBER) {
error("Topmost entry must be a number.");
return false;
}
cout << (double) stack->fdata.getNumerator() / stack->fdata.getDenominator()
<< endl;
return true;
}
void printStack(List stack)
{
if (stack == NULL) {
prompt("Stack empty.\n");
}
while (stack != NULL) {
cout << ">>> ";
if (stack->type == MATRIX) {
cout << stack->mdata.getRows() << "x" << stack->mdata.getCols() << endl;
stack->mdata.print(cout, " ");
} else if (stack->type == NUMBER) {
if (DECIMAL) {
makeDecimal(stack);
} else {
stack->fdata.print(cout);
cout << endl;
}
}
stack = stack->rest;
}
}
void pop(List *stack)
{
List temp = (*stack)->rest;
delete *stack;
*stack = temp;
}
void duplicate(List *stack)
{
if (*stack == NULL) {
return;
}
List temp = new Node;
*temp = **stack;
temp->rest = *stack;
*stack = temp;
}
void swap(List *stack)
{
if (*stack == NULL || (*stack)->rest == NULL) {
return;
}
List temp = (*stack)->rest;
(*stack)->rest = temp->rest;
temp->rest = (*stack);
*stack = temp;
}
void empty(List *stack)
{
List cur = *stack;
while (cur != NULL) {
List temp = cur->rest;
delete cur;
cur = temp;
}
*stack = NULL;
}
void tooFew()
{
error("Too few entries on the stack for that operation.");
}
void error(string message)
{
cout << ">>> " << message << endl;
}
void prompt(string message)
{
if (PROMPT) {
cout << message;
}
}
/* Allows user to toggle display settings. *
*/
void options()
{
char command = '\0';
do {
cin.get(command);
switch(command) {
case '\n':
cout << "Enter 'd' to toggle fraction/decimal display." << endl;
cout << "Enter 'p' to toggle prompts." << endl;
cout << "Enter 'r' to return to the calculator." << endl;
break;
case 'd': DECIMAL = !DECIMAL;
cout << "Numbers will now be displayed as "
<< (DECIMAL ? "decimal" : "fraction") << "s." << endl;
break;
case 'p': PROMPT = !PROMPT;
cout << "Prompts are now " << (PROMPT ? "en" : "dis") << "abled." <<endl;
break;
case 'r': case 'q': break;
default:
cout << "Unknown option: " << command << endl;
}
} while (command != 'r' && command != 'q');
if (command == 'q') {
cin.putback(command);
}
}