C语言 do while循环的问题

33qvvth1  于 2022-12-17  发布在  其他
关注(0)|答案(1)|浏览(189)

该代码允许您在星星和三角形电阻网络转换之间进行选择。还有一个退出选项。我想验证R_a,R_b,R_c等的值。然而,我在do while循环中遇到了一些麻烦。下限是1000,上限是1000000。
我打算让程序在输入值在范围内时继续运行,在输入值不在范围内时提示用户进行另一次输入。但是,到目前为止,如果值在范围内,程序将继续运行,但如果值不在范围内,程序将在给出警告后继续运行-当我希望它循环回到第一次输入提示时。
一旦正确,我将把循环添加到所有输入中。如果有人能够修复/找到这个问题,我将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    printf("\n\n\t\tDelta and Star Converter\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Star \n");
        printf("2. Delta\n");
        printf("0. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);


        switch(choice)
        {
        case 1:;

        float R_a=0,R_b=0,R_c=0,R_ab,R_bc,R_ac;


        printf("Please enter the value of the Star connected resistors:\n");

          do {
        printf("R_a = ");
         scanf("%f",&R_a);
        if (R_a > 1000000) {
            printf("Please");
        } else if (R_a < 1000) {
            printf("Number to low\n");
        }else {
        }

     }while(R_a = -0);



        printf("R_b = ");
        scanf("%f",&R_b);
        printf("R_c = ");
        scanf("%f",&R_c);

        R_ab=R_a+R_b+(R_a*R_b)/R_c;
        R_bc=R_b+R_c+(R_b*R_c)/R_a;
        R_ac=R_a+R_c+(R_a*R_c)/R_b;

        printf("the equivalent Delta values are: \n");
        printf("R_ab = %.2f Ohms\n",R_ab);
        printf("R_bc = %.2f Ohms\n",R_bc);
        printf("R_ac = %.2f Ohms\n",R_ac);
        break;

        case 2:;


        printf("Please enter the values of the Delta connected resistors:\n");

        printf("R_ab = ");
        scanf("%f",&R_ab);
        printf("R_bc = ");
        scanf("%f",&R_bc);
        printf("R_ac = ");
        scanf("%f",&R_ac);


        R_a = (R_ab*R_ac)/(R_ab + R_bc + R_ac);
        R_b = (R_ab*R_bc)/(R_ab + R_bc + R_ac);
        R_c = (R_ac*R_bc)/(R_ab + R_bc + R_ac);

        printf("the equivalent Star values are: \n");
        printf("R_a = %.2f Ohms\n",R_a);
        printf("R_b = %.2f Ohms\n",R_b);
        printf("R_c = %.2f Ohms\n",R_c);
        break;

            case 0:
                printf("\n\nAdios!!\n\n\n");
                exit(0);    // terminates the complete program execution
        }
while (0) ;  }
    printf("\n\n\t\t\tThank you!\n\n\n");
    return 0;
}
dsekswqp

dsekswqp1#

while(R_a = -0)

=是赋值运算符。它将-0赋值给R_a,并计算出相同的 *falsy值 *,从而结束循环。
do ... while更改为无限循环,当值在范围内时将break更改为循环。

while (1) {
    printf("R_a = ");

    if (1 != scanf("%f", &R_a))
        exit(EXIT_FAILURE);

    if (R_a > 1000000) {
        fprintf(stderr, "Number too high.\n");
    } else if (R_a < 1000) {
        fprintf(stderr, "Number to low.\n");
    } else {
        break;
    }
}

相关问题