📜  如何使用 switch in 创建计算器 - C 编程语言代码示例

📅  最后修改于: 2022-03-11 15:04:42.405000             🧑  作者: Mango

代码示例1
#include 
#include 

int main()
{
    float a,b,s;
    char d;

    printf("choose an operation btwn + - * /  : ");
    scanf("%c",&d);
    switch(d){
    case '+' :
        printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a + b ;
        printf("%.1f + %.1f = %.1f",a,b,s);
        break;
    case '-' :
        printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a - b ;
        printf("%.1f - %.1f = %.1f",a,b,s);
        break;
    case '*' :
         printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a * b ;
        printf("%.1f * %.1f = %f",a,b,s);
        break;
    case '/' :
         printf("enter two numbers ");
        scanf("%f%f",&a,&b);
        s = a / b ;
        printf("%.1f / %.1f = %.1f",a,b,s);
        break;
    default :
        printf("enter a valid operation !");
        break;
    }
    return 0;
}