首页 文章

尝试添加代码以计算两个用户输入点之间的距离

提问于
浏览
0

我想做的就是能够计算每个点之间的距离,这样如果用户愿意,可以显示总距离 . 为了澄清,距离将是(Point1-> Point2)(Point2-> Point3)(Point3-> Point4),而不是Point1-> Point4 . 我相信我必须使用这样的等式:
distance = sqrt((int)(...计算一些短值...)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct point
{
        short int x;
        short int y;
}point;
int main(char ** args, int argc)
{
        point append;
        char choice;

        int i = 1;
        int c;
        char dump;

        while(i != 0)
        {
                printf("Enter a to append and c to calculate p to print\n:");
                scanf("%c%c", &choice, &dump);

                switch(choice)
                {
                        case 'a':
                        {
                                FILE * output;
                                output = fopen("test.bin", "ab+");
                                printf("Enter the x:\n");
                                scanf("%hu", &append.x);
                                printf("Enter the y:\n");
                                scanf("%hu%c", &append.y, &dump);
                                fprintf(output, "Point (%hu,%hu)\n", append.x, append.y);
                                fclose(output);
                                break;
                        }
                        case 'p':
                        {
                                FILE * print;
                                print = fopen("test.bin", "rb");
                                if(print)
                                {
                                        while((c = getc(print)) != EOF)
                                        {
                                                putchar(c);
                                        }
                                }
                                fclose(print);
                                break;
                        }
                        case 'q':
                        {
                                i = 0;
                                break;
                        }
                }
        }
}

1 回答

  • 1

    使用distance formula查找每个段的长度 . 添加这些段长度以查找总距离 .

    距离公式取x和y中变化的平方和的平方根:

    Distance = sqrt(dx*dx + dy*dy)
    

    dxx2-x1dyy2-y1 .

相关问题