Calculator

Hello,
I want to write a calculator.
This calculator should have 2 dynamic storages which store as much storage as the user wants to type.
How do I implement such dynamic storages?

Here is my calculator
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
#include <iostream>
using namespace std;

int adding ()
{
	int n1, n2;
	cout << "\nType two new numbers to add: \n" << endl;
	cin >> n1 >> n2;
	cout << n1 << " + " << n2 << " equels " << n1 + n2 << endl;
	
	return 0;
}

int subtracting ()
{
	int n1, n2;
	cout << "\nType two new numbers to subtract: \n" << endl;
	cin >> n1 >> n2;
	cout << n1 << " - " << n2 << " equels " << n1 - n2 << endl;

	return 0;
}

int multiplycation ()
{
	int n1, n2;
	cout << "\nType two new numbers to multiply: \n" << endl;
	cin >> n1 >> n2;
	cout << n1 << " * " << n2 << " equels " << n1 * n2 << endl;

	return 0;
}

int devision ()
{
	int n1, n2;
	cout << "\nType two new numbers to devide: \n First one bigger than second \n" << endl;
	cin >> n1 >> n2;
	cout << n1 << " / " << n2 << " equels " << n1 / n2 << endl;

	return 0;
}

int main()
{
	cout << "Type \n1 for adding \n2 for subtracting \n3 for multiply \n2 for divide \n" << endl;
	int v1;
	cin >> v1;

	if (v1 == 1)
	{
		adding ();
	}
	else if (v1 == 2)
	{
		subtracting ();
	}
	else if (v1 == 3)
	{
		multiplycation ();
	}
	else if (v1 == 4)
	{
		devision ();
	}
	else
	{
		cout << "Only 1 to 4" << endl;
	}
	system("PAUSE");
	return 0;
}
How do I implement such dynamic storages?

http://www.cplusplus.com/doc/tutorial/dynamic/

It's not clear why you would need dynamic storage for a simple calculator.

True
your calculator ist only for 2 numbers. I want my calculator to calculate with as many numbers as the user types in.
Use a vector, it like an array but you dont have to specify its size.

1
2
3
#include <vector>

vector<int> Numbers;
Last edited on
keep is simple.

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

#include <iostream>

using namespace std;

int main()
{
    float a,b;
    char choice;
    char operatore = ('+','-','*','/');
    while(b!=0)
    {
        a=0;                             //delete this is you want add over 2 numbers
        cout<<a<<endl;           //
        cout<<" input number "<<endl;
        cin>>a;
        cout<<" input operator aritmetico: ( + , - , * , / ) "<<endl;
        cin>>operatore;
        switch (operatore)
        {
            case '+':
                cout<<" sum "<<endl;
                cin>>b;
                cout<<(a+=b);
                break;
            case '-':
                cout<<" subctraction "<<endl;
                cin>>b;
                cout<<(a-=b);
                break;
            case '*':
                cout<<" multipler "<<endl;
                cin>>b;
                cout<<(a*=b);
                break;
            case '/':
                cout<<" division "<<endl;
                cin>>b;
                if (b==0)
                    cout<<" divisione per 0 indeterminata"<<endl;
                else
                    cout<<(a/=b);
                break;
        };
        cout<<" more operation? y/n"<<endl;
        cin>>choice;
        if(choice!='y')
            break;
}


    return 0;
}
Last edited on
The trick is that if there is more than one operation to perform, then the result of the first operation becomes the left hand operand:
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
/* A calculator that handles any amount of operations
 * does no input error checking
 * the result of divide by zero is zero
 *
 * example input
 * 1 + 3 / 3 * 6 - 1
 * result 7
*/

#include <iostream>

// performs arithmetic based on two operands and one operator
double do_operation(const double, const double, const char);

int main(void)
{
    // variables
    double left    = 0;  // stores the left hand side of operation
    double right   = 0;  // stores the right hand side of operation
    double total   = 0;  // stores the total
    char operation = 0;  // stores the operator ( a c++ keyword )

    // give a nice welcome
    std::cout << "Please enter the opertion\n: ";

    // if "n" is a number and "o" is an operator, the expected input is
    // n o n o n ... o n
    // ( a number followed by a series of operator-number pairs )

    // read the left number first
    std::cin >> left;

    // keep reading until the user hits enter
    while (std::cin.peek() != '\n')
    {
        // read the operator-number pair
        std::cin >> operation >> right;

        // calculate the result
        total = do_operation(left, right, operation);

        // the total becomes the left hand number
        // in case there is another operator-number pair
        left = total;
    }

    // print result
    std::cout << "The total is: " << total << std::endl;

    return 0;
}

// do the arithmetic
double do_operation(const double left, const double right, const char operation)
{
    double result = 0;

    switch(operation)
    {
    case '+': result = left + right;  break;
    case '-': result = left - right;  break;
    case '*': result = left * right;  break;
    case '/': result = right != 0 ? left / right : 0;  break;
    // add more operations as wanted
    default : std::cout << "unexpected operand\n";
    }
    return result;
}
Last edited on
@undph
your calculator ist only for 2 numbers. I want my calculator to calculate with as many numbers as the user types in.

Try this than
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <iostream> 
using namespace std;

int main() 
{
	

	for (int sum = 0, val = 0; cin >> val;)
	{
		sum += val;
		cout << "Sum is: " << sum << endl;
	}
	system("PAUSE");
	return 0;
}
Playing around with some c++11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <map>
#include <functional>

typedef std::function< double(const double, const double) > my_function;
std::map< char,my_function > calc_func = {
  { '+', []( const double l, const double r) { return l + r; } }
  /* etc */
};

double do_operation(const double l, const double r, const char op)
{
  auto found = calc_func.find(op);
  if (found == calc_func.end())
    throw "error";

  return found->second( l, r );
}

int main(void)
{
  std::cout << do_operation( 3, 3, '+') << '\n';
  return 0;
}
Last edited on
Topic archived. No new replies allowed.