#include #include #include #include #define N 20 struct city { char name_f[80]; char name_p[80]; char date[12]; char type; //1 - âçðîñëûé, 0 - äåòñêèé union { char adult[15]; char child[15]; }type1; struct city *next; struct city *previous; }; struct city *head=NULL; struct city *last=NULL; struct city *current=NULL; //òåêóùèé ýëåìåíò struct city *temp=NULL; struct city *newList=NULL; typedef struct city DataType; struct list // Îñíîâíàÿ ñòðóêòóðà { DataType data; struct list *next; }; /* äîáàâëåíèå */ void add_name(void) { newList = (struct city *) malloc(sizeof(struct city)); printf("Ââåäèòå èìÿ\n"); scanf_s("%s", newList->name_f); printf("Ââåäèòå íàçâàíèå ïðåäñòàâëåíèÿ\n"); scanf_s("%s", newList->name_p); printf("Ââåäèòå äàòó\n"); scanf_s("%s", newList->date); printf("Âûáèðåòå æàíð (1) - âçðîñëûé, (0) - äåòñêèé\n"); scanf_s("%d",&newList->type); if (newList->type) { printf("Ââåäèòå æàíð øîó äëÿ âçðîñëûõ\n"); scanf_s("%s", newList->type1.adult); } else { printf("Ââåäèòå âîçðàñò øîó äëÿ äåòåé"); scanf_s("%d", newList->type1.child); } newList->next = head; newList->previous = NULL; head = newList; if(last == NULL) last = head; else head->next->previous=head; return; } /* Ïðîöåäóðà óäàëåíèÿ ïåðâîãî óçëà */ void delete_unit() { newList=head; if(newList != NULL) //åñëè ñïèñîê íå ïóñòîé { head=head->next; free(newList); //óäàëåíèå ýëåìåíòà if (head!=NULL) //ýëåìåíò íå åäèíñòâåííûé head->previous=NULL; else //óäàëèëè åäèíñòâåííûé, ñïèñîê ñòàë ïóñòûì last = NULL; } } int count_L() /*ïîäñ÷åò ÷èñëà ýëåìåíòîâ â ñïèñêå*/ { int count=0; newList = head; while(newList != NULL) { count=count+1; newList = newList->next; } return count; } /* Ïðîöåäóðà âûâîäà ñïèñêà ñëåâà íàïðàâî */ void show_list(void) { struct city *info; info = head; puts(" ___________________________________________________________________"); puts("| | | | |"); puts("| Íàçâàíèå òåàòðà | Íàçâàíèå ïðåäñòàâëåíèÿ | Äàòà | Æàíð |"); puts("|_________________|________________________|________|______________|"); while(info) { printf("|%1s", info->name_f); printf("%17s|", info->name_p); printf("%27s|", info->date); if (newList->type) { printf("18+: %39s|\n", info->type1.adult); } else { printf("Äåòñêèé: %27s|\n", info->type1.child); } info = info->next; } } /* Ïðîöåäóðà âûâîäà ñïèñêà ñïðàâà íàëåâî */ void show_list_1(void) { struct city *info; info = last; while(info) { printf("%s %s %s", info->name_f, info->name_p, info->date); if (newList->type) { printf("18+: %s\n", info->type1.adult); } else { printf("Äåòñêèé: %d\n", info->type1.child); } info=info->previous; } } ////////////// struct list* read_sp(FILE *fp, struct list *begin) { struct list *temp, *new_element; int flag=1, flag2=1; int count; if((new_element=(struct list*)calloc(1, sizeof(struct list)))!=NULL) { if((fread(&(new_element->data), sizeof(DataType), 1, fp)==0)) { free(new_element); return NULL; } else { begin=new_element; temp=begin; count++; while(1) { if ((new_element=(struct list*)calloc(1, sizeof(struct list)))==NULL) break; if ((fread(&(new_element->data), sizeof(DataType), 1, fp))==0) { free(new_element); break; } count++; temp->next=new_element; new_element->next=NULL; temp=new_element; } return begin; } } else return NULL; } ///////////////// /* Òåëî îñíîâíîé ïðîãðàììû */ int main(int argc, char *argv[]) { setlocale(LC_ALL,"rus"); char name_f; char name_p; int date; char genre; int key=-1; /////////////// DataType x; char *name; FILE * file; //óêàçàòåëü íà ôàéë struct list *begin=NULL; struct list *temp, temp2; //óêàçàòåëü íà ñòðóêòóðó file=fopen("txt.txt", "a+"); if (argc>1) { //åñëè ó íàñ áîëåå îäíîãî àðã. â argc name=argv[1]; //name óêàçûâàåò íà ââåäåííîå â êà÷-âå àðãóìåíòà ôóíêöèè èìÿ ôàéëà } else { name=(char *)malloc((N+1)*sizeof(char)); printf("Enter the name of file and/or path to file:\n"); scanf("%s",name); } if((file=fopen(name,"rb+"))==NULL) { printf("This file don't exist, please try again.\n"); if((file=fopen(name,"wb+"))==NULL) { printf("Error, file couldn't create\n"); return 1; } } begin=read_sp(file, begin); if(begin==NULL) printf("List is empty. Please do something with this, I can't take it anymore\n"); system("PAUSE"); //////////// while(key) { printf("1. Ââåäèòå íàçâàíèå\n"); printf("2. Óäàëåíèå äâóõ ýëåìåíòîâ\n"); printf("3. Ïîêàçàòü çàïèñè\n"); printf("4. Ïîêàçàòü ñïðàâà íà ëåâî\n"); printf("0. Âûõîä\n"); scanf("%d", &key); switch(key) { case 1: { add_name(); break; } case 2: { int n = count_L(); if(n == 0) { printf("Íåâîçìîæíîó óäàëèòü ýëåìåíòû â ñòåêå 0 ýëåìåíòîâ"); } else if (n == 1) { printf("Íåâîçìîæíîó óäàëèòü ýëåìåíòû â ñòåêå 1 ýëåìåíò"); } else if (n > 2) { delete_unit(); delete_unit(); } break; } case 3: { printf("Ñïèñîê ñëåâà íàïðàâî\n"); show_list(); break; } case 4: { printf("Ñïèñîê ñïðàâà íà ëåâî\n"); show_list_1(); break; } case 0: { printf("Ïîêà\n"); getch(); return; } default: { printf("Îøèáêà\n"); getch(); break; } } } temp=begin; while (temp) { fwrite(&(temp->data), sizeof(struct city), 1, file); temp=temp->next; } fclose(file); while(begin) { temp=begin; begin=begin->next; free(temp); } return 0; }