第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之间的最大值,故而不正确.