Сообщение было отмечено Санек199320 как решение
Решение
Сообщение от Санек199320
Если можно полный рабочий код!
Да не вопрос!
kiev.c
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
| #include <stdio.h>
#include <wchar.h>
#include <assert.h>
#include <locale.h>
#include "list.h"
#include "metro.h"
#include "mcons.h"
typedef wchar_t string_t[256];
#define get_string(s) ( scanf("%255l[^\n]%*c", (s)) == 1 )
int main(void) {
list_t * lines;
line_t * tmpLine;
stantion_t * tmpStantionA, * tmpStantionB;
string_t lineName, fromStantionName, toStantionName;
int m;
assert ( setlocale(LC_ALL, "ru_RU.UTF-8") );
fprintf(stderr, "Create lines list\n");
assert ( lines = new_list() );
fprintf(stderr, "Create line\n");
tmpLine = new_line(L"Святошинско-Броварская");
add_stantion(tmpLine, L"Академгородок");
add_stantion(tmpLine, L"Житомирская");
add_stantion(tmpLine, L"Святошино");
add_stantion(tmpLine, L"Нивки");
add_stantion(tmpLine, L"Берестейская");
add_stantion(tmpLine, L"Шулявская");
add_stantion(tmpLine, L"Политехнический институт");
add_stantion(tmpLine, L"Вокзальная");
add_stantion(tmpLine, L"Университет");
add_stantion(tmpLine, L"Театральная");
add_stantion(tmpLine, L"Крещатик");
add_stantion(tmpLine, L"Арсенальная");
add_stantion(tmpLine, L"Днепр");
add_stantion(tmpLine, L"Гидропарк");
add_stantion(tmpLine, L"Левобережная");
add_stantion(tmpLine, L"Дарница");
add_stantion(tmpLine, L"Черниговская");
add_stantion(tmpLine, L"Лесная");
assert ( push_data(lines, tmpLine) == 0 );
fprintf(stderr, "Create line\n");
tmpLine = new_line(L"Куреневско-Красноармейская");
add_stantion(tmpLine, L"Героев Днепра");
add_stantion(tmpLine, L"Минская");
add_stantion(tmpLine, L"Оболонь");
add_stantion(tmpLine, L"Петровка");
add_stantion(tmpLine, L"Тараса Шевченко");
add_stantion(tmpLine, L"Контрактовая Площадь");
add_stantion(tmpLine, L"Почтовая Площадь");
add_stantion(tmpLine, L"Площадь Независимости");
add_stantion(tmpLine, L"Площадь Льва Толстого");
add_stantion(tmpLine, L"Олимпийская");
add_stantion(tmpLine, L"Дворец \"Украина\"");
add_stantion(tmpLine, L"Лыбидская");
add_stantion(tmpLine, L"Демеевская");
add_stantion(tmpLine, L"Голосеевская");
add_stantion(tmpLine, L"Васильковская");
add_stantion(tmpLine, L"Выставочный Центр");
assert ( push_data(lines, tmpLine) == 0 );
fprintf(stderr, "Create line\n");
tmpLine = new_line(L"Сырецко-Печерская");
add_stantion(tmpLine, L"Сырец");
add_stantion(tmpLine, L"Дорогожичи");
add_stantion(tmpLine, L"Лукьяновская");
add_stantion(tmpLine, L"Золотые Ворота");
add_stantion(tmpLine, L"Дворец спорта");
add_stantion(tmpLine, L"Кловская");
add_stantion(tmpLine, L"Печерская");
add_stantion(tmpLine, L"Дружбы Народов");
add_stantion(tmpLine, L"Выдубичи");
add_stantion(tmpLine, L"Славутич");
add_stantion(tmpLine, L"Осокорки");
add_stantion(tmpLine, L"Позняки");
add_stantion(tmpLine, L"Харьковская");
add_stantion(tmpLine, L"Вырлица");
add_stantion(tmpLine, L"Бориспольская");
add_stantion(tmpLine, L"Красный Хутор");
assert ( push_data(lines, tmpLine) == 0 );
fprintf(stderr, "Linking stantions\n");
assert ( tmpStantionA = find_by_name(line_by_name(lines, L"Святошинско-Броварская"), L"Театральная") );
assert ( tmpStantionB = find_by_name(line_by_name(lines, L"Сырецко-Печерская"), L"Золотые Ворота") );
assert ( link_stantions(tmpStantionA, tmpStantionB) == LS_OK );
fprintf(stderr, "Linking stantions\n");
assert ( tmpStantionA = find_by_name(line_by_name(lines, L"Святошинско-Броварская"), L"Крещатик") );
assert ( tmpStantionB = find_by_name(line_by_name(lines, L"Куреневско-Красноармейская"), L"Площадь Независимости") );
assert ( link_stantions(tmpStantionA, tmpStantionB) == LS_OK );
fprintf(stderr, "Linking stantions\n");
assert ( tmpStantionA = find_by_name(line_by_name(lines, L"Сырецко-Печерская"), L"Дворец спорта") );
assert ( tmpStantionB = find_by_name(line_by_name(lines, L"Куреневско-Красноармейская"), L"Площадь Льва Толстого") );
assert ( link_stantions(tmpStantionA, tmpStantionB) == LS_OK );
fprintf(stderr, "Ready to go...\n");
while ( m = menu() ) {
switch ( m ) {
case M_SHOW_LINES :
print_lines(lines);
break;
case M_SHOW_STANTIONS :
printf("Название линии: ");
if ( ! get_string(lineName) ) {
printf("Попробуйте ещё раз!\n");
break;
}
if ( ! ( tmpLine = line_by_name(lines, lineName) ) ) {
printf("Линия не найдена!\n");
break;
}
print_stantions(tmpLine);
break;
case M_FIND_LINKED :
printf("Станция отправления: ");
if ( ! get_string(fromStantionName) ) {
printf("Ошибка!\n");
break;
}
printf("Станция назначения: ");
if ( ! get_string(toStantionName) ) {
printf("Ошибка!\n");
break;
}
print_linked_stantions(lines, fromStantionName, toStantionName);
break;
default :
printf("Неверный выбор!\n");
break;
}
}
for_each(lines, del_line);
del_list(lines);
return 0;
} |
|
list.c
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
| #include <stdlib.h>
#include <assert.h>
#include "list.h"
node_t * new_node(void * data) {
node_t * node = malloc(sizeof(node_t));
if ( ! node )
return NULL;
node->data = data;
node->next = NULL;
return node;
}
node_t * del_node(node_t * node) {
node_t * next;
if ( ! node )
return NULL;
next = node->next;
free(node);
return next;
}
list_t * new_list(void) {
list_t * list = malloc(sizeof(list_t));
if ( ! list )
return NULL;
list->first = NULL;
list->last = NULL;
return list;
}
int push_data(list_t * list, void * data) {
node_t * node;
if ( ! list )
return -1;
if ( ! ( node = new_node(data) ) )
return -1;
if ( ! list->first )
list->first = node;
else
list->last->next = node;
list->last = node;
return 0;
}
void for_each(list_t * list, void (*func)(void *)) {
node_t * node;
assert ( list );
for ( node = list->first; node; node = node->next )
func(node->data);
}
void del_list(list_t * list) {
assert ( list );
while ( list->first = del_node(list->first) )
;
free(list);
} |
|
list.h
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| #ifndef LIST_H
#define LIST_H 1
typedef struct NODE {
void * data;
struct NODE * next;
} node_t;
extern node_t * new_node(void * data);
extern node_t * del_node(node_t * node);
typedef struct LIST {
node_t * first;
node_t * last;
} list_t;
extern list_t * new_list(void);
extern int push_data(list_t * list, void * data);
extern void for_each(list_t * list, void (*func)(void *));
extern void del_list(list_t * list);
#endif /* LIST_H */ |
|
mcons.c
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| #include <stdio.h>
#include <wchar.h>
#include "list.h"
#include "metro.h"
#include "mcons.h"
void print_line_name(const line_t * line) {
printf("%ls\n", line->l_name);
}
void print_lines(const list_t * lines) {
for_each((list_t*)lines, print_line_name);
}
void print_stantion_name(const stantion_t * stantion) {
printf("%ls\n", stantion->st_name);
}
void print_stantions(const line_t * line) {
for_each(line->l_stantions, print_stantion_name);
}
void print_linked_stantions(const list_t * lines, const wchar_t * fromStantionName, const wchar_t * toStantionName) {
linked_t linkedStantions;
line_t * fromLine, * toLine;
if ( ! ( fromLine = find_parent_line(lines, fromStantionName) ) ) {
printf("Станция %ls не найдена, попробуйте ещё раз.\n", fromStantionName);
return;
}
if ( ! ( toLine = find_parent_line(lines, toStantionName) ) ) {
printf("Станция %ls не найдена, попробуйте ещё раз.\n", toStantionName);
return;
}
switch ( findLinkedStantions(fromLine, toLine, &linkedStantions) ) {
case LS_OK :
printf("Со станции %ls перейдите на станцию %ls.\n", (linkedStantions.atThisLine)->st_name, (linkedStantions.atAnotherLine)->st_name);
break;
case LS_SAMELINE :
printf("Станции находятся на одной линии.\n");
break;
case LS_NOTFOUND :
printf("Используйте наземный транспорт.\n");
break;
default :
fprintf(stderr, "WTF?!!\n");
break;
}
}
void flush_input(void) {
char c;
while ( scanf("%c", &c) == 1 && c != '\n' )
;
}
#define SEPARATOR "**********************************************************************\n"
int menu(void) {
int ret;
printf("\n%s0 - Выход\n1 - Все линии\n2 - Станции на линии...\n3 - Найти станцию пересадки...\n> ", SEPARATOR);
if ( scanf("%d", &ret) != 1 )
ret = -1;
flush_input();
printf("%s\n", SEPARATOR);
return ret;
} |
|
mcons.h
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| #ifndef MCONS_H
#define MCONS_H 1
#include "list.h"
#include "metro.h"
extern void print_line_name(const line_t * line);
extern void print_lines(const list_t * lines);
extern void print_stantion_name(const stantion_t * stantion);
extern void print_stantions(const line_t * line);
extern void print_linked_stantions(const list_t * lines, const wchar_t * fromStantionName, const wchar_t * toStantionName);
enum MENU_COMMANDS { M_EXIT = 0, M_SHOW_LINES = 1, M_SHOW_STANTIONS = 2, M_FIND_LINKED = 3 };
extern int menu(void);
#endif /* MCONS_H */ |
|
metro.c
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
| #include <stdlib.h>
#include <assert.h>
#include <wchar.h>
#include "list.h"
#include "metro.h"
stantion_t * new_stantion(const wchar_t * name, line_t * line) {
stantion_t * stantion;
assert ( name );
assert ( line );
assert ( stantion = malloc(sizeof(stantion_t)) );
assert ( stantion->st_name = wcsdup(name) );
stantion->st_line = line;
stantion->st_linked = NULL;
return stantion;
}
void del_stantion(stantion_t * stantion) {
assert ( stantion );
free(stantion->st_name);
if ( stantion->st_linked )
del_list(stantion->st_linked);
free(stantion);
}
line_t * parent_line(const stantion_t * stantion) {
assert ( stantion );
return stantion->st_line;
}
line_t * new_line(const wchar_t * name) {
line_t * line;
assert ( name );
assert ( line = malloc(sizeof(line_t)) );
assert ( line->l_name = wcsdup(name) );
assert ( line->l_stantions = new_list() );
return line;
}
void add_stantion(line_t * line, const wchar_t * stantionName) {
stantion_t * stantion;
assert ( line );
assert ( push_data(line->l_stantions, new_stantion(stantionName, line)) == 0 );
}
stantion_t * find_by_name(const line_t * line, const wchar_t * stantionName) {
node_t * node;
assert ( line );
assert ( stantionName );
for ( node = line->l_stantions->first; node; node = node->next ) {
stantion_t * stantion = (stantion_t*)(node->data);
if ( wcscmp(stantion->st_name, stantionName) == 0 )
return stantion;
}
return NULL;
}
void del_line(line_t * line) {
assert ( line );
for_each(line->l_stantions, del_stantion);
del_list(line->l_stantions);
free(line->l_name);
free(line);
}
line_t * find_parent_line(const list_t * lines, const wchar_t * stantionName) {
node_t * node;
assert ( lines );
assert ( stantionName );
for ( node = lines->first; node; node = node->next ) {
line_t * line = (line_t*)(node->data);
if ( find_by_name(line, stantionName) )
return line;
}
return NULL;
}
int link_stantions(stantion_t * a, stantion_t * b) {
assert ( a && b );
if ( parent_line(a) == parent_line(b) )
return LS_SAMELINE;
if ( ! a->st_linked )
assert ( a->st_linked = new_list() );
assert ( push_data(a->st_linked, b) == 0 );
if ( ! b->st_linked )
assert ( b->st_linked = new_list() );
assert ( push_data(b->st_linked, a) == 0 );
return LS_OK;
}
int findLinkedStantions(const line_t * thisLine, const line_t * anotherLine, linked_t * linked) {
node_t * thisNode;
assert ( thisLine && anotherLine && linked );
if ( thisLine == anotherLine )
return LS_SAMELINE;
for ( thisNode = thisLine->l_stantions->first; thisNode; thisNode = thisNode->next ) {
stantion_t * thisStantion = (stantion_t*)(thisNode->data);
if ( thisStantion->st_linked ) {
node_t * anotherNode;
for ( anotherNode = thisStantion->st_linked->first; anotherNode; anotherNode = anotherNode->next ) {
stantion_t * anotherStantion = (stantion_t*)(anotherNode->data);
if ( parent_line(anotherStantion) == anotherLine ) {
linked->atThisLine = thisStantion;
linked->atAnotherLine = anotherStantion;
return LS_OK;
}
}
}
}
return LS_NOTFOUND;
}
line_t * line_by_name(list_t * lines, const wchar_t * lineName) {
node_t * node;
assert ( lines && lineName );
for ( node = lines->first; node; node = node->next ) {
line_t * line = (line_t*)(node->data);
if ( wcscmp(line->l_name, lineName) == 0 )
return line;
}
return NULL;
} |
|
metro.h
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| #ifndef METRO_H
#define METRO_H 1
#include <wchar.h>
#include "list.h"
typedef struct STANTION {
wchar_t * st_name;
list_t * st_linked;
struct LINE * st_line;
} stantion_t;
typedef struct LINE {
wchar_t * l_name;
list_t * l_stantions;
} line_t;
extern stantion_t * new_stantion(const wchar_t * name, line_t * line);
extern void del_stantion(stantion_t * stantion);
extern line_t * parent_line(const stantion_t * stantion);
extern line_t * new_line(const wchar_t * name);
extern void add_stantion(line_t * line, const wchar_t * stantionName);
extern stantion_t * find_by_name(const line_t * line, const wchar_t * stantionName);
extern void del_line(line_t * line);
extern line_t * find_parent_line(const list_t * lines, const wchar_t * stantionName);
extern int link_stantions(stantion_t * a, stantion_t * b);
extern line_t * line_by_name(list_t * lines, const wchar_t * lineName);
enum LINK_STATE { LS_OK = 0, LS_SAMELINE = 1, LS_NOTFOUND = 2 };
typedef struct LINKED {
const stantion_t * atThisLine;
const stantion_t * atAnotherLine;
} linked_t;
extern int findLinkedStantions(const line_t * thisLine, const line_t * anotherLine, linked_t * linked);
#endif /* METRO_H */ |
|
Рабочая программа со станциями метро города Киева. Можно адаптировать под другое, но пересадки будет искать не больше одной. В прочем можешь доделать при желании...
И не благодари!
2
|