Unix is ——Uva 400

The computer company you work for is introducing a brand new computer line and is developing anew Unix-like operating system to be introduced along with the new computer. Your assignment is towrite the formatter for the ls function.

Your program will eventually read input from a pipe (although for now your program will readfrom the input le). Input to your program will consist of a list of (F) lenames that you will sort(ascending based on the ASCII character values) and format into (C) columns based on the length (L)of the longest lename. Filenames will be between 1 and 60 (inclusive) characters in length and will beformatted into left-justi ed columns. The rightmost column will be the width of the longest lenameand all other columns will be the width of the longest lename plus 2. There will be as many columnsas will t in 60 characters. Your program should use as few rows (R) as possible with rows being lledto capacity from left to right.

Input

The input le will contain an inde nite number of lists of lenames. Each list will begin with a linecontaining a single integer (1 N 100). There will then be N lines each containing one left-justi ed lename and the entire line’s contents (between 1 and 60 characters) are considered to be part of the lename. Allowable characters are alphanumeric (a to z, A to Z, and 0 to 9) and from the following set{._-} (not including the curly braces). There will be no illegal characters in any of the lenames andno line will be completely empty.

Immediately following the last lename will be the N for the next set or the end of le. You shouldread and format all sets in the input le.

Output

For each set of lenames you should print a line of exactly 60 dashes (-) followed by the formattedcolumns of lenames. The sorted lenames 1 to R will be listed down column 1; lenames R + 1 to 2Rlisted down column 2; etc.

Sample Input

10
tiny
2short4me
very_long_file_name
shorter
size-1
size2
size3
much_longer_name
12345678.123
mid_size_name
12
Weaser
Alfalfa
Stimey
Buckwheat
Porky
Joe
Darla
Cotton
Butch
Froggy
Mrs_Crabapple
P.D.
19
Mr._French
Jody
Buffy
Sissy
Keith
Danny
Lori
Chris
Shirley
Marsha
Jan
Cindy
Carol
Mike
Greg
Peter
Bobby
Alice
Ruben

Sample Output

------------------------------------------------------------
12345678.123
2short4me
mid_size_name
much_longer_name     tiny
shorter              very_long_file_name
------------------------------------------------------------
size-1
size2
size3
Joe            Porky
Mrs_Crabapple  Stimey
P.D.           Weaser
Alfalfa
Buckwheat
Butch
------------------------------------------------------------
Alice
Bobby
Buffy
Carol
Chris       Jan         Marsha      Ruben
Cindy       Jody        Mike        Shirley
Cotton
Darla
Froggy
Danny       Keith
Greg        Lori
Mr._French  Sissy
Peter

本题分析:

  • 其实这道题是道水题......但是我还是一开始的时候没有想清楚。。。
  • 具体来说,题目中要求按列优先左对齐输出,这是什么意思呢?意思就是,首先将文件名排序之后,要按列输出,而已知输出都是按行输出的,因此在输出的时候需要先计算出一行中各列对应的文件名是什么。这一点我没有想......直接抄了书上的代码。。。。。。真后悔这样不经过思维的训练直接抄代码的行为,对于自己的思维能力的提高并没有啥太大帮助。。。。。。

书上的代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

const int maxcol = 60;
const int maxn = 100 + 5;
string filenames[maxn];

//输出字符串s,长度不足len时补字符extra
void print(const string& s, int len, char extra)
{
    cout << s;
    for(int i = 0; i < len - s.length(); i++)
        cout << extra;
}

int main(){
    int n;
    while(cin >> n)
    {
        int M = 0;
        for(int i = 0; i < n; i++)
        {
            cin >> filenames[i];
            M = max(M, (int)filenames[i].length());
        }
        //计算列数cols和行数rows 
        int cols = (maxcol - M) / (M + 2) + 1, rows = (n - 1) / cols + 1;
        print("", 60, '-');
        cout << "\n";
        sort(filenames, filenames + n);//排序
        for(int r = 0; r < rows; r++)
        {
            for(int c = 0; c < cols; c++)
            {
                int idx = c * rows + r;
                if(idx < n)
                    print(filenames[idx], c == cols-1 ? M : M+2, ' ');
            }
            cout << "\n";
        }
    }
	return 0;
}
其实分析下书上的代码,它将输出部分抽象成了函数,值得学习。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值