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 dist(Point a, Point b) // 计算两点之间距离的程序 { return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); } void put_info(Car x) // 显示car型结构体各个构成元素, 当前的坐标, 当前的燃料 { printf("current position: %.2f %.2f\n", x.pt.x, x.pt.y); printf("remaining fuel: %.2f\n", x.fuel); } int move(Car *now, Point des) // 进行移动的程序,形参为Car型结构体,和Point型结构体 { double d = dist(now -> pt, des); // 利用dist函数计算当前坐标到目标的距离 if(now -> fuel < d) // 如果燃油低于距离,返回0,(1个单位燃料,1个单位距离) return 0; now -> pt = des; // 将当前的坐标改为目标地的坐标 now -> fuel -= d; // 当前的燃油减去距离(耗油量) return 1; } int main(void) { Car begin = {{0.0, 0.0}, 90.0}; // 声明起始位置 while(1) { int choose; Point dest; // 声明目的地结构体 put_info(begin); // 显示当前的坐标,燃料 printf("start -> 1; no star -> 0: "); // 选择是否启动 scanf("%d", &choose); if(choose != 1) break; int method; puts("method = 0:intput destination directly. methon = 1: intput move distantane on x, y axis respectively."); printf("method = "); scanf("%d", &method); if(method == 0) { printf("x-axis dest: "); scanf("%lf", &dest.x); // 输入目的地坐标 printf("y-axis dest: "); scanf("%lf", &dest.y); } if(method == 1) { double xm, ym; printf("xm = "); scanf("%lf", &xm); printf("ym = "); scanf("%lf", &ym); dest.x = xm + begin.pt.x; dest.y = ym + begin.pt.y; } if(!(move(&begin, dest))) // 如果move的返回值为0, 输出燃料不足提示信息。 puts("fuel sortage, unable to drive!!!"); //程序结束的条件时输入非1的数字 } return 0; }
2、直接输入目的地
#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 dist(Point a, Point b) // 计算两点之间距离的程序 { return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); } void put_info(Car x) // 显示car型结构体各个构成元素, 当前的坐标, 当前的燃料 { printf("current position: %.2f %.2f\n", x.pt.x, x.pt.y); printf("remaining fuel: %.2f\n", x.fuel); } int move(Car *now, Point des) // 进行移动的程序,形参为Car型结构体,和Point型结构体 { double d = dist(now -> pt, des); // 利用dist函数计算当前坐标到目标的距离 if(now -> fuel < d) // 如果燃油低于距离,返回0,(1个单位燃料,1个单位距离) return 0; now -> pt = des; // 将当前的坐标改为目标地的坐标 now -> fuel -= d; // 当前的燃油减去距离(耗油量) return 1; } int main(void) { Car begin = {{5.0, 3.0}, 90.0}; // 声明起始位置 while(1) { int choose; Point dest; // 声明目的地结构体 put_info(begin); // 显示当前的坐标,燃料 printf("start -> 1; no star -> 0: "); // 选择是否启动 scanf("%d", &choose); if(choose != 1) break; int method; puts("method = 0:intput destination directly. methon = 1: intput move distantane on x, y axis respectively."); printf("method = "); scanf("%d", &method); if(method == 0) { printf("x-axis dest: "); scanf("%lf", &dest.x); // 输入目的地坐标 printf("y-axis dest: "); scanf("%lf", &dest.y); } if(method == 1) { double xm, ym; printf("xm = "); scanf("%lf", &xm); printf("ym = "); scanf("%lf", &ym); dest.x = xm + begin.pt.x; dest.y = ym + begin.pt.y; } if(!(move(&begin, dest))) // 如果move的返回值为0, 输出燃料不足提示信息。 puts("fuel sortage, unable to drive!!!"); //程序结束的条件时输入非1的数字 } return 0; }
3、输入移动距离
#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 dist(Point a, Point b) // 计算两点之间距离的程序 { return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); } void put_info(Car x) // 显示car型结构体各个构成元素, 当前的坐标, 当前的燃料 { printf("current position: %.2f %.2f\n", x.pt.x, x.pt.y); printf("remaining fuel: %.2f\n", x.fuel); } int move(Car *now, Point des) // 进行移动的程序,形参为Car型结构体,和Point型结构体 { double d = dist(now -> pt, des); // 利用dist函数计算当前坐标到目标的距离 if(now -> fuel < d) // 如果燃油低于距离,返回0,(1个单位燃料,1个单位距离) return 0; now -> pt = des; // 将当前的坐标改为目标地的坐标 now -> fuel -= d; // 当前的燃油减去距离(耗油量) return 1; } int main(void) { Car begin = {{5.0, 3.0}, 90.0}; // 声明起始位置 while(1) { int choose; Point dest; // 声明目的地结构体 put_info(begin); // 显示当前的坐标,燃料 printf("start -> 1; no star -> 0: "); // 选择是否启动 scanf("%d", &choose); if(choose != 1) break; int method; puts("method = 0:intput destination directly. methon = 1: intput move distantane on x, y axis respectively."); printf("method = "); scanf("%d", &method); if(method == 0) { printf("x-axis dest: "); scanf("%lf", &dest.x); // 输入目的地坐标 printf("y-axis dest: "); scanf("%lf", &dest.y); } if(method == 1) { double xm, ym; printf("xm = "); scanf("%lf", &xm); printf("ym = "); scanf("%lf", &ym); dest.x = xm + begin.pt.x; dest.y = ym + begin.pt.y; } if(!(move(&begin, dest))) // 如果move的返回值为0, 输出燃料不足提示信息。 puts("fuel sortage, unable to drive!!!"); //程序结束的条件时输入非1的数字 } return 0; }