#!/bin/bash # 入力された数字の桁数を引数nameに渡します。 name=`expr length $1` # そのnameの桁数によってこのままシェルを続けるのか途中で終わらせるのか判断させます。 if test $name -ne 4 ; then echo "入力された文字数が4桁ではありません。もう一度入力しなおしてください。" exit 1 fi # 入力された4桁の数字をそれぞれ4つに分離します。 one=`echo $1 | cut -c1` two=`echo $1 | cut -c2` three=`echo $1 | cut -c3` four=`echo $1 | cut -c4` echo "$one,$two,$three,$fourでテンパズルを組みます。" # 結果を書き出すためのquiz2.txtというファイルがすでに存在していれば、中身を白紙へと戻します。 if [ -e quiz2.txt ]; then echo "Oh! You have calculated it once before. Still you are going to re-calculate it. Okay..." cat /dev/null >quiz2.txt fi # ここから計算パートに入っていくのですが( )のおき方だけでも11通りあるので # forを用いた演算を11回繰り返していきます。 # 1. one two three four # forコマンドを用いてi,j,kに、+,-,*を考えられる組み合わせ全て代入していきます。 for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr $one "$i" $two "$j" $three "$k" $four` if test $answer -eq 10 ; then echo "$one $i $two $j $three $k $four" > quiz2.txt fi done done done # 2.(one two) three four for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr \( $one "$i" $two \) "$j" $three "$k" $four` if test $answer -eq 10 then echo "($one $i $two) $j $three $k $four" >> quiz2.txt fi done done done # 3.one (two three) four for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr $one "$i" \( $two "$j" $three \) "$k" $four` if test $answer -eq 10 then echo "$one $i ($two $j $three) $k $four" >> quiz2.txt fi done done done # 4.one two (three four) for i in + - '*' do for j in + - '*' do for k in + - '*' do answear=`expr $one "$i" $two "$j" \( $three "$k" $four \)` if test $answer -eq 10 then echo "$one $i $two $j ($three $k $four)" >> quiz2.txt fi done done done # 5.(one two three) four for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr \( $one "$i" $two "$j" $three \) "$k" $four` if test $answer -eq 10 then echo "($one $i $two $j $three) $k $four" >> quiz2.txt fi done done done # 6.one (two three four) for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr $one "$i" \( $two "$j" $three "$k" $four \)` if test $answer -eq 10 then echo "$one $i ($two $j $three $k $four)" >> quiz2.txt fi done done done # 7.((one two) three) four for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr \( \( $one "$i" $two \) "$j" $three \) "$k" $four` if test $answer -eq 10 then echo "(($one $i $two) $j $three) $k $four" >> quiz2.txt fi done done done # 8.(one (two three)) four for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr \( $one "$i" \( $two "$j" $three \) \) "$k" $four` if test $answer -eq 10 then echo "($one $i ($two $j $three)) $k $four" >> quiz2.txt fi done done done # 9.one(( two three) four) for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr $one "$i" \( \( $two "$j" $three \) "$k" $four \)` if test $answer -eq 10 then echo "$one $i (($two $j $three) $k $four)" >> quiz2.txt fi done done done # 10.one (two (three four)) for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr $one "$i" \( $two "$j" \( $three "$k" $four \) \)` if test $answer -eq 10 then echo "$one $i ($two $j ($three $k $four))" >> quiz2.txt fi done done done # 11.(one two) (three four) for i in + - '*' do for j in + - '*' do for k in + - '*' do answer=`expr \( $one "$i" $two \) "$j" \( $three "$k" $four \)` if test $answer -eq 10 then echo "($one $i $two) $j ($three $k $four)" >> quiz2.txt fi done done done # ここで結果が入力されるquiz2.txtのサイズを調べ容量がある、つまり10を作る式を作れなかったならば # これらの数字では10を作れませんという旨の文章を表示させる。 # また思惑通り10を作る式が出来たのであればquiz2.txtには何かしら入っているのでそれを確認させて # catで実際に作られた式を表示させる。 if test -s quiz2.txt ; then cat quiz2.txt else echo "この4桁の数字では10を作れません。" fi