Repository navigation
Expand file tree
/
Copy pathcalc.cpp
More file actions
128 lines (114 loc) · 4.02 KB
/
Copy pathcalc.cpp
File metadata and controls
128 lines (114 loc) · 4.02 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
#include <iostream>
#include <string>
#include <stack>
#include <cmath>
#include <fstream>
#include <cctype>
using namespace std;
class Calculator {
private:
double performOperation(double a, double b, char op) {
switch (op) {
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/':
if (b == 0) {
cerr << "Error: Division by zero!" << endl;
return NAN;
}
return a / b;
default: return 0;
}
}
int precedence(char op) {
if (op == '+' || op == '-') return 1;
if (op == '*' || op == '/') return 2;
return 0;
}
void applyOperation(stack<double>& values, stack<char>& operators) {
if (values.size() < 2 || operators.empty()) return;
double b = values.top(); values.pop();
double a = values.top(); values.pop();
char op = operators.top(); operators.pop();
values.push(performOperation(a, b, op));
}
double processExpression(const string& expression) {
stack<double> values;
stack<char> operators;
int i = 0;
while (i < expression.length()) {
if (isspace(expression[i])) {
i++;
continue;
}
if (expression[i] == '(') {
operators.push(expression[i]);
i++;
}
else if (expression[i] == ')') {
while (!operators.empty() && operators.top() != '(') {
applyOperation(values, operators);
}
if (!operators.empty() && operators.top() == '(')
operators.pop();
i++;
}
else if (isdigit(expression[i]) || expression[i] == '.' ||
(expression[i] == '-' &&
(i == 0 || (!isdigit(expression[i - 1]) && expression[i - 1] != ')')))) {
// Handle negative number
string numStr = "";
if (expression[i] == '-') numStr += expression[i++];
while (i < expression.length() && (isdigit(expression[i]) || expression[i] == '.')) {
numStr += expression[i++];
}
values.push(stod(numStr));
}
else if (expression[i] == '+' || expression[i] == '-' ||
expression[i] == '*' || expression[i] == '/') {
while (!operators.empty() && precedence(operators.top()) >= precedence(expression[i])) {
applyOperation(values, operators);
}
operators.push(expression[i++]);
}
else {
cerr << "Warning: Unknown character '" << expression[i] << "' skipped." << endl;
i++;
}
}
while (!operators.empty()) {
applyOperation(values, operators);
}
return values.empty() ? 0 : values.top();
}
public:
void processFileInput(const string& inputFile, const string& outputFile) {
ifstream inFile(inputFile);
ofstream outFile(outputFile);
if (!inFile) {
cerr << "Error: Unable to open input file: " << inputFile << endl;
return;
}
if (!outFile) {
cerr << "Error: Unable to open output file: " << outputFile << endl;
return;
}
string expression;
while (getline(inFile, expression)) {
if (expression.empty()) continue;
double result = processExpression(expression);
outFile << "Expression: " << expression << " = " << result << endl;
}
inFile.close();
outFile.close();
cout << "✅ Processing complete. Check '" << outputFile << "' for results." << endl;
}
};
int main() {
Calculator calc;
string inputFile = "input.txt";
string outputFile = "output.txt";
calc.processFileInput(inputFile, outputFile);
return 0;
}