Thread: Math Operations in a Variable?

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Math Operations in a Variable?

    Alright. I've been following the tutorials offered by this site, and I've done a whole slew of lessons. So I'm trying to combine most of them into one program, and I've chosen to make a basic calculator. I completed it, and it works nicely. It first began with integers, now I've allowed it to use decimals as well. So, here's the code for the calculator.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int input;
        float inputadd1;
        float inputadd2;
        float addinganswer;
        float divideanswer;
        float function;
    
    		cout<<"Awesome Calculator is Awesome.\n\n";
        do
        {
    		cout<<"1 = Add\n";
    		cout<<"2 = Subtract\n";
    		cout<<"3 = Multiply\n";
    		cout<<"4 = Divide\n";
    		cout<<"5 = Quit\n";
    		cout<<"Selection: ";
    		cin>> input;
    		switch ( input ){
    			case 1:
    			cout<<"\nFirst number: ";
    			cin>> inputadd1;
    			cout<<"\nAdd what to it?: ";
    			cin>> inputadd2;
    			addinganswer = (inputadd1 + inputadd2);
    			cout<<"--------------------------------------\n";
    			cout<<""<< inputadd1 <<" + "<< inputadd2 <<" = "<< addinganswer <<"\n";
                            cout<<"--------------------------------------\n\n";
    			cin.get();
    			break;
    
    			case 2:
    			cout<<"\nFirst number: ";
    			cin>> inputadd1;
    			cout<<"\nSubtracted by?: ";
    			cin>> inputadd2;
    			addinganswer = (inputadd1 - inputadd2);
    			cout<<"--------------------------------------\n";
    			cout<<""<< inputadd1 <<" - "<< inputadd2 <<" = "<< addinganswer <<"\n";
    			cout<<"--------------------------------------\n";
    			cin.get();
    			break;
    
    			case 3:
    			cout<<"\nFirst number: ";
    			cin>> inputadd1;
    			cout<<"\nMultiplied by?: ";
    			cin>> inputadd2;
    			addinganswer = (inputadd1 * inputadd2);
    			cout<<"--------------------------------------\n";
    			cout<<""<< inputadd1 <<" x "<< inputadd2 <<" = "<< addinganswer <<"\n";
    			cout<<"--------------------------------------\n";
    			cin.get();
    			break;
    
    			case 4:
    			cout<<"\nFirst number: ";
    			cin>> inputadd1;
    			cout<<"\nDivided by?: ";
    			cin>> inputadd2;
    			divideanswer = (inputadd1 / inputadd2);
    			cout<<"--------------------------------------\n";
    			cout<<""<< inputadd1 <<" / "<< inputadd2 <<" = "<< divideanswer <<"\n";
    			cout<<"--------------------------------------\n";
    			cin.get();
    			break;
    
    			case 5:
    			cout<<"Quitting...";
    			break;
    
    			default:
    			cout<<"--------------------------------------\n";
    			cout<<"\nBad input..\n";
    			cout<<"--------------------------------------\n";
    			break;
    		}
    	}while (input != 5);
    }
    Now, there is nothing (that I know), that is wrong with this program. I've compiled it many times, and it works perfectly. I don't need help with fixing it, or anything like that. What I want to do, is to shrink the program down. To optimize it. I see a clear way that I can do it, but I need to know one thing: If it's possible to store math operations in a variable. For example:

    A = +
    B = -
    C = /
    D = *

    Though in my program, it'll all be stored in one variable, but you get the idea. I've tried using the 'float' variable, though it doesn't work...Or I'm just using it incorrectly. So, here's a little snippit of what I tried: (I'll just use case 1, you can see the original above.)

    Code:
    case 1:
    			cout<<"\nFirst number: ";
    			cin>> inputadd1;
    			cout<<"\nAdd what to it?: ";
    			cin>> inputadd2;
                            function = (+);
    			addinganswer = (inputadd1 "<< function <<" inputadd2);
    			cout<<"--------------------------------------\n";
    			cout<<""<< inputadd1 <<" + "<< inputadd2 <<" = "<< addinganswer <<"\n";
                            cout<<"--------------------------------------\n\n";
    			cin.get();
    			break;
    So I bolded the part that I'm really trying to get at. Basically, if I haven't managed to get it across, I want the +,-,*,/ in the variable "function". So in this example, function will be an addition operation, and in essence, it should add inputadd1 and inputadd2 together. But as I've done this wrong, it doesn't. So, is their a specific type of variable I must use, or am I just approaching this the wrong way?

    Quick Edit: As I mentioned above, I have been reading the tutorials, and I just may have missed it. But I did do a quick search for it, and didn't stumble across anything. So thanks for any help. :]

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    First, its good that you realize the unnecessarily repeated code in the program and want to minimize it.

    For "storing an operation in a variable", well, you pretty much are already doing that! Your mapping 1 to add, 2 to subtract, and so on. If your asking how to store the operator in a variable, and use that variable to perform the operation itself, then you cant do that. You, at some point, have to use a switch (or similar method) to say "for character operator '+', I tell the computer to add (+)". Here is some C++/pseudocode that might help:
    Code:
    int main(void)
      - get operation from user (as a "char" which represents the operator, rather than a number as you are now)
      - get number 1 from user
      - get number 2 from user
      - print the result of 'eval' of the function
    }
    
    float eval(char operator, float num1, float num2)
    {
      - use switch statement on the operator to determine what operation to do
        - make sure to prevent the case where op == '/' and num2 == '0'
      - return the value
    }
    Last edited by nadroj; 10-29-2009 at 08:53 AM.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    If it's possible to store math operations in a variable. For example:

    A = +
    B = -
    C = /
    D = *
    for built-in types, no, you cannot.

    for class objects, or a custom function, you can store a method or function pointer, respectively.

    what can work in all cases is a functor, where you encapsulate the operation you want to perform in a class.

    i think you still have a while before you get there though, so i think the switch is the right choice from your current toolbox.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Well that's depressing. Thanks for the tips, though!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 03:35 AM
  2. American math students ...
    By whiteflags in forum A Brief History of Cprogramming.com
    Replies: 36
    Last Post: 05-26-2008, 12:47 PM
  3. variable being reset
    By FoodDude in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2005, 12:30 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Problem with a char variable set as a letter
    By 7smurfs in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2004, 01:25 PM