C程序不使用逻辑运算符比较整数? [英] C program to compare integers without using logical operators?

查看:95
本文介绍了C程序不使用逻辑运算符比较整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何写一个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...

这篇关于C程序不使用逻辑运算符比较整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆