最大和最小算法? (目标C) [英] Algorithm for max and min? (Objective-C)

查看:85
本文介绍了最大和最小算法? (目标C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在阅读以学习Objective-C的书的一部分.

This is a part of a book I'm reading to learn Objective-C.

下面定义了一个名为MAX的宏,该宏最多提供两个 值:#define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )

The following defines a macro called MAX that gives the maximum of two values: #define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )

然后在书中进行一些练习,要求读者定义一个宏(MIN)以查找两个值中的最小值,另一个要求定义一个名为MAX3的宏,该宏最多提供3个值价值观.我认为这两个定义看起来与MAX类似,但我不理解MAX公式如何找到最大值.我的意思是如果我这样做

And then there are some exercises in the book that asks the reader to define a macro (MIN) to find the minimum of two values and another that asks to define a macro called MAX3 that gives the maximum of 3 values. I think these two definitions will look similar to MAX, but I don't understand how the MAXformula finds the maximum value. I mean if I just did this

int limits = MAX (4,8)

它将仅将limits的值分配为8.这与查找变量的最大值有什么关系?

It'll just assign limits the value of 8. What does that have to do with finding a variable's maximum value?

推荐答案

我认为您在混淆值和变量.您列出的宏示例扩展为两个值之间的比较,并返回两个值中较大的一个(即,较大的ab).没错,int limits = MAX(4,8)只是将8分配给limits,与查找可以存储在limits中的最大值无关.

I think you are confusing value and variable. The macro example you listed expands to a comparison between two values and returns the greater of the two values (i.e. which is greater, a or b). So you are right, int limits = MAX(4,8) just assigns 8 to limits and has nothing to do with finding the maximum value you can store in limits.

标头limits.h定义了许多值,例如INT_MAX,它们将告诉您有关系统上变量类型的最小/最大值的信息.

The header limits.h defines many values like INT_MAX that will tell you information about the min/max values of variable types on your system.

这篇关于最大和最小算法? (目标C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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