Closed Thread
Results 1 to 6 of 6

Thread: new programer, help this question! thank you!

  1. #1
    zhan is offline Newbie
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Angry new programer, help this question! thank you!

    For this problem, you are going to construct a very simple calculator program in a file called Calculator.cpp that can just add, subtract, multiply, and divide. You will allow the user to enter sets of three "inputs". The first and third input will be numbers, and the middle input will be one of the mathematical operators +, -, *, or / . Depending upon what the user inputs, you will perform the appropriate mathematical operation, using a function. You will write five functions: add, subtract, multiply, divide, and print.

    The input lines will look like the following (entered one at a time as shown on the right):

    4.2 + 88
    321.6 - 95.1
    18 * 9
    10 / 100

    Note: for the last example above, return the value that you would expect from a calculator, which is NOT the result of integer division in C++!

    Each of the add, subtract, multiply, and divide functions should take two doubles as their parameters, and should return the result of the calculation as a double. The print function should return nothing, and should take the original two operands as doubles, the operator as a char, and the result as a double, and should print the following (corresponding to the examples above):

    4.2 + 88 = 92.2
    321.6 - 95.1 = 226.5
    18 * 9 = 162
    10 / 100 = 0.1

    If you don't write the functions, you get no points (the goal of this problem is to learn how to write functions...). You must write function prototypes before the main function, and then place the function implementations after main. Not writing prototypes will cost you 10 points. You must write file and function comments as specified in the class coding standards. Not doing that correctly will cost you up to 10 points (if you do no commenting at all). You must have a loop in your main program to allow the user to keep calculating until they want to quit. See the example to the right. Do not call print from within the math functions. Call it from main after returning the math result back from each function. Do not print from within the math functions.

    Note: The division function needs to be special, since you can get into trouble in one special case. See the example session. Return zero from the function in this case. That isn't really right, but it would be complicated to do otherwise right now...

    You don't have to worry about trapping errors where the user doesn't enter X op Y expressions, where X and Y are numbers, and op is a single character. You also don't have to trap errors where they enter something other than an integer to continue the program. You may change the prompt or method for continuing the program if you like. But make it obvious and explicit to the user what you want.


    --------------------------------------------------------------------------------

    Problem 2: Calculator Version 2 - real math (60 pts)

    For this problem make sure that you've got Problem 1 done, since you will start with it for this problem. You're going to add a nicer menu to it, and add some functionality that a calculator really should have...

    Copy the solution from Problem 1 into a file called Calculator2.cpp. First, you will add a menu of choices for the user. Really, once we learn more about reading, parsing, and manipulating strings, it would be nicer just to allow the user to enter expressions and have the program figure out what to do, but for now, we need to have the user tell us up-front what they are going to enter. So the menu will have three options: 1) enter two argument expression, 2) Enter one argument expression, and 3) Quit program. The user will enter 1, 2, or 3 to choose. See the example to the right. You will write a function to process the menu and user choice.

    If the user chooses to enter a two-argument expression, then you should read from the console just as in problem 1, and perform exactly the same calculations. However, you will add one more:

    5 ^ 3

    For which you should print:

    5 ^ 3 = 125

    So the ^ operator will represent the "power" operator (so the example means "5 to the 3rd power"). You should support any two double operands. A math library function will probably help you here...

    If the user asks for choice 2 (one argument expression), you should support the following operator-value combinations:

    S 2

    Should result in: The square root of 2 = 1.414

    E 5

    Should result in: The exponential of 5 = 148.413

    and

    L 2

    Should result in: The natural logarithm of 2 is 0.693147

    So the operators are the upper case letters S, E, and L, and the arguments follow, and can be appropriate double values. Note that you can't take the square root of a negative number, and you can't take the natural logarithm of any non-positive numbers (including zero). Make sure you protect your program from these conditions. See the figure to the right for a session of my program. You must meet the following architectural requirements:

    You must have a menu function that presents the menu and get's the user's choice. You should make sure that it will only return valid choices.
    The main program should ask the user for either the two-operand or one-operand data, and then call one of two processArgs functions (see below). Don't do much processing in main. It should then call one of the two overloaded print functions (see below).
    You must write two overloaded processArgs functions. One should take the operator and two calculation arguments, and return the result of the calculation through its return parameter. The other should take the operator and one operator argument, and return the result of the calculation through its return parameter. The first version should process two-argument operators, and the second version should process one-argument operators.
    You must write two overloaded print functions. One version, you already have from problem 1. The second must also be named print, but should take one operator, the operator argument, and the result value, and should print the square root, exponential, or natural logarithm lines, depending on what the operator is.
    To calculate the powers, square roots, exponential function, and logarithm, you need to call C++ math library functions (look them up). Make sure to check arguments when necessary to ensure that they are valid (i.e., don't allow taking the square root of a negative number). Print appropriate error messages. Never quit the program until the user asks to quit.
    You must comment all functions correctly. Document purpose, parameters, and return values (if any).
    Make sure your program input/output is at least as well formatted as in the example session. Sloppy input/output will lose points.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    YAPP's Avatar
    YAPP is offline Newbie
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Re: new programer, help this question! thank you!

    What about the question do you need help with? Just posting a whole homework question and then saying "Help!" doesn't tell what you need help with. If you want someone to do the whole problem for you, then no, I won't help. You will learn a lot more if you try the problem yourself than just asking people for answers. If there is a certain area of the problem you need help with, specify and I'll give you some hints.

  4. #3
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: new programer, help this question! thank you!

    Are you using C++ or just C?

  5. #4
    zhan is offline Newbie
    Join Date
    Jul 2008
    Posts
    2
    Rep Power
    0

    Re: new programer, help this question! thank you!

    what are listed below is my program about the homework, but I don't what's wrong with it, it doesn't work/


    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    //declarations of functions
    double processArgs(double x, double y);//two operands function
    double processArgs(double x);//one operand function
    void print(double x,double y);//two operands print functions
    void print(double x)// one operands print functions
    int processchoice();//a function to process menu or user choice
    
    //main function
    int main () 
    {
        int choice;// menu choice
        double x,y,result;
        do {
            choice=processchoice();
            swith(choice)
            case 1:result=processArgs(x,y);
                   cout<<print(x,y)<<result<<endl;
            break;
            
            case2:
            result=processArgs(x);
            cout<<print(x)<< result<<endl;
            break;
            
            if (choice==3) break;//quit the program
            
        } while (choice!=3);
        return 0;
    }
    
        
        
        //The functions to support the main program above are below
        
        int processchoice() {
            int choice;
            cout<<"1) Enter two-argument expression"<<endl;
            cout<<"2)Enter one-argument expression"<<endl;
            cout<<"3)quit program"<<endl;
            cin>>choice;
            return(choice);
        }
        
        double processArgs(double x, double y) {
            char op;
            double result;
            cout<<"chose your operator";
            cout<<"+:add"<<endl;
            cout<<"-:subtract"<<endl;
            cout<<"*:multiply"<<endl;
            cout<<"/:devide"<<endl;
            cout<<"^:power"<<endl;
            cin>>op;
            switch(op) {
                case '+': result=x+y;
                break;
                case '-': result=x-y;
                break;
                case '*': result=x*y;
                break;
                case'/':
                if (y!=0)
                result=x/y;
                else
                cout<<"Don't try to divide by zero"<<endl;
                break; 
                case'^': result=pow(x,y);
                break;
                }
                return(result);
        }
        double processArgs(double x) {
            char op;
            double result;
            cout<<"S:squar"<<endl;
            cout<<"E:exponential"<<endl;
            cout<<"L: natural logarithm"<<endl;
            cout<<"chose your operator"<<endl;
            cin>>op;
            switch(op) {
                case 'S':
                result=sqr(x);
                break;
                case 'E':
                result=exp(x);
                break;
                case 'L':
                result=log(x);
                break;
            }
            return(result);
        }
        void print(double x,double y) {
            char op;
            cout<<"Enter an X op Y expression: << x << op<<y<<endl;
            cout<< x<<op<<y<<"=";
        }
        void print(double x) {
            char op;
            cout<< " Enter an op X style expression: " << op << x << endl;
            cout << op <<x<<"=";
        }
    Last edited by WingedPanther; 07-07-2008 at 09:11 AM. Reason: add code tags

  6. #5
    YAPP's Avatar
    YAPP is offline Newbie
    Join Date
    Jul 2008
    Posts
    3
    Rep Power
    0

    Re: new programer, help this question! thank you!

    Quote Originally Posted by zhan
    swith(choice)
    You spelled 'switch' wrong, that would make a compiler error. Also, you don't appear to have any braces around your switch statement. You could rewrite it like:
    Code:
    switch(choice)
    {
        case 1:
            //do stuff
        break;
        case 2:
            //do other stuff
        break;
        case 3:
            return 0;  //Quit from within the case statement
        break;
        default:
            //if the user inputted something that wasn't a legal command
    }
    I don't know what the print() function is for, it seems like it just outputs the arguments the user entered, twice. And it will also print some garbage because you never declare the char 'op' to be equal to anything.

  7. #6
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: new programer, help this question! thank you!

    You don't need the print function. Just do this:

    Code:
    cout << x << /*some form of separation here, like a space*/ << y ... << endl;
    I think (maybe) part of what's confusing you is that the output doesn't have any separations between fields, so instead of getting 123 45678.9 you get 12345678.9.
    Last edited by dargueta; 07-07-2008 at 07:54 PM. Reason: Fixed formatting

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Looking for freelance programer for label maker development
    By nexposito in forum Request Services
    Replies: 2
    Last Post: 10-02-2011, 07:52 PM
  2. Autodidactic programer looking for guide
    By gerkrt in forum General Programming
    Replies: 1
    Last Post: 12-25-2010, 05:38 AM
  3. requesting a game trainer programer
    By userkiller in forum C and C++
    Replies: 2
    Last Post: 08-31-2009, 10:23 AM
  4. Old PHP programer
    By phpforfun in forum Introductions
    Replies: 17
    Last Post: 09-09-2008, 05:09 AM
  5. Replies: 1
    Last Post: 12-09-2007, 07:46 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts