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

closed account (EwCjE3v7)
Here is my calculator
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````#include 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.

closed account (EwCjE3v7)
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.

 ``123`` ``````#include vector Numbers;``````
Last edited on
keep is simple.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` `````` #include 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; cout<<" input operator aritmetico: ( + , - , * , / ) "<>operatore; switch (operatore) { case '+': cout<<" sum "<>b; cout<<(a+=b); break; case '-': cout<<" subctraction "<>b; cout<<(a-=b); break; case '*': cout<<" multipler "<>b; cout<<(a*=b); break; case '/': cout<<" division "<>b; if (b==0) cout<<" divisione per 0 indeterminata"<>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:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768`` ``````/* 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 // 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
closed account (EwCjE3v7)
@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
 ``12345678910111213141516`` `````` #include 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

 ``123456789101112131415161718192021222324`` ``````#include #include #include 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.