How do a use math operations using a linked list?

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:

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
 #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|
+------------+

The spaces in the outputs are formatted weird on this website.
Last edited on
the c++ standard function stod() will convert your strings into doubles.
How would I implement this?
I don't follow your code well... is the linked list holding a number or a digit?
it is currently a linked list holding a string but needs to convert to a double to calculate.
std::string data{};
change to
double data{}; //a linked list of doubles

OR
double d = stod(head->data()); //a node with a string into a double
Last edited on
I have done that, but now I get an error due to the use of the '+' and the std::stringstream ss;
Last edited on
If you want to use a list like this (why??), then each node is probably going to be a token from the input. Once = entered then the nodes are evaluated. Something like this (without display, error checking and operator precedence!):

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);
}



12+34=46
1+2+3=6
1+2*3=9
;Returning To Program Menu

Last edited on
Topic archived. No new replies allowed.