文件重定向输入inC

时间:2017-01-29 01:18:25

标签: c

我正在创建一个程序,该程序从单独的.txt文件中获取数字并将它们转换为阶乘,然后查找每个素数在阶乘中发生的次数。

我过去几天一直在研究这段代码,经过几个小时我一直坚持同样的事情,它会编译没有错误但是当我尝试文件重定向从另一个文件获取输入时它停止了就像它在无限循环中要求输入一样。

非常感谢任何帮助!

#include <stdio.h>

int Storage_array[5];

int current_prime, next_prime, factorial, prime_count, prime_number, num, factorial_current, factorial_next_num, current_number, total_number; //various variables used in program

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
    }
}

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
        }
    }
}




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;
}

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
    {
        if (factorial % current_prime == 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
        }
    }
} 

我拥有的文本文件具有以下内容,称为input.txt

7
67
9
43
0

0 个答案:

没有答案