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