谁帮忙讲解下这个代码啊
c语言吧
全部回复
仅看楼主
level 5
做题目的时候,发现了这题,各种困惑啊
main()
{
int i;
int fact();
for(i=0;i<5;i++)
 printf("\40:%d!=%d\n",i,fact(i));
}
int fact(j)
int j;
{
int sum;
if(j==0)
 sum=1;
else
 sum=j*fact(j-1);
return sum;
}
---------
我对C不熟啊,听过C的方法(或者叫函数)引用的时候要先定义啊,就是main()方法里的int fact()了吧
后面的int fact(j){}就是具体的方法内容了吧?可我不理解在(){}中间挤了个int j;是啥意思啊?就算要定义一个J来用,但是函数的(){}之间可以有别的语句的么?还是说其实后面的int fact(j)也只是个定义?
2012年03月11日 02点03分 1
level 5
而且这个代码我运行的时候还报错,但我又不知道哪里错了,好吧,我根本理解不了这个代码TAT,求高人指点
2012年03月11日 02点03分 2
level 6
递归
2012年03月11日 02点03分 3
level 12
那个int j;是声明参数j的类型,一般是这样写int fact(int j)的…
2012年03月11日 02点03分 5
level 5
这样可以直接写在(){}之间么?声明一个参数J的类型,我可以理解啦,我只是不理解他所处于的位置
TAT然后,代码报错,不知道是为什么,报错的提示我也完全看不懂(好吧,英语太烂伤不起)
error C2660: 'fact' : function does not take 1 parameters
error C2448: '<Unknown>' : function-style initializer appears to be a function definition
2012年03月11日 02点03分 6
level 9
貌似大括号都用错拉
2012年03月11日 02点03分 7
level 7
#include<stdio.h>
void main()
{
int i;
int fact();
for(i=0;i<5;i++)
printf("\40:%d!=%d\n",i,fact(i));
}
int fact(int j)
{
int sum;
if(j==0)
sum=1;
else
sum=j*fact(j-1);
return sum;
}
2012年03月11日 04点03分 8
level 10
这才是正解
2012年03月11日 04点03分 9
level 5
还是报错啊,而且没发现代码有哪里不同啊
2012年03月11日 04点03分 10
level 10
直接写在(){}之间 就成为外部变量了
2012年03月11日 04点03分 11
level 5
...哪里有不同么?而且试了下,仍然报错啊
2012年03月11日 04点03分 12
level 10
自己换行
#include "stdio.h"
main(){
int i;long int fact();for(i=0;i<5;i++) printf("\40:%d!=%d\n",i,fact(i));getch();}long int fact(int j){long int sum;if(j==0)sum=1; elsesum=j*fact(j-1);return(sum);
}
2012年03月11日 08点03分 13
level 5
还是报错
2012年03月11日 08点03分 14
level 10
我运行没错啊
2012年03月11日 08点03分 15
level 10
#include "stdio.h"
main(){
int i;
long int fact();
for(i=0;i<5;i++)
printf("\40:%d!=%d\n",i,fact(i));
}
long int fact(int j)
{long int sum;
if(j==0)sum=1;
elsesum=j*fact(j-1);
return(sum);
}
2012年03月11日 08点03分 16
level 5
我重新照着你打了一遍,报错
2012年03月11日 08点03分 17
level 10
你的是TC 编译器
删除 getch();这行 看看
这行是给WC的
2012年03月11日 08点03分 18
level 10
提示什么错误
2012年03月11日 09点03分 19
level 5
是VC++ 6.0
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(11) : error C2660: 'fact' : function does not take 1 parameters
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(17) : error C2065: 'i' : undeclared identifier
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(19) : warning C4244: 'return' : conversion from 'double' to 'long', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(46) : warning C4244: '=' : conversion from 'const double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(46) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(47) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(48) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(49) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(54) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(56) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(58) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(60) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(62) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(64) : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(75) : warning C4244: '=' : conversion from 'double' to 'long', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(76) : warning C4244: '=' : conversion from 'double' to 'long', possible loss of data
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(162) : warning C4305: 'initializing' : truncation from 'const int' to 'char'
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(162) : warning C4309: 'initializing' : truncation of constant value
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(162) : warning C4305: 'initializing' : truncation from 'const int' to 'char'
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(162) : warning C4309: 'initializing' : truncation of constant value
G:\编程学习\C练习\经典C程序100例作业\text_one.cpp(454) : warning C4244: '=' : convers
2012年03月11日 09点03分 20
level 10
我完全不懂 我没折了
2012年03月11日 09点03分 21
1 2 尾页