Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Player
  6. {
  7. char first_name[50],last_name[50];
  8. struct Player *next;
  9.  
  10. }player;
  11.  
  12. typedef struct Team
  13. {
  14. char team_name[50];
  15. struct Team *next_team;
  16. struct Player *next_player;
  17. }team;
  18.  
  19. team *t_head=NULL;
  20. team *t_q,*t_p;
  21. player *p_head=NULL;
  22. player *p_q,*p_p;
  23.  
  24.  
  25. int menu (){
  26. int opt;
  27. printf("\n");
  28. printf(" CURLING TEAM MENU \n");
  29. printf(" 0. Initialise everything \n");
  30. printf(" 1. Add curling team \n");
  31. printf(" 2. Search curling team \n");
  32. printf(" 3. Modify a curling team by name \n");
  33. printf(" 4. Delete a curling team \n");
  34. printf(" 5. Add player \n");
  35. printf(" 6. Search player \n");
  36. printf(" 7. Modify a player \n");
  37. printf(" 8. Delete a player \n");
  38. printf(" 9. Show all \n");
  39. printf(" 10. Move from a curling team to another team \n");
  40. printf(" 11. Exit \n");
  41. printf("\n");
  42. printf("Enter the option:");
  43. scanf("%d",&opt);
  44. return opt;
  45. }
  46.  
  47. void delete_everything()
  48. {
  49. team* to_delete;
  50. t_p=t_head;
  51. while(t_p!=NULL)
  52. {
  53. to_delete=t_p;
  54. to_delete->next_player=p_head;
  55. while(to_delete->next_player!=NULL)
  56. {
  57. p_p=to_delete->next_player;
  58. free(p_p);
  59. p_p=NULL;
  60. to_delete->next_player=to_delete->next_player->next;
  61. }
  62. p_head=NULL;
  63. free(to_delete);
  64. to_delete=NULL;
  65. t_p=t_p->next_team;
  66. }
  67. t_head=NULL;
  68. printf("\n Initialisation complete!");
  69. }
  70.  
  71. void add_team(char *name)
  72. {
  73. t_p=(team *)malloc(sizeof(team));
  74.  
  75. if(t_p==NULL)
  76. {
  77. printf("Can't add the team\n");
  78. return;
  79. }
  80.  
  81. if(t_head!=NULL)
  82. {
  83. strcpy(t_p->team_name,name);
  84. t_q=t_head;
  85. while (t_q!=NULL)
  86. {
  87. if(strcmp(t_p->team_name,t_q->team_name)==0)
  88. {
  89. printf("This team is already in the list\n");
  90. return;
  91. }
  92. t_q=t_q->next_team;
  93. }
  94. }
  95.  
  96. if(t_head==NULL)
  97. {
  98. strcpy(t_p->team_name,name);
  99. t_p->next_team=NULL;
  100. t_p->next_player=NULL;
  101. t_head=t_p;
  102. printf("The new team '%s' was introduced\n",name);
  103. return;
  104. }
  105. strcpy(t_p->team_name,name);
  106. t_q=t_head;
  107. while (t_q->next_team!=NULL)
  108. {
  109. t_q=t_q->next_team;
  110. }
  111. t_q->next_team=t_p;
  112. t_p->next_team=NULL;
  113. t_p->next_player=NULL;
  114. printf("The new team '%s' was introduced\n",name);
  115.  
  116. }
  117.  
  118. team* search_team(char *t_name)
  119. {
  120. t_p=t_head;
  121. int found=0;
  122. while(t_p!=NULL)
  123. {
  124. if( strcmp(t_p->team_name,t_name)==0)
  125. {
  126. found=1;
  127. break;
  128. }
  129. else
  130. {
  131. t_p=t_p->next_team;
  132. }
  133. }
  134.  
  135. if(found==1)
  136. {
  137. // printf("Team %s was found\n",t_name);
  138. return t_p;
  139. }
  140. else
  141. {
  142. // printf("Team %s was not found\n",t_name);
  143. return NULL;
  144. }
  145. }
  146.  
  147.  
  148.  
  149. player* search_player(char *f_name,char *l_name,char *t_name)
  150. {
  151. int found=0;
  152. if(search_team(t_name)==NULL)
  153. {
  154. printf("Team '%s' is not in the list\n",t_name);
  155. return;
  156. }
  157. t_p=search_team(t_name);
  158. p_p=t_p->next_player;
  159. while(p_p!=NULL)
  160. {
  161. if(strcmp(p_p->first_name,f_name)==0 && strcmp(p_p->last_name,l_name)==0)
  162. {
  163. found=1;
  164. break;
  165. }
  166. else p_p = p_p->next;
  167. }
  168. if(found==1)
  169. {
  170. printf("Player '%s %s' is in the team %s\n",f_name,l_name,t_name);
  171. return p_p;
  172. }
  173. else
  174. {
  175. printf("Player '%s %s' is not in the team %s\n",f_name,l_name,t_name);
  176. return NULL;
  177. }
  178. }
  179.  
  180.  
  181. void add_player(char *f_name,char *l_name,char *t_name)
  182. {
  183. t_p=search_team(t_name);
  184. p_p=(player*)malloc(sizeof(player));
  185. p_q=(player*)malloc(sizeof(player));
  186.  
  187. if(t_p==NULL)
  188. {
  189. printf("This team is not in the list\n");
  190. return;
  191. }
  192. if(search_player(f_name,l_name,t_name)!=NULL )
  193. {
  194. printf("This player is already introduced\n");
  195. return;
  196. }
  197. if(t_p->next_player==NULL)
  198. {
  199. t_p->next_player=(player*)malloc(sizeof(player));
  200. strcpy(t_p->next_player->first_name,f_name);
  201. strcpy(t_p->next_player->last_name,l_name);
  202. p_head=t_p->next_player;
  203. t_p->next_player->next=NULL;
  204. printf("The new player was introduced\n");
  205. }
  206. else{
  207. p_p=t_p->next_player;
  208. while(p_p->next != NULL)
  209. {
  210. p_p=p_p->next;
  211. }
  212. strcpy(p_q->first_name,f_name);
  213. strcpy(p_q->last_name,l_name);
  214. p_q->next=NULL;
  215. p_p->next=p_q;
  216. printf("The new player was introduced\n");
  217. }
  218.  
  219. }
  220.  
  221.  
  222.  
  223. void remove_team(char *t_name)
  224. {
  225. team* prev=t_head;
  226. team* del=search_team(t_name);
  227. if(del==NULL)
  228. {
  229. printf("\nTeam was not found");
  230. return;
  231. }
  232. while(del!=NULL){
  233. if(strcmp(t_name,del->team_name)==0)
  234. {
  235. if(del==t_head){
  236. team *aux=t_head;
  237. t_head=t_head->next_team;
  238. free(aux);
  239. }
  240. else{
  241. prev->next_team=del->next_team;
  242. free(del);
  243. del=NULL;
  244. }
  245. }
  246. prev=del;
  247. del=del->next_team;
  248. }
  249. }
  250.  
  251. void modify_team(char *old_name,char *new_name)
  252. {
  253. t_q=search_team(old_name);
  254. if(t_q==NULL)
  255. {
  256. printf("Team %s is not in the list",old_name);
  257. return;
  258. }
  259. strcpy(t_q->team_name,new_name);
  260. printf("Team %s is now %s\n",old_name,new_name);
  261. }
  262.  
  263. void print_teams()
  264. {
  265. t_p=t_head;
  266. while(t_p!=NULL)
  267. {
  268. printf("Team %s: ",t_p->team_name);
  269. printf("\n");
  270. printf("Players of team %s are: \n",t_p->team_name);
  271. //t_p->next_player=p_head;
  272. if(t_p->next_player!=NULL)
  273. {
  274. print_players(t_p->next_player);
  275. }
  276. t_p=t_p->next_team;
  277. }
  278. return;
  279. }
  280.  
  281. void print_players(player *ply){
  282. while(ply!=NULL){
  283. printf("-->%s %s\n",ply->first_name,ply->last_name);
  284. ply=ply->next;
  285. }
  286. }
  287.  
  288. void remove_player(char *f_name,char *l_name,char* t_name)
  289. {
  290. team *ptr = NULL;
  291. team *prev = NULL;
  292. player *remove = NULL;
  293.  
  294. ptr = search_team(t_name);
  295. if(ptr == NULL){
  296. printf("\nSearching %s failed, no such a team\n",t_name);
  297. return;
  298. }
  299. remove = search_player(f_name,l_name,t_name);
  300. if(NULL == remove){
  301. printf("\nThis player doesn't exist");
  302. return;
  303. }
  304. else{
  305. prev = ptr;
  306. while(prev->next_player != NULL && prev -> next_player != remove)
  307. prev = prev->next_player->next;
  308. if(prev->next_player == NULL ){
  309. printf("\nSearching failed.\n");
  310. return;
  311. }
  312. prev->next_player = prev->next_player->next;
  313. }
  314. free(remove);
  315. remove = NULL;
  316. printf("\nPlayer was removed");
  317. }
  318.  
  319. int main()
  320. {
  321. char f_name[20],l_name[20],t_name[50],s_name[50],new_name[50];
  322. int opt;
  323. do{
  324. opt = menu();
  325. switch(opt){
  326. case 0:
  327. delete_everything();
  328. break;
  329. case 1:
  330. printf("Please enter the following:\n");
  331. fflush(stdin);
  332. printf("Team Name: ");
  333. fgets(t_name, 256, stdin);
  334. t_name[strlen(t_name)-1] = '\0';
  335. add_team(t_name);
  336. break;
  337.  
  338. case 2:
  339. printf("Enter the name of the team you want to search:");fflush(stdin);
  340. fgets(s_name,50,stdin);
  341. s_name[strlen(s_name)-1] = '\0';
  342. if(search_team(s_name)==NULL)
  343. printf("Team %s was not found\n",s_name);
  344. else printf("Team %s was found\n",s_name);
  345. break;
  346.  
  347. case 3:
  348. printf("Which team do you want to modify? ");
  349. printf("Enter the team name:");
  350. fflush(stdin);
  351. fgets(t_name,50,stdin);
  352. t_name[strlen(t_name)-1] = '\0';
  353. printf("Enter the new team name:");
  354. fflush(stdin);
  355. fgets(new_name,50,stdin);
  356. new_name[strlen(new_name)-1] = '\0';
  357. modify_team(t_name,new_name);
  358. break;
  359.  
  360. case 4:
  361. printf("Which team do you want to delete? \n");
  362. printf("Enter the name:");
  363. fflush(stdin);
  364. fgets(t_name,50,stdin);
  365. t_name[strlen(t_name)-1] = '\0';
  366. remove_team(t_name);
  367. break;
  368.  
  369. case 5:
  370. printf("\nFirst Name: "); scanf("%s",f_name);
  371. printf("Last Name: "); scanf("%s",l_name);
  372. printf("Where would you like to add the player? (Team name): ");
  373. fflush(stdin);
  374. fgets(t_name,50,stdin);
  375. t_name[strlen(t_name)-1] = '\0';
  376. add_player(f_name,l_name,t_name);
  377. break;
  378.  
  379. case 6:
  380. printf("Which player do you want to search?");
  381. printf("\nFirst Name: "); scanf("%s",f_name);
  382. printf("Last Name: "); scanf("%s",l_name);
  383. printf("Enter the name of the team of the player:");
  384. fflush(stdin);
  385. fgets(t_name,50,stdin);
  386. t_name[strlen(t_name)-1] = '\0';
  387. search_player(f_name,l_name,t_name);
  388. break;
  389.  
  390. case 7:
  391. printf("What is the team,the player belongs? ");
  392. fflush(stdin);
  393. fgets(t_name,50,stdin);
  394. t_name[strlen(t_name)-1] = '\0';
  395. printf("First Name: "); scanf("%s",f_name);
  396. printf("Last Name: "); scanf("%s",l_name);
  397. break;
  398.  
  399. case 8:
  400. printf("From which team is the player? Team Name: ");
  401. fflush(stdin);
  402. fgets(t_name,50,stdin);
  403. t_name[strlen(t_name)-1] = '\0';
  404. printf("\nFirst Name: "); scanf("%s",f_name);
  405. printf("Last Name: "); scanf("%s",l_name);
  406. remove_player(f_name,l_name,t_name);
  407. break;
  408.  
  409. case 9:
  410. print_teams();
  411. break;
  412.  
  413. case 10:
  414.  
  415. break;
  416.  
  417. case 11:
  418. printf("Program terminated\n");
  419. exit(1);
  420. default:
  421. printf("Enter another option:\n");
  422. break;
  423. }
  424. }while(opt>=0 && opt<=50000000);
  425.  
  426. return 0;
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement