Calculator: Chaining Of Operations

Pages: 12
Lmao
Thanks ill run it soon
interesting problem.
i tried Tom56785's code,
but "5+3*10/5" = "16" and "4"="0" !
it worked from first sign, but it should calculate '*' and '/' first

another observation: for '*', '+' and '-' operation can be done anywhere in the string, but in case of multiple successive '/' such as 400/40/10/2, it should be calculated from beginning: 400/40/10/2 > 10/10/2 > 1/2.
Last edited on
I've not quite implemented mathematical grammar such as BIDMAS yet. Getting multiple calculations in one line was the first priority. As for "4" equaling "0", this can be fixed by adding an "if" statement around line 33 to check if the result changed or not. If you know what I mean.
Thank You So Much Tom It Works Great however i would like to know how the program works. I also see a minor problem -
1. After Getting The Result If We dont Press Enter Again and continues to enter another expression then the second calculation doesn't comes right.
Steps To Do It:-
1. Enter 5+5+5 Then Press Key "Enter".
2. You'll See The Result Now.
3. Start Pressing 5+5+5 Again or Any other expression (without pressing "Enter" Key again which clears The Screen).
4. Press "Enter" Key.
5. You'll See The Result of 5+5+5 as 10 instead of 15.
-------------------------------------------------------------------------------------------------------
BTW: Please Explain How The Program Works as some of the functions you used are not familiar to me and i want to understand the loop thats happening in the program.
Last edited on
Underneath is the new code which does as you asks and doesn't require the user to press "enter" to continue. The calculations still work just as they should too. All that's needed now is mathematical grammar and brackets.

I assume you know what the first 3 lines do so I'll skip onto the more complicated lines. Firstly, lines 7 is the prototype for the function that does the calculations, also sometimes referred to as the forward declaration. Basically, it tells the code that it exists. The very first loop in the code is simply an infinite loop (one that doesn't break the code!). This just allows the user to keep on typing expressions until they close the program. The second "do" loop is the loop that keeps on getting input until there is none left to get. If the input fails (like if they enter a letter as a number) then the input buffer now has an error flag and skips all of the next inputs until it is cleared. That's what "cin.fail()" checks. "cin.clear()" and "cin.ignore(INT_MAX, '\n'" clear the error flags from the buffer.

"cin.peek()" gets the very next character from the input buffer, if we didn't do this then the new line (\n) character or the terminator (\0) character would be inputted into the operator variable which we don't want. So we check the next character to see if the end of the stream has been reached or not. If so, the simple "break" line tells the loop to simply stop running and continue the code at line 35.

Line 32 simply calls the function "calc" (which we forward declared earlier) and passes 3 variables to it. These are the variables that we need for the calculation. The values we send to the function are not permanently changed. If you wanted to permanently do this then you would either use a global variable or the more complicated reference or pointer. The function returns the value of the calculation and the code sets that value to "res".

I believe I've explained everything. If you have more questions then I'll be glad to help.

P.S - I hope you didn't fall asleep!


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
#include <iostream>
#include <cmath>
using namespace std;
//Simple Calculator By Chaining Of Operations
//res= result; lval=left value; rval=right value; op=operation symbol/code;

double calc(double, double, char);

int main() {
	do {
		//system("cls"); // clear the screen
		cout << "Please Enter An Expression\n";
		double lval = NULL, rval = NULL, res = NULL;
		char op;
		//cin>>lval;
		//if(cin)
		//	cin>>op;
		//if (cin)
		//	cin>>rval;
		cin >> lval;
		do {
			op = NULL;
			rval = NULL;
			if (!cin.fail() && cin.peek() != '\n' && cin.peek() != '\0')
				cin >> op;
			else
				break;
			if (!cin.fail() && cin.peek() != '\n' && cin.peek() != '\0')
				cin >> rval;
			else
				break;
			res = calc(lval, rval, op);
			lval = res;
		} while (!cin.fail() && !cin.eof());
		cin.clear();
		cin.ignore(INT_MAX, '\n'); // clear the input buffer
		cout << "\nResult: " << res << "\n\n\n";
		//cin.ignore(); // pause
	} while (true); // continue repeating
}

double calc(double lval, double rval, char op) {
	double res;
	if (op=='+') {
		res = lval+rval;
		lval = res;
	} else if (op=='-') {
		res = lval-rval;
		lval = res;
	} else if (op=='*') {
		res = lval*rval;
		lval = res;
	} else if (op=='/') {
		res = lval/rval;
		lval = res;
	}
	return lval;
}
Wow nicely explained thanks again. I learned a lot from you.
Topic archived. No new replies allowed.
Pages: 12