对于我的任务,我的程序需要询问用户他想要执行的五个函数中的哪一个。五个功能包括:
将重复提示用户,直到他希望退出。我所有的功能都很好。但是,我认为我搞砸了其中一个循环,因为一旦我输入了我想要执行的函数并输入一个值,它就会在无限循环中继续显示答案。
#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 :(得分:0)
首先,C具有非结构化switch
语句。
您需要在每个break;
正文后添加case
语句,以便将特定案例的执行限制到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使用pass-by-value进行参数传递,因此对于函数内部的调用summation(n)
,对接收n
值的参数所做的任何更改都是没有反映在调用者中,因此调用者中的n
保持不变。
答案 1 :(得分:0)
在每个案例结束时,您只需要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语句,控件将落到下一个案例中。