C语言编程,求高人解答
怎么把一堆数量未知的数据输到数列当中,并且以如下方式表示出来。范围是0到100之内的任何一个数,区间可以自己定如图:
0- 9 |
10- 19 |**
20- 29 |*
30- 39 |
40- 49 |*
50- 59 |***
60- 69 |*******
70- 79 |*********************
80- 89 |********
90- 99 |**
100-100 |***
#include <stdio.h> #include <string.h> #include <time.h> #include <math.h> #include <stdlib.h> int* RandomNumberGenerator(int*, int, int, int); int* NumberGrouping(int*, int, int, int*); void PrintAGroup(int, int, int*, int, const char*); void ShowGroup(int, int, int*); int main(void) { int* array, *map; int low, high, size, mapSize; scanf("%d%d%d", &low, &high, &size); array = malloc(sizeof(int) * size); mapSize = sizeof(int) * (high / 10 + 1 - low / 10); map = malloc(mapSize); memset(map, 0, mapSize); RandomNumberGenerator(array, low, high, size); NumberGrouping(array, low, size, map); ShowGroup(low, high, map); free(array); free(map); return 0; } int* RandomNumberGenerator(int* array, int low, int high, int size) { srand(time(0)); while (size--) array[size] = rand() % (high - low + 1) + low; return array; } /* map must be initialize to zero */ int* NumberGrouping(int* array, int low, int size, int* map) { int bottom = low / 10; while (size--) ++map[array[size] / 10 - bottom]; return map; } void PrintAGroup(int low, int high, int* map, int index, const char* format) { int i; printf(format, low, high); for (i = 0; i < map[index]; ++i) putchar('*'); putchar('\n'); } void ShowGroup(int low, int high, int* map) { char format[31]; /* "%nd -- %nd |" */ int width = !high ? 1 : (int)log10(high) + 1; int index = 0, i, j; sprintf(format, "%%%dd -- %%%dd |", width, width); for (i = low; i <= high; i = j + 1) { j = (i / 10 + 1) * 10 - 1; PrintAGroup(i, j < high ? j : high, map, index++, format); } }