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

Cod - Guest

  • 19th April 2025 02:22:26 PM
  • C
  • 53 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 pagesList {
  12.         struct page *data;
  13.         struct pagesList *next;
  14.         struct pagesList *prev;
  15. } pagesList;
  16.  
  17. typedef struct stack {
  18.         struct pagesList *top;
  19. } stack;
  20.  
  21. typedef struct tab {
  22.         int id;
  23.         struct page *currentPage;
  24.         struct stack *backwardStack;
  25.         struct stack *forwardStack;
  26. } tab;
  27.  
  28. typedef struct List {
  29.         struct tab *data;
  30.         struct List *next;
  31.         struct List *prev;
  32. } List;
  33.  
  34. typedef struct tabsList {
  35.         struct List *sant;
  36. } tabsList;
  37.  
  38. typedef struct browser {
  39.         struct tab *current;
  40.         struct tabsList list;
  41. } browser;
  42.  
  43. void createPage(struct page *p, int id, char *url, char *description) {
  44.         p->id = id;
  45.         strcpy(p->url, url);
  46.         p->description = strdup(description);
  47. }
  48.  
  49. tab *createTab(struct tab *t, int id_tab, struct page pages[400]) {
  50.         t->id = id_tab;
  51.  
  52.         // createPage(t->currentPage, pages[0].id, pages[0].url, pages[0].description);
  53.         t->currentPage = &pages[0];
  54.         t->backwardStack = (struct stack *)malloc(sizeof(struct stack));
  55.         t->backwardStack->top = NULL;
  56.  
  57.         t->forwardStack = (struct stack *)malloc(sizeof(struct stack));
  58.         t->forwardStack->top = NULL;
  59.         return t;
  60. }
  61.  
  62. void freeStack(struct stack *s) {
  63.         while (s->top) {
  64.                 struct pagesList *temp = s->top;
  65.                 s->top = s->top->next;
  66.                 free(temp);
  67.         }
  68. }
  69.  
  70. void freeTab(struct tab *t) {
  71.         // if (t->currentPage) {
  72.         //     if (t->currentPage->description) {
  73.         //         free(t->currentPage->description);
  74.         //         t->currentPage->description = NULL;
  75.         //     }
  76.         //     free(t->currentPage);
  77.         //     t->currentPage = NULL;
  78.         // }
  79.         if (t->backwardStack) {
  80.                 freeStack(t->backwardStack);
  81.                 free(t->backwardStack);
  82.                 t->backwardStack = NULL;
  83.         }
  84.         if (t->forwardStack) {
  85.                 freeStack(t->forwardStack);
  86.                 free(t->forwardStack);
  87.                 t->forwardStack = NULL;
  88.         }
  89. }
  90.  
  91. void createBrowser(struct browser *b, struct page pages[400]) {
  92.         // Initialize the sentinel node
  93.         b->list.sant = (struct List *)malloc(sizeof(struct List));
  94.         b->list.sant->data = NULL;
  95.         b->list.sant->next = b->list.sant;
  96.         b->list.sant->prev = b->list.sant;
  97.  
  98.         // Create the first tab
  99.         struct List *new_tab = (struct List *)malloc(sizeof(struct List));
  100.         new_tab->data = (struct tab *)malloc(sizeof(struct tab));
  101.  
  102.         createPage(&pages[0], 0, "https://acs.pub.ro/", "Computer Science");
  103.  
  104.         createTab(new_tab->data, 0, pages);
  105.  
  106.         // Insert the new tab after the sentinel node
  107.         new_tab->next = b->list.sant->next;
  108.         new_tab->prev = b->list.sant;
  109.         b->list.sant->next->prev = new_tab;
  110.         b->list.sant->next = new_tab;
  111.  
  112.         // Set the current tab
  113.         b->current = new_tab->data;
  114. }
  115.  
  116. void freeBrowser(struct browser *b) {
  117.         if (b->list.sant == NULL)
  118.                 return;
  119.  
  120.         struct List *aux = b->list.sant->next;
  121.         while (aux != b->list.sant) {
  122.                 struct List *next = aux->next;
  123.                 freeTab(aux->data);
  124.                 free(aux);
  125.                 aux = next;
  126.         }
  127.  
  128.         free(b->list.sant);
  129.         b->list.sant = NULL;
  130.         b->current = NULL;
  131. }
  132.  
  133. void freePages(struct page pages[400], int n) {
  134.         for (int i = 0; i <= n; i++) {
  135.                 if (pages[i].description) {
  136.                         // printf("eliberez: %d\n%s\n%s\n", pages[i].id,
  137.                         //                              pages[i].url, pages[i].description);
  138.                         free(pages[i].description);
  139.                         pages[i].description = NULL;
  140.                 }
  141.         }
  142. }
  143.  
  144. void read(FILE *f, struct page pages[400], char comands[501][501], int *n,
  145.                                         int *m) {
  146.         int i;
  147.         fscanf(f, "%d\n", n);
  148.         for (i = 1; i <= *n; i++) {
  149.                 fscanf(f, "%d\n", &pages[i].id);
  150.                 fscanf(f, "%s\n", pages[i].url);
  151.  
  152.                 int index = strcspn(pages[i].url, "\n");
  153.                 pages[i].url[index] = '\0';
  154.  
  155.                 char buffer[501];
  156.                 fgets(buffer, 501, f);
  157.                 int ind = strcspn(buffer, "\n");
  158.  
  159.                 // Nu am lasat un '\n' la finalul descrierii
  160.                 buffer[ind] = '\0';
  161.                 pages[i].description = strdup(buffer);
  162.         }
  163.  
  164.         fscanf(f, "%d", m);
  165.         fgetc(f);
  166.  
  167.         for (i = 0; i < *m; i++) {
  168.                 fgets(comands[i], 501, f);
  169.                 comands[i][strcspn(comands[i], "\n")] = 0;
  170.         }
  171. }
  172.  
  173. void NEW_TAB(struct browser *b, int new_id, struct page pages[400]) {
  174.         struct List *new = (struct List *)malloc(sizeof(struct List));
  175.         new->data = (struct tab *)malloc(sizeof(struct tab));
  176.  
  177.         // Initialize the new tab
  178.         new->data = createTab(new->data, new_id, pages);
  179.  
  180.         if (b->list.sant) {
  181.                 if (b->current) {
  182.                         // Insert the new tab after the last tab
  183.                         struct List *aux = b->list.sant->next;
  184.                         while (aux->next != b->list.sant) {
  185.                                 aux = aux->next;
  186.                         }
  187.  
  188.                         new->next = aux->next;
  189.                         new->prev = aux;
  190.                         aux->next->prev = new;
  191.                         aux->next = new;
  192.                 } else {
  193.                         // If no current tab, insert after the sentinel node
  194.                         new->next = b->list.sant->next;
  195.                         new->prev = b->list.sant;
  196.                         b->list.sant->next->prev = new;
  197.                         b->list.sant->next = new;
  198.                 }
  199.         } else {
  200.                 // If the list is empty, initialize the sentinel node
  201.                 new->next = new->prev = new;
  202.                 b->list.sant = new;
  203.         }
  204.  
  205.         // Set the new tab as the current tab
  206.         b->current = new->data;
  207. }
  208.  
  209. void CLOSE(struct browser *b, FILE *g) {
  210.         struct List *aux = b->list.sant->next;
  211.         while (aux->data != b->current) {
  212.                 aux = aux->next;
  213.         }
  214.  
  215.         if (aux->data->id == 0) {
  216.                 fprintf(g, "403 Forbidden\n");
  217.         } else {
  218.                 // Remove the current tab from the list
  219.                 aux->prev->next = aux->next;
  220.                 aux->next->prev = aux->prev;
  221.  
  222.                 // Set the current tab to the previous one, if it exists
  223.                 if (aux->prev != b->list.sant) {
  224.                         b->current = aux->prev->data;
  225.                 } else {
  226.                         b->current = NULL; // No tabs left
  227.                 }
  228.  
  229.                 // Free the current tab
  230.                 freeTab(aux->data);
  231.                 free(aux);
  232.         }
  233. }
  234.  
  235. void OPEN(struct browser *b, int id_specif, FILE *g) {
  236.         struct List *aux = b->list.sant->next;
  237.  
  238.         // Traverse the list to find the tab with the specified ID
  239.         do {
  240.                 if (aux->data->id == id_specif) {
  241.                         b->current =
  242.                                         aux->data; // Set the current tab to the one with the specified ID
  243.                         return;
  244.                 }
  245.                 aux = aux->next;
  246.         } while (aux != b->list.sant);
  247.  
  248.         // If the tab with the specified ID was not found
  249.         fprintf(g, "403 Forbidden\n");
  250. }
  251.  
  252. void NEXT(struct browser *b, FILE *g) {
  253.         struct List *aux = b->list.sant->next;
  254.  
  255.         while (aux->data != b->current) {
  256.                 aux = aux->next;
  257.         }
  258.  
  259.         if (aux->next != b->list.sant) {
  260.                 aux = aux->next;
  261.                 b->current = aux->data;
  262.         } else {
  263.                 aux = b->list.sant->next;
  264.                 b->current = aux->data;
  265.         }
  266. }
  267.  
  268. void PREV(struct browser *b, FILE *g) {
  269.         struct List *aux = b->list.sant->next;
  270.  
  271.         while (aux->data != b->current) {
  272.                 aux = aux->next;
  273.         }
  274.  
  275.         if (aux->prev != b->list.sant) {
  276.                 aux = aux->prev;
  277.                 b->current = aux->data;
  278.         } else {
  279.                 aux = b->list.sant->prev;
  280.                 b->current = aux->data;
  281.         }
  282. }
  283.  
  284. void push(struct stack *s, struct page *p) {
  285.         struct pagesList *new = (struct pagesList *)malloc(sizeof(struct pagesList));
  286.         new->data = p;
  287.         new->next = s->top;
  288.         s->top = new;
  289. }
  290.  
  291. struct page *pop(struct stack *s) {
  292.         if (s->top == NULL)
  293.                 return NULL; // Return an empty page if the stack is empty
  294.         struct pagesList *temp = s->top;
  295.         struct page *p = temp->data;
  296.         s->top = s->top->next;
  297.         free(temp);
  298.         return p;
  299. }
  300.  
  301. void PAGE(struct browser *b, FILE *g, int id_specif, struct page pages[400],
  302.                                         int *n) {
  303.         int i, ind = -1;
  304.         for (i = 1; i <= *n; i++) {
  305.  
  306.                 if (id_specif == pages[i].id)
  307.                         ind = i;
  308.         }
  309.  
  310.         if (ind != -1) {
  311.                 push(b->current->backwardStack, b->current->currentPage);
  312.                 b->current->currentPage = &pages[ind];
  313.                 freeStack(b->current->forwardStack);
  314.         } else {
  315.                 fprintf(g, "403 Forbidden\n");
  316.         }
  317. }
  318.  
  319. void BACKWARD(struct browser *b, FILE *g) {
  320.         struct page *p = pop(b->current->backwardStack);
  321.         if (p) {
  322.                 push(b->current->forwardStack, b->current->currentPage);
  323.                 b->current->currentPage = p;
  324.         } else {
  325.                 fprintf(g, "403 Forbidden\n");
  326.         }
  327. }
  328.  
  329. void FORWARD(struct browser *b, FILE *g) {
  330.         struct page *p = pop(b->current->forwardStack);
  331.         if (p) {
  332.                 push(b->current->backwardStack, b->current->currentPage);
  333.                 b->current->currentPage = p;
  334.         } else {
  335.                 fprintf(g, "403 Forbidden\n");
  336.         }
  337. }
  338.  
  339. void PRINT(struct browser *b, FILE *g) {
  340.         struct List *temp = b->list.sant->next;
  341.         while (temp->data != b->current) {
  342.                 temp = temp->next;
  343.         }
  344.  
  345.         // Print all tabs in the circular doubly linked list
  346.         do {
  347.                 fprintf(g, "%d", temp->data->id);
  348.                 temp = temp->next;
  349.                 if (temp == b->list.sant)
  350.                         temp = temp->next;
  351.                 if (temp->data->id != b->current->id)
  352.                         fprintf(g, " ");
  353.         } while (temp->data->id != b->current->id);
  354.  
  355.         fprintf(g, "\n");
  356.  
  357.         // Print the current tab's page description if it exists
  358.         if (b->current && b->current->currentPage) {
  359.                 fprintf(g, "%s\n", b->current->currentPage->description);
  360.         }
  361. }
  362.  
  363. int is_empty(struct stack *s) { return s->top == NULL; }
  364.  
  365. void inv_stack(struct stack *s, struct stack *inv) {
  366.         struct stack *temp = (struct stack *)malloc(sizeof(struct stack));
  367.         temp->top = NULL;
  368.  
  369.         while (!is_empty(s)) {
  370.                 push(inv, pop(s));
  371.                 push(temp, pop(s));
  372.         }
  373.  
  374.         while (!is_empty(temp)) {
  375.                 push(s, pop(temp));
  376.         }
  377.         free(temp);
  378. }
  379.  
  380. void PRINT_HISTORY(struct browser *b, FILE *g, int id_specif) {
  381.         struct List *temp = b->list.sant->next;
  382.         while (temp->data->id != id_specif && temp->next != b->list.sant) {
  383.                 temp = temp->next;
  384.         }
  385.  
  386.         if (temp->data->id == id_specif) {
  387.                 struct stack *inv = (struct stack *)malloc(sizeof(struct stack));
  388.                 inv->top = NULL;
  389.  
  390.                 inv_stack(temp->data->forwardStack, inv);
  391.  
  392.                 while (!is_empty(inv))
  393.                         fprintf(g, "%s\n", pop(inv)->url);
  394.  
  395.                 free(inv);
  396.                 fprintf(g, "%s\n", temp->data->currentPage->url);
  397.  
  398.                 struct pagesList *aux = temp->data->backwardStack->top;
  399.                 while (aux != NULL) {
  400.                         fprintf(g, "%s\n", aux->data->url);
  401.                         aux = aux->next;
  402.                 }
  403.  
  404.         } else {
  405.                 fprintf(g, "403 Forbidden\n");
  406.         }
  407. }
  408.  
  409. int main() {
  410.         FILE *f = fopen("tema1.in", "r");
  411.         FILE *g = fopen("tema1.out", "w");
  412.  
  413.         struct page pages[400];
  414.         char comands[501][501];
  415.         struct browser b; // Declare the browser
  416.         int i, n, m, id_specif, new_id = 0;
  417.  
  418.         // Initialize the browser directly in main
  419.         createBrowser(&b, pages);
  420.  
  421.         read(f, pages, comands, &n, &m);
  422.  
  423.         for (i = 0; i < m; i++) {
  424.                 if (strcmp(comands[i], "NEW_TAB") == 0) {
  425.                         new_id++;
  426.                         NEW_TAB(&b, new_id, pages);
  427.                 } else if (strcmp(comands[i], "PRINT") == 0)
  428.                         PRINT(&b, g);
  429.                 else if (strcmp(comands[i], "CLOSE") == 0)
  430.                         CLOSE(&b, g);
  431.                 else if (strstr(comands[i], "OPEN")) {
  432.                         sscanf(comands[i], "OPEN %d", &id_specif);
  433.                         OPEN(&b, id_specif, g);
  434.                 } else if (strstr(comands[i], "NEXT")) {
  435.                         NEXT(&b, g);
  436.                 } else if (strstr(comands[i], "PREV")) {
  437.                         PREV(&b, g);
  438.                 } else if (strstr(comands[i], "PAGE")) {
  439.                         sscanf(comands[i], "PAGE %d", &id_specif);
  440.                         PAGE(&b, g, id_specif, pages, &n);
  441.                 } else if (strstr(comands[i], "BACKWARD")) {
  442.                         BACKWARD(&b, g);
  443.                 } else if (strstr(comands[i], "FORWARD")) {
  444.                         FORWARD(&b, g);
  445.                 } else if (strstr(comands[i], "PRINT_HISTORY")) {
  446.                         sscanf(comands[i], "PRINT_HISTORY %d", &id_specif);
  447.                         PRINT_HISTORY(&b, g, id_specif);
  448.                 }
  449.         }
  450.  
  451.         freeBrowser(&b);
  452.         freePages(pages, n);
  453.         fclose(f);
  454.         fclose(g);
  455.         return 0;
  456. }

Raw Paste
Recent Pastes
NEW UPDATED 19.04.25 ÇP FOLDERS
  • 1 hour
  • 12 mins
  • 1

Mediacdn.ru - iptv v1
  • 2 hours
  • 10 mins
  • 22

Ñéw folder 19-04
  • 2 hours
  • 41 mins
  • 57

Face stripping PRIMIUM
  • 2 hours
  • 51 mins
  • 35

Клининг СПб
  • 3 hours
  • 24 mins
  • 19

About Us - Terms of Use