我正在学习函数以及如何调用它们并在课堂上使用它们。我不太明白我在哪里出错了。我知道int main
部分存在一些错误。我问过我的老师,他不愿意给我一个例子来解决我的问题或帮助我。我认为我的主要问题是factorial_result = factorial();
#include <stdio.h>
void mystamp(void)
{
printf("My name is John Appleseed\n");
printf("My lab time is 12:30 on Sunday\n");
return;
}
int getnum(void)
{
int local_var;
printf("Please enter an integer: ");
scanf("%d%*c", local_var);
return(local_var);
}
int factorial(void)
{
int x,f=1,local_var;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial();
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
答案 0 :(得分:1)
您的阶乘应根据输入(即您的int result
}计算。
因此,您的方法factorial()
应如下所示:
int factorial( int number )
{
int factorial_value = 1;
while( number > 0 )
{
factorial_value *= number;
number--;
}
return factorial_value;
}
然后,将返回正确的阶乘并相应打印!关于您使用的变量的范围,请参阅您的问题下的评论。
答案 1 :(得分:1)
将local_var声明为全局变量并执行:
local_var = getnum();
或强>
将main()
更改为:
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial(result);
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
factorial()
来:
int factorial(int n)
{
int x,f=1,local_var=n;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
答案 2 :(得分:0)
Suppose you have videos name list like this:
<ul id="videoList">
<li vidUserId="<?=$f['id']?>-<?=$rows['id']?>" class="currentVideo">Video1</li>
<li vidUserId="<?=$f['id']?>-<?=$rows['id']?>">Video2</li>
<li vidUserId="<?=$f['id']?>-<?=$rows['id']?>">Video3</li>
<li vidUserId="<?=$f['id']?>-<?=$rows['id']?>">Video4</li>
<li vidUserId="<?=$f['id']?>-<?=$rows['id']?>">Video5</li>
</ul>
vidUserId attr has video id and user id separated with the hyphen and adds a "currentVideo" class on currently running video, then on the end of this video get the next video id and user id and fire the theFunction() like the below
<script type="text/javascript">
var video = document.getElementsByTagName('video')[0];
video.onended = function(e) {
var currentVideo = $('#videoList li.currentVideo');
var nextVideo = $('#videoList li.currentVideo').next();
if(nextVideo.length){
nextVideo.addClass('currentVideo');
}else{
nextVideo = $('#videoList li').first();
nextVideo.addClass('currentVideo');
}
var videoUserId = nextVideo.attr('vidUserId');
var arrIds = videoUserId.split('-');
var videoId = arrIds[0];
var userId = arrIds[1];
currentVideo.removeClass('currentVideo');
theFunction(videoId,userId);
};
</script>
这是一个使用递归调用函数的简单因子程序!
答案 3 :(得分:-3)
包括
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
for (c = 1; c <= n; c++) fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}