c语言医院路线导航代码
时间: 2023-08-12 14:20:18 浏览: 46
以下是一个简单的 C 语言代码示例,可以用于医院的路线导航:
```c
#include <stdio.h>
#include <string.h>
#define MAX_HOSPITALS 100
#define MAX_NAME_LENGTH 50
#define MAX_DESCRIPTION_LENGTH 100
typedef struct {
char name[MAX_NAME_LENGTH];
char description[MAX_DESCRIPTION_LENGTH];
int floor;
int room;
} Hospital;
typedef struct {
Hospital start;
Hospital end;
float distance;
} Path;
void print_hospital(Hospital h) {
printf("%s\n", h.name);
printf("Description: %s\n", h.description);
printf("Floor: %d\n", h.floor);
printf("Room: %d\n", h.room);
}
void print_path(Path p) {
printf("Start: ");
print_hospital(p.start);
printf("End: ");
print_hospital(p.end);
printf("Distance: %.2f meters\n", p.distance);
}
float distance_between_hospitals(Hospital h1, Hospital h2) {
// Here we are just making up a formula for calculating distance between hospitals
float x_diff = h1.floor - h2.floor;
float y_diff = h1.room - h2.room;
return sqrt(x_diff * x_diff + y_diff * y_diff) * 10; // Multiply by 10 to get distance in meters
}
int main() {
Hospital hospitals[MAX_HOSPITALS];
int num_hospitals = 0;
// Add some example hospitals
Hospital h1 = {"St. Mary's Hospital", "General hospital", 2, 205};
hospitals[num_hospitals++] = h1;
Hospital h2 = {"City Hospital", "Specialized in emergency care", 1, 102};
hospitals[num_hospitals++] = h2;
Hospital h3 = {"University Hospital", "Research hospital", 3, 306};
hospitals[num_hospitals++] = h3;
// Ask user for start and end hospitals
printf("Enter start hospital name: ");
char start_input[MAX_NAME_LENGTH];
fgets(start_input, MAX_NAME_LENGTH, stdin);
start_input[strcspn(start_input, "\n")] = '\0'; // Remove newline character
printf("Enter end hospital name: ");
char end_input[MAX_NAME_LENGTH];
fgets(end_input, MAX_NAME_LENGTH, stdin);
end_input[strcspn(end_input, "\n")] = '\0'; // Remove newline character
// Search for start and end hospitals
Hospital start_hospital, end_hospital;
int found_start = 0, found_end = 0;
for (int i = 0; i < num_hospitals; i++) {
if (!found_start && strcmp(start_input, hospitals[i].name) == 0) {
start_hospital = hospitals[i];
found_start = 1;
}
if (!found_end && strcmp(end_input, hospitals[i].name) == 0) {
end_hospital = hospitals[i];
found_end = 1;
}
if (found_start && found_end) {
break;
}
}
// If start or end hospital not found, print error message
if (!found_start) {
printf("Start hospital not found!\n");
return 0;
}
if (!found_end) {
printf("End hospital not found!\n");
return 0;
}
// Calculate distance between start and end hospitals
float distance = distance_between_hospitals(start_hospital, end_hospital);
// Print path information
Path path = {start_hospital, end_hospital, distance};
print_path(path);
return 0;
}
```
该示例中,我们定义了一个 `Hospital` 结构体,其中包含了医院的名称、描述、楼层和房间号。我们还定义了一个 `Path` 结构体,其中包含了起始医院、目标医院和距离。我们使用两个数组来存储多个医院的信息,并通过循环和字符串比较来查找用户输入的起始医院和目标医院。如果找到了对应的医院,我们将计算它们之间的距离,并打印出路径信息。如果没有找到某个医院,则会打印出相应的错误信息。在此示例中,我们只是简单地使用了一个距离计算公式来计算医院之间的距离,实际应用中可能需要更复杂的算法来计算路径。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)