编写一个C程序,输入a,b,c,3个值,输出其中最大者。

我是个刚开始学习这门语言,请问高手我提的那个问题,这样子编写正确不?
main()
{
int a,b,c,d;
scanf("%d,%d,%d",&a,&b,&c);
d=max(a,b,c);
printf("max=%d",d);
getch();
}
int max(int x,int y, int s)
{int z;
if(x>y)z=x;
else z=y;
if(y>s)z=y;
else z=s;
return(z);
}
有哪个C语言的高手可以指点我下(当然要在你有时间的情况下),免的小弟多走弯路,拜托哈!!!```

#include "stdio.h"
int max(int x,int y,int s)//声明,如果被调函数在主函数后
main()
{
int a,b,c,d;//定义4个整形变量
scanf("%d,%d,%d",&a,&b,&c);//输入3个数
d=max(a,b,c);//调用MAX函数,其中3个参数a,b,c,把结果存入变量d中
printf("max=%d",d);//输出d
getch();
}
int max(int x,int y, int s) //自定义函数,其中3个形式参数x,y,s
{int z; //定义实际参数z
if(x>y)z=x;// 如果,x大于y,把x放入z中
else z=y;// 否则把y放入z中
if(y>s)z=y;// 如果y大于s,那么把y放入z
else z=s;// 否着把s放入z
return(z);//每次比较完返回z
}
你的 程序是正确的,我的解释就是这些了!!!
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-10-03
你的程序是对的,只是max函数位置不正确,应先声明,我给你改了改
#include<stdio.h>
int max(int x,int y, int s)
{int z;
if(x>y)z=x;
else z=y;
if(y>s)z=y;
else z=s;
return(z);
}
main()
{
int a,b,c,d;
scanf("%d,%d,%d",&a,&b,&c);
d=max(a,b,c);
printf("max=%d",d);
getch();
}
第2个回答  2009-10-03
源程序中有错误,在以下代码段中:
int max(int x,int y, int s)
{int z;
if(x>y)z=x;
else z=y;
if(y>s)z=y; //这里有错误
else z=s;
return(z);
}
应修改为:
int max(int x,int y, int s)
{int z;
if(x>y)z=x;
else z=y;
if(z<s) //将x与y中的最大值即z与s比较,而不是将y与s比较
z=s;
return(z);
}
第3个回答  2009-10-03
mingcc1988已经更改过了!
我再补充一下,如果你想把调用函数放在main函数下,那么main前面一定要声明
#include<stdio.h>
int max(int x,int y, int s);
main()
{
int a,b,c,d;
scanf("%d,%d,%d",&a,&b,&c);
d=max(a,b,c);
printf("max=%d",d);
getch();
}
int max(int x,int y, int s)
{int z;
if(x>y)z=x;
else z=y;
if(y>s)z=y;
else z=s;
return(z);
}
第4个回答  2009-10-03
max(int x, int y,int z)
{
int temp1,temp2;
/*
First get the larger value between x and y and store this in temp1
*/
if(x>y)temp1=x;
else temp1=y;

/*
Second get the larger number between temp1 and z, return larger value.
*/
if(temp1>z) temp2=temp1;
else temp2=y;

return temp2;

}

也可以用宏定义
#define max(x,y) ((x)>(y))?(x):(y))

max(x,y,z)
{ int temp1,temp2;
temp1=max(x,y);
return(temp1,z);
}

关键的思路是,先确定其中两个的最大值,然后在确定这个最大值和第三个值的较大值.

原程序除了没有在main()中申明,max(x,y,z)也只能得到y和z之间的最大值,故而不正确.