
Vadim A. Kazantsev
 
Профиль
Группа: Участник
Сообщений: 291
Регистрация: 3.12.2006
Где: Moscow, Russia
Репутация: 7 Всего: 14
|
Было настроение по[ш]кодить => Вот, наваял это чудо: | Код | /* ======================================================================== */ /* ============================= Includes ============================= */ /* ======================================================================== */
#include <stdio.h> /* for scanf(), printf(), puts(), putchar() */ #include <stdlib.h> /* for qsort() */ #include <malloc.h> /* for [m/c]alloc() */ #include <sys/types.h> /* for size_t and ssize_t */
/* ======================================================================== */ /* ========================== User data types ========================= */ /* ======================================================================== */
/* Boolean data type. */ #define TRUE 1 #define FALSE 0 typedef int bool;
/* ======================================================================== */ /* ============================= Constants ============================ */ /* ======================================================================== */
#define MINN (-100) /* minimally admissible value for array elements */ #define MAXN (+100) /* maximally admissible value for array elements */
#define MAX_NUMBERS 20 /* maximum number of numbers */
/* ======================================================================== */ /* =========================== Mode settings ========================== */ /* ======================================================================== */
#define INTERACTIVE_MODE 0 /* set to 1 for interactive mode, 0 -- otherwise (default) */ #define DYNAMIC_MEM_ALLOC 0 /* set to 1 for dynamic memory allocating, 0 -- static (default) */ #define USE_QSORT 0 /* set to 1 for usage of standard qsort() function, 0 -- for usage of my bubble_sort() function (default) */
/* ======================================================================== */ /* ============================== Macros ============================== */ /* ======================================================================== */
#define SWAP(a, b) { \ int t; \ (t) = (a); \ (a) = (b); \ (b) = (t); \ }
/* ======================================================================== */ /* ============================ Prototypes ============================ */ /* ======================================================================== */
bool ge(const void *a, const void *b); bool le(const void *a, const void *b); int min(int *arr, size_t nelem); int max(int *arr, size_t nelem); int sum(int *arr, size_t nelem); void bubble_sort(int *arr, ssize_t nelem, bool (*compar)(const void *a, const void *b)); void print_array(const int *arr, size_t nelem);
/* ======================================================================== */ /* ================== User procedures and functions =================== */ /* ======================================================================== */
/* ====================== Comparisons functions ======================= */
/* ``ge(&a, &b)'' is similar to ``a >= b''. */ bool ge(const void *a, const void *b) { return ( *((int*) a) - *((int*) b) ); }
/* ``le(&a, &b)'' is similar to ``a <= b''. */ bool le(const void *a, const void *b) { return ( *((int*) b) - *((int*) a) ); }
/* ============================ Min & Max ============================= */
/* Minimum element of array. */ int min(int *arr, size_t nelem) { int min; int i; /* index, counter */ if (nelem == 0) return (0); min = arr[0]; for (i = 1; i < nelem; ++i) if (arr[i] < min) min = arr[i]; return (min); }
/* Maximum element of array. */ int max(int *arr, size_t nelem) { int max; int i; /* index, counter */ if (nelem == 0) return (0); max = arr[0]; for (i = 1; i < nelem; ++i) if (arr[i] > max) max = arr[i]; return (max); }
/* =============================== Sum ================================ */
/* Amount of elements of array. */ int sum(int *arr, size_t nelem) { int sum; int i; /* index, counter */ sum = 0; for (i = 0; i < nelem; ++i) sum += arr[i]; return (sum); }
/* ======================== Sorting of arrays ========================= */
/* Bubble sort. */ void bubble_sort(int *arr, ssize_t nelem, bool (*compar)(const void *a, const void *b)) { int i, j; /* indexes, counters */ for (i = 0; i < nelem - 1; ++i) for (j = 0; j < nelem - 1 - i; ++j) if ((*compar)(&arr[j], &arr[j + 1]) > 0) SWAP(arr[j], arr[j + 1]); }
/* ======================== Printing of arrays ======================== */
/* Prints an array. */ void print_array(const int *arr, size_t nelem) { int i; /* index, counter */ putchar('['); if (nelem > 0) printf("%d", arr[0]); for (i = 1; i < nelem; ++i) printf(", %d", arr[i]); putchar(']'); putchar('\n'); }
/* ======================================================================== */ /* =============================== Main =============================== */ /* ======================================================================== */
/* Main. */ int main(void) { #if (DYNAMIC_MEM_ALLOC) /* dynamic memory allocating */ int *positives; /* array with positive integers */ int *negatives; /* array with negative integers */ #else /* static memory allocating */ int positives[MAX_NUMBERS]; /* array with positive integers */ int negatives[MAX_NUMBERS]; /* array with negative integers */ #endif int cnt = 0; /* counter for entered numbers */ int pc = 0; /* counter for positive integers */ int nc = 0; /* counter for negative integers */ int n; #if (DYNAMIC_MEM_ALLOC) positives = (int*) calloc(MAX_NUMBERS, sizeof(int)); negatives = (int*) calloc(MAX_NUMBERS, sizeof(int)); #endif #if (INTERACTIVE_MODE) printf("Please, enter up to 20 nonzero integers from range " "[%d, %d].\n", MINN, MAXN); #endif while (TRUE) { #if (INTERACTIVE_MODE) printf("number[%d] := ", cnt); #endif if (!((scanf("%d", &n) == 1) && (cnt < MAX_NUMBERS))) break; if (MINN <= n && n <= MAXN && n != 0) { if (n > 0) positives[pc++] = n; else if (n < 0) negatives[nc++] = n; ++cnt; } #if (INTERACTIVE_MODE) else printf("Please, enter nonzero integers from range " "[%d, %d].\n", MINN, MAXN); #endif } #if (INTERACTIVE_MODE) putchar('\n'); #endif puts("Before sorting: "); printf("positives == "); print_array(positives, pc); printf("negatives == "); print_array(negatives, nc); #if (USE_QSORT) qsort(positives, pc, sizeof(int), (*ge)); qsort(negatives, nc, sizeof(int), (*le)); #else bubble_sort(positives, pc, (*ge)); bubble_sort(negatives, nc, (*le)); #endif puts("After sorting: "); printf("positives == "); print_array(positives, pc); printf("minimum element of positives = %d\n", min(positives, pc)); printf("maximum element of positives = %d\n", max(positives, pc)); printf("sum of elements of positives = %d\n", sum(positives, pc)); printf("negatives == "); print_array(negatives, nc); printf("minimum element of negatives = %d\n", min(negatives, nc)); printf("maximum element of negatives = %d\n", max(negatives, nc)); printf("sum of elements of negatives = %d\n", sum(negatives, nc)); #if (INTERACTIVE_MODE) putchar('\n'); #endif #if (DYNAMIC_MEM_ALLOC) free(positives); free(negatives); #endif return (0); }
|
Тестировать прогу удобнее всего в неинтерактивном режиме, подав ей на вход текстовый файлик с чиселками. | Код | bash-2.05b$ cat input0.txt bash-2.05b$ ./a.out < input0.txt > output0.txt bash-2.05b$ cat output0.txt Before sorting: positives == [] negatives == [] After sorting: positives == [] minimum element of positives = 0 maximum element of positives = 2009247405 sum of elements of positives = 0 negatives == [] minimum element of negatives = 0 maximum element of negatives = 0 sum of elements of negatives = 0 bash-2.05b$ bash-2.05b$ cat input1.txt 7 -7 8 -12 -19 64 0 101 16 -100 100 99 -101 13 -14 bash-2.05b$ ./a.out < input1.txt > output1.txt bash-2.05b$ cat output1.txt Before sorting: positives == [7, 8, 64, 16, 100, 99, 13] negatives == [-7, -12, -19, -100, -14] After sorting: positives == [7, 8, 13, 16, 64, 99, 100] minimum element of positives = 7 maximum element of positives = 100 sum of elements of positives = 307 negatives == [-7, -12, -14, -19, -100] minimum element of negatives = -100 maximum element of negatives = -7 sum of elements of negatives = -152 bash-2.05b$ bash-2.05b$ cat input2.txt 4 -16 -3 1 12 7 -101 78 -90 -19 30 7 -100 36 -6 0 55 100 5 -5 101 18 0 78 2 34 -81 -8 1 74 -71 3 67 8 -12 bash-2.05b$ ./a.out < input2.txt > output2.txt bash-2.05b$ cat output2.txt Before sorting: positives == [4, 1, 12, 7, 78, 30, 7, 36, 55, 100, 5, 18, 78] negatives == [-16, -3, -90, -19, -100, -6, -5] After sorting: positives == [1, 4, 5, 7, 7, 12, 18, 30, 36, 55, 78, 78, 100] minimum element of positives = 1 maximum element of positives = 100 sum of elements of positives = 431 negatives == [-3, -5, -6, -16, -19, -90, -100] minimum element of negatives = -100 maximum element of negatives = -3 sum of elements of negatives = -239 bash-2.05b$
|
========================================================================================== P.S. Всегда следуйте правилам форма! В частности: Если вопрос решён, то воспользуйтесь ссылкой "Пометить как решённый", которая находится под кнопками создания темы или флажком при ответе  ========================================================================================== Это сообщение отредактировал(а) V.A.KeRneL - 5.3.2007, 21:43
--------------------
«C'est un pense-creux d'ici. C'est le meilleur et le plus irascible homme du monde...» © Ф.М. Достоевский, «Бесы» ---/)/)---(\.../)---(\(\ --(':'=)---(=';'=)---(=':') (")(")..)-(").--.(")-(..(")(")
|