c语言 12 - 5

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;
}
复制代码

 

posted @   小鲨鱼2018  阅读(74)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示