/*
*******************************************************************************
*
* Filename: 400.c
*
* Description:
*
* Version: 0.1
* Created: 4/7/2009 8:30:02 AM
*
* Author: Ye Xiaofeng , yexf # gmail.com
*
*******************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void ls_func(int line_num);
int compare(const void *a, const void *b);
void find_row_col(int count, int max_col, int *row, int *col);
int main(int argc, char **argv)
{
int line_num = 0;
int rc = 0;
rc = scanf("%d", &line_num);
while (rc != -1) {
ls_func(line_num);
rc = scanf("%d", &line_num);
}
}
void ls_func(int line_num)
{
char line_buffer[100][61];
int i = 0, j = 0, k = 0;
int max_len = 0;
int cur_len = 0;
int cul_num = 0;
int row_num = 0;
char cel_buffer[61];
int count = 0;
int flag = 0;
int last_col_len = 0;
memset(cel_buffer, 0, 61);
for (i = 0; i < line_num; i++) {
scanf("%s", &line_buffer[count][0]);
for (j = 0; j < count; j++) {
if (0 == strcmp(line_buffer[j], line_buffer[count])) {
flag = 1;
break;
}
}
if (!flag) {
cur_len = strlen(line_buffer[count]);
if (max_len < cur_len) {
max_len = cur_len;
}
count++;
}
}
qsort(line_buffer, count, 61, compare);
find_row_col(count, (60+2)/(max_len+2), &row_num, &cul_num);
last_col_len = 60 - (cul_num-1)*(max_len+2);
printf("------------------------------------------------------------\n");
for (j = 0; j < row_num; j++) {
k = 0;
for (i = j; i < count; i+=row_num) {
if (k == cul_num-1) {
/* Last column */
memset(cel_buffer, ' ', max_len);
memcpy(cel_buffer, line_buffer[i], strlen(line_buffer[i]));
} else {
memset(cel_buffer, ' ', max_len+2);
memcpy(cel_buffer, line_buffer[i], strlen(line_buffer[i]));
}
printf("%s", cel_buffer);
k++;
}
printf("\n");
}
}
int compare(const void *a, const void *b)
{
const char *v1 = a;
const char *v2 = b;
return strcmp(v1, v2);
}
void find_row_col(int count, int max_col, int *row, int *col)
{
*row = 1;
*col = count;
while (*col > max_col) {
(*row)++;
if (count % (*row) == 0) {
*col = count / (*row);
} else {
*col = count / *row + 1;
}
}
}
|