【问题标题】:using several different functions使用几种不同的功能
【发布时间】:2017-01-18 17:58:36
【问题描述】:

对于我的作业,我的程序需要询问用户他想要执行五个功能中的哪一个。五个功能包括:

  1. 数的总和
  2. 数字的阶乘
  3. 第 n 项的斐波那契值。
  4. 两个数字的gcd
  5. a 的 b 次方。

将反复提示用户,直到他希望退出。我所有的功能都可以正常工作。但是,我认为我搞砸了其中一个循环,因为一旦我输入要执行的函数并输入一个值,它就会一直在无限循环中显示答案。

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

【问题讨论】:

  • 你介意创建一个MCVE吗?
  • if(option == 1) 等等.. D.R.Y.
  • 检查scanf()的返回值。
  • SO 不是调试服务。使用符号编译,在调试器中运行代码以逐行跟踪程序,检查相关变量的值以了解实际情况。如果出现具体问题,请随时返回此处。

标签: c switch-statement break


【解决方案1】:

首先,C 有非结构化的switch 语句。

您需要在每个case 正文之后添加一个break; 语句,以特定案例的执行限制在该case 下提到的正文中。

否则,默认情况下(不存在break 语句)所有case 语句都以直接通过 方式工作。你可以阅读更多关于它的信息here


也就是说,对于单个函数的重复执行,大部分(如果不是全部)逻辑都存在严重缺陷。例如,让我们采取这个

   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 在某个时候变为 0 以跳出循环,但您根本没有修改 n

详细地说,C 使用按值传递参数传递,因此对于函数内部的调用summation(n),无论您对接收n 值的参数所做的任何更改都不会反映在调用者,因此调用者中的n 保持不变。

【讨论】:

  • 是的,我完全忘记了 break 语句。但是即使添加了它们,它仍然有同样的问题。
  • @ShoaibAhmed 你的意思是它仍然执行所有的功能?
  • 所有函数都没有执行。假设我为变量“选项”输入 2,它找到输入数字的阶乘。该功能已正确执行。唯一的问题是它一直在无限循环中不断地显示答案。
  • 啊..我明白了,我现在明白了..等待更新回答。
【解决方案2】:

您只需要在每个案例的末尾添加一个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;

如果没有 break 语句,控件将下降到下一个案例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多