简单的一个加减乘除计算器实现

不算好的方法:
#!/bin/sh
 echo "input num1:" 
 read a 
 echo "input num2:" 
 read b 
 echo "input operator:" 
 read o 
 case $o in
 +)
        awk 'BEGIN{printf "\t" "'$a'+'$b'=%.2f\n",'$a'+'$b'}'
        #result=$((a+b))
 ;;
 -)
        awk 'BEGIN{printf "\t" "'$a'-'$b' =%.2f\n", '$a'-'$b'}'
        #result=$((a-b))
 ;;
 /)if [ $b -eq 0 ]
          then
          echo "错误提示:除数不能为零!!!"
          else
                awk 'BEGIN{printf "\t" "'$a'/'$b' =%.2f\n", '$a'/'$b'}'
                fi
                ;;
 \*)
        awk 'BEGIN{printf "\t" "'$a'*'$b' =%.2f\n", '$a'*'$b'}'
        #result=$((a*b))
 ;;
 *)
        echo "输入格式错误"
 esac


较好的方法:
#!/bin/sh

 ps3="select your will exec Menu:"
 select i in "运算"
 do
        case $i in 
 运算)
        echo "请输入运算符1:\c"
        read a 
        echo "\n"
        echo "请输入运算符2:\c"
        read b
        echo "\n"
        ac=`echo $a|awk '{if($1 !~/[a-zA-Z]/) print}'` 
        bc=`echo $b|awk '{if($1 !~/[a-zA-Z]/) print}'`
    #ccc=`echo $a $b|awk '{if("$a" ~/[0-9]/ || "$b" ~/[0-9]/) print}'`
        #echo $a $b|awk '{if("$a" !~/[0-9]/ && "$b" !~/[0-9]/) print }'
 if [ "$ac" = "$a" -a "$bc" = "$b" ]
  then
                #echo "运算符只能为数字..."
                #break
                #else
                echo "提示:请选择下列序号进行运算,退出请输入错误格式!\n"
        
 select i in "+" "-" "*" "/" 
 do
        case $i in
 +)
        awk 'BEGIN {printf "\t" "'$a'+'$b' =%.2f\n", '$a'+'$b'}'
 ;;
 -)
                awk 'BEGIN{printf "\t" "'$a'-'$b' =%.2f\n", '$a'-'$b'}'
 ;;

 /)if [ $b -eq 0 ]
          then
          echo "错误提示:除数不能为零!!!a\n"
          else
                awk 'BEGIN{printf "\t" "'$a'/'$b' =%.2f\n", '$a'/'$b'}'
                fi
                ;;
 \*)
        awk 'BEGIN{printf "\t" "'$a'*'$b' =%.2f\n", '$a'*'$b'}'
        ;;
   *)
   echo "提示:输入1继续运算,否则退出。。。"
   break
   ;;
  esac
  done
  else
        echo "提示:运算符错误,请输入数字\n"
        continue
  fi
  ;; 
  *)
  echo "-----------------------------退出---------------------------"
  exit 0
  ;;
  esac
 done

可参考的实现方法3:
#!/bin/sh
  case $2 in  
   +)
           awk 'BEGIN{printf "\t" "'$1'+'$3'=%.2f\n",'$1'+'$3'}'    
  ;;
  esac
注:这个方法就是在执行的时候输入参数

 

posted @ 2016-12-01 16:12  feng_kui  阅读(1027)  评论(0编辑  收藏  举报