gpt4 book ai didi

C:只有if语句中的第一个条件有效

转载 作者:行者123 更新时间:2023-11-30 15:13:49 25 4
gpt4 key购买 nike

我目前正在 PuTTY 中编码一些内容,并且我的 if 语句似乎遇到了技术困难。我的程序目前还不完整,但我想在完成代码之前修复这个错误。该程序将编译良好并打印;然而,只有进行第一种类型的微分才有效,而尝试进行积分或其他类型的微分则会退出程序:

int main()
{
int probtype, diftype, intype, vartype, vartest;
probtype = 0;
diftype = 0;
intype = 0;
vartype = 0;
vartest = 0;
char variable, variable2;
float fpower, fconstant, fpower2, fconstant2, fdivisor;
int ipower, iconstant, ipower2, iconstant2, idivisor;
printf("Alright, starting off would you like to do differentiation or integration? Type '1' for differentiation or '2' for integration. ");
scanf("%d", &probtype);
if (probtype == 1)
{
printf("Okay, what kind of differentiation problem are you interested in? Type 1 for 'Power Rule with a Constant', Type 2 for 'Product Rule', Type 3 for 'Quotient Rule, Type 4 for 'Chain Rule', Type 5 for 'Trigonometric Problems', Type 6 for 'Exponential Problems' or Type 7 for 'Natural Log Problems. ");
scanf("%d", &diftype);
if (diftype == 1)
{
printf("Before we begin. Type 1, if the constant and power are both integers, type 2 if the constant is a decimal and the power is an integer, and type 3 for all other cases. " );
scanf("%d", &vartype);
if (vartype == 1)
{
printf("First off enter the function's constant. If it has no visible constant enter 1. ");
scanf("%d", &iconstant);
printf("Alright, next enter the variable you are using. ");
scanf("%s", &variable);
printf("Finally, enter the power of the variable. ");
scanf("%d", &ipower);
iconstant = iconstant * ipower;
ipower = ipower - 1;
if (ipower == 0)
{
printf("The derivative of the function would be: %d \n", iconstant);
}
else
{
printf("The derivative of the function would be: %d%s^%d \n", iconstant, &variable, ipower);
}
}
if (vartype == 2)
{
printf("First off enter the function's constant. Enter it in DECIMAL form! ");
scanf("%f", &fconstant);
printf("Next, enter the variable you are using. ");
scanf("%s", &variable);
printf("Finally, enter the power of the variable. ");
scanf("%d", &ipower);
fconstant = fconstant * ipower;
ipower = ipower - 1;
if (ipower == 0)
{
printf("The derivative of the function would be %f \n", fconstant);
}
else
{
printf("The derivative of the function would be %f%s^%d \n", fconstant, &variable, ipower);
}
}
if (vartype == 3)
{
printf("First off, enter the function's constant. Enter it in DECIMAL form! ");
scanf("%f", &fconstant);
printf("Okay, now enter which variable you are using. ");
scanf("%s", &variable);
printf("Finally, enter the power of the variable. Enter this in DECIMAL form also! ");
scanf("%f", &fpower);
fconstant = fconstant * fpower;
fpower = fpower - 1;
printf("The derivative of the function would be: %f%s^%f \n", fconstant, &variable, fpower);
}
else if (diftype == 2)
{
printf("Enter the first function's constant. If it doesn't have one, type a 1 ");
}
else if (diftype == 3)
{
printf("Quotient Rule");
}
else if (diftype == 4)
{
printf("Chain Rule");
}
else if (diftype == 5)
{
printf("Trigoometric Problems");
}
else if (diftype == 6)
{
printf("Exponential Problems");
}
else if (diftype == 7)
{
printf("Natural Log Problems");
}
}
else if (probtype == 2)
{
printf("Okay, now what kind of integration problem are you interested in? Type 1 for 'Indefinite Integrals', Type 2 for 'Definite Integrals', Type 3 for 'Substitution', Type 4 for 'Trignometric Integrals', Type 5 for 'Integrations by Part', Type 6 for 'Exponential Problems', Type 7 for 'Natural Log Problems. ");
scanf("%d", &intype);
if (intype == 1)
{
printf("Before we get started: type 1 if both the constant and power are integers, type 2 if the constant is an integer and the power is a decimal, type 3 if the constant is a decimal and the power is an integer, and type 4 if both are decimals." );
scanf("%d", &vartype);
if (vartype == 1)
{
printf("Alright first off, enter the constant. If there is no constant, type a 1. " );
scanf("%d", &iconstant);
printf("Is there a variable in this equation? Type 1 if there is, Type 2 if there isn't. ");
if (vartest == 2)
{
printf("The indefinite integral of the function is: %dx + C", iconstant);
}
else
{
printf("Next, enter the variable that will be used for the problem. ");
scanf("%s", &variable);
printf("Finally enter the power of the function. ");
scanf("%d", &ipower);
ipower = ipower + 1;
idivisor = ipower + 1;
printf("The indefinte integral of the function is: %d%s^%d / %d \n", iconstant, &variable, ipower, idivisor);
}
}
}
else if (intype == 2)
{
printf("Definite Integrals");
}
else if (intype == 3)
{
printf("Substitution");
}
else if (intype == 4)
{
printf("Trignometric Integrals");
}
else if (intype == 5)
{
printf("Integrations by Part");
}
else if (intype == 6)
{
printf("Exponential Problems");
}
else if (intype == 7)
{
printf("Natural Log Problems");
}
}
}
}

我似乎无法找到导致错误的原因,如果我的信息不够具体,我深表歉意。我知道有更有效的方法来执行此程序,因此请不要更改我的代码,而只是向我展示我做错了什么以及如何修复它。谢谢。

最佳答案

这是因为条件 else if (probtype == 2) 位于条件 if (probtype == 1) 内部,并连接到其他一些 if。相反,在之前添加另一个大括号:

} // <--- Add an extra brace here in addition to what you have.
else if (probtype == 2)
{

并删除外部 if 语句末尾的一个。

您有太多嵌套的 if else 语句。我建议您重新编写代码以使用 switch-case 和/或对子任务使用单独的函数,以便您的代码更具可读性并且您能够轻松调试。

关于C:只有if语句中的第一个条件有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com