Four Arithmetic Operation

Tp1: calculation of a formula with only a kind of operator

#! /bin/sh
n=$#
if [ $n -eq 0 ]
then
    echo "Opération manquante"
    exit
elif [ $n -eq 1 ]
then
    echo "Opréation manquante"
else
    res=$2
    case $1 in
    "add")
        echo -n  $2
        shift 2
            while [ $# -ne 0 ]
        do
        echo -n "+$1"
        res=`expr $res + $1`
        shift
        done
        echo  "=${res}";;
    "supp")
        echo -n  $2
        shift 2
            while [ $# -ne 0 ]
        do
        echo -n "-$1"
        res=`expr $res - $1`
        shift
        done
        echo  "=${res}";;
    "mult")
        echo -n  $2
        shift 2
            while [ $# -ne 0 ]
        do
        echo -n "x$1"
        res=`expr $res \* $1`
        shift
        done
        echo  "=${res}";;
    "div")
        echo -n  $2
        shift 2
            while [ $# -ne 0 ]
        do
        echo -n "/$1"
        if [ $1 -eq 0 ]
        then
            echo -e "\nDivision par 0 interdite"
            exit
        else            
            res=`expr $res / $1`
            shift
        fi
        done
        echo  "=${res}";;
    *)
        echo "Opération non traitée";;
    esac
 fi

Tp2: calculation of a formula without considering the priority of each operator

#! /bin/sh

if [ $# -eq 0 ]
then
    echo " Opérande manquante"
    exit
else
    res=$1
    echo -n "$1"

    shift
    while [ $# -ne 0 ]
    do
    case $1 in
        "+")
        echo -n " $1 $2" 
        res=`expr $res + $2`
        shift 2
        ;;
        "-")
        echo -n " $1 $2" 
        res=`expr $res - $2`
        shift 2
        ;;
        "x")
        echo -n " $1 $2" 
        res=`expr $res \* $2`
        shift 2
        ;;
        "/")
        echo -n " $1 $2"
        if [ $2 -eq 0 ]
        then
            echo  " ->Divivion par 0 interdite"
            exit
        else
            res=`expr $res / $2`
            shift 2
        fi
        ;;
        *)
        echo  " $1 -> Opération non traitée"
        exit
    esac
    done

    echo " = $res"
fi  

Tp3: calculation of a formula without brackets

#! /bin/sh
index=0

while [ $# -gt 0 ]
do
    if [ $1 != x -a $1 != / ]
    then
    low_priority[$index]=$1
    index=`expr $index + 1`
    shift 1
    else
    case $1 in
        "x")
        pre_index=`expr $index - 1`
        res=`expr ${low_priority[$pre_index]} \* $2`
        low_priority[$pre_index]=$res
            shift 2
            ;;

        "/")
        pre_index=`expr $index - 1`
        res=`expr ${low_priority[$pre_index]} / $2`
        low_priority[$pre_index]=$res
        shift 2
        ;;
    esac
   fi
done

n=1
length=${#low_priority[@]}
end=`expr $length - 2`
res=${low_priority[0]}
while [ $n -le $end ]
do
     case ${low_priority[$n]} in
    "+")
        nex_n=`expr $n + 1`
        res=`expr $res + ${low_priority[$nex_n]}`
        n=`expr $n + 2`;;
    "-")
        nex_n=`expr $n + 1`
        res=`expr $res - ${low_priority[$nex_n]}`
        n=`expr $n + 2`;;
    *)
        exit;;
     esac
done

echo $res
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值