- C 编程有用的资源
- 通过示例学习 C - 快速指南
- 通过示例学习 C - 资源
- 通过示例学习 C - 讨论
比较 C 中的三个整数
比较三个整数变量是你轻松编写的最简单的程序之一。在此程序中,你可以使用 scanf() 函数从用户那里获取输入,或者直接在程序本身中静态定义。
我们希望它对你来说也是一项简单的程序。我们将一个值与另外两个值进行比较,并检查结果,并且对所有变量都应用相同的流程。对于此程序,所有值都应该是不同的(唯一的)。
算法
让我们首先看看比较三个整数的分步过程 -
START Step 1 → Take two integer variables, say A, B& C Step 2 → Assign values to variables Step 3 → If A is greater than B & C, Display A is largest value Step 4 → If B is greater than A & C, Display B is largest value Step 5 → If C is greater than A & B, Display A is largest value Step 6 → Otherwise, Display A, B & C are not unique values STOP
流程图
我们可以为此程序绘制出如下所示的流程图 -
此图表显示了三个 if-else-if 和一个 else 比较语句。
伪代码
现在,让我们看看此算法的伪代码 -
procedure compare(A, B, C)
IF A is greater than B AND A is greater than C
DISPLAY "A is the largest."
ELSE IF B is greater than A AND A is greater than C
DISPLAY "B is the largest."
ELSE IF C is greater than A AND A is greater than B
DISPLAY "C is the largest."
ELSE
DISPLAY "Values not unique."
END IF
end procedure
实现
现在,我们将看到程序的实际实现 -
#include <stdio.h>
int main() {
int a, b, c;
a = 11;
b = 22;
c = 33;
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;
}
输出
此程序的输出应该是 -
33 is the largest.
simple_programs_in_c.htm
广告