Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Elements Manager (v 0.01)
- * Copyright ©2011 - @uthor #Utroz(RsC)#.
- * File: Elements.c
- * Blog: http://Gcoders.wordpress.com/ (Access it)!
- */
- /* How to Use:
- ----------------------
- add_Element(5); //Add Elements.
- print_Elements(); //Print All Elements.
- chg_Element(4); // Search for a element and change it.
- del_Element(5); // Delete a Element.
- size_Elements(); // Return amount of elements.
- ----------------------------
- Print Example:
- -------------
- Pos | Value
- [0]: 5
- [1]: 4
- [2]: 6
- [3]: 2
- -------------
- */
- #include <stdio.h>
- // Start Array.
- int array[1];
- //Size Array.
- static int count = 0;
- //Resize Array.
- void res_Elements(int signal) // 0 = Positive / 1 = Negative.
- {
- int new_Array[count], i;
- if(signal == 0) ++count;
- else --count;
- for(i = 0; i < count; i++)
- {
- new_Array[i] = array[i];
- }
- *array = *new_Array;
- }
- //Change a Element.
- void chg_Element(int arg)
- {
- int search = sch_Element(arg);
- if(search != 0)
- {
- printf("Please choose a new value:\n");
- scanf("%d", &array[search]);
- }
- }
- // Add a Element.
- void add_Element(int arg)
- {
- array[count] = arg;
- res_Elements(0);
- }
- // Search a Element.
- int sch_Element(int arg)
- {
- int i, search = 0;
- for(i = 0; i < count; i++)
- {
- if(array[i] == arg)
- {
- printf("This element: [%d] was found sucessfully!\n", arg);
- search = 1;
- return i;
- }
- }
- if(search != 1)
- {
- printf("Sorry this element: [%d] don't was founded!\n", arg);
- }
- return 0;
- }
- // Sort All Elements.
- void org_Elements(void)
- {
- int i, pos;
- for(i = 0; i < count; i++)
- {
- if(array[i] == 0)
- {
- pos = 1;
- continue;
- }
- if(pos == 1)
- {
- array[i-1] = array[i];
- array[i] = 0;
- }
- }
- }
- // Remove a Element.
- void del_Element(int arg)
- {
- int i, search = 0;
- for(i = 0; i < count; i++)
- {
- if(array[i] == arg)
- {
- printf("This element: [%d] was removed sucessfully!\n", array[i]);
- array[i] = 0;
- search = 1;
- org_Elements();
- res_Elements(1);
- }
- }
- if(search != 1)
- {
- printf("Sorry, this element dont's exists!\n");
- }
- }
- // Print All Elements.
- void print_Elements(void)
- {
- int i;
- printf("-------------\n");
- printf("Pos | Value\n");
- for(i = 0; i < count; i++)
- {
- printf("[%d]: \t%d\n", i, array[i]);
- }
- printf("-------------\n");
- }
- void getSize(void)
- {
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement