C语言实现的简易计算器

     实现简易计算器,可以处理两个操作数的加减乘除和取余。

 

#include 

int main()
{
     double num1,num2;
     char oper;
     printf("input:1+2 output:3\n");
     printf("Enter your:\n");
     scanf("%lf%c%lf",&num1,&oper,&num2);

     switch(oper)
     {
         case '+':
          printf("=%.2f",num1+num2);
          break;
         case '-':
          printf("=%.2f",num1-num2);
          break;
         case '*':
          printf("=%.2f",num1*num2);
          break;
         case '/':
          if(num2==0) printf("\nError!!Try again");
          else
               printf("%.2f",num1/num2);
               break;
         case '%':
          if(num2==0) printf("\nError!!Try again");
          else
               printf("%ld",(long)num1/(long)num2);
               break;
         default:
              printf("\nError!!Try again");

     }

     return 0;
}

 

你可能感兴趣的:(C,简易计算器-C)