当前位置: 代码迷 >> C语言 >> 编程问题4
  详细解决方案

编程问题4

热度:157   发布时间:2005-02-06 22:01:00.0
编程问题4
谭爷爷的《C程序设计》中的P12页第6题
#include<stdio.h>
main()
{
int a,b,c,d;
printf("please key in 3 number for checking:\n");
scanf("%d,%d,%d",a,b,c);
d=max(a,b,c);
printf("the max one is%d",d);
getch();
}
int max(int x, int y, int z)
{int w;
  if (x>y)
    if (z>x) w=z;
    else w=x;
  else
    if (z>y) w=y;
    else w=z;
    return(w);
}
我想了一天了,也没有正确答案。郁闷,不好意思,很弱智的问题,嬉笑之余还请不吝赐教。谢谢。
搜索更多相关的解决方案: int  max  else  checking  程序设计  

----------------解决方案--------------------------------------------------------
不知道你想干什么?
----------------解决方案--------------------------------------------------------
谭爷爷的书上的题啊,输入三个数比较大小。请各位指点以下吧,刚学编程就受到打击,心里承受力太差了!
----------------解决方案--------------------------------------------------------

你的程序是错的

#include<stdio.h> main() { int a,b,c,d; printf("please key in 3 number for checking:\n"); scanf("%d,%d,%d",a,b,c); ---> 应该是 scanf("%d,%d,%d",&a,&b,&c); d=max(a,b,c); printf("the max one is%d",d); getch(); } int max(int x, int y, int z) {int w; if (x>y) if (z>x) w=z; else w=x; else if (z>y) w=y; ---> 应该是 if (z>y) w=z; else w=z; ---> 应该是 else w=y; return(w); }


----------------解决方案--------------------------------------------------------
谢谢您!
----------------解决方案--------------------------------------------------------
#include&lt;stdio.h&gt;
main()
{
int a,b,c,d;
printf("please key in 3 number for checking:\n");//随便输入3个数
scanf("%d,%d,%d",&amp;a,&amp;b,&amp;c);//读入
d=max(a,b,c);//MAX为自定义的函书数,用来比较大小
printf("the max one is%d",d);
getch();
}
int max(int x, int y, int z)//下面是在交换,使得D最大
{int w;
  if (x&gt;y)
    if (z&gt;x) w=z;
    else w=x;
  else
    if (z&gt;y) w=y;
    else w=z;
    return(w);//返回W这个值,上面D=W
}
我也是初学者,多多指教
----------------解决方案--------------------------------------------------------
发现一个有趣的现象,在printf("please key in 3 number for checking:\n");中,在调试时,如果输入一个数按回车,则显示: the max one is 12830.
这是什么原因呢?
如果想达到上述目的(即输入一个数-回车-再输入-再回车...)应该怎么样改写代码呢?
----------------解决方案--------------------------------------------------------
那是因为你只输入了一个数,后面两个数还没有初始化是一个随机数。
所以才会出现:the max one is 12830.

把scanf("%d,%d,%d",&amp;a,&amp;b,&amp;c); 改成scanf("%d%d%d",&amp;a,&amp;b,&amp;c);就可以了
----------------解决方案--------------------------------------------------------
书上说,引号中的内容按原格式输出,书上错了
----------------解决方案--------------------------------------------------------
谭浩强的书错漏百出。。。建议看The C Programming Language(Second Edition)C程序设计语言第二版
----------------解决方案--------------------------------------------------------
  相关解决方案