最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C程序不使用逻辑运算符比较整数?

SEO心得admin47浏览0评论
本文介绍了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...

发布评论

评论列表(0)

  1. 暂无评论