2014-01-24 15 views

回答

1

你可以做

#define new_max(x,y) ((x) >= (y)) ? (x) : (y) 
#define new_min(x,y) ((x) <= (y)) ? (x) : (y) 

瓦爾特

+0

感謝您的答覆valter。 – RottieRango

+0

+1爲樂趣商; – Govardhan

0

以下是使用IF語句確定3個數字中最大的一個示例。像艾哈邁德說的。

/* C program to find largest number using if statement only */ 

#include <stdio.h> 
int main(){ 
     float a, b, c; 
     printf("Enter three numbers: "); 
     scanf("%f %f %f", &a, &b, &c); 
     if(a>=b && a>=c) 
     printf("Largest number = %.2f", a); 
     if(b>=a && b>=c) 
     printf("Largest number = %.2f", b); 
     if(c>=a && c>=b) 
     printf("Largest number = %.2f", c); 
     return 0; 
} 
1

可以使用比較符如><>=<=輕鬆編寫自己的函數,並==.

下面是一個例子from this site

#include <stdio.h> 

int main() 
{ 
    int a, b; 
    scanf("%d %d", &a, &b);//get two int values from user 

    if (a < b)//is a less than b? 
    printf("%d is less than %d\n", a, b); 

    if (a == b)//is a equal to b? 
    printf("%d is equal to %d\n", a, b); 

    if (a > b)//is a greater than b? 
    printf("%d is greater than %d\n", a, b); 

    return 0; 
} 

您可以使用此知識做一個簡單的功能,遵循這個僞代碼:

//get a and b int variable 
//is a greater than or equal to b? 
    //return a 
//else 
    //return b 

一些其他有用的資源:

http://c.comsci.us/tutorial/icomp.html

http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

+1

Steve和Mohammad,感謝您的有用答案 – RottieRango

相關問題