/ Published in: C
Expand |
Embed | Plain Text
/* ------------------------------------------------------------------------- + ! ! ! Составить программу, которая содержит динамическую информацию о наличии ! ! автобусов в автобусном парке. ! ! ! ! Сведения о каждом автобусе включают: ! ! - номер автобуса; ! ! - ФИО водителя; ! ! - номер маршрута; ! ! - признак того, где находится автобус - на маршруте или в парке. ! ! ! ! Программа должна обеспечивать: ! ! - начальное формирование данных обо всех автобусах в виде списка: ! ! - при выезде каждого автобуса из парка вводится номер автобуса, ! ! и программа устанавливает значение признака "автобус в парке"; ! ! - при въезде каждого автобуса из парка вводится номер автобуса, ! ! и программа устанавливает значение признака "автобус на маршруте"; ! ! - по запросу выдаютс сведения об автобусах, находящихся в парке, ! ! или об автобусах, находящихся на маршрутах. ! ! ! + ------------------------------------------------------------------------- */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define L_NAME 64 #define IN 0 #define OUT 1 /* ---------------------------- STRUCT NODE ------------------------------ */ struct node { int number, /* (0 <-> 13) */ route, /* (0 <-> 3]) */ is_in_park; /* (IN, OUT) */ char driver[ L_NAME ], conductor[ L_NAME ]; struct node* next; }; /* ------------------------------ LIST ADD ------------------------------- */ struct node* list_add( struct node **p, const int number, const int route, const char* driver ){ struct node *n = malloc( sizeof(struct node) ); if (n == NULL) { puts( "Can't add new item. Something with memory." ); return NULL; } n->next = *p; /* The previous elm (*p) becomes the "next" element. */ *p = n; /* Add new empty element to the head of the list. */ n->number = number; n->route = route; n->is_in_park = 0; strcpy( n->driver, driver ); return *p; } /* ---------------------------- LIST REMOVE ------------------------------ */ /* Remove head. */ void list_remove(struct node **p) { if (*p != NULL) { struct node *n = *p; *p = (*p)->next; free(n); } } /* ------------------------- LIST SEARCH NUMBER -------------------------- */ struct node** list_search_number(struct node **n, const int number) { while (*n != NULL) { if ( (*n)->number == number ) return n; n = &(*n)->next; } return NULL; } /* ----------------------------- LIST PRINT ------------------------------ */ void list_print(struct node *n, const int inout) { puts(""); if (n == NULL) while (n != NULL) { if ( inout == IN && n->is_in_park == OUT ); else if ( inout == OUT && n->is_in_park == IN ); else n->number, n->route, n->driver, n->is_in_park ); n = n->next; } } /* ------------------------- LIST CHANGE INOUT --------------------------- */ void list_inout(struct node *n, const int inout) { int number; struct node *p = n; if ( list_search_number(&n, number) == NULL ) puts("No such bus."); return; p = *(list_search_number(&n, number)); p->is_in_park = inout; } /* -------------------------------- MENU --------------------------------- */ void help() { puts( " +------------------------+" ); puts( " ! pa: print list !" ); puts( " ! co: bus getting out >> !" ); puts( " ! ci: bus getting in << !" ); puts( " ! li: list_inside << !" ); puts( " ! lo: list_outside >> !" ); puts( " ! q : quit !" ); puts( " +------------------------+" ); } /* -------------------------------- MAIN --------------------------------- */ int main(void) { char buffer[ L_NAME ]; struct node *n = NULL; int number; int error = 999; /* forsomefirecase. (c)promt*/ list_add( &n, 1, 1, "V. Pupkin" ); list_add( &n, 2, 3, "G. Zopin" ); list_add( &n, 3, 3, "Mad Max" ); list_add( &n, 4, 2, "<BB>" ); puts( "Print 'h' for help." ); /* Main Loop. */ /* while (1) { */ while (error--) { /* forsomefirecase. (c)promt*/ scanf( "%s", &buffer ); if ( !strcmp(buffer, "pa" ) ) list_print( n, 2 ); else if ( !strcmp(buffer, "co" ) ) list_print( n, OUT ); else if ( !strcmp(buffer, "ci" ) ) list_print( n, IN ); else if ( !strcmp(buffer, "li" ) ) list_inout( n, IN ); else if ( !strcmp(buffer, "lo" ) ) list_inout( n, OUT ); else if ( !strcmp(buffer, "q" ) ) exit (0); else if ( !strcmp(buffer, "h" ) ) help(); else puts( "No such command. See 'h'." ); } return 0; }
You need to login to post a comment.
