Post by huangno1 on Feb 26, 2017 21:21:54 GMT -8
程式說明
Please input an expression like 100 + 3
Operators should be in {+, -, *, /, %}, others with end the program
>100 + 3
100 + 3 = 103.
>100 - 3
100 - 3 = 97.
>100 * 3
100 * 3 = 300.
>100 / 3
100 / 3 = 33.
>100 % 3
100 % 3 = 1.
>100 / 0
Wrong expression.
>100 , 0
Bye.
#include <stdio.h>
int main()
{
printf("Please input an expression like 100 + 3 \n"
"Operators should be in {+, -, *, /, %}, others with end the program\n");
bool cont = true;
do{
int num1, num2;
char op;
printf(">");
scanf(" %d %c %d" , &num1 , &op , &num2);
switch (op)
{
case '+':
printf("%d + %d = %d.\n", num1, num2, num1+num2);
break;
case '-':
printf("%d - %d = %d.\n", num1, num2, num1-num2);
break;
case '*':
printf("%d * %d = %d.\n", num1, num2, num1*num2);
break;
case '/':
case '%':
if (num2 == 0) printf("Wrong expression.\n");
else if (op == '/')
{
printf("%d / %d = %d.\n", num1, num2, num1/num2);
}
else
{
printf("%d %% %d = %d.\n", num1, num2, num1%num2);
}
break;
default:
cont = 0 ;
}
}while (cont);
printf("Bye.");
return 0;
}
Please input an expression like 100 + 3
Operators should be in {+, -, *, /, %}, others with end the program
>100 + 3
100 + 3 = 103.
>100 - 3
100 - 3 = 97.
>100 * 3
100 * 3 = 300.
>100 / 3
100 / 3 = 33.
>100 % 3
100 % 3 = 1.
>100 / 0
Wrong expression.
>100 , 0
Bye.
#include <stdio.h>
int main()
{
printf("Please input an expression like 100 + 3 \n"
"Operators should be in {+, -, *, /, %}, others with end the program\n");
bool cont = true;
do{
int num1, num2;
char op;
printf(">");
scanf(" %d %c %d" , &num1 , &op , &num2);
switch (op)
{
case '+':
printf("%d + %d = %d.\n", num1, num2, num1+num2);
break;
case '-':
printf("%d - %d = %d.\n", num1, num2, num1-num2);
break;
case '*':
printf("%d * %d = %d.\n", num1, num2, num1*num2);
break;
case '/':
case '%':
if (num2 == 0) printf("Wrong expression.\n");
else if (op == '/')
{
printf("%d / %d = %d.\n", num1, num2, num1/num2);
}
else
{
printf("%d %% %d = %d.\n", num1, num2, num1%num2);
}
break;
default:
cont = 0 ;
}
}while (cont);
printf("Bye.");
return 0;
}