Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- #include <string.h>
- /*
- * CONST DECLARATION
- */
- int const SORT_ASC = 1;
- int const SORT_DESC = -1;
- int const REMOVE_FIRST = 0;
- int const REMOVE_ALL = 1;
- // END of CONST DEDCLARATION
- int search(int *, int, int);
- int getInt(int, int, const char*);
- int *copy(int *, int);
- void searchFuntion(int *, int);
- void allocMemory(int **, int *);
- void addToLast(int *, int);
- void addValue(int **, int *);
- void sort(int *, int, int);
- void sortAsc(int *, int);
- void sortDesc(int *, int);
- void removeFunction(int **, int*, int);
- void removeFirst(int **, int *, int);
- void removeAll(int **, int *, int);
- void display(int *, int);
- void clean(void);
- void menu(void);
- int main(void) {
- int *array = NULL;
- int array_size = 0;
- int choose;
- int value;
- int keepgoing = 1;
- do {
- menu();
- scanf("%d", &choose);
- switch (choose) {
- case 1:
- addValue(&array, &array_size);
- break;
- case 2:
- if (!array_size) {
- puts("No element in array, please input first");
- addValue(&array, &array_size);
- } else {
- searchFuntion(array, array_size);
- }
- break;
- case 3:
- removeFunction(&array, &array_size, REMOVE_FIRST);
- break;
- case 4:
- removeFunction(&array, &array_size, REMOVE_ALL);
- break;
- case 5:
- display(array, array_size);
- break;
- case 6:
- sort(array, array_size, SORT_ASC);
- break;
- case 7:
- sort(array, array_size, SORT_DESC);
- break;
- default:
- keepgoing = !1;
- }
- } while (keepgoing);
- free(array); //freeing memory
- return 0;
- }
- /**
- * This function's job is allocation the memory for the array passed into it
- * @param array: the array need to alloc memory
- * @param array_size: the size of the array
- */
- void allocMemory(int **array, int *array_size) {
- /*
- * if the array point to NULL them malloc it with one element
- * also make the size to 0
- */
- if (!*array) {
- *array_size = 0;
- (*array_size)++;
- *array = malloc(*array_size * sizeof (int));
- return;
- }
- /*
- * if not null, them realloc it
- */
- *array = realloc(*array, *array_size * sizeof (int));
- }
- /**
- * Make user input an integer them assigned it to the last element
- * @param array: the array need to add element to last
- * @param array_size: size of array
- */
- void addToLast(int *array, int array_size) {
- printf("Enter a value: ");
- int number = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
- //assign number's value to last element
- array[array_size - 1] = number;
- }
- /**
- * This funtion increase the size of array by one,
- * and add an element to the last
- * @param array: the array need to add element to last
- * @param size: size of array
- */
- void addValue(int **array, int *array_size) {
- (*array_size)++;
- allocMemory(array, array_size);
- addToLast(*array, *array_size);
- }
- /**
- * Simply print out the menu
- */
- void menu(void) {
- puts("\n");
- puts("===================================================================");
- puts("1. Add a value");
- puts("2. Search a value");
- puts("3. Remove the first existence of a value");
- puts("4. Remove all existences of a value");
- puts("5. Print out the array");
- puts("6. Sort the array in ascending order (positions are preserved)");
- puts("7. Sort the array in descending order (positions are preserved)");
- puts("Others - Quit");
- printf("Enter your choice: ");
- }
- /**
- * Return the copy version of the array passed in
- * @param array: the array need to copy
- * @param array_size: size of the source array
- * @return a new array that copy the array passed in
- */
- int *copy(int *array, int array_size) {
- int *array_copy = NULL;
- array_copy = malloc(array_size * sizeof (int));
- memcpy(array_copy, array, array_size * sizeof (int)); //copying byte by byte
- return array_copy;
- }
- /**
- * Sort the array in Ascending/Descending
- * @param array: the array need to sort
- * @param array_size: size of the array
- * @param option: (1) means sort Ascending, (-1) means sort Descending
- */
- void sort(int *array, int array_size, int option) {
- int *array_copy = copy(array, array_size);
- if (option == SORT_ASC) {
- //sort Ascending
- sortAsc(array_copy, array_size);
- } else if (option == SORT_DESC) {
- //sort Descending
- sortDesc(array_copy, array_size);
- }
- display(array_copy, array_size);
- free(array_copy);
- }
- /**
- * Sort the array Ascending
- * @param array: the array need to sort
- * @param array_size: size of the array
- */
- void sortAsc(int *array, int array_size) {
- int i;
- for (i = 0; i < array_size; i++) {
- int j;
- for (j = i + 1; j < array_size; j++) {
- if (array[i] > array[j]) {
- //swapping
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
- }
- }
- /**
- * Sort the array Descending
- * @param array: the array need to sort
- * @param array_size: size of the array
- */
- void sortDesc(int *array, int array_size) {
- int i;
- for (i = 0; i < array_size; i++) {
- int j;
- for (j = i + 1; j < array_size; j++) {
- if (array[i] < array[j]) {
- //swapping
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
- }
- }
- /**
- * This funtion help user to remove (all/one) element have the particular value
- * @param array: array to remove element
- * @param array_size: size of the source array
- * @param option: (0) means REMOVE_FIRST, (1) means REMOVE_ALL
- */
- void removeFunction(int **array, int *array_size, int option) {
- printf("Enter value to remove %s:", option == REMOVE_ALL ? "all" : "first");
- int value = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
- if (option == REMOVE_ALL) {
- removeAll(array, array_size, value);
- } else if (option == REMOVE_FIRST) {
- removeFirst(array, array_size, value);
- }
- }
- /**
- * Remove all element has particular value
- * @param array: the array need to remove element
- * @param array_size: size of the array
- * @param value: value to remove all
- */
- void removeAll(int **array, int *array_size, int value) {
- while (search(*array, *array_size, value) != -1) {
- removeFirst(array, array_size, value);
- }
- }
- /**
- * Remove the first element has particular value
- * @param array: the array need to remove element
- * @param array_size: size of the array
- * @param value: value to remove first
- */
- void removeFirst(int **array, int *array_size, int value) {
- int index = search(*array, *array_size, value);
- if (index != -1) {
- int i;
- for (i = index; i < *array_size; i++) {
- (*array)[i] = (*array)[i + 1];
- }
- (*array_size)--; //decrease size
- allocMemory(array, array_size);
- }
- }
- /**
- * Print out all element of the array
- * @param array: array to be printed
- * @param array_size: size of array
- */
- void display(int *array, int array_size) {
- if (array_size) {
- int i;
- for (i = 0; i < array_size; i++) {
- printf("(%d) ", array[i]);
- }
- } else {
- puts("Empty array . . .");
- }
- }
- /**
- * Search an value in the array
- * @param array: array to be search
- * @param array_size: size of array
- */
- void searchFuntion(int *array, int array_size) {
- printf("Enter value to search: ");
- int value = getInt(INT_MIN, INT_MAX, "Please enter an integer: ");
- int index = search(array, array_size, value);
- if (index == -1) {
- printf("No element in array has value %d.", value);
- } else {
- printf("Element %d located at %d position.", value, index);
- }
- }
- /**
- * Search an value in the array and return it's index
- * @param array: array to be search
- * @param array_size: size of the array
- * @param value: the value to search
- * @return the index of the first element in the array that have
- * value
- */
- int search(int *array, int array_size, int value) {
- int index = -1;
- int i;
- for (i = 0; i < array_size; i++) {
- if (array[i] == value) {
- index = i;
- break;
- }
- }
- return index;
- }
- /**
- * Function to validate int input from user
- * @param min minimum value user can enter
- * @param max maximum value user can enter
- * @param error the String print when user enter invalid input
- * @return valid number
- */
- int getInt(int min, int max, const char* error) {
- int number;
- int k;
- char c;
- while (1) {
- k = scanf("%d%c", &number, &c);
- if (k != 2 || c != '\n') {
- clean();
- printf("%s", error);
- } else if (number < min || number > max)
- printf("Please input number form %d to %d: ", min, max);
- else return number;
- }
- }
- /**
- * Cleaning the '\n' character in the stream
- */
- void clean(void) {
- while (getchar() != '\n');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement