.笨小孩. 2021-10-05 15:45 采纳率: 25%
浏览 33

浙大版《C语言程序设计(第4版)》习题3-5 三角形判断

我写了一个用结构体指针写的判断三角形的程序。编译没有报错,但是程序无法正常进行。调试完后发现数据就没有输入成功,调试报错为segmentation fault,请大家帮我看看是那里出错了。谢谢大家


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

typedef struct _point
{
    double x;
    double y;
} Point, *pPoint;

//求边长函数
double lengthCalculate(pPoint p1,pPoint p2);
//判断是否可以构成三角形函数
_Bool judge(pPoint p1, pPoint p2, pPoint p3);

int main(int argc, char const *argv[])
{
    pPoint p1=NULL, p2=NULL, p3=NULL;
    double L, A;

    scanf("%lf %lf %lf %lf %lf %lf", &(p1->x), &(p1->y), &(p2->x), &(p2->y), &(p3->x), &(p3->y));
    if(judge(p1,p2,p3))
    {
        L = lengthCalculate(p1, p2) + lengthCalculate(p2, p3) + lengthCalculate(p1, p3);
        A = L * (L - lengthCalculate(p1, p2)) * (L - lengthCalculate(p2, p3)) * (L - lengthCalculate(p1, p3));
        printf("L = %.2f, A = %.2f", L, A);
    }
    else
    {
        printf("Impossible");
    }
    return 0;
}

double lengthCalculate(pPoint p1,pPoint p2)
{
    double length;

    length = sqrt(pow(p1->x - p2->x, 2) + pow(p1->y - p2->y, 2));

    return length;
}

_Bool judge(pPoint p1, pPoint p2, pPoint p3)
{
    double length1, length2, length3;

    length1 = lengthCalculate(p1, p2);
    length3 = lengthCalculate(p1, p3);
    length2 = lengthCalculate(p2, p3);
    if(length1+length2>length3&&length3+length2>length1&&length1+length3>length2)
    {
        return 1;
    }
    else
    {
        return 0;
    }

}
  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2021-10-05 15:47
    关注

    你的p1、p2、p3都是NULL,怎么可能scanf输入数据呢,得先分配空间啊

    评论

报告相同问题?

问题事件

  • 创建了问题 10月5日

悬赏问题

  • ¥15 惠普360g9的最新bios
  • ¥15 配置hadoop时start-all.sh老是启动失败
  • ¥30 这个功能用什么软件发合适?
  • ¥60 微信小程序,取消订单,偶尔订单没有改变状态
  • ¥15 用pytorch实现PPO算法
  • ¥15 关于调制信号的星座图?
  • ¥30 前端传参时,后端接收不到参数
  • ¥15 这是有什么问题吗,我检查许可证了但是显示有呢
  • ¥15 机器学习预测遇到的目标函数问题
  • ¥15 Fluent,液体进入旋转区域体积分数不连续