-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.cpp
More file actions
141 lines (132 loc) · 3.33 KB
/
calc.cpp
File metadata and controls
141 lines (132 loc) · 3.33 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
#include "dlist.h"
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
void handle_math(string op, Dlist<double> &stack){
if(!stack.isEmpty()){
double y = stack.removeFront();
if(!stack.isEmpty()){
double x = stack.removeFront();
double result;
if(op == "+"){
result = x + y;
}else if(op == "-"){
result = x - y;
}else if(op == "*"){
result = x * y;
}else{
if(y == 0){
stack.insertFront(y);
stack.insertFront(x);
cout << "Divide by zero\n";
return;
}
result = x / y;
}
stack.insertFront(result);
}else{
stack.insertFront(y);
cout << "Not enough operands\n";
}
}else{
cout << "Not enough operands\n";
}
}
void negation(Dlist<double> &stack){
if(!stack.isEmpty()){
double x = stack.removeFront();
double result = x * -1;
stack.insertFront(result);
}else{
cout << "Not enough operands\n";
}
}
void duplication(Dlist<double> &stack){
if(!stack.isEmpty()){
double x = stack.removeFront();
stack.insertFront(x);
stack.insertFront(x);
stack.insertFront(x);
}else{
cout << "Not enough operands\n";
}
}
void reversal(Dlist<double> &stack){
if(!stack.isEmpty()){
double x = stack.removeFront();
if(!stack.isEmpty()){
double y = stack.removeFront();
stack.insertFront(x);
stack.insertFront(y);
}else{
stack.insertFront(x);
cout << "Not enough operands\n";
}
}else{
cout << "Not enough operands\n";
}
}
void print(Dlist<double> &stack){
if(!stack.isEmpty()){
double x = stack.removeFront();
cout << x << endl;
stack.insertFront(x);
}else{
cout << "Not enough operands\n";
}
}
void clear(Dlist<double> &stack){
while(!stack.isEmpty()){
stack.removeFront();
}
}
void print_all(Dlist<double> &stack){
if(!stack.isEmpty()){
Dlist<double> stack_copy(stack);
while(!stack_copy.isEmpty()){
cout << stack_copy.removeFront() << " ";
}
}
cout << " \n";
}
void operate(string op, Dlist<double> &stack){
if(op == "+" || op == "-" || op == "*" || op == "/"){
handle_math(op, stack);
}else if(op == "n"){
negation(stack);
}else if(op == "d"){
duplication(stack);
}else if(op == "r"){
reversal(stack);
}else if(op == "p"){
print(stack);
}else if(op == "c"){
clear(stack);
}else if(op == "a"){
print_all(stack);
}else{
cout << "Bad input\n";
}
}
int main() {
//string input[] = {"90", "34", "12", "33", "55", "66", "+", "*", "-", "+", "-", "p"};
Dlist<double> stack;
string s;
//int i = 0;
while(cin >> s){
//s = input[i];
//i++;
if(isdigit(s[0])){
double number = atof(s.c_str());
stack.insertFront(number);
}else{
if(s == "q"){
break;
}
operate(s, stack);
}
//stack.print();
}
return 0;
}