Advertisement
Merkava

Untitled

May 3rd, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4. #include <string.h>
  5. #define N 20
  6. struct city
  7. {
  8. char name_f[80];
  9. char name_p[80];
  10. char date[12];
  11. char type; //1 - âçðîñëûé, 0 - äåòñêèé
  12. union
  13. {
  14. char adult[15];
  15. char child[15];
  16.  
  17. }type1;
  18. struct city *next;
  19. struct city *previous;
  20. };
  21. struct city *head=NULL;
  22. struct city *last=NULL;
  23. struct city *current=NULL; //òåêóùèé ýëåìåíò
  24. struct city *temp=NULL;
  25. struct city *newList=NULL;
  26.  
  27. typedef struct city DataType;
  28. struct list // Îñíîâíàÿ ñòðóêòóðà
  29. {
  30. DataType data;
  31. struct list *next;
  32. };
  33.  
  34. /* äîáàâëåíèå */
  35. void add_name(void)
  36. {
  37. newList = (struct city *) malloc(sizeof(struct city));
  38. printf("Ââåäèòå èìÿ\n");
  39. scanf_s("%s", newList->name_f);
  40. printf("Ââåäèòå íàçâàíèå ïðåäñòàâëåíèÿ\n");
  41. scanf_s("%s", newList->name_p);
  42. printf("Ââåäèòå äàòó\n");
  43. scanf_s("%s", newList->date);
  44. printf("Âûáèðåòå æàíð (1) - âçðîñëûé, (0) - äåòñêèé\n");
  45. scanf_s("%d",&newList->type);
  46. if (newList->type)
  47. {
  48. printf("Ââåäèòå æàíð øîó äëÿ âçðîñëûõ\n");
  49. scanf_s("%s", newList->type1.adult);
  50. }
  51. else
  52. {
  53. printf("Ââåäèòå âîçðàñò øîó äëÿ äåòåé");
  54. scanf_s("%d", newList->type1.child);
  55. }
  56. newList->next = head;
  57. newList->previous = NULL;
  58. head = newList;
  59. if(last == NULL) last = head;
  60. else head->next->previous=head;
  61. return;
  62. }
  63.  
  64. /* Ïðîöåäóðà óäàëåíèÿ ïåðâîãî óçëà */
  65. void delete_unit()
  66. { newList=head;
  67. if(newList != NULL) //åñëè ñïèñîê íå ïóñòîé
  68. {
  69. head=head->next;
  70. free(newList); //óäàëåíèå ýëåìåíòà
  71. if (head!=NULL) //ýëåìåíò íå åäèíñòâåííûé
  72. head->previous=NULL;
  73. else //óäàëèëè åäèíñòâåííûé, ñïèñîê ñòàë ïóñòûì
  74. last = NULL;
  75. }
  76. }
  77. int count_L() /*ïîäñ÷åò ÷èñëà ýëåìåíòîâ â ñïèñêå*/
  78. {
  79. int count=0;
  80. newList = head;
  81. while(newList != NULL)
  82. {
  83. count=count+1;
  84. newList = newList->next;
  85. }
  86. return count;
  87. }
  88. /* Ïðîöåäóðà âûâîäà ñïèñêà ñëåâà íàïðàâî */
  89. void show_list(void)
  90. {
  91. struct city *info;
  92. info = head;
  93. puts(" ___________________________________________________________________");
  94. puts("| | | | |");
  95. puts("| Íàçâàíèå òåàòðà | Íàçâàíèå ïðåäñòàâëåíèÿ | Äàòà | Æàíð |");
  96. puts("|_________________|________________________|________|______________|");
  97. while(info)
  98. {
  99.  
  100. printf("|%1s", info->name_f);
  101. printf("%17s|", info->name_p);
  102. printf("%27s|", info->date);
  103. if (newList->type)
  104. {
  105. printf("18+: %39s|\n", info->type1.adult);
  106. }
  107. else
  108. {
  109. printf("Äåòñêèé: %27s|\n", info->type1.child);
  110. }
  111. info = info->next;
  112. }
  113. }
  114. /* Ïðîöåäóðà âûâîäà ñïèñêà ñïðàâà íàëåâî */
  115. void show_list_1(void)
  116. {
  117. struct city *info;
  118. info = last;
  119. while(info)
  120. {
  121. printf("%s %s %s", info->name_f, info->name_p, info->date);
  122. if (newList->type)
  123. {
  124. printf("18+: %s\n", info->type1.adult);
  125. }
  126. else
  127. {
  128. printf("Äåòñêèé: %d\n", info->type1.child);
  129. }
  130. info=info->previous;
  131. }
  132. }
  133.  
  134.  
  135. //////////////
  136.  
  137. struct list* read_sp(FILE *fp, struct list *begin)
  138. {
  139. struct list *temp, *new_element;
  140. int flag=1, flag2=1;
  141. int count;
  142. if((new_element=(struct list*)calloc(1, sizeof(struct list)))!=NULL)
  143. {
  144. if((fread(&(new_element->data), sizeof(DataType), 1, fp)==0))
  145. {
  146. free(new_element);
  147. return NULL;
  148. }
  149. else
  150. {
  151. begin=new_element;
  152. temp=begin;
  153. count++;
  154. while(1)
  155. {
  156. if ((new_element=(struct list*)calloc(1, sizeof(struct list)))==NULL)
  157. break;
  158. if ((fread(&(new_element->data), sizeof(DataType), 1, fp))==0)
  159. {
  160. free(new_element);
  161. break;
  162. }
  163. count++;
  164. temp->next=new_element;
  165. new_element->next=NULL;
  166. temp=new_element;
  167. }
  168. return begin;
  169. }
  170. }
  171. else
  172. return NULL;
  173. }
  174.  
  175. /////////////////
  176.  
  177. /* Òåëî îñíîâíîé ïðîãðàììû */
  178. int main(int argc, char *argv[])
  179. {
  180. setlocale(LC_ALL,"rus");
  181. char name_f;
  182. char name_p;
  183. int date;
  184. char genre;
  185. int key=-1;
  186.  
  187. ///////////////
  188.  
  189. DataType x;
  190. char *name;
  191. FILE * file; //óêàçàòåëü íà ôàéë
  192. struct list *begin=NULL;
  193. struct list *temp, temp2; //óêàçàòåëü íà ñòðóêòóðó
  194. file=fopen("txt.txt", "a+");
  195. if (argc>1)
  196. { //åñëè ó íàñ áîëåå îäíîãî àðã. â argc
  197. name=argv[1]; //name óêàçûâàåò íà ââåäåííîå â êà÷-âå àðãóìåíòà ôóíêöèè èìÿ ôàéëà
  198. }
  199. else
  200. {
  201. name=(char *)malloc((N+1)*sizeof(char));
  202. printf("Enter the name of file and/or path to file:\n");
  203. scanf("%s",name);
  204. }
  205. if((file=fopen(name,"rb+"))==NULL)
  206. {
  207. printf("This file don't exist, please try again.\n");
  208. if((file=fopen(name,"wb+"))==NULL)
  209. {
  210. printf("Error, file couldn't create\n");
  211. return 1;
  212. }
  213. }
  214. begin=read_sp(file, begin);
  215. if(begin==NULL)
  216. printf("List is empty. Please do something with this, I can't take it anymore\n");
  217. system("PAUSE");
  218.  
  219. ////////////
  220.  
  221. while(key)
  222. {
  223. printf("1. Ââåäèòå íàçâàíèå\n");
  224. printf("2. Óäàëåíèå äâóõ ýëåìåíòîâ\n");
  225. printf("3. Ïîêàçàòü çàïèñè\n");
  226. printf("4. Ïîêàçàòü ñïðàâà íà ëåâî\n");
  227. printf("0. Âûõîä\n");
  228. scanf("%d", &key);
  229. switch(key)
  230. {
  231. case 1:
  232. {
  233. add_name();
  234. break;
  235. }
  236. case 2:
  237. {
  238. int n = count_L();
  239. if(n == 0)
  240. {
  241. printf("Íåâîçìîæíîó óäàëèòü ýëåìåíòû â ñòåêå 0 ýëåìåíòîâ");
  242. }
  243. else if (n == 1)
  244. {
  245. printf("Íåâîçìîæíîó óäàëèòü ýëåìåíòû â ñòåêå 1 ýëåìåíò");
  246. }
  247. else if (n > 2)
  248. {
  249. delete_unit();
  250. delete_unit();
  251. }
  252. break;
  253. }
  254. case 3:
  255. {
  256. printf("Ñïèñîê ñëåâà íàïðàâî\n");
  257. show_list();
  258. break;
  259. }
  260. case 4:
  261. {
  262. printf("Ñïèñîê ñïðàâà íà ëåâî\n");
  263. show_list_1();
  264. break;
  265. }
  266. case 0:
  267. {
  268. printf("Ïîêà\n");
  269. getch();
  270. return;
  271. }
  272. default:
  273. {
  274. printf("Îøèáêà\n");
  275. getch();
  276. break;
  277. }
  278. }
  279. }
  280. temp=begin;
  281. while (temp)
  282. {
  283. fwrite(&(temp->data), sizeof(struct city), 1, file);
  284. temp=temp->next;
  285. }
  286. fclose(file);
  287. while(begin)
  288. {
  289. temp=begin;
  290. begin=begin->next;
  291. free(temp);
  292. }
  293. return 0;
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement