Calculator Help part 2

This is a discussion on Calculator Help part 2 within the C Programming forums, part of the General Programming Boards category; Write a program to model a simple calculator. Each data line should consist of the next operation to be performed ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    19

    Calculator Help part 2

    Write a program to model a simple calculator. Each data line should consist of the next operation to be performed from the list below and the right operand. Assume the left operand is the accumulator value (with an initial value of 0). You need a function scan_data() with two output parameters that returns the operator and the right operand scanned from the data line. You need a function do_next_op() that performs the required operation. do_next_op() has two input parameters (the operator and the operand) and one input/out put parameters ( the accumulator). The valid operators are:

    + add
    - minus
    * multiply
    / divide
    ^ power(raise left operand to power of right operand)
    q quit

    Your calculator should display the accumulator value after each operation.

    Sample run:

    Calculator program ( RESULT=0)

    Input: + 5.0 (enter)
    Result:5.0

    Input: ^ 2
    (enter)
    Result:25.0

    Input: / 2
    (enter)
    Result:12.5

    Input: q
    (enter)
    Final Result:12.5

    my code:
    Code:
     
    Code:
    #include<stdio.h>
    
    float plus(float result, float number);
    float minus(float result, float number);
    float divide(float result, float number);
    float multiply(float result, float number);
    float power(float result, float number);
    float quit(float result, float number);
    
    
    main(){
        
        float result=0;
        float number;
        char input;
        
        printf("Calculator Program (RESULT=0)\n");
        
        do{
            printf("INPUT: ");
            scanf("%c %f", &input, &number);
        
            printf("%c %.2f", input, number);
    
            if(input='+'){
            result=plus(result,number);
            }
        
            else if(input='-'){    
            result=minus(result,number);
            }
            else if(input='*'){
                result=multiply(result,number);
            }
            else if(input='/'){
            result=divide(result,number);
            }
            else if(input='^'){
            result=power(result,number);
            }
            else if(input='q'){
            result=quit(result,number);
            }
            else{
            printf("INVALID INPUT");
            }
    
        }while(1);
    
    }
    
    
    float plus(float result, float number){
        result=result+number;
        printf("RESULT:", result);
        return result;
    }
    
    float minus(float result, float number){
        result=result-number;
        printf("RESULT:", result);
        return result;
    }
    
    float multiply(float result, float number){
        result=result*number;
        printf("RESULT:", result);
        return result;
    }
    
    float divide(float result, float number){
        result=result/number;
        printf("RESULT:", result);
        return result;
    }
    
    float power(float result, float number){
        result=result*result;
        printf("RESULT:", result);
        return result;
    }
    
    float quit(float result, float number){
        printf("FINAL RESULT:", result);
        exit (0);
    }



    is my code correct?? it not working properly...

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    NEW CODE:
    Code:
    #include<stdio.h>
    
    float plus(float result, float number);
    float minus(float result, float number);
    float divide(float result, float number);
    float multiply(float result, float number);
    float power(float result, float number);
    float quit(float result, float number);
    
    
    main(){
        
        float result=0;
        float number;
        char input;
        
        printf("Calculator Program (RESULT=0)\n");
        
        do{
            printf("INPUT: ");
            scanf("%c %f", &input, &number);    
    
            if(input='+'){
            result=plus(result,number);
            }
        
            else if(input='-'){    
            result=minus(result,number);
            }
            else if(input='*'){
            result=multiply(result,number);
            }
            else if(input='/'){
            result=divide(result,number);
            }
            else if(input='^'){
            result=power(result,number);
            }
            else if(input='q'){
            result=quit(result,number);
            }
            else{
            printf("INVALID INPUT");
            }
    
        }while(1);
    
    }
    
    
    float plus(float result, float number){
        result=result+number;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float minus(float result, float number){
        result=result-number;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float multiply(float result, float number){
        result=result*number;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float divide(float result, float number){
        result=result/number;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float power(float result, float number){
        result=result*result;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float quit(float result, float number){
        printf("FINAL RESULT: %.2f\n\n", result);
        exit (0);
    }

    power multiply and divide has bug..how to fix it??

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    > if(input='+')
    There's a lot of = where there should be ==

    PS
    There is little need for font/size candy in your posts, especially when you apply it to ALL your text.
    Careful use to emphasise specific points works well. A big wall of large text just puts people off.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    THANKS SIR!!!
    my new code:

    Code:
    #include<stdio.h>
    
    float plus(float result, float number);
    float minus(float result, float number);
    float divide(float result, float number);
    float multiply(float result, float number);
    float power(float result, float number);
    float quit(float result, float number);
    
    
    main(){
        
        float result=0;
        float number;
        char input;
        
        printf("Calculator Program (RESULT=0)\n");
        
        do{
            printf("INPUT: ");
            fflush(stdin);
            scanf("%c %f", &input, &number);    
    
            if(input=='+'){
            result=plus(result,number);
            }
        
            else if(input=='-'){    
            result=minus(result,number);
            }
            else if(input=='*'){
            result=multiply(result,number);
            }
            else if(input=='/'){
            result=divide(result,number);
            }
            else if(input=='^'){
            result=power(result,number);
            }
            else if(input=='q'){
            result=quit(result,number);
            }
            else{
            printf("INVALID\n");
            }
    
        }while(1);
    
    }
    
    
    float plus(float result, float number){
        fflush(stdin);
        result=result+number;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float minus(float result, float number){
        fflush(stdin);
        result=result-number;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float multiply(float result, float number){
        fflush(stdin);
        result=result*number;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float divide(float result, float number){
        fflush(stdin);
        result=result/number;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float power(float result, float number){
        fflush(stdin);
        result=result*result;
        printf("RESULT: %.2f\n\n", result);
        return result;
    }
    
    float quit(float result, float number){
        printf("FINAL RESULT: %.2f\n\n", result);
        exit (0);
    }

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    Use int main()
    Instead of exit 0; say return 0;
    You don't check for any invalid number input
    You should protect the divide function against division by 0
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Also, don't use fflush(stdin).
    SourceForge.net: Fflush - cpwiki
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ROM Qualifier (Part 2)
    By Syndacate in forum C Programming
    Replies: 7
    Last Post: 02-06-2011, 04:21 PM
  2. Part 243?!?
    By RadRacer in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 11-28-2004, 07:56 PM
  3. Who are you? Part 2
    By Yoshi in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 12-05-2003, 11:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21