2017-01-18 11 views
0

私の割り当てのために、私のプログラムは、5つの機能のうちどれを実行したいかをユーザに尋ねる必要があります。 5つの関数は:n番目の用語の数 いくつかの異なる機能を使用する

  • フィボナッチ値の数
  • 階乗の

    1. 総和を。
    2. 2つの数字のgcd
    3. aのbのパワー。

    ユーザーは、終了するまで繰り返しメッセージを表示します。私のすべての機能は正常に動作します。しかし、私は実行すると値を入力したい関数を入力すると、無限ループで答えを表示し続けるので、私はループの1つを台無しだと思う。

    #include <stdio.h> 
    #include <math.h> 
    
    // function to find summation of a number 
    int summation(int k) { 
        int i; 
    
        for(i = k; i >= 0; i--) { 
        k = i + (i-1); 
        } 
        return k; 
    } 
    
    // function to find the factorail of a number 
    int factorial(int num) { 
        int i; 
    
        for(i = num - 1; i > 0; i--) { 
        num = num * i; 
        } 
        return num; 
    } 
    
    // dunxtion to find the fibonacci of the nth term 
    int fibonacci(int n){ 
        int i, t1 = 0, t2 = 1, nextTerm; 
    
        for(i = 1; i <= n; i++) { 
        if(i == 1) { 
         printf("%d, ", t1); 
         continue; 
        } 
        if(i == 2) { 
         printf("%d, ", t2); 
         continue; 
        } 
        nextTerm = t1 + t2; 
        t1 = t2; 
        t2 = nextTerm; 
        printf("%d, ", nextTerm); 
        } 
        return nextTerm; 
    } 
    
    // function to find the gcd of two numbers 
    int gcd(int n, int m) { 
        int i, gcd; 
    
        for(i=1; i <= n && i <= m; i++) { 
        // Checks if i is a factor of both integers 
        if(n % i == 0 && m % i == 0) 
         gcd = i; 
        } 
        return gcd; 
    } 
    
    // function to find value of n to the power of m 
    int power(int n, int m) { 
        return pow(n, m); 
    } 
    
    int main(void) {; 
        int option ,n, m; 
    
    //Asks user for what they want to find 
        printf("If you would like to find the summation of a number, enter 1 \n"); 
        printf("If you would like to find the factorial of a number, enter 2 \n"); 
        printf("If you would like to find the fibonacci sequence of a number, enter 3 \n"); 
        printf("If you would like to find the gcd of two numbers, enter 4 \n"); 
        printf("If you would like to find the power of a number a to b, enter 5 \n"); 
        printf("If you would like to exit, enter 0 \n"); 
        scanf("%d", &option); 
    
    // Enables the program to prompt the user until they wish to exit 
        while(option != 0) { 
        switch(option) { //If user wishes to find the summation 
         case 1: if(option == 1) { 
            printf("Enter a number: "); 
            scanf("%d", &n); 
            while(n > 0) { 
             if(n < 1) { //message displayed if an invalid value is entered 
              printf("invalid value"); 
             } 
             else { 
              printf("Summation of %d is %d", n, summation(n)); 
             } 
            } 
           } 
         case 2: if(option == 2) { //if user wishes to find factorial of a number 
            printf("Enter a number: "); 
            scanf("%d", &n); 
            while(n >= 0) {//message displayed if an invalid value is entered 
             if(n < 0) { 
              printf("invalid value"); 
             } 
             else { 
              printf("factorial of %d is %d", n, factorial(n)); 
             } 
            } 
           } 
         case 3: if(option == 3) { //if user wishes to find the fibonacci value of the nth term 
            printf("Enter a number: "); 
            scanf("%d", &n); 
            while(n >= 0) {//message displayed if an invalid value is entered 
             if(n < 0) { 
              printf("invalid value"); 
             } 
             else { 
              printf("fibonacci of %d is %d", n, fibonacci(n)); 
             } 
            } 
           } 
         case 4: if(option == 4) { 
            printf("Enter a number: "); 
            scanf("%d %d", &n, &m); 
            while(n >= 0 && m >= 0) { 
             if(n < 0 || m < 0) {//message displayed if an invalid value is entered 
              printf("invalid value"); 
             } 
             else { 
              printf("GCD of %d and %d is %d", n, m, gcd(n, m)); 
             } 
            } 
           } 
         case 5: if(option == 5) { 
            printf("Enter a number: "); 
            scanf("%d %d", &n, &m); 
            while(n >= 0 && m >= 0) { 
             if(n <= 0 || m < 0) { 
              printf("invalid value"); 
             } 
             else { 
              printf("%d to the power of %d is %d", n, m, power(n, m)); 
             } 
            } 
           } 
         default: if(option == 0) { 
          break; 
         } 
        } 
        scanf("%d", &option); 
    } 
    } 
    
  • +0

    あなたは[___MCVE___](http://stackoverflow.com/help/mcve)の作成を気にしますか? –

    +1

    'if(option == 1)'と同様です。D.R.Y. –

    +1

    'scanf()'の戻り値を確認してください。 –

    答えて

    0

    まず、Cは構造化されていないswitchステートメントです。

    あなたは制限そのcaseで述べた体に特定のケースの実行にあなたのcase身体の各後break;ステートメントを追加する必要があります。

    そうでない場合は、デフォルトでは、(break文の不在で)すべてcase文は、フォールスルーように動作します。あなたはそれについての詳細を読むことができますhere。つまり、1つの関数が繰り返し実行されていると、ロジックのほとんど(すべてではないにしても)に重大な欠陥があります。たとえば、あなたがループから抜け出すためにいくつかの点で0になるnに返信している、ここではこの

    while(n > 0) { 
        if(n < 1) { //message displayed if an invalid value is entered 
         printf("invalid value"); 
        } 
        else { 
         printf("Summation of %d is %d", n, summation(n)); 
        } 
        } 
    

    てみましょう、しかし、あなたはすべてで、nを変更しませんでした。

    具体的には、Cは引数の受け渡しに値渡しを使用するため、関数内のsummation(n)という呼び出しに対して、nの値を受け取った変更は呼び出し元に反映されません。したがって、発信者のnは変更されません。

    +0

    ええ、私は完全にブレークステートメントについて忘れてしまった。しかし、それらを追加した後も、それは同じ問題を抱えています。 –

    +0

    @ShoaibAhmedそれはまだ機能を実行していることを意味しますか? –

    +0

    すべての機能は実行されません。入力変数の階乗を求める変数 "option"に2を入力したとしましょう。この関数は適切に実行されます。唯一の問題は、常に無限ループで答えを表示し続けることです。 –

    0

    あなただけのようなすべてのケース の終わりにbreak文を必要とする:なしbreak文が存在しない場合、コントロールは、次の場合に落ちるだろうと

    case 1: if(option == 1) { 
           printf("Enter a number: "); 
           scanf("%d", &n); 
           while(n > 0) { 
            if(n < 1) { //message displayed if an invalid value is entered 
             printf("invalid value"); 
            } 
            else { 
             printf("Summation of %d is %d", n, summation(n)); 
            } 
           } 
          } 
    break; 
    

    関連する問題