微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c语言 12

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;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐