6-2 使用函数判断完全平方数 (10分)
重点:if ( (int)sqrt(n)== sqrt(n) )
int IsSquare( int n )
{
int x;
if ((int)sqrt(n) == sqrt(n))
x=1;
else
x=0;
return x;
}
错误点:
int IsSquare( int n );这里没有分号(这里的n表示输入的函数n)
{
int x; 这里表示新定义的函数
if ((int)sqrt(n) == sqrt(n))----------------判断输入函数是否为完全平方数,完全平方数的表达方式为:sqrt(n). 判断是否定于用符号==
x=1;
else
x=0;
return x(0); 返回初始值x,错误点写成0
}
纠错点:第一行分号输入中文;
a.c:20:22: error: stray ‘\357’ in program int IsSquare( int n );
^ a.c:20:23: error: stray ‘\274’ in program int IsSquare( int n );
^ a.c:20:24: error: stray ‘\233’ in program int IsSquare( int n );
^ a.c: In function ‘IsSquare’: a.c:26:5: warning: this ‘else’ clause does not guard... [-Wmisleading-indentation] else ^~~~
a.c:28:9: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘else’ return x; ^~~~~~ a.c: In function ‘main’:
a.c:10:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &n); ^~~~~~~~~~~~~~~
第一行输入英文分号:
a.c:21:1: error: expected identifier or ‘(’ before ‘{’ token { ^
a.c: In function ‘main’:
a.c:10:5: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result] scanf("%d", &n); ^~~~~~~~~~~~~