比較三個(gè)整數(shù)變量是您可以很容易編寫的一個(gè)簡(jiǎn)單的程序之一。 在這個(gè)程序中,您可以使用scanf()函數(shù)從用戶處獲取輸入,也可以在程序本身中靜態(tài)定義。
我們期望它也是一個(gè)簡(jiǎn)單的程序,將一個(gè)值與其余兩個(gè)進(jìn)行比較,并檢查結(jié)果,并對(duì)所有變量應(yīng)用相同的過(guò)程。 對(duì)于此程序,所有值應(yīng)該是唯一的值。
我們可以為下面給出的程序繪制一個(gè)流程圖 -

該圖中顯示了三個(gè)if-else-if和另一個(gè)對(duì)比語(yǔ)句。
下面我們來(lái)看看這個(gè)程序的具體實(shí)現(xiàn) -
#include <stdio.h>
int main() {
int a, b, c;
a = 10;
b = 20;
c = 30;
if ( a > b && a > c )
printf("%d is the largest.", a);
else if ( b > a && b > c )
printf("%d is the largest.", b);
else if ( c > a && c > b )
printf("%d is the largest.", c);
else
printf("Values are not unique");
return 0;
}
執(zhí)行上面示例代碼,得到以下結(jié)果 -
30 is the largest.