"UVa 400" Unix ls

Source: Internet
Author: User

Unix ls

The computer company-introducing a brand new computer line and is developing a new unix-like operating sys TEM to is introduced along with the new computer. Your assignment is-write the formatter for the LS function.

Your program would eventually read input from a pipe (although for now Your program would read from the input file). Input to your program would consist of a list of (F) filenames that you'll sort (ascending based on the ASCII CH Aracter values) and format into (C) columns based on the length (L) of the longest filename. Filenames'll be between 1 and a (inclusive) characters in length and would be formatted into left-justified columns. The rightmost column is being the width of the longest filename and all other columns would be the width of the longest file Name plus 2. There'll be as many columns as would fit in characters. Your program should, use as few rows (R) as possible with rows being filled to capacity from left to right.

Input

The input file is contain an indefinite number of lists of filenames. Each list would begin with a line containing a single integer (). There'll then being N lines each containing one left-justified filename and the entire line ' s contents (between 1 and characters) is considered to is part of the filename. allowable characters is alphanumeric (a to z, a to z, and 0 to 9) A nd from the following set { ._- } (not including the curly braces). There won't illegal characters in any of the filenames and no line would be completely empty.

Immediately following the last filename is the N for the next set or the end of file. You should read and format all sets in the input file.

Output

For each set of filenames should print a line of exactly dashes (-) followed by the formatted columns of F Ilenames. The sorted filenames 1 to R would be is listed down column 1; Filenames R+1 to 2R listed down column 2; etc.

Sample Input

10tiny2short4mevery_long_file_nameshortersize-1size2size3much_longer_name12345678.123mid_size_ Name12weaseralfalfastimeybuckwheatporkyjoedarlacottonbutchfroggymrs_crabapplep.d.19mr._ Frenchjodybuffysissykeithdannylorichrisshirleymarshajancindycarolmikegregpeterbobbyaliceruben

Sample Output

------------------------------------------------------------12345678.123         size-1               2short4me            size2                mid_size_name        size3                much_longer_name     tiny                 shorter              very_long_file_name  ----------- -------------------------------------------------Alfalfa        Cotton         Joe            Porky          Buckwheat      Darla          mrs_crabapple  Stimey         Butch          Froggy         p.d.           Weaser         ------------------------------------------------------------Alice       Chris       Jan         Marsha      Ruben       Bobby       Cindy       Jody        Mike        Shirley     Buffy       Danny       Keith       mr._french  sissy       Carol       Greg        Lori        Peter

It's very important to read the question. The Purple book is a little too clear to write.
At my level to see the English question + word translation is simply an egg ache ah.
This problem is pure analog output.
#include <cstdio>#include<cstring>#include<iostream>#include<string>#include<algorithm>using namespacestd;Const intMaxcol = -;stringdict[ the];intN;voidPrintstringSintLenCharc) {cout<<s;  for(inti =0; I < len-s.length (); ++i) printf ("%c", c);}intMain () { while(SCANF ("%d", &n) = =1)    {        intM =0;  for(inti =0; I < n; ++i) {cin>>Dict[i]; M= Max (M, (int) dict[i].length ()); } Print ("", Maxcol,'-'); printf ("\ n"); intCol = (maxcol-m)/(M +2) +1; /*These sentences is very important for the program.*/        introw = (N-1)/col +1; Sort (dict, Dict+N);  for(inti =0; i < row; ++i) { for(intj =0; J < Col; ++j) {if(i + j * Row >= N) Break; Print (Dict[i+ J * Row], (j = = col-1) ? M:m +2,' '); } printf ("\ n"); }    }    return 0;}

"UVa 400" Unix ls

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.