计算中的C结构

时间:2018-11-28 18:00:09

标签: c function struct

我已经设置好了我的功能并正在运行,当我运行程序时,它会跳到末尾,如果我按Enter键,它会要求我输入值,但将所有内容计算为0.00。

我很确定自己        “结构SEGMENT Calculation_segment_v1(结构SEGMENT s)”功能 或应该在其中输入值的函数。

任何帮助表示赞赏!

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

double atan(double x);

struct POINT {
    double x;
    double y;
};

struct SEGMENT {
    struct POINT p1;
    struct POINT p2;
    double length;
    double angle;
};

double distance_of_points(struct POINT p1, struct POINT p2)
{
    double length;
    length = sqrt(pow((p1.x - p2.x), 2) + pow((p1.y - p2.y), 2));
    return length;
};

double angle_x(struct POINT p1, struct POINT p2)
{
    double angle;
    double berechnung;
    berechnung=(pow((p1.x - p2.x), 2)/(pow((p1.y - p2.y), 2)));
    angle = atan(berechnung);
    return angle;
}

void print_segment(struct SEGMENT s)
{
    printf("\n--> Die Strecke verbindet die Punkte"
           " (%.2f,%.2f) und (%.2f,%.2f);\n"
           "--> Sie hat eine Laenge von %.4f;\n"
           "--> Der Winkel betraegt: %.2f Grad.\n\n",
           s.p1.x, s.p1.y, s.p2.x, s.p2.y, s.length, s.angle);
}

struct SEGMENT read_segment_v1()
{
    struct SEGMENT s;
    printf(">> Bitte Koordinaten x,y des ersten"
           " Punktes der Strecke eingeben: ");
    scanf ("%lf%lf", &s.p1.x, &s.p1.y);
    printf(">> Bitte Koordinaten x,y des zweiten"
           " Punktes der Strecke eingeben: ");
    scanf ("%lf%lf", &s.p2.x, &s.p2.y);
    return s;
}

struct SEGMENT calculate_segment_v1(struct SEGMENT s)
{
    struct POINT X={s.p1.x, s.p1.y}, Y={s.p2.x ,s.p2.y};
    s.length = distance_of_points(X, Y);
    s.angle = angle_x(X, Y);
}

int main(void)
{
    struct SEGMENT S;
    char c;
    double l;

    while (1) {
        printf("Bitte # eingeben fuer Programmende: ");
        fflush(stdin);
        scanf ("%c", &c);

        if (c=='#') break;

        S = read_segment_v1();
        S = calculate_segment_v1(S);
        print_segment(S);
    }

    printf("Ende des Programms\n");
    return 0;
}

1 个答案:

答案 0 :(得分:-1)

您的代码中有一个错误。添加

return s;

calculate_segment_v1函数的末尾进行修复。

PS:我不禁要指出,您发布的代码除此特定错误外,在许多方面还很糟糕。如果这是您在学习编码时写的东西,请考虑更改您到目前为止所采取的学习方式。