The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.

Cod - Guest

  • 15th April 2025 08:36:14 PM
  • C
  • 67 views
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. typedef struct page {
  6.     int id;
  7.     char url[50];
  8.     char *description;
  9. } page;
  10.  
  11. typedef struct stack {
  12.     struct page *top;
  13. } stack;
  14.  
  15. typedef struct tab {
  16.     int id;
  17.     struct page *currentPage;
  18.     struct stack *backwardStack;
  19.     struct stack *forwardStack;
  20. } tab;
  21.  
  22. typedef struct List {
  23.     struct tab *data;
  24.     struct List *next;
  25.     struct List *prev;
  26. } List;
  27.  
  28. typedef struct tabsList {
  29.     struct List *sant;
  30. } tabsList;
  31.  
  32. typedef struct browser {
  33.     struct tab *current;
  34.     struct tabsList list;
  35. } browser;
  36.  
  37. void createPage(struct page *p, int id, char *url, char *description) {
  38.     p->id = id;
  39.     strcpy(p->url, url);
  40.     p->description = strdup(description);
  41. }
  42.  
  43. void initTab(struct tab *t) {
  44.     t->id = 0;
  45.     t->currentPage = (struct page *)malloc(sizeof(struct page));
  46.  
  47.     createPage(t->currentPage, 0, "https://acs.pub.ro/", "Computer Science");
  48.     t->backwardStack = (struct stack *)malloc(sizeof(struct stack));
  49.     t->backwardStack->top = NULL;
  50.  
  51.     t->forwardStack = (struct stack *)malloc(sizeof(struct stack));
  52.     t->forwardStack->top = NULL;
  53. }
  54.  
  55. tab* createTab(struct tab *t, int id_tab, int id, char *url, char *description) {
  56.     t->id = id_tab;
  57.     t->currentPage = (struct page *)malloc(sizeof(struct page));
  58.  
  59.     createPage(t->currentPage, id, url, description);
  60.     t->backwardStack = (struct stack *)malloc(sizeof(struct stack));
  61.     t->backwardStack->top = NULL;
  62.  
  63.     t->forwardStack = (struct stack *)malloc(sizeof(struct stack));
  64.     t->forwardStack->top = NULL;
  65.     return t;
  66. }
  67.  
  68. void freeTab(struct tab *t) {
  69.     if (t->currentPage) {
  70.         free(t->currentPage->description);  // Eliberăm memoria descrierii paginii
  71.         free(t->currentPage);               // Eliberăm memoria paginii
  72.     }
  73.     if (t->backwardStack) free(t->backwardStack);  // Eliberăm stiva backward
  74.     if (t->forwardStack) free(t->forwardStack);    // Eliberăm stiva forward
  75. }
  76.  
  77. void createBrowser(struct browser *b) {
  78.     // Initialize the sentinel node
  79.     b->list.sant = (struct List *)malloc(sizeof(struct List));
  80.     b->list.sant->data = NULL;
  81.     b->list.sant->next = b->list.sant;
  82.     b->list.sant->prev = b->list.sant;
  83.  
  84.     // Create the first tab
  85.     struct List *new = (struct List *)malloc(sizeof(struct List));
  86.     new->data = (struct tab *)malloc(sizeof(struct tab));
  87.     createTab(new->data, 0, 0, "https://acs.pub.ro/", "Computer Science");
  88.  
  89.     // Insert the new tab after the sentinel node
  90.     new->next = b->list.sant->next;
  91.     new->prev = b->list.sant;
  92.     b->list.sant->next->prev = new;
  93.     b->list.sant->next = new;
  94.  
  95.     // Set the current tab
  96.     b->current = new->data;
  97. }
  98.  
  99. void freeBrowser(struct browser *b) {
  100.     if (b->list.sant == NULL) return;
  101.  
  102.     struct List *aux = b->list.sant->next;
  103.     while (aux != b->list.sant) {
  104.         struct List *next = aux->next;
  105.         freeTab(aux->data); // Eliberăm tabul asociat nodului
  106.         free(aux);          // Eliberăm nodul din lista dublu înlănțuită
  107.         aux = next;
  108.     }
  109.  
  110.     // Eliberăm memoria pentru sentinelă
  111.     free(b->list.sant);  // Eliberăm santinela
  112.     b->list.sant = NULL;
  113.     b->current = NULL;    // Resetăm tabul curent
  114. }
  115.  
  116. void freePages(struct page pages[51], int n) {
  117.     for (int i = 0; i < n; i++) {
  118.         free(pages[i].description);  // Eliberăm memoria pentru descrierea fiecărei pagini
  119.     }
  120. }
  121.  
  122. void read(FILE *f, struct page pages[51], char comands[501][51], int *n, int *m) {
  123.     int i;
  124.     fscanf(f, "%d\n", n);
  125.     for (i = 0; i < *n; i++) {
  126.         fscanf(f, "%d\n", &pages[i].id);
  127.  
  128.         printf("%d\n", pages[i].id);
  129.  
  130.         fgetc(f);
  131.         fscanf(f, "%s", pages[i].url);
  132.         pages[i].url[strlen(pages[i].url) - 1] = '\0';
  133.  
  134.         printf("%s\n", pages[i].url);
  135.  
  136.         char buffer[501];
  137.         fgets(buffer, 501, f);
  138.         pages[i].description = strdup(buffer);
  139.  
  140.         printf("%s\n", pages[i].description);
  141.     }
  142.  
  143.     fscanf(f, "%d", m);
  144.     fgetc(f);
  145.  
  146.     for (i = 0; i < *m; i++) {
  147.         fgets(comands[i], 51, f);
  148.         comands[i][strcspn(comands[i], "\n")] = 0;
  149.     }
  150. }
  151.  
  152. void NEW_TAB(struct browser *b) {
  153.     struct List *new = (struct List *)malloc(sizeof(struct List));
  154.     new->data = (struct tab *)malloc(sizeof(struct tab));
  155.  
  156.     // Generate a new ID
  157.     int new_id = b->current->id + 1;
  158.  
  159.     // Initialize the new tab
  160.     new->data = createTab(new->data, new_id, 0, "https://acs.pub.ro/", "Computer Science");
  161.  
  162.     if (b->list.sant) {
  163.         if (b->current) {
  164.             // Insert the new tab after the current tab
  165.             struct List *aux = b->list.sant->next;
  166.             while (aux->data != b->current) {
  167.                 aux = aux->next;
  168.             }
  169.  
  170.             new->next = aux->next;
  171.             new->prev = aux;
  172.             aux->next->prev = new;
  173.             aux->next = new;
  174.         } else {
  175.             // If no current tab, insert after the sentinel node
  176.             new->next = b->list.sant->next;
  177.             new->prev = b->list.sant;
  178.             b->list.sant->next->prev = new;
  179.             b->list.sant->next = new;
  180.         }
  181.     } else {
  182.         // If the list is empty, initialize the sentinel node
  183.         new->next = new->prev = new;
  184.         b->list.sant = new;
  185.     }
  186.  
  187.     // Set the new tab as the current tab
  188.     b->current = new->data;
  189.  
  190. }
  191.  
  192. void CLOSE(struct browser *b, FILE *g) {
  193.     struct List *aux = b->list.sant->next;
  194.     while (aux->data != b->current) {
  195.         aux = aux->next;
  196.     }
  197.  
  198.     if(aux->data->id == 0) {
  199.         fprintf(g, "403 Forbidden\n");
  200.     } else {
  201.         // Remove the current tab from the list
  202.         aux->prev->next = aux->next;
  203.         aux->next->prev = aux->prev;
  204.  
  205.         // Set the current tab to the previous one, if it exists
  206.         if (aux->prev != b->list.sant) {
  207.             b->current = aux->prev->data;
  208.         } else {
  209.             b->current = NULL; // No tabs left
  210.         }
  211.  
  212.         // Free the current tab
  213.         freeTab(aux->data);
  214.         free(aux);
  215.     }
  216. }
  217.  
  218. void OPEN(struct browser *b, int id_specif, FILE *g) {
  219.     struct List *aux = b->list.sant->next;
  220.  
  221.     // Traverse the list to find the tab with the specified ID
  222.     do {
  223.         if (aux->data->id == id_specif) {
  224.             b->current = aux->data; // Set the current tab to the one with the specified ID
  225.             return;
  226.         }
  227.         aux = aux->next;
  228.     } while (aux != b->list.sant);
  229.  
  230.     // If the tab with the specified ID was not found
  231.     fprintf(g, "403 Forbidden\n");
  232. }
  233.  
  234. void PRINT(struct browser *b, FILE *g) {
  235.     struct List *temp = b->list.sant->next;
  236.     while (temp->data != b->current) {
  237.          temp = temp->next;
  238.     }
  239.  
  240.     // Print all tabs in the circular doubly linked list
  241.     do {
  242.         fprintf(g, "%d", temp->data->id);
  243.         temp = temp->next;
  244.         if(temp == b->list.sant)
  245.             temp = temp->next;
  246.         if (temp->data->id != b->current->id)
  247.             fprintf(g, " ");
  248.     } while (temp->data->id != b->current->id);
  249.  
  250.     fprintf(g, "\n");
  251.  
  252.     // Print the current tab's page description if it exists
  253.     if (b->current && b->current->currentPage) {
  254.         fprintf(g, "%s\n", b->current->currentPage->description);
  255.     }
  256.  
  257. }
  258.  
  259. int main() {
  260.     FILE *f = fopen("tema1.in", "r");
  261.     FILE *g = fopen("tema1.out", "w");
  262.  
  263.     struct page pages[51];
  264.     char comands[501][51];
  265.     struct browser b; // Declare the browser
  266.     int i, n, m;
  267.  
  268.     // Initialize the browser directly in main
  269.     b.list.sant = (struct List *)malloc(sizeof(struct List));
  270.     b.list.sant->data = NULL;
  271.     b.list.sant->next = b.list.sant;
  272.     b.list.sant->prev = b.list.sant;
  273.     b.current = NULL;
  274.     createBrowser(&b);
  275.  
  276.     read(f, pages, comands, &n, &m);
  277.  
  278.     for (i = 0; i < m; i++) {
  279.         if (strcmp(comands[i], "NEW_TAB") == 0)
  280.             NEW_TAB(&b);
  281.         else if (strcmp(comands[i], "PRINT") == 0)
  282.             PRINT(&b, g);
  283.         else if (strcmp(comands[i], "CLOSE") == 0)
  284.             CLOSE(&b, g);
  285.         else if (strstr(comands[i], "OPEN") != NULL) {
  286.             int id_specif;
  287.             sscanf(comands[i], "OPEN %d", &id_specif);
  288.             OPEN(&b, id_specif, g);
  289.         }
  290.     }
  291.  
  292.     freePages(pages, n);
  293.     freeBrowser(&b);
  294.     fclose(f);
  295.     fclose(g);
  296.     return 0;
  297. }

Raw Paste
Recent Pastes
Æ
  • 1 hour
  • 31 mins
  • 4

Buy fresh based tools
  • 1 hour
  • 44 mins
  • 59

NEW UPDATED 16.04.25 ÇP FOLDERS
  • 1 hour
  • 49 mins
  • 35

Z-content
  • 2 hours
  • 38 mins
  • 17

8Filmai, Lietuviški filmai...
  • 3 hours
  • 33 mins
  • 9

About Us - Terms of Use