Advertisement
Hamikadze

Untitled

Apr 28th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #include "txt.h"
  2. #include "list.h"
  3. #include <iostream>
  4.  
  5. void setField(struct elem *&lst, struct txt* field) {
  6.     lst->field = field;
  7. }
  8.  
  9. void setNext(struct elem *&lst, struct elem* next) {
  10.     lst->next = next;
  11. }
  12.  
  13. struct elem* getNext(struct elem *&lst) {
  14.     return lst->next;
  15. }
  16.  
  17. struct txt* getField(struct elem *&lst) {
  18.     return lst->field;
  19. }
  20.  
  21. struct elem* init(struct txt *field) {
  22.     struct elem *lst;
  23.     lst = (struct elem*)malloc(sizeof(struct elem));
  24.     setField(lst, field);
  25.     setNext(lst, NULL);
  26.     return lst;
  27. }
  28.  
  29. void deletelst(struct elem* &h) {
  30.     struct elem *tmp;
  31.     while (h != NULL) {
  32.         tmp = getNext(h);
  33.         free(h);
  34.         h = tmp;
  35.     }
  36. }
  37.  
  38. int compareWthField(struct txt*& f1, struct txt *&f2) {
  39.     //std::cout << "___COMPARE\n";
  40.     int a = 0;
  41.     if (getLen(f1) > getLen(f2))
  42.         a = getLen(f1);
  43.     else
  44.         a = getLen(f2);
  45.     if (getLen(f1) == getLen(f2)) {
  46.         //std::cout << getLen(f2);
  47.         for (int i = 0; i < a; i++) if (f1->a[i] != f2->a[i])return 0;
  48.     }
  49.     else return 0;
  50.     return 1;
  51. }
  52.  
  53. struct elem* inElem(struct elem *h, struct txt* field) {
  54.     struct elem *tmp = h, *prev = nullptr, *last, *g;
  55.     int i = 0;
  56.  
  57.     //std::cout << "ELEM:" << i;
  58.  
  59.     struct txt *str = (struct txt*)malloc(sizeof(struct txt));
  60.     char c[20] = { '`' }, *t = (char*)malloc(sizeof(char));
  61.     for (int i = 0; i < 20; i++) {
  62.         c[i] = '`';
  63.     }
  64.     std::cout << "\nIn elem:";
  65.     std::cin >> c;
  66.  
  67.     i = 0;
  68.     while (c[i] != '`') {
  69.         i++;
  70.     }
  71.     --i;
  72.  
  73.     int len = 0;
  74.     t = (char*)malloc(sizeof(char)*i);
  75.     //inFile >> c;
  76.     for (; len < i; len++) {
  77.         t[len] = c[len];
  78.     }
  79.  
  80.     for (int j = 0; j < i; j++) {
  81.         std::cout << t[j];
  82.     }
  83.     setLen(str, len);
  84.     setA(str, t);
  85.     //std::cout << "ELEM:" << news->field->a[0];
  86.  
  87.     while (tmp != nullptr)
  88.     {
  89.         if (compareWthField(tmp->field, field))
  90.         {
  91.             struct elem *news = static_cast<struct elem*>(malloc(sizeof(struct elem)));
  92.             setField(news, str);
  93.             if (!prev)
  94.             {
  95.                 news->next = tmp;
  96.                 h = tmp = news;
  97.                 tmp = getNext(tmp);
  98.                 if (tmp == nullptr)
  99.                     break;
  100.             }
  101.             else {
  102.                 prev->next = news;
  103.                 news->next = tmp;
  104.             }
  105.         }
  106.         prev = tmp;
  107.         tmp = getNext(tmp);
  108.  
  109.         i++;
  110.     }
  111.  
  112.     //news->next = g;
  113.     //tmp = news;
  114.  
  115.     return h;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement