简单计算器程序
#include <stdio.h>
int main()
{
float num1;
float num2;
float res;
int i;
int ret;
while( 1 )
{
printf("-------------calc---------------- \n");
printf("\033[47;34mplease input 0 - 4:\033[0m \n");
printf("\033[47;34m OFF : please input 0\033[0m\n");
printf("\033[47;31mnum1 + num2 : please input 1\033[0m\n");
printf("\033[47;31mnum1 - num2 : please input 2\033[0m\n");
printf("\033[47;31mnum1 * num2 : please input 3\033[0m\n");
printf("\033[47;31mnum1 / num2 : please input 4\033[0m\n");
printf("\033[47;31mcompare the num1 and num2 : please input 5\033[0m\n");
printf("-------------calc---------------- \n");
scanf("%d",&i);
if( 0 == i)
{
break;
}
if((i<1)||(i>5))
{
printf("inputs error please inputs 1 - 4:\n");
continue;
}
switch(i)
{
case 1:
printf("\033[47;34mplease input the first number:\033[0m\n");
scanf("%f",&num1);
printf("\033[47;34mplease input the second number:\033[0m\n");
scanf("%f",&num2);
res = num1 + num2;
printf("\033[47;31m%.2f + %.2f =%.2f\033[0m\n",num1,num2,res);
break;
case 2:
printf("\033[47;34mplease input the first number:\033[0m\n");
scanf("%f",&num1);
printf("\033[47;34mplease input the second number:\033[0m\n");
scanf("%f",&num2);
res = num1 - num2;
printf("\033[47;31m%.2f - %.2f =%.2f\033[0m\n",num1,num2,res);
break;
case 3:
printf("\033[47;34mplease input the first number:\033[0m\n");
scanf("%f",&num1);
printf("\033[47;34mplease input the second number:\033[0m\n");
scanf("%f",&num2);
res = num1 * num2;
printf("\033[47;31m%.2f * %.2f =%.2f\033[0m\n",num1,num2,res);
break;
case 4:
printf("\033[47;34mplease input the first number:\033[0m\n");
scanf("%f",&num1);
printf("\033[47;34mplease input the second number:\033[0m\n");
scanf("%f",&num2);
printf("\033[47;31m%.2f / %.2f =%.2f\033[0m\n",num1,num2,res);
res = num1/ num2;
break;
case 5:
printf("\033[47;34mplease input the first number:\033[0m\n");
scanf("%f",&num1);
printf("\033[47;34mplease input the second number:\033[0m\n");
scanf("%f",&num2);
ret = (num1 > num2) ? 1 : (num1 == num2) ? 0 : -1 ;
printf("-------------notes---------------- \n");
printf("\033[47;31mnum1 > num2 : output is 1\033[0m\n");
printf("\033[47;31mnum1 = num2 : output is 0\033[0m\n");
printf("\033[47;31mnum1 < num2 : output is -1\033[0m\n");
printf("-------------notes---------------- \n");
printf("\033[47;31mret = %d\033[0m\n",ret);
break;
}
}
return 0;
}