谁能告诉我如何写一个C程序,而无需使用逻辑运算符来比较数字(包括负数)?
Can anyone tell me how to write a C program to compare numbers(including negative numbers) without using logical operators?
推荐答案下面是两个数字,而无需使用关系运算符进行比较的节目(它也适用于负数)。检查出来。
Here is a program that compares the two numbers without using relation operators (it also works for negative numbers). check it out.
/ ------ C程序------- /
void main() { int a,b,c,temp; printf("enter a and b:"); scanf("%d%d",&a,&b); c=a-b; temp=c+abs(c); // to check if the difference is negative or not if(temp==0) printf("a is smaller than b"); else printf("a is bigger than b"); getch(); }编辑:马迪的修订code(从下他的意见)如下
Maddy's revised code (from his comment below) follows.
void main() { int a,b,c,d,temp; printf("enter a and b:"); scanf("%d%d",&a,&b); c=a-b; d=abs(c); temp=c+d; if (c==0) printf("a is equal to b"); else if(temp==0) printf("a is smaller than b"); else printf("a is bigger than b"); printf("%d",d); getch(); }我想现在的code非常适用于所有输入...
i think now the code works well for all inputs...