我写了一个程序,它接受一个整数,并根据该数字采取进一步的行动,如计算Factorial,Fibonacci和退出程序。但是代码有问题。当它计算Fibonacci或Factorial时,它会在输出的最后一个显示随机数,这很奇怪,我无法弄明白。代码和屏幕截图位于
之下#include <stdio.h>
#include <conio.h>
int Menu(int num) {
printf("Press 1 to calculate Factorial\n");
printf("Press 2 to calculate Fibannaci series\n");
printf("Press 0 to Quit\n");
scanf("%d", &num);
LOOP:
if (num == 1) {
int n;
printf("Enter the number whose Factorial you wanna calculate: " );
scanf("%d", &n);
printf("%d", Fact(n));
} else
if (num == 2) {
int n;
printf("Enter the term whose Fibannaci series you wanna calculate: ");
scanf("%d", &n);
printf("%d", Fib(n));
} else
if (num == 0) {
printf("Program is Quitting!");
exit(0);
} else {
while (num < 0 ^ num > 2) {
printf("Enter Correct number: ");
scanf("%d", &num);
}
goto LOOP;
}
}
int Fact(int n) {
int Factorial;
if (n == 1 || n == 0) {
return 1;
} else {
Factorial = n * Fact(n - 1);
}
}
int Fib(int n) {
int Fibonacci;
if (n <= 1) {
return n;
} else {
Fibonacci = Fib(n - 1) + Fib(n - 2);
}
}
int main() {
printf("%d", Menu(1));
}
答案 0 :(得分:3)
&#34;额外&#34;输出是因为您正在打印Menu()
的返回值(但它不会返回一个)。这是undefined behaviour。
printf()
为您选择的5输入打印120(printf("%d", Fact(n));
),其返回值(打印的字符数)为3,由{{1}打印在main()中。但不要依赖于此,因为这只是对您案件中发生的事情的解释,绝不是保证。如前所述,您的代码具有未定义的行为。
如果您不需要从printf()
返回值,则可以将其设为Menu()
函数。
我最初发现的问题还有很多。编译代码,GCC报告:
void
1)在编译器声明之前,您正在调用test.c: In function ‘Menu’:
test.c:12:18: warning: implicit declaration of function ‘Fact’ [-Wimplicit-function-declaration]
printf("%d", Fact(n));
^
test.c:18:18: warning: implicit declaration of function ‘Fib’ [-Wimplicit-function-declaration]
printf("%d", Fib(n));
^
test.c:22:5: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
exit(0);
^
test.c:22:5: warning: incompatible implicit declaration of built-in function ‘exit’
test.c:22:5: note: include ‘<stdlib.h>’ or provide a declaration of ‘exit’
test.c:26:14: warning: suggest parentheses around comparison in operand of ‘^’ [-Wparentheses]
while(num<0 ^ num>2){
^
test.c: In function ‘Fact’:
test.c:36:9: warning: variable ‘Factorial’ set but not used [-Wunused-but-set-variable]
int Factorial;
^
test.c: In function ‘Fib’:
test.c:46:9: warning: variable ‘Fibinnaci’ set but not used [-Wunused-but-set-variable]
int Fibinnaci;
^
test.c: In function ‘Menu’:
test.c:33:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
test.c: In function ‘Fact’:
test.c:43:2: warning: control reaches end of non-void function [-Wreturn-type]
}
^
test.c: In function ‘Fib’:
test.c:53:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
和Fact()
。您可以通过在顶部声明来修复它:
Fib()
2)int Fact(int);
int Fib(int);
的原型未包含stdlib.h>
。
3)不从exit()
和1
函数返回0
或Fact()
以外的输入的任何内容。您可以通过返回Fib()
部分的值来解决此问题:
else
和
else {
return n* Fact(n-1);
}
答案 1 :(得分:1)
您可以将阶乘函数更改为:
<!doctype html>
<head>
<meta charset="utf-8">
<base href="https://polygit.org/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link href="polymer/polymer.html" rel="import">
<link href="paper-dropdown-menu/paper-dropdown-menu.html" rel="import">
<link href="paper-listbox/paper-listbox.html" rel="import">
<link href="paper-item/paper-item.html" rel="import">
</head>
<body>
<dom-module id="x-element">
<template>
<style></style>
<p>
<button on-tap="show">Click to show item</button>
</p>
<paper-dropdown-menu label="Numbers">
<paper-listbox class="dropdown-content"
selected="{{item.number}}"
attr-for-selected="data-item">
<paper-item data-item="One">One</paper-item>
<paper-item data-item="Two">Two</paper-item>
<paper-item data-item="Three">Three</paper-item>
<paper-item data-item="Four">Four</paper-item>
</paper-listbox>
</paper-dropdown-menu>
</template>
</dom-module>
<x-element></x-element>
</body>
答案 2 :(得分:1)
与其他语言不同,返回值由return
语句在函数代码中生成。您Fib
和Fact
函数不正确,因为如果使用适当的标志(例如gcc -Wall
)调用,编译器会强调警告。
以下是更正后的版本:
int Fact(int n) {
if (n == 1 || n == 0) {
return 1;
} else {
return n * Fact(n - 1);
}
}
int Fib(int n) {
if (n <= 1) {
return n;
} else {
return Fib(n - 1) + Fib(n - 2);
}
}
由于函数没有为一般情况返回任何内容,printf
正在打印通常用返回值设置的寄存器中的任何值。这是一种未定义的行为:任何事情都会发生,试图解释观察到的行为是无用的。
此外,您应该在数字后输出换行符:
printf("%d\n", Fact(n));
另请注意,while (num < 0 ^ num > 2)
中的条件可能与您的意思不同:逻辑OR的运算符写为||
。 ^
是按位异或运算符。
while (num < 0 || num > 2)