Calculator class

So I need help using classes to build a calculator class with the same functionality as the following calculator, but having the functions embedded with an object class. Any help would be greatly appreciated.
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
#include <iostream>
#include <vector>

using namespace std;

int main() {
    system("TITLE George's Calculator");
    system("COLOR 17");
	double temp;
	int i=0, j=0, counter=1;
	double result=0;
	char opr='*'; 
	vector<double> num(20); 
	vector<int> pluMin(19); 
	num[0]=1; 
	bool loop=1; 

	cout << "You can multiply, divide, add and subtract using *,/,+,-, respectively\n\n";
    cout << "Enter the = sign at the end of what you want calculated and press enter\n\n";
	do {
		cout << "Enter what you want to be calculated: ";
		cin >> temp;
		switch (opr) {
			case '+':
				pluMin[j]=1; j++; i++; counter++; num[i]=temp;
				break;
			case '-':
				pluMin[j]=2; j++; i++; counter++; num[i]=temp;
				break;
			case '*':
				num[i]=num[i]*temp;
				break;
			case '/':
				if(temp == 0){
				cout << "You can't divide by zero" << endl;
				}else{
				num[i]=num[i]/temp;
				}
				break;
			default:
				cout << "That is an invalid operation" << endl;
				break;
		}

		cout << "Operator: ";
		cin >> opr;
		if (opr=='=') loop=0;

	} while (loop);

	result=num[0]; i=1; j=0;
	for(;counter;i++,j++,counter--) {
		if (pluMin[j]==1) result = result + num[i];
		else if (pluMin[j]==2) result = result - num[i];
		else if (pluMin[j]==0) break;
		else cout << "Improper operations!"; 
	}
	cout << "\nResult:   " << result;
	cin >> result;
	return 0;
}

Post the code you have so far, and what problems youre having with it

If your problem is i dont know how to write classes or structs, read the tutorial http://cplusplus.com/doc/tutorial/classes/
Here is what I have so far...
any help would be appreciated

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
#include <iostream>
#include <vector>
using namespace std;

class Calculator {
      public:
             void (i, j);
             int value;
             };
             
int main() {
    system("TITLE George's Calculator");
    system("COLOR 17");
	double temp;
	int i=0, j=0, counter=1;
	double result=0;
	char opr='*'; 
	vector<double> num(20); 
	vector<int> pluMin(19); 
	num[0]=1; 
	bool loop=1; 

	cout << "You can multiply, divide, add and subtract using *,/,+,-, respectively\n\n";
    cout << "Enter the = sign at the end of what you want calculated and press enter\n\n";
	do {
		cout << "Enter what you want to be calculated: ";
		cin >> temp;
		switch (opr) {
			case '+':
				pluMin[j]=1; j++; i++; counter++; num[i]=temp;
				break;
			case '-':
				pluMin[j]=2; j++; i++; counter++; num[i]=temp;
				break;
			case '*':
				num[i]=num[i]*temp;
				break;
			case '/':
				if(temp == 0){
				cout << "You can't divide by zero" << endl;
				}else{
				num[i]=num[i]/temp;
				}
				break;
			default:
				cout << "That is an invalid operation" << endl;
				break;
		}

		cout << "Operator: ";
		cin >> opr;
		if (opr=='=') loop=0;

	} while (loop);

	result=num[0]; i=1; j=0;
	for(;counter;i++,j++,counter--) {
		if (pluMin[j]==1) result = result + num[i];
		else if (pluMin[j]==2) result = result - num[i];
		else if (pluMin[j]==0) break;
		else cout << "Improper operations!"; 
	}
	cout << "\nResult:   " << result;
	cin >> result;
	return 0;
}

First of all;

void (i, j);What are you trying to make here?

Also, remove either the j or i variable in main, theyre both the same throughout the whole function, you only need one, and itll make your code look a bit cleaner

3rd, what do you want your class to contain, just the numbers youre working with, or also the functions?
Last edited on
I need both the i and j otherwise my program will not properly execute. I would like the class to contain the functions and i thought that void (i, j) is needed to declare the class
Heres an example on how to have the numbers and operators in your class, with an example function that returns operator(...)
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 <vector>
#include <windows.h>
using namespace std;

class Calculator
{
	public:
	double num[20];
	int    ope[19];

	int GetOperator(int i)
	{
	    return ope[i];
	}
};

int main() {
    system("TITLE George's Calculator");
    system("COLOR 17");

    Calculator calc;

	double temp;
	int i=0;
	int counter=1;
	double result=0;
	char opr='*';

	calc.num[0]=1;
	bool loop=1;

	cout << "You can multiply, divide, add and subtract using *,/,+,-, respectively\n\n";
    cout << "Enter the = sign at the end of what you want calculated and press enter\n\n";
	do {
		cout << "Enter what you want to be calculated: ";
		cin >> temp;
		switch (opr) {
			case '+':
				calc.ope[i]=1;
				i++;
				counter++;
				calc.num[i]=temp;
				break;
			case '-':
				calc.ope[i]=2;
				i++;
				counter++;
				calc.num[i]=temp;
				break;
			case '*':
				calc.num[i]=calc.num[i]*temp;
				break;
			case '/':
				if(temp == 0){
				cout << "You can't divide by zero" << endl;
				}else{
				calc.num[i] = calc.num[i]/temp;
				}
				break;
			default:
				cout << "That is an invalid operation" << endl;
				break;
		}

		cout << "Operator: ";
		cin >> opr;
		if (opr=='=') loop=0;

	} while (loop);
	result = calc.num[0];
	i = 1;
	for(i = 1; i < counter; i++) {
		if (calc.ope[i-1]==1)
			result = result + calc.num[i];
		else if (calc.ope[i-1]==2)
			result = result - calc.num[i];
		else if
			(calc.ope[i-1]==0) break;
		else
			cout << "Improper operations!";

		cout << "(Operator[" << i-1 << "] = " << calc.GetOperator(i-1) << ")";
	}
	cout << "\nResult:   " << result;
	cin >> result;
	return 0;
}

Last edited on
Topic archived. No new replies allowed.