【问题标题】:a calculator in c using functions and switch statementa calculator in c using functions and switch statement
【发布时间】:2022-12-02 04:01:03
【问题描述】:

I am trying to write a calculator using functions for each operation and switch case in c language but i can't seem to call functions inside cases. thanks in advance. `

#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:
    add = addTwoNumbers(num1, num2);
    printf("%f + %f = %f" ,num1,num2,add);
      break;
    case 2:
    subt = subtTwoNumbers(num1, num2);
    printf("%d - %d = %d" ,num1,num2,subt);
      break;
    case 3:
     div = divideTwoNumbers(num1, num2);
     printf("%d / %d = %d" ,num1,num2,div);
      break;
    case 4:
      mult = multTwoNumbers(num1, num2);
       printf("%d * %d = %d" ,num1,num2,mult);
    case 5:
      pow = powerTwoNumbers(num1, num2);
       printf("%d ? %d = %d" ,num1,num2,pow);
      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");
}


 

`

i might be giving the type of inputs wrong. when i change float to int, my program outputs numbers like 39431845 instead of printing 5 for example and finds 5+2=0 (not the exact results but similar). when i use float it just doesn't output anything. I wanted to call functions inside cases.

【问题讨论】:

  • Why are you using %d in some printf statements? That's for integers. Use %f for float.
  • Look closely at this line: scanf("%f %f" ,&amp;num1,num2); One param has a &amp;, the other does not.
  • sorry i forgot to change them but when i use %f in all of them, it still doesn't work.
  • @DilaraA Show themost correctcode that you have that is not working. Not some stale version which you know have bugs.
  • Best to not re-use pow as a variable as pow() is a &lt;math.h&gt; function. Use another name.

标签: c function switch-statement case calculator


【解决方案1】:

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* format_map[]=
{
    "%.2f + %.2f = %.2f
",
    "%.2f - %.2f = %.2f
",
    "%.2f / %.2f = %.2f
",
    "%.2f * %.2f = %.2f
",
    "%.2f ^ %.2f = %.2f
"
};

int main() {

    int choice;
    
    printf("choose one operation:
 1.addition
 2.substraction
 3.division
 4.multiplication
 5.power
");
    scanf("%d" ,&choice);
    
    printf("Enter two numbers: ");
    float num1, num2;
    scanf("%f %f" ,&num1, &num2);
    
    float result = operation_map[choice-1](num1, num2);
    printf(format_map[choice-1], num1, 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

【讨论】:

    【解决方案2】:

    You need to pass the address of the variables you want scanf() to populate (num2 should be &amp;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 num1 (float). Added a newline, too. Finally, you call all functions before the switch instead of just the you need in the switch:

    #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 pow(num1, num2);
    }
    
    int main() {
        float num1, num2,add,subt,div,mult,pow;
        int choice;
        printf("choose one operation:
     1.addition
     2.substraction
     3.division
     4.multiplication
     5.power
    ");
        if(scanf("%d" ,&choice) != 1) {
            printf("scanf failed
    ");
            return 1;
        }
        printf("Enter two numbers: ");
        if(scanf("%f %f" ,&num1, &num2) != 2) {
            printf("scanf failed
    ");
            return 1;
        }
        switch (choice) {
            case 1:
                add = addTwoNumbers(num1, num2);
                printf("%f + %f = %f
    " ,num1,num2,add);
                break;
            case 2:
                subt = subtTwoNumbers(num1, num2);
                printf("%f - %f = %f
    " ,num1,num2,subt);
                break;
            case 3:
                div = divideTwoNumbers(num1, num2);
                printf("%f / %f = %f
    " ,num1,num2,div);
                break;
            case 4:
                mult = multTwoNumbers(num1, num2);
                printf("%f * %f = %f
    " ,num1,num2,mult);
            case 5:
                pow = powerTwoNumbers(num1, num2);
                printf("%f^%f = %f
    " ,num1,num2,pow);
                break;
            default:
                printf("Error!");
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2022-12-01
      • 2017-04-07
      • 2022-12-02
      • 1970-01-01
      • 2022-12-01
      • 2021-11-30
      • 2011-10-05
      • 1970-01-01
      相关资源
      最近更新 更多