下列给定程序中,函数fun()的功能是:根据输入的3个边长(整型值),判断能否构成三角形:若能构成等边三角形,则返回3,若是等腰三角形,则返回2,若能构成三角形则返回1,若不能,则返回0。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include <math. h>
int fun(int a, int b, int c)
if (a+b>c&&b+c>a&&a+c>b)
if (a==b&&b==c)
/*************found**************/
return 1;
else if(a==b|| b==c||a==c)
return 2;
/*************found**************/
else return 3;
else return 0;
main ( )
int a,b, c, shape;
printf("\nInput a,b,c: ");
scanf ("%d%d%d", &a, &b, &c);
printf ("\na=%d, b=%d, c=%d\n",a,b,c);
shape=fun (a,b, c);
printf ("\n\nThe shape : %d\n", shape);
参考答案:错误:return 1; 正确:return 3;
(2)错误:return 3; 正确:return 1;
解析: 该题只要理解了if语句中的条件,并能读懂题目定义就可轻松解决,函数返回值搭配错误,是一个简单的数学逻辑问题。