[英]Smallest of three numbers using macro MINIMUM3
I have to create a code that determines the smallest of three numeric values. 我必须创建一个确定三个数值中最小值的代码。 Macro MINIMUM3 should use macro MINIMUM2 (the difference btwn two numeric values) to determine the smallest number. 宏MINIMUM3应该使用宏MINIMUM2(btwn两个数字之差)来确定最小数字。 the input values come from the user input. 输入值来自用户输入。 I am not very familiar with using macros and my textbook doesn't really help in showing me an example of how they can work together to carry out their function. 我对使用宏不是很熟悉,我的教科书并没有真正帮助我展示一个示例,说明它们如何协同工作以实现其功能。 The code below is the work in progress that I have so far but I am running into errors on lines 3, 13, 16, and 20. 下面的代码是到目前为止的工作,但是我在第3、13、16和20行遇到了错误。
#define MINIMUM2 (a,b) (a < b ? a:b)
#define MINIMUM3 (a,b,c) (MINIMUM2(a,b) c? MINIMUM (a,b) :c)
int main (void) {
int x,y,z;
int temp;
printf("Please enter three numbers\n");
scanf("%d%d%d, &x&y&z);
temp = MIN(x,y,z);
printf("The smallest number entered is:\n");
printf("%d", &temp);
getchar ();
return0;
}
You have a couple of issues in your code: 您的代码中有几个问题:
MINIMUM3
uses MINIMUM
instead of MINIMUM2
MINIMUM3
使用MINIMUM
而不是MINIMUM2
MINIMUM3
is broken MINIMUM3
的逻辑已损坏 scanf
您缺少scanf
调用中的双引号和逗号 MIN
in place of MINUMUM3
您正在使用MIN
代替MINUMUM3
temp
to printf
您正在将temp
地址传递给printf
Here is how you can fix this: 解决方法如下:
#define MINIMUM2(a,b) (a < b ? a:b)
#define MINIMUM3(a,b,c) (MINIMUM2(MINIMUM2(a,b),c))
int main (void) {
int x,y,z;
int temp;
printf("Please enter three numbers\n");
scanf("%d%d%d", &x, &y, &z);
temp = MINIMUM3(x, y, z);
printf("The smallest number entered is:\n");
printf("%d", temp);
getchar ();
return0;
}
You can improve your macros by enclosing each parameter in parentheses: 您可以通过将每个参数括在括号中来改善宏:
#define MINIMUM2 (a,b) ((a) < (b) ? (a) : (b))
Your definition of the MINIMUM3
macro will lead to a syntax error. 您对MINIMUM3
宏的定义将导致语法错误。 You should try something like 你应该尝试像
#define MINIMUM3(a, b, c) MINIMUM2(MINIMUM2(a, b), c)
or 要么
#define MINIMUM3(a, b, c) MINIMUM2(a, MINIMUM2(b, c))
Also make sure to call MINIMUM3
in main
, not MIN
. 还要确保在main
而不是MIN
调用MINIMUM3
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.