-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalculate.cpp
More file actions
166 lines (139 loc) · 4.26 KB
/
Copy pathCalculate.cpp
File metadata and controls
166 lines (139 loc) · 4.26 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
//
// Created by quinc on 2021-04-22.
//
#include <string>
#include <sstream>
#include <cmath>
#include "Calculate.h"
//TODO Remake calculate struct completely.
std::string calculate::add(double numA, double numB) {
const double add = numA + numB;
return writetoVector("The sum of the two numbers are ", add, numA, numB);
}
std::string calculate::subtract(double numA, double numB) {
const double subtract = numA - numB;
return writetoVector("The difference of the two numbers are ", subtract, numA, numB);
}
std::string calculate::multiply(double numA, double numB) {
const double multiply = numA * numB;
return writetoVector("The product of the two numbers you put in are ", multiply, numA, numB);
}
std::string calculate::divide(double numA, double numB) {
const double divide = numA / numB;
return writetoVector("The dividend of the two numbers you put in are ", divide, numA, numB);
}
std::string calculate::power(double numA, double numB) {
const double power = pow(numA, numB);
return writetoVector(" to the power of ", power, numA, numB);
}
std::string calculate::square(double numA, double numB) {
const double square = sqrt(numA);
return writetoVector("The square root of ", square, numA, numB);
}
std::string calculate::writetoVector(std::string st, const double value, double numA, double numB) {
std::stringstream outputStream;
std::stringstream writeStream;
if (oper == 's') {
outputStream << st;
outputStream << numA;
outputStream << " is ";
outputStream << value;
outputStream << std::endl;
} else if (oper == '^') {
outputStream << numA;
outputStream << st;
outputStream << numB;
outputStream << " is ";
outputStream << value;
outputStream << std::endl;
} else {
outputStream << st;
outputStream << value;
outputStream << std::endl;
}
if (oper == 's') {
writeStream << "Square root of: " << numA << " = " << value;
} else {
writeStream << numA << " " << oper << " " << numB << " = " << value;
}
vt.push_back(writeStream.str());
return outputStream.str();
}
void calculate::setOperator(char oper) {
this->oper = oper;
}
//TODO
void calculate::openCalculator() {
writeHistory.open(fileName, std::ios::app);
readHistory.open(fileName);
while (!readHistory.eof()) {
std::string readHistoryto;
std::getline(readHistory, readHistoryto);
vt.push_back(readHistoryto);
}
std::reverse(vt.begin(), vt.end());
}
void calculate::closeCalculator() {
writeToFile();
if (writeHistory.is_open()) {
writeHistory.close();
}
if (readHistory.is_open()) {
readHistory.close();
}
}
[[deprecated("Try not to use this")]]
std::ofstream &calculate::getWriteHistory() {
return writeHistory;
}
void calculate::readHistoryandShow() {
for (int i = vt.size() - 1; i >= 0; i--) {
std::cout << vt[i] << std::endl;
}
}
void calculate::deleteHistory() {
if (writeHistory.is_open()) {
writeHistory.close();
writeHistory.open(fileName, std::ios::out | std::ios::trunc);
vt.clear();
} else {
writeHistory.open(fileName, std::ios::out | std::ios::trunc);
vt.clear();
}
std::cout << "\nHistory Deleted!!\n";
system("pause");
CLEAR;
}
//TODO
void calculate::writeToFile() {
if (writeHistory.is_open()) {
writeHistory.close();
writeHistory.open(fileName, std::ios::out | std::ios::trunc);
for (int i = vt.size() - 1; i >= 0; i--) {
if (i != 0) {
writeHistory << vt[i] << std::endl;
} else {
writeHistory << vt[i];
}
}
} else if (!writeHistory.is_open()) {
writeHistory.open(fileName, std::ios::out | std::ios::trunc);
for (int i = vt.size() - 1; i >= 0; i--) {
if (i != 0) {
writeHistory << vt[i] << std::endl;
} else {
writeHistory << vt[i];
}
}
} else {
try {
throw "ERROR WRITING HISTORY TO FILE";
}
catch (
const char *e
) {
CLEAR;
std::cout << e << std::endl;
}
}
}