#include<stdio.h>
int main()
{
float a , b ;
char c ;
printf("please enter your expression : ") ;
scanf("%f%c%f", & a , & c , & b) ;
switch (c) {
case '+' : printf("The result is %.2f\n" , a + b) ; break ;
case '-' : printf("The result is %.2f\n" , a - b) ; break ;
case '*' : printf("The result is %.2f\n" , a * b) ; break ;
case '/' : if(b != 0) printf("The result is %.2f\n" , a / b) ; else printf("error : divide by zero !\n") ; break ;
default : printf("Are you looking for trouble ?\n") ;
}
}
编译、运行实况:D:\[00.Exerciese.2022]\C>g++ -o x x.c
D:\[00.Exerciese.2022]\C>x
please enter your expression : 8+5
The result is 13.00
D:\[00.Exerciese.2022]\C>x
please enter your expression : 8/5
The result is 1.60
D:\[00.Exerciese.2022]\C>x
please enter your expression : 8/0
error : divide by zero !
D:\[00.Exerciese.2022]\C>x
please enter your expression : 3^3
Are you looking for trouble ?
D:\[00.Exerciese.2022]\C>
|