输入三个数字,求其中最大的数字
#include
int main()
{
int a = 0, b = 0,c = 0;
int max = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
max = a > b ? a : b;
max = max > c ? max : c;
printf("max %d \n", max);
return 0;
}
#include
int main()
{
int a = 0, b = 0,c = 0;
int max = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
if (a > b && a > c)
{
max = a;
}
else if (b > a && b > c)
{
max = b;
}
else
{
max = c;
}
printf("max %d \n", max);
return 0;
}
#include
int Max_Int(int a, int b)
{
int c = a > b ? a : b;
return c;
}
int main()
{
int a = 0, b = 0, c = 0;//初始化
int max = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
max = Max_Int(a, Max_Int(b, c));//传值调用,实参赋值给形参
printf("max %d \n", max);
return 0;
}
//代码复用程度最高,即为最佳执行方法
输入三个数字,求输出中间值。
算法实现:冒泡排序,输入三个值,先将三个值进行排序,在输出中间值,即为所求数字。
代码冗余,未注意代码书写规范
#include
int main()
{
int a = 0, b = 0, c = 0;
int mid = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
if (b > c)
{
int tmp = b;
b = c;
c = tmp;
}
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
mid = b;
printf("mid=%d \n", mid);
return 0;
}
主函数与其他函数分开,代码算法实现清晰可见,代码书写规范清晰良好
#include
int Mid_Int(int a, int b, int c)
{
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
if (b > c)
{
int tmp = b;
b = c;
c = tmp;
}
if (a > b)
{
int tmp = a;
a = b;
b = tmp;
}
return b;
}
int main()
{
int a = 0, b = 0, c = 0;
int mid = 0;
printf("input data:");
scanf_s("%d %d %d", &a, &b, &c);
mid = Mid_Int(a, b, c);
printf("mid=%d \n", mid);
return 0;
}
形参定义为指针类型,传递地址(交换函数)
#include
void Swap_Int(int a, int b)
{
int tmp = a;
a = b;
b = tmp;
}
int main()
{
int x = 10, y = 20;
Swap_Int(x, y);
printf("x=%d,y=%d \n", x, y);
return 0;
}
(1)需求:提出自己对于代码的需求,即提出问题
(2)分析:对提出的问题需求进行分析
(3)设计:即对代码的算法进行设计
(4)实施:程序代码的实施,即编码的实现
(5)测试:对自己编写代码的代码模块进行测试