[C]分別用函數和帶參的宏,求3個數中的最大值


 

 

//帶參宏求3個數的最大值
#include <stdio.h>
#define MAX(a, b, c) (a>b?a:b)>c?(a>b?a:b):c
int main()
{
    int a, b, c;
    puts("input three numbers, use space to seperate each other:");
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", MAX(a,b,c));
    return 0;
}

 

 

//用函數求三個數的最大值
#include <stdio.h>
int MAX(int a, int b, int c); //函數聲明
int main()
{
    int a, b, c;
    puts("input three numbers, use space to seperate each other:");
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", MAX(a,b,c));
    return 0;
}

int MAX(int a, int b, int c)
{
    int max;
    if (a > b)
        max = a;
    else
        max = b;
    if (c > max)
        max = c;
    return max;
}

 

 

//以上兩個方法合起來,投機取巧地采用函數求三個數的最大值
#include <stdio.h>
int MAX(int a, int b, int c); //函數聲明
int main()
{
    int a, b, c;
    puts("input three numbers, use space to seperate each other:");
    scanf("%d%d%d", &a, &b, &c);
    printf("%d\n", MAX(a,b,c));
    return 0;
}

int MAX(int a, int b, int c)
{
    return (a>b?a:b)>c?(a>b?a:b):c;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM