2016-02-12 44 views
-1

文件使用打印 - Isprime_function.c如何在c中的if函數中使用main函數中的函數返回值。我不想在我的Isprime_function文件

#include <stdio.h> 
#include "all_header.h" 

int isprime(int input) 
{ 
    int i, stop = 0; 

    if(input >= 1) 
    { 
     for(i=2; i<=input/2; i++) 
     { 
     if(input%i==0) 
     { 
      stop = 1; 
      break; 
     } 
     } 

     if(stop==0 && input!=1) 
     { 
     printf("%d is a prime number\n",input); 
     } 
     else 
     { 
     printf("%d is not a prime number\n",input); 
     } 
    } 
    else if(input == 1) 
    { 
     printf("%d is not prime by definition!\n", input); 
    } 
    else if(input == 0) 
    { 
     printf("%d is not a valid number.\n", input); 
    } 
    else 
    { 
     printf("%d Please use positive nonzero integers! Try again!\n", input); 
    } 
    return 0; 
} 

文件 - Isprime_main.c

#include <stdio.h> 
#include "all_header.h" 

int main() 
{ 
    int input; 

    printf("\nPlease Enter a Positive Integer: "); 
    scanf("%d", &input); 

    isprime(input); 

    return 0; 
} 

文件 - All_header.h

#ifndef PRESENT_VALUE_FUNCTIONS 
#define PRESENT_VALUE_FUNCTIONS 

int isprime(int input);//check whether number is prime or not. 

#endif 

我用以下命令

gcc -Wall -o isprime isprime_main.c isprime_function.c all_header.h 

我是編程新手我有一個小小的疑問。 此功能正常工作,但在我的isprime_fuction.c文件我使用「printf」我不想在函數中使用我想返回特定的值和控制行爲從主函數我該如何做,因爲我想用這個函數來獲取數字的主要因素。

+0

您的返回值賦值給一個變量,並使用它像任何其他變量。 –

+0

如果你只是想讓函數返回一個布爾值「是/否」的值,並且沒有打印,那麼在你調用該函數之前進行輸入驗證。 –

回答

2

你可以做這樣的事情:

文件 - Isprime_function.c

#include <stdio.h> 
#include "all_header.h" 

int isprime(int input) 
{ 
    int i, stop = 0; 

    if (input > 1) 
    { 
     for (i = 2; i <= (input/2); i++) 
     { 
      if ((input % i) == 0) 
      { 
       stop = 1; 
       break; 
      } 
     } 
     if ((stop == 0) && (input != 1)) { 
      return ISPRIME_YES; 
     } else { 
      return ISPRIME_NO; 
     } 
    } 
    else if (input == 1) 
    { 
     return ISPRIME_NO_BY_DEF; 
    } 
    else if (input == 0) 
    { 
     return ISPRIME_INVALID; 
    } 
    else 
    { 
     return ISPRIME_POSITIVE_ONLY; 
    } 
} 

文件 - Isprime_main.c

#include <stdio.h> 
#include "all_header.h" 

int main() 
{ 
    int input, result; 

    printf("\nPlease Enter a Positive Integer: "); 
    scanf("%d", &input); 

    result = isprime(input); 
    switch (result) 
    {  
     case ISPRIME_YES: 
      printf("%d is a prime number\n", input); 
      break; 

     case ISPRIME_NO: 
      printf("%d is not a prime number\n", input); 
      break; 

     case ISPRIME_NO_BY_DEF: 
      printf("%d is not prime by definition!\n", input); 
      break; 

     case ISPRIME_INVALID: 
      printf("%d is not a valid number.\n", input); 
      break; 

     case ISPRIME_POSITIVE_ONLY: 
      printf("%d Please use positive nonzero integers! Try again!\n", input); 
      break; 
    } 

    return 0; 
} 

文件 - All_header.h

#ifndef PRESENT_VALUE_FUNCTIONS 
#define PRESENT_VALUE_FUNCTIONS 

#define ISPRIME_POSITIVE_ONLY -3 
#define ISPRIME_INVALID -2 
#define ISPRIME_NO_BY_DEF -1 
#define ISPRIME_NO 0 
#define ISPRIME_YES 1 

int isprime(int input); //check whether number is prime or not. 

#endif 
0

我認爲the answer by Remy是正確的。我會推薦一點點重構main

int get_input(); 
void process_input(int input); 

int main() 
{ 
    int input = get_input(); 
    process_input(input); 

    return 0; 
} 

與定義爲功能get_inputprocess_input

int get_input() 
{ 
    int input; 
    while (1) 
    { 
     printf("\nPlease Enter a Positive Integer: "); 
     if (scanf("%d",&input) != 1) 
     { 
     // Error reading input. 
     // Discard the rest of the line and try again. 
     int c; 
     while ((c = getchar()) != EOF && c != '\n'); 
     } 
     else 
     { 
     break; 
     } 
    } 

    return input; 
} 

void process_input(int input) 
{ 
    int result = isprime(input); 
    switch (result) 
    {  
     case ISPRIME_YES: 
     printf("%d is a prime number\n", input); 
     break; 

     case ISPRIME_NO: 
     printf("%d is not a prime number\n", input); 
     break; 

     case ISPRIME_NO_BY_DEF: 
     printf("%d is not prime by definition!\n", input); 
     break; 

     case ISPRIME_INVALID: 
     printf("%d is not a valid number.\n", input); 
     break; 

     case ISPRIME_POSITIVE_ONLY: 
     printf("%d Please use positive nonzero integers! Try again!\n", input); 
     break; 
    } 
} 
相關問題