Thread: handling basic math functions

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    19

    handling basic math functions

    Okay so I have my program almost completed but, I am stuck on something... I need to include basic math functions such as abs, sqrt, pow, exp, log, sin, cos, tan, asin, acos, atan, sinh, cosh, and tanh.

    Do I put these math functions as a case... and if so how do I put it as the result of a function or variable inputed... did that question make sense?

    ok lets say...

    if one of my cases is

    Code:
    case '+':
    res=a+b;
    cout<<res;
    how do I get one of the basic math functions to process one of the prior cases... so in better words i want to say something like

    sqrt(res)...

    except when it gets to pow of course... I will NOT use "res".

    heres my code

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    int getOperand(char ch);
    int IsInList(char ch);
    
    char cVar[10]; 
    double dVar[10]; 
    int varNum; 
    
    int main ( )
    {
        char ch;
        char op;
        int op1;
        int op2;
        double res;
        varNum = 0;
    
        cout<<"You will be inputing numbers associated with variables.\n";
        cout<<"For Example a=3, b=2, etc.  Once the values are inputed\n";
        cout<<"you are able to input a specific math function, such as\n";
        cout<<"a+b, a*b, etc. Please input a value below:\n";
        cout<<"\n";
        cout<<"\n";
    
        do 
        {
            cout << ">> "; 
            cin >> ch;
            if (ch == 'q') 
            {
                break;
            }
            
        op1 = getOperand(ch);
    
        cin >> ch;
        if(ch == '=')
        {
            cin >> dVar[op1];
            cout << cVar[op1] << " =\n\n";
            cout << "      " << dVar[op1] << "\n\n";
        }
        else
        {
            op = ch;
    
            cin >> ch;
            op2 = getOperand(ch);
    
            switch(op)
            {
            case '+':
                    res = dVar[op1] + dVar[op2];
                    cout << "Ans = \n\n"; 
                    cout << "       " << res << "\n\n";
                    break;
            case '*':
                    res = dVar[op1] * dVar[op2];
                    cout << "Ans = \n\n";
                    cout << "        " << res << "\n\n";
                    break;
            case '/':
                    res = dVar[op1]/dVar[op2];
                    cout << "Ans = \n\n";
                    cout << "        " << res << "\n\n";
                    break;
            case '-':
                    res = dVar[op1] - dVar[op2];
                    cout << "Ans = \n\n";
                    cout << "        " << res << "\n\n";
                    break;
            case '%':
                    res = fmod(dVar[op1], dVar[op2]);
                    cout << "Ans = \n\n";
                    cout << "        " << res << "\n\n";
                    break;
                      
            }
        }
    
        }
        while (1); 
        
    }
    
    int getOperand(char ch) 
    {
        int iVar;
        iVar = IsInList(ch);
    
            if (iVar == -1)
            {
                cVar[varNum] = ch;
                iVar = varNum;
                varNum++;
            }
        
        return iVar; 
    }
    
    int IsInList(char ch)
    {
        for (int i = 0; i < 10; i++)
        {
            if(ch == cVar[i]) 
            {
                return i;
            }
        }
    
        return -1;    
    }
    I hope that made sense... thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    I think you need to read in a string, then decide whether that string has the form
    value operator value
    or
    function( value )

    Then you can make the appropriate determination in your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Um, not much sense... You cannot use strings in switch statements, only integer values (and similar). You may want to do some kind of special treatment for these cases:
    Code:
    std::string operator;
    
    std::cin >> operator
    
    switch(operator[0]) //Check the first letter
    {
      case '+':
      {
        ...
        break;
      }
    
      ...
    
      default:
      {
        if(operator == "sin")
        {
          ...
        }
        else if(operator == "cos")
        {
          ...
        }
    
        ...
    
        break;
      }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'd just use a number mapped to an operator using a menu:
    Code:
    int opNum;
    cout << "enter the number next to the operation you want to perform" << endl;
    cout << "1) +" << endl;
    cout << "2) - " << endl;
    cout << "3) %" << endl;
    cout << "4) sin" << endl;
    cout << "5) sqrt" << endl;
    //etc
    cin >> opNum;
     
    switch (opNum)
    {
      case 1:
    	return op1 + op2;
    	break;
      case 2:
    	return op1 - op2;
    	break;
      .
      .
      .
      case 4:
    	return sin(op1);
    	break;
      case 5:
    	return sqrt(op1);
    	break;
      //etc.
    }
    Alternatively, you could declare an enum type to "encapsulate" the values and allow user to type in name of operation to use (instead of indicating the number). Probably other approaches, too.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 07:22 PM
  2. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  3. Math Functions
    By glider_pilot123 in forum C Programming
    Replies: 1
    Last Post: 10-14-2004, 02:22 PM
  4. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM