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
|
#include <iostream>
#include <string>
#include <iomanip>
#include <conio.h>
#include <cctype>
using namespace std;
struct Node {
std::string data {};
Node* next {};
Node(const std::string& s) : data(s) {}
};
void insert(const std::string& s, Node*& head, Node*& tail) {
const auto current {new Node(s)};
if (tail)
tail->next = current;
else
head = current;
tail = current;
}
void remove(Node*& head, Node*& tail) {
for (auto curr {head}; curr; head = curr) {
curr = curr->next;
delete head;
}
head = tail = nullptr;
}
void eval(Node*& head, Node*&) {
double val {std::stod(head->data)};
for (auto curr {head->next}; curr; curr = curr->next) {
char op {curr->data[0]};
curr = curr->next;
const double v2 {std::stod(curr->data)};
switch (op) {
case '+':
val += v2;
break;
case '-':
val -= v2;
break;
case '*':
val *= v2;
break;
case '/':
val /= v2;
break;
}
}
std::cout << val << '\n';
/*
std::cout << "+------------+" << std::endl;
std::cout << "|" << std::setw(12) << ss.str() << "|" << std::endl;
std::cout << "+------------+" << std::endl << std::endl;
*/
}
void calculator(Node*& head, Node*& tail) {
std::string num;
for (unsigned char c {}; (c = static_cast<unsigned char>(_getche())) != 'e'; ) {
if (std::isspace(c))
continue;
if (c == ';') {
cout << "Returning To Program Menu\n";
return;
}
if (std::isdigit(c))
num += c;
else {
insert(num, head, tail);
num.clear();
if (c == '+' || c == '-' || c == '*' || c == '/') {
std::string temp(1, c);
insert(temp, head, tail);
} else
if (c == '=') {
eval(head, tail);
remove(head, tail);
} else {
std::cout << "Invalid char\n";
continue;
}
}
}
}
int main() {
Node *head {}, *tail {};
calculator(head, tail);
remove(head, tail);
}
|