简体   繁体   English

使用宏MINIMUM3的三个数中的最小值

[英]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
  • The logic of MINIMUM3 is broken MINIMUM3的逻辑已损坏
  • You need to remove spaces after macro names 您需要在宏名称后删除空格
  • You are missing a closing double quote and commas in call of scanf 您缺少scanf调用中的双引号和逗号
  • You are using MIN in place of MINUMUM3 您正在使用MIN代替MINUMUM3
  • You are passing an address of 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;
}

Demo. 演示

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM