Advertisement
utroz

ElementsManager

Jul 10th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. /*  Elements Manager (v 0.01)
  2.  *  Copyright ©2011 - @uthor #Utroz(RsC)#.
  3.  *  File: Elements.c
  4.  *  Blog: http://Gcoders.wordpress.com/ (Access it)!
  5. */
  6.  
  7. /*  How to Use:
  8.     ----------------------
  9.     add_Element(5); //Add Elements.
  10.     print_Elements(); //Print All Elements.
  11.     chg_Element(4); // Search for a element and change it.
  12.     del_Element(5); // Delete a Element.
  13.     size_Elements(); // Return amount of elements.
  14.     ----------------------------
  15.  
  16.     Print Example:
  17.     -------------
  18.     Pos | Value
  19.     [0]:    5
  20.     [1]:    4
  21.     [2]:    6
  22.     [3]:    2
  23.     -------------
  24. */
  25.  
  26. #include <stdio.h>
  27.  
  28. // Start Array.
  29. int array[1];
  30.  
  31. //Size Array.
  32. static int count = 0;
  33.  
  34. //Resize Array.
  35. void res_Elements(int signal) // 0 = Positive / 1 = Negative.
  36. {
  37.     int new_Array[count], i;
  38.  
  39.     if(signal == 0) ++count;
  40.     else --count;  
  41.  
  42.     for(i = 0; i < count; i++)
  43.     {
  44.         new_Array[i] = array[i];
  45.     }
  46.  
  47.     *array = *new_Array;
  48. }
  49.  
  50. //Change a Element.
  51. void chg_Element(int arg)
  52. {
  53.     int search = sch_Element(arg);
  54.  
  55.     if(search != 0)
  56.     {
  57.         printf("Please choose a new value:\n");
  58.         scanf("%d", &array[search]);
  59.     }
  60. }
  61.  
  62. // Add a Element.
  63. void add_Element(int arg)
  64. {
  65.     array[count] = arg;
  66.     res_Elements(0);
  67. }
  68.  
  69. // Search a Element.
  70. int sch_Element(int arg)
  71. {
  72.     int i, search = 0;
  73.  
  74.     for(i = 0; i < count; i++)
  75.     {
  76.         if(array[i] == arg)
  77.         {
  78.             printf("This element: [%d] was found sucessfully!\n", arg);
  79.             search = 1;
  80.             return i;
  81.         }
  82.     }
  83.  
  84.     if(search != 1)
  85.     {
  86.         printf("Sorry this element: [%d] don't was founded!\n", arg);
  87.     }
  88.     return 0;
  89. }
  90.  
  91.  
  92. // Sort All Elements.
  93. void org_Elements(void)
  94. {
  95.     int i, pos;
  96.  
  97.     for(i = 0; i < count; i++)
  98.     {
  99.         if(array[i] == 0)
  100.         {
  101.             pos = 1;
  102.             continue;
  103.         }
  104.  
  105.         if(pos == 1)
  106.         {
  107.             array[i-1] = array[i];
  108.             array[i] = 0;
  109.         }
  110.     }
  111. }
  112.  
  113. // Remove a Element.
  114. void del_Element(int arg)
  115. {
  116.     int i, search = 0;
  117.  
  118.     for(i = 0; i < count; i++)
  119.     {
  120.         if(array[i] == arg)
  121.         {
  122.             printf("This element: [%d] was removed sucessfully!\n", array[i]);
  123.             array[i] = 0;
  124.             search = 1;
  125.             org_Elements();
  126.             res_Elements(1);
  127.         }
  128.     }
  129.    
  130.     if(search != 1)
  131.     {
  132.         printf("Sorry, this element dont's exists!\n");
  133.     }
  134.            
  135. }
  136.  
  137. // Print All Elements.
  138. void print_Elements(void)
  139. {
  140.     int i;
  141.     printf("-------------\n");
  142.     printf("Pos | Value\n");
  143.     for(i = 0; i < count; i++)
  144.     {
  145.         printf("[%d]: \t%d\n", i, array[i]);
  146.     }
  147.     printf("-------------\n");
  148. }
  149.  
  150. void getSize(void)
  151. {
  152.     return count;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement