C语言初学结构体,不太懂错哪了

P5735 【深基7.例1】距离函数

Loving_coding @ 2023-01-13 02:39:35

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

int main(){
int i,j;
float dis1=0,dis2=0,dis=0;
    struct point{
        int  x, y;
    };
    point p[3] = 
    { 
    {scanf("%f%f",&p[0].x,&p[0].y)},
    {scanf("%f%f",&p[1].x,&p[1].y)},
    {scanf("%f%f",&p[2].x,&p[2].y)} 
    };
    for (i = 0; i <= 1; i++) {
        dis1 =dis1+ sqrt(pow(p[i].x-p[i + 1].x, 2)+ pow(p[i].y - p[i + 1].y, 2));
    }
    for (j = 0; j < 1; j++) {
        dis2  = sqrt(pow(p[j].x - p[j + 2].x, 2) + pow(p[j].y - p[j + 2].y, 2));
    }

    dis = dis1 + dis2;
    printf("%f", &dis);
    return 0;
}

by MARSandEARTH @ 2023-01-13 06:38:52

struct


by MicroSun @ 2023-01-13 07:16:46

你为什么要把读入语句放在赋值语句里面呢


by MicroSun @ 2023-01-13 07:53:30

lz,等下,正在帮你调


by PCwqyy @ 2023-01-13 07:54:12

你这代码跟某Py有点像呢
scanf 返回的是成功读入的数量


by MicroSun @ 2023-01-13 07:56:25

for (i = 0; i <= 1; i++) 

请问这句话有意义吗。。。


by MicroSun @ 2023-01-13 07:57:25

而且输入整型,你怎么用%f啊


by PCwqyy @ 2023-01-13 07:57:27

还有,printf 是不用加 &


by PCwqyy @ 2023-01-13 07:58:50

printf 保留两位小数用 %.2f


by PCwqyy @ 2023-01-13 07:59:39

你试一下:

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

int main(){
int i,j;
float dis1=0,dis2=0,dis=0;
    struct point{
        int  x, y;
    };
    point p[3];
    scanf("%d%d",&p[0].x,&p[0].y);
    scanf("%d%d",&p[1].x,&p[1].y);
    scanf("%d%d",&p[2].x,&p[2].y);
    for (i = 0; i <= 1; i++) {
        dis1 =dis1+ sqrt(pow(p[i].x-p[i + 1].x, 2)+ pow(p[i].y - p[i + 1].y, 2));
    }
    for (j = 0; j < 1; j++) {
        dis2  = sqrt(pow(p[j].x - p[j + 2].x, 2) + pow(p[j].y - p[j + 2].y, 2));
    }

    dis = dis1 + dis2;
    printf("%.2f", dis);
    return 0;
}

by MicroSun @ 2023-01-13 08:00:00

dis1=0;
 dis1 =dis1+……;

这里可以省略赋初始值,后面直接附上


| 下一页