#include #include #include #define CROSSING_SIZE 100 /* 交差点数=100 */ #define MAX_NAME_SIZE 50 /* 最大文字数50文字(半角) */ typedef struct { double x, y; /* 位置 x, y */ } Position; /* 位置を表す構造体 */ typedef struct { int id; /* 交差点番号 */ Position pos; /* 位置を表す構造体 */ double wait; /* 平均待ち時間 */ char jname[MAX_NAME_SIZE]; /* 日本語交差点名 */ /* 名前が二つに*/ char ename[MAX_NAME_SIZE]; /* ローマ字交差点名 */ /* なってます */ int points; /* 交差道路数 */ int next[5]; /* 隣接する交差点番号 */ } Crossing; Crossing cross[CROSSING_SIZE]; int map_read(char *filename) { /* すでに用意してありますので * int map_read(char *filename) のみ * readfile.c からコピーして下さい */ FILE *fp; int i, j; int crossing_number; /* 交差点数 */ fp = fopen(filename, "r"); if (fp == NULL) { printf("File %s is not created\n", filename); return -1; } /* はじめに交差点数を読み込む */ fscanf(fp, "%d", &crossing_number); if ((crossing_number < 1) || (crossing_number >= CROSSING_SIZE)) { printf("Illegal data number (%d)\n", crossing_number); return 0; } for (i = 0; i < crossing_number; i++) { fscanf(fp, "%d,%lf,%lf,%lf,%[^,],%[^,],%d", &(cross[i].id), &(cross[i].pos.x), &(cross[i].pos.y), &(cross[i].wait), cross[i].jname, cross[i].ename, &(cross[i].points)); for (j = 0; j < cross[i].points; j++) fscanf(fp, ",%d", &(cross[i].next[j])); } fclose(fp); /* ファイルから読み込んだ交差点数を返す */ return crossing_number; } void print_cross(int i) { /* i 番目の交差点 の表示関数 好きに改造してください*/ int j; printf("交差点番号:%2d, 座標(%5.2lf,%5.2lf), 名前: %s ( %s ),", cross[i].id, cross[i].pos.x, cross[i].pos.y, cross[i].jname,cross[i].ename); printf("\n 待ち時間:%5.1lf, 隣接交差点 :%d個 ( ", cross[i].wait, cross[i].points); /* 交差道路数だけ繰り返し */ for (j = 0; j < cross[i].points; j++) printf("%d ", cross[i].next[j]); printf(")\n\n"); } /* 最短距離検索 */ void search_min_distance(int crossing_number) { int i; int minindex = 0; /* 暫定トップの位置 */ double dist, mdist; double x, y; printf("検索地点座標を入力してください(%%lf %%lf 形式): "); scanf("%lf %lf", &x, &y); for (i = 0; i < crossing_number; i++) { /* データ最後までループ */ /* 現在の暫定トップの評価数値 */ mdist = hypot(cross[minindex].pos.x-x, cross[minindex].pos.y-y); /* 挑戦者の評価数値 */ dist = hypot(cross[i].pos.x-x, cross[i].pos.y-y); if (dist < mdist) /* 挑戦者の方が近い */ minindex = i; } printf("座標( %.2lf, %.2lf )の地点から最も近い交差点 (距離 %.2lf):\n", x, y, hypot(cross[minindex].pos.x-x, cross[minindex].pos.y-y)); printf("交差点名 %s(%s) 座標 %.2lf,%.2lf 駅からの距離 %.2lf\n", cross[minindex].jname, cross[minindex].ename, cross[minindex].pos.x, cross[minindex].pos.y, hypot(cross[minindex].pos.x, cross[minindex].pos.y)); } int main(void) { int crossing_number; /* 交差点数 */ /* ファイルの読み込み */ crossing_number = map_read("map2.dat"); printf("loaded %d crossings\n", crossing_number); search_min_distance(crossing_number); return 0; }