该程序将创建一个菜单,可以


char数组中获取2个char作为点
计算点之间的距离
显示方程式y = mx + b中的行
计算最接近直线的点
退出[仅当选择e时程序才会终止]


某处存在逻辑错误,导致没有正确的输出显示。我一直在用键盘敲打我的头,我非常希望我犯了一个愚蠢的简单错误。菜单显示太多次,所以我怀疑while循环出错。我不是程序员,但我的数学应该很扎实。请帮助我修正我的逻辑。

int main()
{
    char PtLbl[9] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' }; /*char and xcoordinates and y coordinates arrays hardcoded*/
    double Xcoord[9] = { 50.2, 45.7, 76.7, 12, -25, 23, 34.6 };
    double Ycoord[9] = { 75.8, 48, 99.1, 14, 48, -36, -123 };
    double temp, m = 0, b = 0, dist;
    double small = 99999999999.0;
    char choice = 'z';
    char pt1 = 'z', pt2 = 'z';
    int in1 = -1, in2 = -1, i, finalin;
    int k = 0, l = 1;

    while (1)
    {
        printf("a) Enter Points:\nb) Compute the distance\nc) Display the line equation\nd) Find the closest point to the line equation in c\ne) Exit\n");
        scanf("%c", &choice);  /*Displays the menu options, takes in a menu choice, and converts it to lowercase if upper*/
        choice = tolower(choice);

        if (choice == 'a') /*choice a selected*/
        {
            k = 1;
            while (in1 == -1 && in2 == -1)
            {
                printf("Please enter 2 points separated by one space\n");
                scanf("%c %c", &pt1, &pt2);
                pt1 = toupper(pt1);
                pt2 = toupper(pt2);

                for (i = 0; i < 9; i++) /*stores the index of the char in PtLbl array as index1 and index2*/
                {
                    if (pt1 == PtLbl[i])
                    {
                        in1 = i;
                    }

                    if (pt2 == PtLbl[i])
                    {
                        in2 = i;
                    }
                }
            }
        }

        else if ((choice == 'b' || choice == 'c' || choice == 'd') && k == 0) /*since points need to be selected first, this checks if a has been selected*/
        {
            printf("Please enter 2 points before making a further selection\n");
            continue;
        }

        else if (choice == 'b') /*calulates the distance between the points with the corresponding indexes in the x-coordinates and y-coordinates arrays*/
        {
            temp = sqrt(pow(Xcoord[in2] - Xcoord[in1], 2) + pow(Ycoord[in2] - Ycoord[in1], 2));
            printf("The distance between points %c and %c is %f\n", PtLbl[in1], PtLbl[in2], temp);
        }

        else if (choice == 'c') /*calculates slope (m) and y intercept (b) to put in a line equations and display*/
        {
            m = (Ycoord[in2] - Ycoord[in1]) / (Xcoord[in2] - Xcoord[in1]);
            b = (Ycoord[in1] - m * Xcoord[in1]);
            printf("The equation of the line passing through %c and %c is y = %f x+ %f\n", PtLbl[in1], PtLbl[in2], m, b);
        }

        else if (choice == 'd') /*calculates the from the line to nearest point*/
        {
            if (m == 0 && b == 0) /*only calculates (m) and (b) if c hasnt been selected yet*/
            {
                m = (Ycoord[in2] - Ycoord[in1]) / (Xcoord[in2] - Xcoord[in1]);
                b = (Ycoord[in1] - m * Xcoord[in1]);
            }

            for (i = 0; i < 9; i++)
            {
                if (i == in1 || i == in2)
                {
                    continue;
                }

                dist = abs(m*Xcoord[i] + (-1)*Ycoord[i] + b) / sqrt((m*m) + 1); /*smallest distance is recorded and index is stored*/
                if (dist < small)
                {
                    small = dist;
                    finalin = i;
                }
            }
            printf("The closest point to the line y = %f x + %f is %c at a distance of %f\n", m, b, PtLbl[finalin], small); /*displays data from d*/
        }

        else if (choice == 'e') /*program only exits when choice e is selected*/
        {
            exit(0);
        }

        else
        {
            printf("Please enter a valid selection"); /*prompts user to try again if input invalid*/
            continue;
        }

    }
}

最佳答案

明显的错误-您不会在scanf调用中忽略空格(例如换行符),因此您的代码将被混淆并被它们甩掉。您想在格式字符串中的%c指令前放置空格以忽略空格:

scanf(" %c", &choice);




scanf(" %c %c", &pt1, &pt2);

关于c - C程序编译没有错误。它显示垃圾。我认为,首先循环可能是原因,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52900827/

10-12 01:28