C 使用条件运算符查找两个数中的最小数
请您也详细解释一下。C 使用条件运算符查找两个数中的最小数,c,conditional-operator,C,Conditional Operator,请您也详细解释一下。 它是如何工作的?返回一个条件运算符:的工作方式与if else类似 return a<b ? a : b 因此: 与 int A; int B; // some code that sets the values of A and B if A>B return B; else return A; 条件运算符的说明: `<Perform operation that gives a boolean result>`
它是如何工作的?
返回一个条件运算符:
的工作方式与if else
类似
return a<b ? a : b
因此:
与
int A;
int B;
// some code that sets the values of A and B
if A>B
return B;
else
return A;
条件运算符的说明:
`<Perform operation that gives a boolean result>` ? <return this answer if true> : <return this answer if false>
``?:
所以你可以:
int smallestValue;
int inputA;
int inputB;
//some code that sets the value of inputA and inputB - perhaps from console input
smallestValue = (inputA < inputB) ? inputA : inputB;
int最小值;
int输入;
int输入b;
//一些设置inputA和inputB值的代码-可能来自控制台输入
smallestValue=(输入<输入B)?输入:输入B;
这是一个宏,它做同样的工作
#define min(a, b) a<b ? a : b
#定义min(a,b)a+1-也比我强。今天更新速度慢。我可以用数字代替吗?比如如何在两个数字中找出最小的数字,我不明白a
和b
可以是您想要的任何内容。它们可以是数字。如果你的意思是如何使用条件运算符来找到像6
和9
这样的最小文本,那么这是毫无意义的。你可以通过检查来做。另外,如果你有特定的问题要问,不要在评论中添加,而是编辑问题。我可以用数字代替吗?比如如何在两个数字中找到最小的数字。这可能是一个非常糟糕的宏。对a和b的多重评估。@DavidHeffernan:我不知道你为什么这么说,但你可以看看这个链接几乎涵盖了所有问题的油井
int smallestValue;
int inputA;
int inputB;
//some code that sets the value of inputA and inputB - perhaps from console input
smallestValue = (inputA < inputB) ? inputA : inputB;
#define min(a, b) a<b ? a : b