【问题标题】:C: Only the first condition in the if statement worksC:只有 if 语句中的第一个条件有效
【发布时间】:2015-12-16 18:16:07
【问题描述】:

我目前正在使用 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");
            }
        }
    }
}

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

【问题讨论】:

  • 使用函数会是更好的选择恕我直言
  • 您需要仔细查看您的牙套。它们不是你认为的那样。
  • 阅读有关 char、char* 和 char[] 的更多信息。
  • 您没有检查任何scanf() 调用的返回值。你怎么知道它们是否有效?

标签: c if-statement


【解决方案1】:

这是因为条件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 和/或为子任务使用单独的函数,以便您的代码更具可读性并且您可以轻松调试。

【讨论】:

    【解决方案2】:

    我看到的一个问题是:

    scanf("%s", &variable);
    

    这在语法上是正确的,但在语义上是不正确的。由于variable 的类型为char,因此您确实无法将字符串读入其中。你最终会访问你不应该访问的内存,这会导致未定义的行为。

    你需要这样的东西:

    char variable[100]; // Make the array as large you need to
    scanf("%s", variable);
    

    PS修复此问题可能无法解决任何其他问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多