Advertisement
xXx_Fortis_xXx

Untitled

May 22nd, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Buyer{
  6.     char *name;
  7.     char *surname;
  8.     char *city;
  9.     char *street;
  10.     int house;
  11.     int flat;
  12.     char *account;
  13.     int avCheqSum;
  14. } Buyer;
  15.  
  16. typedef struct Tree{
  17.     struct Buyer *buyer;
  18.     struct Tree *left;
  19.     struct Tree *right;
  20.     struct Tree *parent;
  21. } Tree;
  22.  
  23. Buyer *createBuyer (){
  24.     Buyer *newBuyer = malloc(sizeof(*newBuyer));
  25.     newBuyer->name = NULL;
  26.     newBuyer->surname = NULL;
  27.     newBuyer->city = NULL;
  28.     newBuyer->street = NULL;
  29.     newBuyer->house = 0;
  30.     newBuyer->flat = 0;
  31.     newBuyer->account = NULL;
  32.     newBuyer->avCheqSum = 0;
  33.     return newBuyer;
  34. }
  35.  
  36. void destroyBuyer (Buyer *buyer){
  37.     free(buyer->name);
  38.     free(buyer->surname);
  39.     free(buyer->city);
  40.     free(buyer->street);
  41.     free(buyer->account);
  42.     free(buyer);
  43. }
  44.  
  45. Buyer *copyBuyer (Buyer *buyer){
  46.     Buyer *newBuyer = createBuyer();
  47.     newBuyer->name = malloc(sizeof(*(newBuyer->name)) * 32);
  48.     newBuyer->surname = malloc(sizeof(*(newBuyer->surname)) * 32);
  49.     newBuyer->city = malloc(sizeof(*(newBuyer->city)) * 32);
  50.     newBuyer->street = malloc(sizeof(*(newBuyer->street)) * 32);
  51.     newBuyer->account = malloc(sizeof(*(newBuyer->account)) * 32);
  52.     strcpy(newBuyer->name, buyer->name);
  53.     strcpy(newBuyer->surname, buyer->surname);
  54.     strcpy(newBuyer->city, buyer->city);
  55.     strcpy(newBuyer->street, buyer->street);
  56.     newBuyer->house = buyer->house;
  57.     newBuyer->flat = buyer->flat;
  58.     strcpy(newBuyer->account, buyer->account);
  59.     newBuyer->avCheqSum = buyer->avCheqSum;
  60.     return newBuyer;
  61. }
  62.  
  63. void saveBuyer (Buyer *buyer, FILE *stream){
  64.     fprintf(stream, "%s\n", buyer->surname);
  65.     fprintf(stream, "%s\n", buyer->name);
  66.     fprintf(stream, "%s\n", buyer->city);
  67.     fprintf(stream, "%s\n", buyer->street);
  68.     fprintf(stream, "%d\n", buyer->house);
  69.     fprintf(stream, "%d\n", buyer->flat);
  70.     fprintf(stream, "%s\n", buyer->account);
  71.     fprintf(stream, "%d\n", buyer->avCheqSum);
  72. }
  73.  
  74. Buyer *loadBuyer (FILE *stream){        //TODO: function for reading string of any length
  75.     if (feof(stream)){
  76.         return NULL;
  77.     }
  78.     Buyer *newBuyer = malloc(sizeof(*newBuyer));
  79.     newBuyer->name = malloc(sizeof(*(newBuyer->name)) * 32);
  80.     newBuyer->surname = malloc(sizeof(*(newBuyer->surname)) * 32);
  81.     newBuyer->city = malloc(sizeof(*(newBuyer->city)) * 32);
  82.     newBuyer->street = malloc(sizeof(*(newBuyer->street)) * 32);
  83.     newBuyer->account = malloc(sizeof(*(newBuyer->account)) * 32);
  84.     fscanf(stream, "%s\n", newBuyer->surname);
  85.     fscanf(stream, "%s\n", newBuyer->name);
  86.     fscanf(stream, "%s\n", newBuyer->city);
  87.     fscanf(stream, "%s\n", newBuyer->street);
  88.     fscanf(stream, "%d\n", &(newBuyer->house));
  89.     fscanf(stream, "%d\n", &(newBuyer->flat));
  90.     fscanf(stream, "%s\n", newBuyer->account);
  91.     fscanf(stream, "%d\n", &(newBuyer->avCheqSum));
  92.     return newBuyer;
  93. }
  94.  
  95. void initBuyer (Buyer *buyer,
  96.                 char *surname,
  97.                 char *name,
  98.                 char *city,
  99.                 char *street,
  100.                 int house,
  101.                 int flat,
  102.                 char *account,
  103.                 int avCheqSum){
  104.     buyer->name = malloc(sizeof(*(buyer->name)) * 32);
  105.     buyer->surname = malloc(sizeof(*(buyer->surname)) * 32);
  106.     buyer->city = malloc(sizeof(*(buyer->city)) * 32);
  107.     buyer->street = malloc(sizeof(*(buyer->street)) * 32);
  108.     buyer->account = malloc(sizeof(*(buyer->account)) * 32);
  109.     strcpy(buyer->surname, surname);
  110.     strcpy(buyer->name, name);
  111.     strcpy(buyer->city, city);
  112.     strcpy(buyer->street, street);
  113.     strcpy(buyer->account, account);
  114.     buyer->house = house;
  115.     buyer->flat = flat;
  116.     buyer->avCheqSum = avCheqSum;
  117. }
  118.  
  119. Tree *createTree (){
  120.     Tree *newTree = malloc(sizeof(*newTree));
  121.     newTree->buyer = NULL;
  122.     newTree->left = NULL;
  123.     newTree->right = NULL;
  124.     newTree->parent = NULL;
  125.     return newTree;
  126. }
  127.  
  128. void addBuyer (Tree *tree, Buyer *buyer){
  129.     if (tree == NULL){
  130.         fprintf(stderr, "addBuyer: tree is NULL\n");
  131.         return;
  132.     }
  133.     if (buyer == NULL){
  134.         fprintf(stderr, "addBuyer: buyer is NULL\n");
  135.         return;
  136.     }
  137.     if (tree->buyer == NULL){
  138.         tree->buyer = buyer;
  139.         return;
  140.     }
  141.     if (strcmp(buyer->surname, tree->buyer->surname) < 0){
  142.         if (tree->left == NULL){
  143.             Tree *newTree = createTree();
  144.             newTree->buyer = buyer;
  145.             newTree->parent = tree;
  146.             tree->left = newTree;
  147.             return;
  148.         }
  149.         addBuyer(tree->left, buyer);
  150.     }
  151.     else{
  152.         if (tree->right == NULL){
  153.             Tree *newTree = createTree();
  154.             newTree->buyer = buyer;
  155.             newTree->parent = tree;
  156.             tree->right = newTree;
  157.             return;
  158.         }
  159.         addBuyer(tree->right, buyer);
  160.     }
  161. }
  162.  
  163. void removeBuyer (Tree *tree, char *keySurname){
  164.     if (tree == NULL){
  165.         return;
  166.     }
  167.     if (tree->buyer == NULL){
  168.         return;
  169.     }
  170.     int t = strcmp(keySurname, tree->buyer->surname);
  171.     if (t < 0){
  172.         removeBuyer (tree->left, keySurname);
  173.     }
  174.     else if (t > 0){
  175.         removeBuyer (tree->right, keySurname);
  176.     }
  177.     else{
  178.         while (tree->right && tree->right->buyer &&
  179.                strcmp(tree->right->buyer->surname, keySurname) == 0){
  180.             tree->right = tree->right->right;
  181.             destroyBuyer(tree->right->parent->buyer);
  182.             free(tree->right->parent);
  183.             tree->right->parent = tree;
  184.         }
  185.         if (tree->left == NULL && tree->right == NULL){
  186.             if (tree == tree->parent->right){
  187.                 tree->parent->right = NULL;
  188.             }
  189.             else{
  190.                 tree->parent->left = NULL;
  191.             }
  192.             destroyBuyer(tree->buyer);
  193.             free(tree);
  194.         }
  195.         else if (tree->left == NULL){
  196.             if (tree == tree->parent->right){
  197.                 tree->parent->right = tree->right;
  198.             }
  199.             else{
  200.                 tree->parent->left = tree->right;
  201.             }
  202.             destroyBuyer(tree->buyer);
  203.             free(tree);
  204.         }
  205.         else if (tree->right == NULL){
  206.             if (tree == tree->parent->right){
  207.                 tree->parent->right = tree->left;
  208.             }
  209.             else{
  210.                 tree->parent->left = tree->left;
  211.             }
  212.             destroyBuyer(tree->buyer);
  213.             free(tree);
  214.         }
  215.         else{
  216.             if (tree->right->left == NULL){
  217.                 if (tree == tree->parent->right){
  218.                     tree->parent->right = tree->right;
  219.                 }
  220.                 else{
  221.                     tree->parent->left = tree->right;
  222.                 }
  223.                 tree->right->parent = tree->parent;
  224.                 tree->right->left = tree->left;
  225.             }
  226.             else{
  227.                 Tree *currTree = tree->right->left;
  228.                 while (currTree->left){
  229.                     currTree = currTree->left;
  230.                 }
  231.                 currTree->parent->left = currTree->right;
  232.                 if (currTree->right){
  233.                     currTree->right->parent = currTree->parent;
  234.                 }
  235.                 if (tree == tree->parent->right){
  236.                     tree->parent->right = currTree;
  237.                 }
  238.                 else{
  239.                     tree->parent->left = currTree;
  240.                 }
  241.                 currTree->right = tree->right;
  242.                 currTree->left = tree->left;
  243.                 currTree->parent = tree->parent;
  244.             }
  245.             destroyBuyer(tree->buyer);
  246.             free(tree);
  247.         }
  248.     }
  249. }
  250.  
  251. Tree *findBuyer (Tree *tree, char *keySurname, int needCopies){
  252.     if (tree == NULL){
  253.         return NULL;
  254.     }
  255.     if (tree->buyer == NULL){
  256.         fprintf(stderr, "findBuyer: buyer is NULL\n");
  257.         return NULL;
  258.     }
  259.     int t = strcmp(keySurname, tree->buyer->surname);
  260.     if (t < 0){
  261.         return findBuyer(tree->left, keySurname, needCopies);
  262.     }
  263.     if (t > 0){
  264.         return findBuyer(tree->right, keySurname, needCopies);
  265.     }
  266.     Tree *newTree = createTree();
  267.     while (tree && tree->buyer &&
  268.            strcmp(keySurname, tree->buyer->surname) == 0){
  269.         if (needCopies){
  270.             addBuyer(newTree, copyBuyer(tree->buyer));
  271.         }
  272.         else{
  273.             addBuyer(newTree, tree->buyer);
  274.         }
  275.         tree = tree->right;
  276.     }
  277.     return newTree;
  278. }
  279.  
  280. void searchBuyerPrivate (Tree *tree, Tree *newTree, int (*cond)(Buyer *),
  281.                          int needCopies){
  282.     if (tree == NULL){
  283.         return;
  284.     }
  285.     if (tree->buyer == NULL){
  286.         return;
  287.     }
  288.     if ((*cond)(tree->buyer)){
  289.         if (needCopies){
  290.             addBuyer(newTree, copyBuyer(tree->buyer));
  291.         }
  292.         else{
  293.             addBuyer(newTree, tree->buyer);
  294.         }
  295.     }
  296.     searchBuyerPrivate(tree->left, newTree, cond, needCopies);
  297.     searchBuyerPrivate(tree->right, newTree, cond, needCopies);
  298. }
  299.  
  300. Tree *searchBuyer (Tree *tree, int (*cond)(Buyer *), int needCopies){
  301.     Tree *newTree = createTree();
  302.     searchBuyerPrivate(tree, newTree, cond, needCopies);
  303.     return newTree;
  304. }
  305.  
  306. void saveTreePrivate (Tree *tree, FILE *stream,
  307.                       void (*saveBuyer)(Buyer *, FILE *)){
  308.     if (tree == NULL){
  309.         return;
  310.     }
  311.     (*saveBuyer)(tree->buyer, stream);
  312.     saveTreePrivate(tree->left, stream, saveBuyer);
  313.     saveTreePrivate(tree->right, stream, saveBuyer);
  314. }
  315.  
  316. void saveTree (Tree *tree, char *path, void (*saveBuyer)(Buyer *, FILE *)){
  317.     FILE *stream = fopen(path, "w");
  318.     if (stream == NULL){
  319.         fprintf(stderr, "saveTree: no such file %s\n", path);
  320.         return;
  321.     }
  322.     saveTreePrivate(tree, stream, saveBuyer);
  323.     fclose(stream);
  324. }
  325.  
  326. Tree *loadTree (char *path, Buyer *(*loadBuyer)(FILE *)){
  327.     FILE *stream = fopen(path, "r");
  328.     if (stream == NULL){
  329.         fprintf(stderr, "loadTree: no such file %s\n", path);
  330.         return NULL;
  331.     }
  332.     Tree *newTree = createTree();
  333.     Buyer *newBuyer;
  334.     while ((newBuyer = (*loadBuyer)(stream)) != NULL){
  335.         addBuyer(newTree, newBuyer);
  336.     }
  337.     fclose(stream);
  338.     return newTree;
  339. }
  340.  
  341. void destroyTree (Tree *tree, int needDataFree){
  342.     if (tree == NULL){
  343.         return;
  344.     }
  345.     destroyTree(tree->left, needDataFree);
  346.     destroyTree(tree->right, needDataFree);
  347.     if (needDataFree && tree->buyer){
  348.         destroyBuyer(tree->buyer);
  349.     }
  350.     free(tree);
  351. }
  352.  
  353. int main (int argc, char **argv){
  354.     Tree *foo = loadTree("dls_r.txt", &loadBuyer);
  355.     Tree *baz = findBuyer(foo, "Putin", 1);
  356.     saveTree(baz, "dls_w2.txt", &saveBuyer);
  357.     destroyTree(baz, 1);
  358.     removeBuyer(foo, "Medvedev");
  359.     Buyer *bar = createBuyer();
  360.     initBuyer(bar, "Ivanov", "Ivan", "Ivanovsk",
  361.               "Ivanovskaya", 13, 666, "5675", 0);
  362.     addBuyer(foo, bar);
  363.     saveTree(foo, "dls_w.txt", &saveBuyer);
  364.     destroyTree(foo, 1);
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement