C语言中带文件重定向的浮点异常

C语言中带文件重定向的浮点异常,c,C,我正在开发一个程序,该程序从输入文件中获取一个数字列表,然后获取每个数字的阶乘,并找到阶乘中每个素因子的数量。例如:如果输入文件只有一行上有“4”,程序将获取该文件(input.txt),然后输出每个素数因子出现的量,在本例中为3 2和1 3,(4!=4*3*2*1=2*3*2*2=(2^3)*(3^1),这将输出以下内容: 4! = (2^3)*(3^1) 每次我编译程序时,它都可以正常工作,但当我输入文件(./a.out

我正在开发一个程序,该程序从输入文件中获取一个数字列表,然后获取每个数字的阶乘,并找到阶乘中每个素因子的数量。例如:如果输入文件只有一行上有“4”,程序将获取该文件(input.txt),然后输出每个素数因子出现的量,在本例中为3 2和1 3,(4!=4*3*2*1=2*3*2*2=(2^3)*(3^1),这将输出以下内容:

4! = (2^3)*(3^1)
每次我编译程序时,它都可以正常工作,但当我输入文件(./a.out
#include <stdio.h>

int Storage_array[5]; // array creating to store the values of the input text file (input.txt)

int current_prime, next_prime, prime_count, prime_number; // variables that involve the calculation and count of prime numbers
int factorial, factorial_current, factorial_next_num; //variables that invlove the handling of the input factorials
int num, current_number, total_number; //variables at help test if a number is prime of not

int find_prime_count(int factorial, int prime_number); //3 functions declared below main()
int find_next_prime(int factorial, int current_prime);
int is_prime(int num);

int a, b, c; //utility varbles

main() 
{
    if((a=scanf("%d", &c)) == EOF) //checks to see if file is empty
    {       
        return 0;
    }
    for(b=0; b<c; b++) 
    {
        if((a=scanf("%d", &c)) == EOF) //checks to see if reached end of the file
        {
            break;
        }
        scanf("%d", &Storage_array[b]); //paces values from file into the storage array
        printf("%d! = ", Storage_array[b]); //formatting the output
        for(c=0;c<5;c++)
        {
            if(c=0) //creating a base case
            {
                current_prime = 2;
            }
            else
            {           
                current_prime = next_prime; //updates the current prime after the base case
                next_prime = find_next_prime(Storage_array[b], current_prime); //performes the outside functions that fine the next                                                     prime number within the factorial
                prime_count = find_prime_count(Storage_array[b], current_prime); //performs outside function that finds the number                                                  of the current prime within the factorial
                printf("(%d^%d)",current_prime ,next_prime); //prints the results in proper format
            }

        }
        printf("\n"); //spaces between factorial numbers
    }
}

/* This function takes an integer as input and then determines if it is a prime number by finding the remainder when the input number is divided by all numbers leading up to the input number exept the number itself and 1, if it is a prime number it returns the prime number, if not it returns 0 */

int is_prime(int num) //function that determines if the input number is a prime number or not
{
    for(a=2; a<num; a++) //base case knowing lowest prime is 2
    {
        if (num % a == 0) //determines if the number is a prime number using remainder when divided by various numbers
        {
            return a; //returns the number if it is prime
        }
    }
}

/* This function takes 2 integers as input (a factorial number and a prime number) and then counts the amount of times that prime number can fit into the factorial, if it can divide with a 0 remainder it divides the factorial by that amount and then stores the amount of times before going to next number within the factorial, after it has divided the entire factorial it returns the number of times the prime number fit */

int find_prime_count (int factorial, int prime_number)
{
    for(a=0;a<factorial;a++)//tests all possible prime numbers under the input factorial
    {
        if(a=0) //base case for lowest possible prime in factorial
        {       
            factorial_current = 1;
            factorial_next_num = 1;
        }
        else
        {
            factorial_current = (a*factorial_next_num);
        }
        if(factorial_current % prime_number == 0) //determines if the prime number fits into current factorial number
        {
            current_number = (factorial_current/prime_number); //finds number of times prime number fits in
            total_number = (total_number + current_number); //adds total amount of times that prime has fit within the factorial
        } 
    }
    return total_number;
}

/* This function takes 2 integers as input (a factorial and a prime number) and then determines the next prime number within the limits of the factorial by finding the next prime number and testing it to see if it is a factor of the factorial */

int find_next_prime (int factorial, int current_prime) //function that determines the next prime within the factorial
{
    for(a=current_prime;a<factorial;a++) //checks all possible primes within the factorial
    {

        for(b=0;b<a;b++) 
        {       
            if (a % b == 0) //determines if the number is a prime number using remainder when divided by various numbers
            {
                return a; //returns the number if it is next prime
            }
        }   
    }
}

在外循环中,有两个东西被认为是受干扰的
c
控制的。第一个
a=scanf(“%d”,&c)
,还有
for(c=0;cy您的文件说“有五个测试编号”,但只包含四个。正确处理零可能需要特殊代码;0!定义为1。注意
scanf()
如果需要一个数字,则可以返回0。最好检查
如果(scanf(“%d”,&c)!=1){…处理错误…}
。你支持什么样的因子范围?你可以很容易地为1到100之间的每一个数字创建一个素数因子表,然后在计算中使用它。如果你打算放大,你必须稍微小心一点,但1到1000之间的素数并不多(准确地说是168个)。因此,任何高达1000!的阶乘都不需要超过168个分量素数。
5 
46 
89 
36 
0