'How do a use math operations using a linked list? C++
So I need help with making a linked list use math operations. I haven't got any code to do with the math because I don't know how to convert linked lists to a double. Once a operation is pressed, the linked list store the previous elements and allow the user to input more values then once the '=' is pressed, the program will do the function.
Thank you for the help in advance.
I'll provide my current code and the current output and desired output.
Code:
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
#include <fstream>
#include <iomanip>
#include <map>
#include <sstream>
using namespace std;
struct Node {
std::string data{};
Node* next{ nullptr };
};
void calculator(Node*& head, Node*& tail) {
bool quit{ false };
std::string temp{};
Node* current{ nullptr };
char c = ' ';
while (c != 'e')
{
char c = ' ';
c = _getch();
if (c == '\t' || c == '\n' || c == '\r' || c == ' ') continue;
if (c == ';') {
cout << "Returning To Program Menu" << endl;
return;
}
if (c == '+') {
}
temp = temp + c;
current = new Node;
current->data = temp;
if (tail) {
tail->next = current;
tail = current;
}
else {
head = current;
tail = current;
}
std::stringstream ss;
for (current = head; current; current = current->next) {
ss << current->data;
}
std::cout << "+------------+" << std::endl;
std::cout << "|" << std::setw(12) << ss.str() << "|" << std::endl;
std::cout << "+------------+" << std::endl << std::endl;
temp = "";
}
}
int main() {
bool quit = false;
int choice = 0;
Node* head = nullptr;
Node* tail = nullptr;
calculator(head, tail);
for (auto tmp{ head }; tmp;) {
tmp = tmp->next;
delete head;
head = tmp;
}
}
Current Output:
+------------+
| 1|
+------------+
+------------+
| 12|
+------------+
+------------+
| 12+|
+------------+
+------------+
| 12+1|
+------------+
+------------+
| 12+12|
+------------+
+------------+
| 12+12=|
+------------+
Desired Output:
+------------+
| 1|
+------------+
+------------+
| 12|
+------------+
+------------+
| +|
+------------+
+------------+
| 1|
+------------+
+------------+
| 12|
+------------+
+------------+
| =|
+------------+
+------------+
| 24|
+------------+
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
