C 将-1返回给用户定义的函数将导致程序终止,退出代码为0
在这个阶乘程序中,当输入任何非数字或负数时,程序应该请求租借值,但在输出中程序被终止 是因为我在C 将-1返回给用户定义的函数将导致程序终止,退出代码为0,c,return,factorial,C,Return,Factorial,在这个阶乘程序中,当输入任何非数字或负数时,程序应该请求租借值,但在输出中程序被终止 是因为我在display()函数中返回-1?如果是这样,那么如果函数要返回值,是否必须向函数返回变量(或其他函数)值 #include <stdio.h> int display(); void fact_fun(int num_fact); int main() { int num = 0; char next; next = display(); if (
display()
函数中返回-1
?如果是这样,那么如果函数要返回值,是否必须向函数返回变量(或其他函数)值
#include <stdio.h>
int display();
void fact_fun(int num_fact);
int main() {
int num = 0;
char next;
next = display();
if (next == -1) { //WHEN ANY CHARACTER OR NEGATIVE NUMBER IS ENTERED IT WILL ASK TO RENTER
printf("\nOnly positive number is allowed");
display();
}
while (next >= 0) { //WHEN NEGATIVE NUMBER IS ENTERED IT WILL END THE LOOP
num = next;
fact_fun(num);
next = display();
}
return 0;
}
int display() {
char inp[10] = { 0 };
int input;
int index = 0;
printf("\nEnter number to find factorial or press ENTER KEY to exit: ");
while (((input = getchar()) != EOF) & (index < 10)) {
if ((input >= '0') && (input <= '9')) {
inp[index++] = input;
} else
if (input == '\n')
break;
else
return -1;
}
input = atoi(inp);
return input;
}
void fact_fun(int num_fact) {
int fact = 1;
if (num_fact == 0) {
printf("\nFactorial of %d is 1", num_fact);
return;
} else {
for (int i = 1; i <= num_fact; i++) {
fact = fact * i;
}
printf("\nFactorial of %d is %d", num_fact, fact);
}
}
当输入\n
时,程序将终止。根据我的理解,输入键和\n
应该是一样的。如果没有,那么区别是什么?我应该如何检查输入键值
在这个阶乘程序中,当输入任何非数字或负数时,程序应要求租借值
main()
函数中的while
循环仅循环请求新数字,直到input()
返回负数为止。您甚至记录了它:
while(next>=0) //WHEN NEGATIVE NUMBER IS ENTERED IT WILL END THE LOOP
...
在input()
中执行return-1
操作时,函数返回-1,这将next
设置为-1并结束循环。此后不久,该程序就存在了
使用return
语句从用户定义的函数返回-1
本身并没有什么错误。这样做很正常,也很常见
在这个阶乘程序中,当输入任何非数字或负数时,程序应要求租借值
main()
函数中的while
循环仅循环请求新数字,直到input()
返回负数为止。您甚至记录了它:
while(next>=0) //WHEN NEGATIVE NUMBER IS ENTERED IT WILL END THE LOOP
...
在input()
中执行return-1
操作时,函数返回-1,这将next
设置为-1并结束循环。此后不久,该程序就存在了
使用
return
语句从用户定义的函数返回-1
本身并没有什么错误。这样做很正常,也很常见。默认情况下,用户输入是行缓冲的。就您的目的而言,从用户处一次读取一行输入,解析它以断言输入有效性,并仅为有效输入计算阶乘要简单得多
还请注意,您可以简化计算,因为0
的特殊情况与一般情况下的代码是冗余的。您还应该检查潜在的算术溢出,因为计算可能很容易超出类型int
的范围,并产生未定义的行为
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int display();
void fact_fun(int num_fact);
int main() {
int num;
while ((num = display()) >= 0) {
fact_fun(num);
}
return 0;
}
int display() {
char buf[256];
char *p;
long value;
for (;;) {
printf("Enter number to find factorial or press ENTER KEY to exit: ");
if (fgets(buf, sizeof buf, stdin) == NULL || *buf == '\n')
return -1;
errno = 0;
value = strtol(buf, &p, 0);
if (p == buf) {
printf("Invalid input: not a number\n");
} else {
if (value < 0) {
printf("Invalid input: negative values not allowed\n");
} else
if (errno != 0 || value > INT_MAX) {
printf("Invalid input: value too large for type int\n");
} else {
return (int)value;
}
}
}
void fact_fun(int num_fact) {
int fact = 1;
for (int i = 1; i <= num_fact; i++) {
if (fact > INT_MAX / i) {
printf("Invalid input: arithmetic overflow\n");
return;
}
fact = fact * i;
}
printf("Factorial of %d is %d\n", num_fact, fact);
}
#包括
#包括
#包括
int display();
无效事实(整数事实);
int main(){
int-num;
而((num=display())>=0){
事实乐趣(num);
}
返回0;
}
int显示(){
char-buf[256];
char*p;
长期价值;
对于(;;){
printf(“输入数字以查找阶乘,或按Enter键退出:”;
如果(fgets(buf,sizeof buf,stdin)=NULL | |*buf=='\n')
返回-1;
errno=0;
值=strtol(buf,&p,0);
如果(p==buf){
printf(“无效输入:不是数字”);
}否则{
如果(值<0){
printf(“无效输入:不允许负值\n”);
}否则
如果(错误号!=0 | |值>整数最大值){
printf(“无效输入:值对于int类型太大\n”);
}否则{
返回(int)值;
}
}
}
无效事实乐趣(整数事实){
int-fact=1;
对于(int i=1;i int_MAX/i){
printf(“无效输入:算术溢出\n”);
返回;
}
事实=事实*i;
}
printf(“%d的阶乘是%d\n”,num\u fact,fact);
}
默认情况下,用户输入是行缓冲的。就您的目的而言,从用户处一次读取一行输入,解析它以断言输入有效性,并仅为有效输入计算阶乘要简单得多
还请注意,您可以简化计算,因为0
的特殊情况与一般情况下的代码是冗余的。您还应该检查潜在的算术溢出,因为计算可能很容易超出类型int
的范围,并产生未定义的行为
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int display();
void fact_fun(int num_fact);
int main() {
int num;
while ((num = display()) >= 0) {
fact_fun(num);
}
return 0;
}
int display() {
char buf[256];
char *p;
long value;
for (;;) {
printf("Enter number to find factorial or press ENTER KEY to exit: ");
if (fgets(buf, sizeof buf, stdin) == NULL || *buf == '\n')
return -1;
errno = 0;
value = strtol(buf, &p, 0);
if (p == buf) {
printf("Invalid input: not a number\n");
} else {
if (value < 0) {
printf("Invalid input: negative values not allowed\n");
} else
if (errno != 0 || value > INT_MAX) {
printf("Invalid input: value too large for type int\n");
} else {
return (int)value;
}
}
}
void fact_fun(int num_fact) {
int fact = 1;
for (int i = 1; i <= num_fact; i++) {
if (fact > INT_MAX / i) {
printf("Invalid input: arithmetic overflow\n");
return;
}
fact = fact * i;
}
printf("Factorial of %d is %d\n", num_fact, fact);
}
#包括
#包括
#包括
int display();
无效事实(整数事实);
int main(){
int-num;
而((num=display())>=0){
事实乐趣(num);
}
返回0;
}
int显示(){
char-buf[256];
char*p;
长期价值;
对于(;;){
printf(“输入数字以查找阶乘,或按Enter键退出:”;
如果(fgets(buf,sizeof buf,stdin)=NULL | |*buf=='\n')
返回-1;
errno=0;
值=strtol(buf,&p,0);
如果(p==buf){
printf(“无效输入:不是数字”);
}否则{
如果(值<0){
printf(“无效输入:不允许负值\n”);
}否则
如果(错误号!=0 | |值>整数最大值){
printf(“无效输入:值对于int类型太大\n”);
}否则{
返回(int)值;
}
}
}
无效事实乐趣(整数事实){
int-fact=1;
对于(int i=1;i int_MAX/i){
printf(“无效输入:算术溢出\n”);
返回;
}
事实=事实*i;
}
printf(“%d的阶乘是%d\n”,num\u fact,fact);
}
这是您的代码和一些更正。有两个问题
首先,您必须完成输入的读取,直到到达行或EOF的末尾
第二个是您需要两个错误代码,一个用于无效输入,另一个用于无输入。(您在代码中的注释表示您希望在没有输入的情况下退出)
#包括
#包括
int display();
无效事实(整数事实);
int main(){
//int num=0;
下一步;
而(1){
下一步=显示();
如果(下一个==-2){
打破