how can i solve this problem ???

Discussion in 'C++' started by cooop, Jul 13, 2009.

  1. cooop

    cooop New Member

    Joined:
    Jul 8, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Write a program to simulate hand-held electronic calculator. Your program should execute as follows:

    • Step 1: display a prompt and wait for the user to enter an instruction code (a single character):
    ‘+’ for addition
    ‘-‘ for subtraction
    ‘*’ for multiplication
    ‘/’ for division
    ‘p’ for power
    ‘c’ to clear the current accumulator
    ‘s’ for square root
    ‘l’ for log
    ‘n’ for ln
    ‘q’ for quit
    • Step 2: (if needed) display a prompt and wait for the user to enter a type float number (which we will call left-operand)
    • Step 3: (if needed) display a prompt and wait for the user to enter a type float number (which we will call right-operand)
    • Step 4: display the accumulated result at any point during processing and repeat steps 1 through 3 using a GOTO statement (no loops in this assignment).

    Use a separate function enter_code to prompt the user for instruction code and to ensure that a valid code is entered. Also, use a separate function enter_operand for the entry of the left-operand and the right-operand. Finally, use another function compute to perform the indicated operation. (unless q was entered).
     
  2. fob500

    fob500 New Member

    Joined:
    Jul 12, 2009
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Ok this wont be that difficult.In the main you got to take the input from the user(what he wants to do) and than use switch(choice) or else you can use nested if else statements.Depending on the choice the user makes each choice would invoke the required function from main.For example
    ----------------------------------------------------------
    int i;
    switc(i)
    {
    case 1: assuming case 1 is addition
    int sum = add (x,y)
    .
    .
    }
    ----------------------------------------------------------
    So go about this way and you obviously will have to define each function aditon ,subtraction,multiplication and so on.
    I suggest you write something and post it coz no one's going to code for you.Let me know if it helped
     
  3. cooop

    cooop New Member

    Joined:
    Jul 8, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Thnx.
    If I want to use nsted if can u give me an example from one of them ?
     
  4. cooop

    cooop New Member

    Joined:
    Jul 8, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Thnx.
    If I want to use nsted if can u give me an example from one of them ?
     
  5. fob500

    fob500 New Member

    Joined:
    Jul 12, 2009
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    main()
    {

    int choice;
    printf("enter 1 for addition,2 for subtraction............")
    if(choice==1)
    {
    int sum = add(x,y);
    ..
    .
    }
    else
    if(choice ==2)
    {
    int diff = sub(x,y);
    .
    .
    }
    else
    if
    {
    .
    }
    -----------------------------------------------------------------
    and so on.You can also google "syntax of if-else" for more help.Hope that was helpful
     
  6. cooop

    cooop New Member

    Joined:
    Jul 8, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
  7. cooop

    cooop New Member

    Joined:
    Jul 8, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    is this right ?
    if it is wrong can u correct it so i can do the complite it :D

    Code:
     
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main ()
    {
        float k,sum,x,y,p;
        cout << " enter 2 values" ;
        cin >> x,y;
        if ( k == 1 )
        {
        sum = x+y;
        cout << " addition is : " << sum << endl;
    }
    else if ( k == 2 )
    {
    float division =( x/y && y!=0) || (y/x && x!=0);
    cout << " divisionis " << division << endl;
    }
    else if ( k == 3 )
    {
    float subtraction = x-y || y-x ;
    cout << "subtraction" << subtraction << endl;
    }
    else if ( k == 4 ) 
    { 
    float multiplication = x*y;
    cout << "multiplication" << multiplication << endl ;
    }
    else if ( k == p ) 
    {
    float power = pow(x,y) || pow (y,x);
    cout << "power" << power << endl;
    }
     
    Last edited by a moderator: Jul 16, 2009
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > is this right ?

    Well, does it work as you want? If not then it's not right. If it does then it is right.
    Have you tried compiling it?
    Did you get any errors?

    Programming is very much about doing stuff yourself. If you're so stuck that you can't even plug code into a compiler then I would suggest you're way out of your depth already and should consider getting some extra tuition outside class otherwise you're never going to make it.
     
  9. hotnspicy

    hotnspicy New Member

    Joined:
    Jul 6, 2009
    Messages:
    44
    Likes Received:
    4
    Trophy Points:
    0
    in VB i can make it
     
    naimish likes this.
  10. cooop

    cooop New Member

    Joined:
    Jul 8, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <iostream>
    #include <cmath>
    #include <stdio>
    using namespace std;
    int main ()
        { 
          float first_num, second_num; 
          int which_one;          
          float result;  
          float dfirst;
    float  dsecond;
    float  dnum1;
    float  dnum2;          
          cout << "Please enter the first number : ";
          cin >> first_num;
          cout << "Please enter the second number : ";
          cin >> second_num;
    
          cout << "Press 1 to add the two numbers"
               << endl
               << "Press 2 to subtract the two numbers."
               << endl
               << "Press 3 to multiply the two numbers."
               << endl
               << "Press 4 to divide the two numbers."
               << endl
               << "Press 5 to sqrt numbers."
               << endl
               << "Press 6 to power the two numbers."
               << endl
                << "Press 7 to log the two numbers."
               << endl;
               
               
               
          cin >> which_one;
          if (which_one == 1)
            result = first_num + second_num;
          if (which_one == 2)
            result = first_num - second_num;
          if (which_one == 3)
            result = first_num * second_num;
          if (which_one == 4)
            result = first_num / second_num;
          if (which_one ==5)
           result = sqrt(first_num);
           if (which_one == 6)
            result = pow(first_num , second_num);
             if (which_one == 7)
            result = log(first_num );
            
            
          cout << "The result is " << result << endl;
        
    int f;
    cin >> f;
    return 0;
    }



    what is goto statement ?
    and how can i write c,q,ln ?
     
    Last edited by a moderator: Jul 17, 2009
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    > what is goto statement ?
    http://lmgtfy.com/?q=c+++goto

    > and how can i write c,q,ln ?
    Don't understand the question. cout << c << q << ln << endl; ? Or did you mean something other than "display the contents of the variables on the screen"?
     
  12. cooop

    cooop New Member

    Joined:
    Jul 8, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    i mean how can i write this ?
    ‘c’ to clear the current accumulator
    ‘n’ for ln
    ‘q’ for quit
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Oh I see. You can't. The way you've done it - taking numeric input for the operation the user wants to do - means you can only accept numerical commands. But the assignment explicitly says you should get + for add, - for subtract and so on. So instead of int which_one; cin >> which_one; you need to declare which_one to be a char so that the user can enter a character command.

    Also note the assignment states "Use a separate function enter_code to prompt the user for instruction code" and mentions another function as well - if you don't do this then after all the work you've done you'll still fail it.

    Untested:
    Code:
    char enter_option()
    {
      char option;
      int valid=0;
      while (!valid)
      {
        cin >> option;
        if (strchr(option,"+-*/pcslnq"))
          valid=1;
        else
          cout<<"Invalid operation; please re-enter"<<endl;
      }
      return option;
    }
    
    The assignment doesn't ask you to display a menu.

    One thing I'm not clear on is why left_operand *and* right_operand are needed. On a handheld calculator typically you use the current accumulator and a single new number, so you would enter + to add, then enter a new number and press =, and that would add the new number to the accumulator. Perhaps you could ask what should happen if the user enters +: does the program ask for two numbers, or does it ask for one and add the number to the accumulator? If it asks for two numbers and + adds those, what does it do with the number in the accumulator - does the new result overwrite that?

    More untested:
    Code:
    int main()
    {
      char opt;
      float left_operand=0,right_operand=0,acc=0;
      top:
      opt=enter_option();
      if (opt=='q')
        return 0;
      enter_operand(&left_operand,&right_operand,opt);
      acc=compute(left_operand,right_operand,opt);
      cout<<acc;
      goto top;
      return 0;
    }
    
    void enter_operand(float *left, float *right, char opt)
    {
      if (strchr(opt,"+-*/psln"))
      {
        cin>>*left;
      }
      if (strchr(opt,"+-*/p"))
      {
        cin>>*right;
      }
    }
    
    float compute(float left, float right, char opt)
    {
      switch (opt)
      {
      case '+': return left+right;
      case '-': return left-right;
      case '*': return left*right;
      case '/': return left/right;
      case 'p': return pow(left,right);
      case 'c': return 0;
      case 's': return sqrt(left);
      case 'l': return log(left);
      case 'n': return ln(left);
      }
      return 0;
    }
    
    Well there you go. Surprised myself; I don't normally do homework, but watching you struggle with this is too painful. This is the first pass - I haven't compiled this code so I don't know if there are any silly errors (there probably are). You'll have to figure out what to #include to make the function calls strchr, log, pow etc work OK but I imagine those will be in your course notes somewhere.

    I've resolved the question about the accumulator by having the result overwrite the previous value. If you want to add up a few numbers then you'll need to keep re-entering the current result, e.g. to add 2,3,5,2 you need to do 2+3=5, 5+5=10, 10+2=12. This is not like a calculator where you would usually do 2+3+5+2= and that would display 12 with the interim results displayed as you enter each option.
     
    shabbir likes this.
  14. cooop

    cooop New Member

    Joined:
    Jul 8, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    switch (opt)
    {
    case '+': return left+right;
    case '-': return left-right;
    case '*': return left*right;
    case '/': return left/right;
    case 'p': return pow(left,right);
    case 'c': return 0;
    case 's': return sqrt(left);
    case 'l': return log(left);
    case 'n': return ln(left);

    can i use if statement instead of witch ?
    cuz i cant use anything but "if" in this hw ;S
     
  15. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yes; switch statements are functionally equivalent to if/else if ladders and are easier to read. You can replace
    Code:
      switch (opt)
      {
      case '+': return left+right;
      case '-': return left-right;
      case '*': return left*right;
      // etc
      }
    
    with
    Code:
    if (opt=='+') return left+right;
    else if (opt=='-') return left-right;
    else if (opt=='*') return left*right;
    // etc
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice