#include #include #include typedef enum { ABSOLUTE, DELTA, NUM_INPUT_TYPE } InputType; typedef struct { double x; double y; } Point; typedef struct { Point pt; double fuel; } Car; double calc_dist(Point *p1, Point *p2) { double x2 = (p1->x - p2->x) * (p1->x - p2->x); double y2 = (p1->y - p2->y) * (p1->y - p2->y); return sqrt(x2 + y2); } void get_info(Car *c) { printf("Current Position: (%.2f, %.2f)\nFuel Available: %.2f Liter\n", c->pt.x, c->pt.y, c->fuel ); } int move(Car *c, Point *dest) { double d = calc_dist(&c->pt, dest); if (d > c->fuel) return 0; c->pt = *dest; c->fuel -= d; return 1; } int main(void) { Car myCar = {{0.0,0.0}, 90.0}; Point pt = {0}; while(1) { get_info(&myCar); char c; printf("Continue? [y/n]: "); scanf("%1[a-zA-Z]", &c); if (c == 'n') break; InputType t; bool isValidInput = false; while (!isValidInput) { printf("Select mode [0 = Absolute Position / 1 = Relative Position]: "); scanf("%u", (unsigned int*)&t); if (t < NUM_INPUT_TYPE) isValidInput = true; else puts("Invalid mode!"); } printf("Input x coordinate: "); scanf("%lf", &pt.x); printf("Input y coordinate: "); scanf("%lf", &pt.y); switch (t) { case ABSOLUTE: break; case DELTA: pt.x += myCar.pt.x; pt.y += myCar.pt.y; break; default: break; } int res = move(&myCar, &pt); if (res == 0) { puts("Low Fuel!"); } getchar(); // environment specific fix } return 0; }