Advertisement
varun1729

Untitled

Feb 5th, 2023
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.79 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. // Define a structure to represent a seat in the theater
  5. typedef struct node
  6. {
  7. char row_no; // Row number of the seat
  8. int seat_no,pin; // Seat number and unique pin code of the seat
  9. char book; // Book status (a for available, b for booked)
  10. struct node *next,*prev; // Pointers to next and previous nodes in the linked list
  11. }node;
  12.  
  13. node *hn=NULL; // Head pointer for the linked list
  14.  
  15. // Function to create the linked list of seats
  16. void create()
  17. {
  18. node *nn,*cn; // nn: new node, cn: current node
  19. int j=1;
  20. int k=2;
  21. char c ='A';
  22.  
  23. // Loop to create nodes for each row of seats
  24. do
  25. {
  26. int i=1;
  27. // Loop to create nodes for each seat in a row
  28. do
  29. {
  30. k=(kk)/10+100-k/2; // Calculation to generate unique pin codes
  31. nn=(struct node)malloc(sizeof(node)); // Allocate memory for a new node
  32. nn->next=nn->prev=NULL;
  33. nn->row_no=c; // Assign row number to the node
  34. nn->seat_no=i; // Assign seat number to the node
  35. nn->pin=k; // Assign pin code to the node
  36. nn->book='a'; // Set the book status to available
  37.  
  38. // If linked list is empty, make the new node the head node
  39. if(hn==NULL)
  40. {
  41. hn=nn;
  42. nn->next=nn->prev=hn;
  43. }
  44. // If linked list is not empty, add the new node to the end
  45. else
  46. {
  47. cn=hn;
  48.  
  49. // Traverse the linked list to find the last node
  50. while(cn->next!=hn)
  51. cn=cn->next;
  52.  
  53. // Add the new node to the end of the linked list
  54. cn->next=nn;
  55. nn->prev=cn;
  56. nn->next=hn;
  57. hn->prev=nn;
  58. }
  59. i++;
  60. }while(i<=7); // Repeat the loop for each seat in a row
  61. j++;
  62. c++;
  63. }while(j<=10); // Repeat the loop for each row of seats
  64. }
  65.  
  66. // Function to display the seats with their availability status
  67. void display()
  68. {
  69. node *cn; // Current node
  70. cn=hn;
  71.  
  72. // Display the header for Platinum section
  73. printf("------------------------------------------------------------------\n");
  74. printf("| Platinum |\n");
  75.  
  76. // Traverse the linked list to display each seat
  77. while(cn->next!=hn)  // cn->next is not equal to the head node
  78. {
  79.   if((cn->prev)->row_no!=cn->row_no) // If the row number of the previous node is not equal to the current node
  80.     printf("| ");  // Print a "|"
  81.   printf("%c ",cn->row_no);  // Print the row number
  82.   printf("%d ",cn->seat_no);  // Print the seat number
  83.   if(cn->book=='a') // If the seat is booked with 'a'
  84.     printf("\033[32;40m -> a  \033[0m");  // Print green text with black background
  85.   else  // If the seat is booked with 'b'
  86.     printf("\033[1;31;43m -> b  \033[0m");  // Print red text
  87.   if((cn->next)->row_no!=cn->row_no)  // If the row number of the next node is not equal to the current node
  88.     printf("|\n");  // Print a "|" and a new line
  89.   if(cn->row_no=='C'&&cn->seat_no==7)  // If the row number is 'C' and the seat number is 7
  90.   {
  91.     printf("-----------------------------------------------------------------------\n");  // Print a separator line
  92.     printf("|                             gold                               |\n");  // Print a "gold" category label
  93.   }
  94.   if(cn->row_no=='H'&&cn->seat_no==7)  // If the row number is 'H' and the seat number is 7
  95.   {
  96.     printf("----------------------------------------------------------------------\n");  // Print a separator line
  97.     printf("|                            Silver                              |\n");  // Print a "silver" category label
  98.   }
  99.   cn=cn->next;  // Move to the next node
  100. }
  101.  
  102. // Print the details of the last node
  103. printf("%c",cn->row_no);
  104. printf("%d",cn->seat_no);
  105. if(cn->book=='a')
  106.   printf("\033[32;40m -> a  \033[0m");
  107. else
  108.   printf("\033[1;31;43m -> b  \033[0m");
  109. printf("|\n");
  110.  
  111. printf("------------------------------------------------------------------\n\n");
  112. printf("\033[1;33;49mPLATINUM-> 150              GOLD-> 100                  SILVER->60\033[0m\n");
  113. }
  114.  
  115. // Function to display the booking details after the seats have been successfully booked
  116. void display1(node *tmp[20],int n)
  117. {
  118.     if(n != 1) // Check if any seats were actually booked
  119.     {
  120.         printf("------------------------------------------------------------------\n");
  121.         printf("THANK YOU!\nYOU HAVE SUCCESFULLY BOOKED THE SEATS\n");
  122.  
  123.         for(int i = 1; i < n; i++)
  124.         {
  125.             printf("%c %d ", tmp[i]->row_no, tmp[i]->seat_no);
  126.             printf("   PIN : %d \n", tmp[i]->pin);
  127.         }
  128.        
  129.         printf("!!!!!!!!!!!!!!!!!!KEEP PIN SAFELY!!!!!!!!!!!!!!!!!!!\n");
  130.         printf("PINS ARE REQUIRED AT THE TIME OF CANCELATION OF SEATS\n");
  131.         printf("------------------------------------------------------------------\n");
  132.     }
  133. }
  134. void book_seat() {
  135. node *cn, *temp[20];
  136. int n, z, flag;
  137. char row;
  138. int seat;
  139. int ch1;
  140. do {
  141. z = 1;
  142. // Ask user for number of tickets they want to buy
  143. printf("\nEnter the number of tickets you want to buy: ");
  144. scanf("%d", &n);
  145.     // Ask user for the seat numbers
  146.     printf("\nEnter Seat Numbers: \n");
  147.     for (int i = 1; i <= n; i++) {
  148.         printf("NO %d = ", i);
  149.         scanf(" %c", &row);
  150.         scanf("%d", &seat);
  151.        
  152.         // Check if seat is available and book it
  153.         cn = hn;
  154.         while (cn->next != hn) {
  155.             if (cn->row_no == row && cn->seat_no == seat) {
  156.                 if (cn->book == 'a') {
  157.                     cn->book = 'b';
  158.                     temp[z] = cn;
  159.                     z++;
  160.                 } else {
  161.                     printf("INVALID CHOICE!\n");
  162.                     printf("%c%d Seat is already reserved\n", cn->row_no, cn->seat_no);
  163.                 }
  164.             }
  165.             cn = cn->next;
  166.         }
  167.         // Check if the last node is the required seat
  168.         if (cn->row_no == row && cn->seat_no == seat) {
  169.             if (cn->book == 'a') {
  170.                 cn->book = 'b';
  171.                 temp[z] = cn;
  172.                 z++;
  173.             } else {
  174.                 printf("INVALID CHOICE!\n");
  175.                 printf("%c%d Seat is already reserved\n", cn->row_no, cn->seat_no);
  176.             }
  177.         }
  178.     }
  179.     // Display the seats that have been booked successfully
  180.     display1(temp, z);
  181.    
  182.     // Ask user if they want to check seat status, book other seats, or exit
  183.     printf("\n\nPress 1 to check seat status\n");
  184.     printf("Press 2 to book other seats\n");
  185.     printf("Press 3 to exit booking portal\n");
  186.     printf("Input: ");
  187.     scanf("%d", &ch1);
  188.     if (ch1 == 1) {
  189.         // display();
  190.     }
  191. } while (ch1 == 2);
  192. }
  193.  
  194. // Function to cancel a booked seat
  195. void cancel()
  196. {
  197.     // variable to store the row number of the seat to be cancelled
  198.     char row;
  199.     // variable to store the user's choice of action after cancelling a seat
  200.     int ch;
  201.     // variables to store the seat number and pin of the seat to be cancelled
  202.     int seat,pin;
  203.     // pointer to traverse the linked list of seats
  204.     node *cn;
  205.     do
  206.     {
  207.     // initializing ch to 1
  208.     ch=1;
  209.     // setting cn to the head node of the linked list
  210.     cn=hn;
  211.     // Prompting the user to input the seat number
  212.     printf("SEAT NUMBER :");
  213.     // Reading the row number of the seat
  214.     scanf(" %c",&row);
  215.     // Reading the seat number
  216.     scanf("%d",&seat);
  217.     // Prompting the user to input the pin
  218.     printf("PIN :");
  219.     // Reading the pin
  220.     scanf("%d",&pin);
  221.     // Traversing the linked list of seats
  222.     while(cn->next!=hn)
  223.     {
  224.     // If the seat number and pin match, ask the user for confirmation to cancel the seat
  225.     if(cn->row_no==row && cn->seat_no==seat && cn->pin==pin)
  226.     {
  227.         printf("Are you sure u want to cancle the Seat (y/n) ");
  228.         char c;
  229.         scanf(" %c",&c);
  230.         if(c=='y'||c=='Y')
  231.         {
  232.             printf("SEAT CANCELED SUCCESFULLY!\n");
  233.             // changing the booking status of the seat to 'a' (available)
  234.             cn->book='a';
  235.         }
  236.     }
  237.     // If the seat number matches but the pin does not, display an error message
  238.     else if(cn->row_no==row && cn->seat_no==seat && cn->pin!=pin)
  239.     {
  240.         printf("invalid SEAT NUMBER && PIN combination!!!!\n");
  241.     }
  242.     // moving to the next seat
  243.     cn=cn->next;
  244.     }
  245.     // same as above but for the last seat in the linked list
  246.     if(cn->row_no==row && cn->seat_no==seat && cn->pin==pin)
  247.     {
  248.         printf("Are you sure u want to cancle (y/n) ");
  249.         char c;
  250.         scanf("%c",&c);
  251.         if(c=='y'||c=='Y')
  252.         {
  253.             printf("SEAT CANCELED SUCCESFULLY!\n");
  254.             cn->book='a';
  255.         }
  256.     }
  257.     else if(cn->row_no==row && cn->seat_no==seat && cn->pin!=pin)
  258.     {
  259.         printf("invalid SEAT NUMBER && PIN combination!!!!\n");
  260.     }
  261.     // Prompting the user to either check the seat status, cancel more seats, or exit the cancellation portal
  262.     printf("\n\nPRESS 1 To Check Seat Status\n");
  263.     printf("PRESS 2 To Cancle More Tickets\n
  264.    printf("PRESS 3 To Exit CANCELATION PORTAL\n");
  265.    printf("\ninput: ");
  266.    scanf("%d",&ch);
  267.    if(ch==1);
  268.        // display();
  269.    }
  270.    while(ch==2);
  271. }
  272.  
  273.  
  274.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement