问题:有一个test.data 文件,里面有一个区域内的各个城市元素(例如:学校、饭店等)的分布,文件中每行表示一个城市元素,其中每行的第1列为该城市元素的序号,用整数表示(比如KFC第2分店则序号为2),第2列为该城市元素的类型,用一个字符表示(比如KFC连锁),第3列和第4列分别为该城市元素所在的坐标。若以欧氏距离作为衡量两个城市元素之间距离的标准,对于两个城市元素P和Q,若P和Q之间的欧式距离不大于1,则认为P和Q是邻近的,P和Q互为邻居。请编程解决如下问题:
1、请在控制台中打印“C.1”(即序号为1,类型为C的城市元素)的所有邻居;(5分)
2、请删除所有位于C.1的左侧或者下侧的城市元素,并将删除后的城市元素按照<城市元素类型.序号 X坐标 Y坐标>的方式写入到当前目录下的result.txt中。(5分)
程序设计与问题求解
test.data 内容 如下所示:
article_content A 20.81 3.95
2 A 90.73 80.52
3 A 49.69 9.1
4 A 72.31 25.42
5 A 76.26 19.15
6 A 52.64 75.29
7 A 0.82 17.81
8 A 94.56 91.81
9 A 97.82 73.66
10 A 24.91 9.07
11 A 61.61 70.25
12 A 68.9 66.75
13 A 71.28 56
14 A 67.52 96.85
15 A 95.39 95.5
16 A 69.99 45.72
17 A 77.44 28.27
18 A 94.21 6.06
19 A 6.19 36.55
20 A 55.8 51.17
21 A 5.97 82.13
22 A 41.72 54.49
23 A 4.55 70.53
24 A 21.94 0.49
25 A 97.04 10.16
26 A 65.58 94.51
27 A 89.19 23.49
28 A 73.53 50.56
29 A 35.05 23.98
30 A 9.24 93.69
31 A 41.59 75.46
32 A 30.03 80.75
33 A 49.16 6.93
34 A 97.85 72.55
35 A 23.6 88.5
36 A 85.62 61.27
37 A 17.86 73.73
1 B 43.84 92.14
2 B 19.01 96.59
3 B 93.26 49.6
4 B 58.55 59.71
5 B 56.31 39.75
6 B 25.22 72.79
7 B 37.89 56.37
8 B 70.03 14.2
9 B 41.79 59.67
10 B 49.8 12.04
11 B 22.85 23.3
12 B 55.41 84.65
13 B 2.25 75.97
14 B 91.44 38.96
15 B 29.8 97.4
16 B 56.42 34.46
17 B 61.05 6.22
18 B 13.12 17.54
19 B 98.93 4.65
20 B 27.32 57.21
21 B 30.55 76.05
22 B 35.72 48.97
23 B 93.32 89.86
24 B 24.04 2.41
25 B 64.11 58.55
26 B 41.45 4.5
27 B 20.01 75.25
28 B 60.46 20.54
29 B 36.73 34.36
30 B 94.34 17.29
31 B 58.16 19.59
32 B 29.91 58.91
33 B 83.33 90.76
34 B 11.73 31.67
35 B 43.25 52.49
36 B 15.27 37.14
37 B 32.34 92.59
38 B 81.5 46.8
39 B 87.59 6.44
40 B 91.33 12.39
41 B 51.93 89.04
42 B 74.85 71.25
43 B 55.96 51.92
44 B 14.45 85.81
45 B 45.75 92.58
46 B 80.82 64.57
47 B 10.31 10.03
48 B 99.04 10.96
49 B 46.13 76.81
50 B 65.63 2.56
51 B 78.13 92.36
52 B 59.16 11.62
53 B 43.28 4.31
54 B 72.15 27.42
55 B 93.75 13.66
56 B 32.95 30.29
57 B 43.03 81.95
58 B 65.36 69.13
59 B 44.74 58.66
60 B 2.95 31.41
61 B 76.31 25.92
62 B 85.92 50.67
63 B 71.67 50.7
64 B 26.83 22.31
65 B 93.55 70.54
66 B 0.67 10.78
67 B 40.81 88.77
68 B 34.26 59.57
69 B 29.88 63.23
70 B 27.97 51.65
71 B 79.77 87.03
72 B 92.06 32.09
73 B 59.63 50.53
74 B 21.54 28.35
75 B 44.68 29.92
76 B 66.22 87.47
77 B 73.85 34.22
78 B 26.25 52.21
79 B 6.44 8.49
80 B 39.95 92.58
81 B 77.47 49.84
82 B 33.87 23.21
83 B 12.99 22.17
1 C 12.37 3.1
2 C 71.46 53.71
3 C 90.34 97.65
4 C 38.41 8.43
5 C 37.21 69.4
6 C 86.5 32.69
7 C 31.01 73.32
8 C 77.55 72.35
9 C 45.24 35.73
10 C 48.04 59.59
11 C 2.67 72.21
12 C 39.55 70.93
13 C 97.77 95.43
14 C 74.28 12.65
15 C 80.38 27.81
16 C 39.29 54.42
17 C 98.91 53.34
18 C 15.62 1.3
19 C 82.76 94.21
20 C 71.6 53.72
21 C 23.83 78.62
22 C 39.67 60.68
23 C 13.55 99.18
24 C 57.54 33.09
25 C 37.26 32.68
26 C 55.26 64.26
27 C 13.1 3.39
28 C 51.46 40.16
29 C 63.54 0.53
30 C 55.94 94.91
31 C 23.37 25.77
32 C 28.06 9.4
33 C 30.11 32.08
34 C 33.78 77.66
35 C 57.7 76.25
36 C 89.58 51.03
37 C 64.12 94.31
38 C 35.75 24.05
39 C 32.54 78.87
40 C 57.38 98.59
41 C 37.89 41.19
42 C 75.39 47.69
43 C 72.55 75.02
44 C 41.32 95.52
45 C 1.89 75.46
46 C 37.75 82.23
47 C 38.28 76.69
48 C 30.36 98.07
49 C 50.56 30.26
50 C 30.57 81.47
51 C 65.4 79.87
52 C 75.39 61
53 C 43.1 4.23
54 C 0.14 18.79
55 C 67.63 27.41
1 D 35.41 91.28
2 D 99.74 69.78
3 D 33.91 38.15
4 D 24.66 42.73
5 D 17.27 90
1 E 58.44 79.47
2 E 28.01 85.85
3 E 77.47 78.66
4 E 10.9 77.02
5 E 97.32 10.61
6 E 31.65 27.68
7 E 5.15 50.44
8 E 28.49 17.12
9 E 33.17 7.73
10 E 97.82 65.53
11 E 25.15 78.32
12 E 12.57 6.71
13 E 59.71 35.36
14 E 22.14 96.86
15 E 49.2 31.61
16 E 12.15 31.91
17 E 66.14 9.24
18 E 53.43 24.27
19 E 68.24 30.41
20 E 14.64 65.79
21 E 73 66.44
22 E 27.66 49.64
23 E 91.08 37.84
24 E 61.72 36.93
25 E 71.4 29.45
26 E 6.99 84.24
27 E 74.73 6.9
28 E 25.33 80.12
29 E 66.9 21.3
30 E 26.14 42.12
31 E 56.52 14.04
32 E 27.83 65.71
33 E 98.46 99.74
34 E 61.54 95.91
35 E 97.01 4.22
36 E 48.87 2.76
37 E 93.09 32.03
38 E 92.53 58.7
39 E 50.66 62.93
40 E 15.63 21.87
41 E 73.63 85.21
42 E 25.46 19.75
43 E 24.62 27.91
44 E 57.18 59.24
45 E 83.9 73.32
46 E 57.37 46.94
47 E 4.27 72.52
48 E 9.27 58.96
49 E 38.62 61.95
50 E 51.18 91.34
51 E 76.3 65.81
1 F 81.47 67.65
2 F 56.29 1.92
3 F 21.04 19.16
4 F 97.14 11.32
5 F 77.38 31.21
6 F 4.23 25.18
7 F 42.22 89
8 F 3.96 39.5
9 F 77.14 93.74
10 F 22.71 68.5
11 F 86.39 31.38
12 F 99.08 24.61
13 F 90.68 55.32
14 F 46.07 38.96
15 F 83.61 33.51
16 F 98.57 20.66
17 F 49.76 87.18
18 F 72.34 35.75
19 F 60.98 98.51
20 F 86.16 71.82
21 F 97.58 60.35
22 F 21.66 44.13
23 F 79.85 57.18
24 F 63.82 38.85
25 F 38.47 77.84
26 F 82.85 94.22
27 F 5.55 58.66
28 F 12.27 50.09
29 F 68.57 31.69
30 F 11.24 65.72
31 F 73.09 58.18
32 F 27.71 43.87
33 F 32.63 83.58
34 F 75.42 55.04
35 F 16.66 68.21
36 F 78.52 78.63
37 F 7.58 50.89
38 F 70.93 26.03
39 F 9.72 54.95
40 F 44.76 33.51
41 F 91.51 7.22
42 F 0.49 5.77
43 F 50.66 4.35
44 F 15.11 91.1
45 F 74.91 22.25
46 F 67.18 29.29
47 F 87.27 20.44
48 F 98.73 39.4
49 F 32.64 77.79
50 F 11.49 96.27
51 F 81.76 8.78
52 F 22.19 70.26
53 F 99.32 4.22
54 F 86.64 54.67
55 F 87.74 86.24
56 F 40.37 13.62
57 F 86.93 77.73
58 F 74.21 65.88
59 F 57.69 21.94
60 F 93.21 28.28
61 F 68.47 84.32
62 F 80.12 94.41
63 F 1.03 49.22
64 F 91.36 87.14
65 F 52.16 31.53
66 F 93.39 31.93
67 F 43.46 48.49
68 F 51.75 82.72
69 F 85.03 30.23
70 F 6.46 46.44
71 F 17.71 60.44
72 F 3.36 88.68
73 F 81.87 53.56
74 F 42.37 9.54
75 F 46.23 32.85
76 F 81.72 10.71
77 F 84.58 83.21
78 F 27.8 97.43
79 F 28.66 27.77
80 F 69.23 50.09
81 F 12.86 99.5
82 F 2.51 56.21
83 F 92.85 8.44
84 F 70.16 38.44
85 F 25.98 91.49
86 F 80.84 27.46
87 F 15.48 46.77
88 F 10.57 8.03
89 F 80.96 91.45
90 F 12.54 47.47
91 F 6.86 81.82
92 F 40.91 19.3
93 F 49.81 82.68
94 F 47.36 60.25
95 F 20.69 54.31
96 F 54.06 57.26
97 F 61 56.37
98 F 33.58 18.21
1 G 4.51 55.84
2 G 84.57 17.99
3 G 64.61 59.66
4 G 83.38 45.61
5 G 57.44 51.81
6 G 76.81 22.67
7 G 79.29 27.56
8 G 79.43 61.89
9 G 21.1 79.74
10 G 47.6 71.47
11 G 47.62 84.43
12 G 85.59 42.5
13 G 21.65 75.29
14 G 69.99 81.07
15 G 18.02 35.41
16 G 85 9.41
17 G 33.37 65.13
18 G 91.25 47.23
19 G 53.72 66.6
20 G 57.67 77.85
21 G 22.16 54.26
22 G 15.65 38.61
23 G 68.61 76.51
24 G 65.91 40.77
25 G 5.54 26.22
26 G 58.71 4.21
27 G 36.37 10.42
28 G 99.21 20.07
29 G 70.25 42.07
30 G 96.34 89.32
31 G 89.67 2.31
32 G 27.59 22.03
33 G 66.81 67.41
34 G 89.3 14.16
35 G 36.31 32.19
36 G 8.16 54.5
37 G 22.07 69.74
38 G 49.32 93.36
39 G 68.78 46.98
40 G 73.89 45.14
41 G 9.38 29.23
42 G 75.52 91.8
43 G 76.7 80.8
44 G 73.04 22.95
45 G 65.92 71.18
46 G 76.99 11.65
47 G 70.27 68.36
48 G 88.19 19.85
1 H 96.07 54.98
2 H 65.3 91.17
3 H 5.26 48.21
4 H 49.48 28.63
5 H 18.39 2.06
6 H 10.67 80.07
7 H 9.47 83.07
8 H 62.42 42.42
9 H 68.52 41.81
10 H 70.73 21.99
11 H 88.69 86.39
12 H 56.24 46.67
13 H 48.14 14.71
14 H 76.76 96.86
15 H 3.01 67.72
16 H 54.3 18.11
17 H 54.84 90.2
18 H 12.66 42.47
19 H 30.29 24.27
20 H 73.47 80.41
21 H 40.03 50.75
22 H 13.6 44.8
23 H 77.61 5.16
24 H 1.51 73.37
25 H 45.76 48.74
26 H 48.39 73.96
27 H 60.27 90.32
28 H 77.14 9.68
29 H 98.74 18.63
30 H 43.03 90.55
31 H 71.45 52.62
32 H 25.62 50.67
33 H 47.76 92.56
34 H 25.23 19.27
35 H 70.42 19.95
36 H 12.13 44.25
37 H 68.33 90.32
38 H 81.97 37.93
39 H 72.8 11.44
40 H 69.06 42.99
41 H 13.21 3.39
42 H 51.1 54.27
43 H 19.33 80.34
44 H 57.83 64.52
45 H 13.06 2.98
46 H 43.73 11.67
47 H 81.23 82.93
48 H 8.95 87.4
49 H 25.13 62.92
50 H 97.04 85.05
51 H 79.93 82.23
52 H 2.95 25.82
53 H 36.61 4.14
54 H 5.63 69.95
55 H 8.35 39.2
56 H 36.88 87.3
57 H 49.57 0.05
58 H 77.34 68.06
59 H 1.53 71.35
60 H 31.35 67.42
61 H 34.73 60.7
62 H 6.21 67.43
63 H 76.22 0.46
64 H 63.02 85.4
65 H 81.37 94.56
66 H 12.8 9.87
1 I 64.6 65.95
2 I 17.75 48.29
3 I 2.34 96.26
4 I 29.35 77.35
5 I 99.29 31.71
6 I 71.94 39.98
7 I 2.59 0.03
8 I 69.93 0.57
9 I 71.96 17.87
10 I 68.97 69.54
11 I 68.51 35.3
12 I 40.39 32.95
13 I 43.67 34.17
14 I 59.6 70.55
15 I 53.58 98.12
16 I 37.17 38.06
17 I 92.7 37.33
18 I 15.16 26.22
19 I 14.12 13.83
20 I 17.76 76.92
21 I 33.31 53.33
22 I 17.55 56.52
23 I 97.85 14.47
24 I 35.01 4.06
25 I 18.91 22.88
26 I 62.2 33.73
27 I 53.36 18.46
28 I 68.13 29.3
29 I 25.56 84.8
30 I 4.63 68.17
31 I 36.66 58.8
32 I 23.77 1.17
33 I 94.54 33.88
34 I 47.28 65.26
35 I 84.86 43.71
36 I 86.45 58.13
37 I 0.11 92.04
38 I 36.22 15.18
1 J 87.64 54.13
2 J 46.02 64.36
3 J 45.91 36.76
4 J 15.59 11.64
5 J 79.35 52.32
6 J 44.52 37.47
7 J 39.66 38.58
8 J 45.4 22.96
9 J 15.93 3.87
10 J 93.86 72.51
11 J 29.75 88.36
12 J 26.9 50.84
13 J 74.64 54.14
14 J 83.53 12.65
15 J 88 0.02
16 J 23.6 26.81
17 J 76.31 15.27
18 J 34.07 37.71
19 J 6.86 81.93
20 J 89.27 82.96
21 J 57.89 47.24
22 J 11.55 51
23 J 86.61 33.81
24 J 37.1 5.97
25 J 85.98 71.26
26 J 38.06 43.71
27 J 84.18 70.22
28 J 55.07 99.28
29 J 27.24 95.18
30 J 89.73 91.77
31 J 53.23 2.94
32 J 23.65 79.32
33 J 28.72 17.71
34 J 61.16 24.38
35 J 4.52 7.7
36 J 16.09 34
37 J 14.6 10.9
38 J 14.61 82.51
39 J 76.81 75.9
40 J 64.23 40.83
41 J 17.04 77.56
42 J 26.67 16.74
43 J 61.96 79.89
44 J 42.63 6.1
45 J 60.2 34.79
46 J 10.47 11.68
47 J 92.2 97.5
48 J 29.72 54.96
49 J 23.59 32.22
50 J 22.28 68.9
51 J 72.65 12.72
52 J 1.46 78.29
53 J 55.17 4.06
54 J 29.12 73.28
55 J 55.61 72.56
56 J 22.47 51.34
57 J 30.95 48.9
58 J 74.74 75.68
59 J 76.27 6.91
60 J 17.36 48.83
61 J 75.09 55.06
62 J 64.18 69.73
63 J 97.23 4.41
64 J 41.8 17.09
65 J 81.18 59.66
66 J 58.92 44.59
67 J 53.61 95.33
68 J 68.18 4.32
69 J 76.1 48.38
70 J 61.57 57.9
71 J 50.77 69.35
72 J 50.49 87.64
73 J 12.32 59.13
74 J 80.74 43.3
75 J 22.77 54.05
76 J 78.69 16.67
77 J 14.86 73.46
78 J 93.81 17.59
79 J 48.54 31.73
80 J 81.06 50.76
81 J 62.26 12.02
82 J 92.1 95.95
83 J 13.64 77.88
84 J 97.72 35.61
85 J 80.72 70.42
#include<stdio.h> #include<stdlib.h> #include<math.h> typedef struct m { int num; char a; double x; double y; struct md* next; }kun; int main() { FILE* fp; kun* p,*q; kun* head, * tail; head = tail = NULL; fp = fopen("D:\\test.data", "r+"); if (feof(fp)) { printf("NULL"); return 0; } else { while (!feof(fp))//创建链表 { p = (kun*)malloc(sizeof(kun)); fscanf(fp, "%d %c %lf %lf", &p->num, &p->a, &p->x, &p->y); p->next = NULL; if (head == NULL) head = p; else { tail->next = p; } tail = p; } } //第一问答案 p = head; while (p !=NULL) { printf("%d %c %f %f\n", p->num,p->a,p-> x,p->y); p = p->next; } p = head; while (p != NULL) { if (p->num == 1 && p->a == 'C') break; p = p->next; } q = (kun*)malloc(sizeof(kun)); q = p; p = head; while (p != NULL) { if(sqrt(pow(q->x-p->x,2)+pow(q->y-p->y,2))<=1.0&&p->num!=q->num) printf("%d %c %.2lf %.2lf\n", p->num, p->a, p->x, p->y); p=p->next; } fclose(fp); puts(""); //第二问答案 FILE* f = fopen("D:\\result.txt", "w"); p= head; kun* kk,*k; k = (kun*)malloc(sizeof(kun)); kk = (kun*)malloc(sizeof(kun)); while (p != NULL) { kk=p->next; if (kk->x<q->x||kk->y<q->y) { k = kk->next; p -> next =k; } fprintf(f, "%d %c %.2lf %.2lf\n", p->num, p->a, p->x, p->y); printf("%d %c %.2lf %.2lf\n", p->num, p->a, p->x, p->y); p=p->next; } free(kk); }