Home > Blockchain >  a calculator in functions
a calculator in functions

Time:12-04

I am trying to write a calculator using functions for each operation and switch case in c language but output is as follows: for example when i enter 5 and 6, and choose addition it prints sss5.000000 6.000000 = 11.000000 how can i remove the sss before 5.000000 and what causes this problem? thanks for helping.

    #include <stdio.h>
#include <math.h>

float addTwoNumbers(float num1, float num2);
float subtTwoNumbers(float num1, float num2);
float divideTwoNumbers(float num1, float num2);
float multTwoNumbers(float num1, float num2);
float powerTwoNumbers(float num1, float num2);

int main() {

  float num1, num2,add,subt,div,mult,pow;
  int choice;
 
  printf("choose one operation:\n 1.addition\n 2.substraction\n 
  3.division\n 4.multiplication\n 5.power\n");
  scanf("%d" ,&choice);
  printf("Enter two numbers: ");
  scanf("%f %f" ,&num1,&num2);
  add=addTwoNumbers(num1, num2);
  subt=subtTwoNumbers(num1, num2);
  div=divideTwoNumbers(num1, num2);
  mult=multTwoNumbers(num1, num2);
  pow=powerTwoNumbers(num1, num2);
  
    switch (choice) {
    case 1:
    
    printf("%f   %f = %f" ,num1,num2);
      break;
    case 2:
    
    printf("%f - %f = %f" ,num1,num2);
      break;
    case 3:
    
     printf("%f / %f = %f" ,num1,num2);
      break;
    case 4:
     
       printf("%f * %f = %f" ,num1,num2);
    case 5:
     
       printf("%f ? %f = %f" ,num1,num2);
      break;
     
    default:
      printf("Error!");
    
  }

    
  return 0;
}

float addTwoNumbers(float num1, float num2)
{
        return num1 num2;
        
}
float subtTwoNumbers(float num1, float num2)
{
        float result2;
        result2 = num1-num2;
        return result2;
}
float divideTwoNumbers(float num1, float num2)
{
        printf("s");
}
float multTwoNumbers(float num1, float num2)
{
        printf("s");
}
float powerTwoNumbers(float num1, float num2)
{
        printf("s");
}

CodePudding user response:

Much shorter and simpler:

#include <stdio.h>
#include <math.h>

float addTwoNumbers   (float num1, float num2) { return num1 num2;       }
float subtTwoNumbers  (float num1, float num2) { return num1-num2;       }
float divideTwoNumbers(float num1, float num2) { return num1/num2;       }
float multTwoNumbers  (float num1, float num2) { return num1*num2;       }
float powerTwoNumbers (float num1, float num2) { return powf(num1,num2); }

float(*operation_map[])(float,float) =
{
     addTwoNumbers,
     subtTwoNumbers,
     divideTwoNumbers,
     multTwoNumbers,
     powerTwoNumbers
};

char choice_map[]= { ' ', '-', '/', '*', '^' };

int main() {

    int choice;
    printf("choose one operation:\n"
           " 1.addition\n"
           " 2.substraction\n"
           " 3.division\n"
           " 4.multiplication\n"
           " 5.power\n");
    scanf("%d" ,&choice);
    
    printf("Enter two numbers: ");
    float num1, num2;
    scanf("%f %f" ,&num1, &num2);
    
    float result = operation_map[choice-1](num1, num2);
    printf("%.2f %c %.2f = %.2f", num1, choice_map[choice-1], num2, result);

    return 0;
}

Output

choose one operation:  1
 1.addition
 2.substraction
 3.division
 4.multiplication
 5.power
Enter two numbers: 3.14 2.71

3.14   2.71 = 5.85

CodePudding user response:

You need to pass the address of the variables you want scanf() to populate (num2 should be &num2). Check the return value of scanf() otherwise your values may be undefined. The other major problem that your printf() format strings "%d" doesn't match your arguments float arguments. Added a newline, too. Finally, you call all functions before the switch instead of just the you need in the switch. Implemented your dummy functions. Moved main() to the bottom so you don't need the prototypes of functions.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float addTwoNumbers(float num1, float num2) {
    return num1   num2;
}

float subtTwoNumbers(float num1, float num2) {
    return num1 - num2;
}

float divideTwoNumbers(float num1, float num2) {
    if(num2 == 0) {
       printf("divide by 0!\n");
       exit(1);
    }
    return num1 / num2;
}

float multTwoNumbers(float num1, float num2) {
    return num1 * num2;
}

float powerTwoNumbers(float num1, float num2) {
    return powf(num1, num2);
}

int main() {
    printf("choose one operation:\n 1.addition\n 2.substraction\n 3.division\n 4.multiplication\n 5.power\n");
    int choice;
    if(scanf("%d", &choice) != 1) {
        printf("scanf failed\n");
        return 1;
    }
    printf("Enter two numbers: ");
    float num1, num2;
    if(scanf("%f %f", &num1, &num2) != 2) {
        printf("scanf failed\n");
        return 1;
    }
    switch (choice) {
        case 1:
            printf("%f   %f = %f\n" ,
                num1,
                num2,
                addTwoNumbers(num1, num2)
            );
            break;
        case 2:
            printf("%f - %f = %f\n" ,
                num1,
                num2,
               subtTwoNumbers(num1, num2)
            );
            break;
        case 3:
            printf("%f / %f = %f\n",
                num1,
                num2,
                divideTwoNumbers(num1, num2)
            );
            break;
        case 4:
            printf("%f * %f = %f\n" ,
                 num1,
                 num2,
                 multTwoNumbers(num1, num2)
            );
            break;
        case 5:
            printf("%f^%f = %f\n" ,
                num1,
                num2,
                powerTwoNumbers(num1, num2)
            );
            break;
        default:
            printf("Error!");
    }
}

example run:

choose one operation:
 1.addition
 2.substraction
 3.division
 4.multiplication
 5.power
1 
Enter two numbers: 1.2 2.3
1.200000   2.300000 = 3.500000

@abelenky showed you how to refactor it further. Using tables like that is a powerful technique and I use it often. Another idea, and mainly to show you something different, is to observe that the only difference between the 4 cases is the operator and the last case it's the same expect you apply a powf() function. SO let's write a print() function instead, and optionally define a couple of macros OP() and OP_FUNC() that applies the operator or function and does the break. I like how the macros highlight what changes, but others will dislike the that the variables and break are hidden.

#include <stdio.h>
#include <math.h>

void print(float num1, const char *op, float num2, float result) {
    printf("%f %s %f = %f\n", num1, op, num2, result);
}

int main() {
    printf("choose one operation:\n 1.addition\n 2.substraction\n 3.division\n 4.multiplication\n 5.power\n");
    int choice;
    if(scanf("%d" ,&choice) != 1) {
        printf("scanf failed\n");
        return 1;
    }
    printf("Enter two numbers: ");
    float num1, num2;
    if(scanf("%f %f" ,&num1, &num2) != 2) {
        printf("scanf failed\n");
        return 1;
    }

#define OP(O) print(num1, #O, num2, num1 O num2);\
    break

#define OP_FUNC(O, F) print(num1, #O, num2, F(num1, num2));\
    break

    switch (choice) {
        case 1: OP( );
        case 2: OP(-);
        case 3: if(num2 != 0) OP(/); else printf("divide by 0\n");
        case 4: OP(*);
        case 5: OP_FUNC(^, powf);
        default: printf("Error!");
    }

#undef OP
#undef OP_FUNC

}
  • Related