本文介绍了功能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好,如果有一个简单的解决方案,请原谅我。我将通过下面的代码来完成 ,这些代码只是计算出一本书中的阶乘代价,但是当我运行它时,无论数字是什么,我得到答案0 > 我输入。有人可以帮忙吗? 提前致谢 严格_mk #include" stdio.h" unsigned long long int return_factorial(unsigned int num); //功能 原型 int main(无效){ char输入[3]; //键盘输入 int number; //使用的数字 //提示用户 printf("输入一个正整数,其中阶乘将是 计算:"); fgets(输入,sizeof(输入),stdin); //阅读输入 sscanf(输入,%d,&数字); //检查输入为有条件的 if(number> 0){ printf("%d的阶乘是%lu。\ n" ,数字, return_factorial(数字)); }否则{ printf("你必须输入一个正整数!\ n" ;); } getchar(); //暂停并等待用户 返回0; } //此函数需要一个数字返回其阶乘 unsigned long long int return_factorial(unsigned int num){ unsigned long long int sum = 1; unsigned int i; //用于计算阶乘的乘数 //遍历每个乘数,包括总和 for(sum = 1, i = 1; i< = num; ++ i){ sum * = i; } 返还金额; } // return_factorial函数结束 Hi all, forgive me if there is a simple solution for this. I am goingthrough the following piece of code which simply calculates factorialsout of a book, but when i run it I get the answer 0 for whatever numberI input. Can anyone help? Thanks in advancestrictly_mk #include "stdio.h" unsigned long long int return_factorial(unsigned int num);//functionprototype int main (void) { char input[3]; //keyboard inputint number; // number to work with //prompt user printf("Enter a positive integer for which the factorial will becalculated: "); fgets (input, sizeof(input), stdin); // read the input sscanf (input, "%d", &number); //check the input as conditional if (number > 0) { printf ("The factorial of %d is %lu.\n", number,return_factorial(number));} else {printf ("You must enter a positive integer!\n");} getchar(); //pause and wait for user return 0;} //This function takes a number and returns its factorial unsigned long long int return_factorial(unsigned int num) { unsigned long long int sum = 1; unsigned int i; //multiplier to be used in calculating factorial //Loop through every multiplier up to and including sum for (sum = 1, i = 1; i <= num; ++i) { sum *= i;} return sum; } // End of return_factorial function 推荐答案 return_factorial返回一个unsigned long long,然后你用b $ b尝试使用适合 unsigned long的格式说明符进行打印。尝试使用%llu的格式说明符 - 好​​吧,仅限流行语。两个音节,上衣。 - Laurie Anderson return_factorial returns an unsigned long long, which you thenattempt to print using a format specifier suitable for anunsigned long. Try using a format specifier of %llu--Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson 如果我用lcc-win32编译你的代码,它会说: 警告tprintfw.c:23 printf参数格式不匹配。预计 int得到无符号多长 使用%llu If I compile your code with lcc-win32 it will say: Warning tprintfw.c: 23 printf argument mismatch for format u. Expectedint got unsigned long long use %llu 这篇关于功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 23:26