用C语言编写一个程序:定义一个点的坐标,然后定义两个点,求这两个点间的距离。

Python013

用C语言编写一个程序:定义一个点的坐标,然后定义两个点,求这两个点间的距离。,第1张

#include <stdio.h>

#include <math.h>

struct Point

{

    double x, y

}

/** Calculate the distance of two points. */

double distance(const struct Point *a, const struct Point *b)

{

    return sqrt((a->x-b->x)*(a->x-b->x)+(a->y-b->y)*(a->y-b->y))

}

int main()

{

    struct Point a, b

    printf("Please input the first point: ")

    scanf("%lf%lf", &a.x, &a.y)

    printf("Please input the second point: ")

    scanf("%lf%lf", &b.x, &b.y)

    printf("The distance of the two point is %f.\n", distance(&a, &b))

    return 0

}

说明:

1、distance() 函数的两个参数 const struct Point *a 和 b 使用了 const 修饰,是表示 a 和 b 在函数执行过程中不会被修改;这样即使函数体内部写错,修改了 a 和 b 的值,编译也不会通过。

2、对 double,scanf 用 %lf,printf 用 %f。

以上。

#include "Conio.h"

#include "graphics.h"

#define closegr closegraph

void initgr(void) /* BGI初始化 */

{

int gd = DETECT, gm = 0/* 和gd = VGA,gm = VGAHI是同样效果 */

registerbgidriver(EGAVGA_driver)/* 注册BGI驱动后可以不需要.BGI文件的支持运行 */

initgraph(&gd, &gm, "")

}

void DrawCoord()

void Drawstg()

void Drawcurve()

int main(void)

{

initgr()/* BGI初始化 */

DrawCoord()

Drawstg()

Drawcurve()

getch()/* 暂停一下,看看前面绘图代码的运行结果 */

closegr()/* 恢复TEXT屏幕模式 */

return 0

}

void DrawCoord() /*画坐标系*/

{

line(50,40,50,400)/*y轴*/

line(50,400,600,400)/*x轴*/

line(50,40,45,50)/*箭头*/

line(50,40,55,50)

line(600,400,590,395)

line(600,400,590,405)

outtextxy(35,45,"y")

outtextxy(590,410,"x")

outtextxy(40,410,"O")

}

void Drawstg() /*画标尺*/

{

int x,y,i

x=50,y=400

for(i=0i<17i++)

{

line(x+5,y,x,y)

y-=20

}

x=50,y=400

for(i=0i<26i++)

{

line(x,y-5,x,y)

x+=20

}

}

void Drawcurve()/*画图示例*/

{

line(50,400,500,400-250)

}

怎么在C语言的图形模式下实现匀速圆周运动?为什么我用圆的对称性的方程做出来的是变速的(就是建立一个直角坐标系,X由从小到大递增,然后画出点)?#include "stdio.h" #include "math.h" #include "graphics.h" #include "conio.h" #define R 50 /*半径*/ #define V 100000 /*延迟时间*/ main() { int x,y,ta,tb,a=1 ta=DETECT initgraph(&ta,&tb,"c:\\tc")/*初始化图形驱动*/ x=-R while(1) { x+=a/*X的递增或递减(由a而定)*/ y=sqrt(R*R-x*x)*a/*方程*/putpixel(x+240,y+250,7)/*画点*/ delay(V)/*延时*/ putpixel(x+240,y+250,0)/*擦除点*/ if(x==R||x==-R)/*换方向*/ a=-a if(bioskey(1)!=0)/*控制退出的(按下任意键结束)*/ break } closegraph() }