0

I'm trying to write a shell program that, given an unsorted list of numbers (one per line, any sign, any size), returns the largest number given one argument or returns the smallest number given another argument. That is, if I type "myProgram 1 list", it outputs the largest number, but if I type "myProgram 0 list", it outputs the smallest number.

I think I know how to determine which number to output (switch case with the first argument?) but I have no idea how to go about finding the largest/smallest number in the list. Is there some simple way to go about finding this number? Could anyone give me a few tips? Thanks.

4

1 に答える 1

0

私たちはすべての楽しみを台無しにしたくありませんよね? あなたは解決策を求めたのではなく、ヒントを求めただけです。だからここに行きます:

  1. 設定min=""
  2. ファイルを一行ずつ読む
    1. x取得した値を比較します(空のmin場合は?)min
    2. 小さい場合は設定min=x
  3. 印刷するmin

あなたがしなければならないことは、これを sh に変換することだけですmax

もっと簡単な方法があるかもしれませんが、これは私が考えることができる最も単純な純粋な sh アルゴリズムです。

于 2012-10-08T21:54:00.283 に答える