首页 > 编程语言 > 详细

c语言 12 - 5

时间:2021-06-06 00:41:24      阅读:14      评论:0      收藏:0      [点我收藏+]

1、

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

#define sqr(x) ((x) * (x))

typedef struct{
    double x;
    double y;
}Point;

typedef struct{
    Point pt;
    double fuel;
}Car;

double dis(Point p1, Point p2)
{
    return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y));    
} 

void put_in(Car tmp)
{
    printf("tmp x: %.2f;  tmp y: %.2f\n", tmp.pt.x, tmp.pt.y);
    printf("remaining fuel: %.2f\n", tmp.fuel);
}

int move(Car *tmp, Point ter)
{
    double d = dis(tmp -> pt, ter);
    if(d > tmp -> fuel)
        return 0;
    tmp -> pt = ter;
    tmp -> fuel -= d;
    return 1;
}

int main(void)
{
    Car mycar = {{0.0, 0.0}, 90.0};
    while(1)
    {
        int select;
        double tmpx, tmpy;
        Point dest;
        put_in(mycar);
        printf("1: start; 0: stay put: "); scanf("%d", &select);
        if(select != 1)
            break;
        int j;
        puts("1: input destination coordinates.  2: move on x or y axis.");
        printf("j = "); scanf("%d", &j);
        switch(j)
        {
            case 1: 
            printf("dest x: "); scanf("%lf", &dest.x);
            printf("dest y: "); scanf("%lf", &dest.y);
            break;
            case 2:
            printf("tmpx = "); scanf("%lf", &tmpx);
            printf("tmpy = "); scanf("%lf", &tmpy);
            dest.x = mycar.pt.x + tmpx;
            dest.y = mycar.pt.y + tmpy;
            break;
        }
        if(!(move(&mycar, dest)))
            puts("fuel shortage, unable to drive!!!"); 
    }
    return 0;
}

技术分享图片

 

c语言 12 - 5

原文:https://www.cnblogs.com/liujiaxin2018/p/14853921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!