bashでいこう.インデントが全角スペースなのはWordPressのせいです.
・方針
$ touch ./test.sh
$ chmod 744 ./test.sh
$ gedit ./test.sh
・足し算
#!/bin/bash
a=1
b=2
c=`expr ${a} + ${b}`
printf "%dn" ${c}
$ ./test.sh
3
・整数除算
#!/bin/bash
a=20
b=3
c=`expr ${a} '/' ${b}`
printf "%dn" ${c}
./test.sh
6
20÷3=6あまり2ですね.
・浮動小数点除算
#!/bin/bash
P=4
a=20
b=3
c=`echo "scale=${P}; ${a} / ${b}" |bc`
printf "%.${p}fn" ${c}
$ ./test.sh
6.6666
小数点以下はPで指定した桁数
・Forループもどき
#!/bin/bash
i=0
while [ "${i}" -lt 5 ]
do
printf "%d " ${i}
i=`expr ${i} + 1`
done
printf "n"
$ ./test.sh
0 1 2 3 4
・入れ子のForループ
#!/bin/bash
TFILE=./t.txt
MAX=100
P=7rm ${TFILE}
touch ${TFILE}i=1
while [ "${i}" -le ${MAX} ]
do
a=`echo "scale=${P}; 1+1/${i}" |bc`
j=1
b=${a}
while [ "${j}" -lt ${i} ]
do
a=`echo "scale=${P}; ${a}*${b}" |bc`
j=`expr ${j} + 1`
done
echo "${i} ${a}" >> ${TFILE}
i=`expr ${i} + 1`
done
(1+1/n)^nをn=1 to MAXで計算して結果をgnuplotが食べられるフォーマットにしてTFILEに出力する.
$ gnuplot
$ plot "t.txt" w l, 2.71828 w l
もう分かったからC言語でやれって感じですね