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


    不算好的方法:
    #!/bin/sh
     echo "input num1:" 
     read a 
     echo "input num2:" 
     read b 
     echo "input operator:" 
     read o 
     case $o in
     +)
            awk 'BEGIN{printf "	" "'$a'+'$b'=%.2f
    ",'$a'+'$b'}'
            #result=$((a+b))
     ;;
     -)
            awk 'BEGIN{printf "	" "'$a'-'$b' =%.2f
    ", '$a'-'$b'}'
            #result=$((a-b))
     ;;
     /)if [ $b -eq 0 ]
              then
              echo "错误提示:除数不能为零!!!"
              else
                    awk 'BEGIN{printf "	" "'$a'/'$b' =%.2f
    ", '$a'/'$b'}'
                    fi
                    ;;
     *)
            awk 'BEGIN{printf "	" "'$a'*'$b' =%.2f
    ", '$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 "
    "
            echo "请输入运算符2:c"
            read b
            echo "
    "
            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 "提示:请选择下列序号进行运算,退出请输入错误格式!
    "
            
     select i in "+" "-" "*" "/" 
     do
            case $i in
     +)
            awk 'BEGIN {printf "	" "'$a'+'$b' =%.2f
    ", '$a'+'$b'}'
     ;;
     -)
                    awk 'BEGIN{printf "	" "'$a'-'$b' =%.2f
    ", '$a'-'$b'}'
     ;;
    
     /)if [ $b -eq 0 ]
              then
              echo "错误提示:除数不能为零!!!a
    "
              else
                    awk 'BEGIN{printf "	" "'$a'/'$b' =%.2f
    ", '$a'/'$b'}'
                    fi
                    ;;
     *)
            awk 'BEGIN{printf "	" "'$a'*'$b' =%.2f
    ", '$a'*'$b'}'
            ;;
       *)
       echo "提示:输入1继续运算,否则退出。。。"
       break
       ;;
      esac
      done
      else
            echo "提示:运算符错误,请输入数字
    "
            continue
      fi
      ;; 
      *)
      echo "-----------------------------退出---------------------------"
      exit 0
      ;;
      esac
     done
    
    可参考的实现方法3:
    #!/bin/sh
      case $2 in  
       +)
               awk 'BEGIN{printf "	" "'$1'+'$3'=%.2f
    ",'$1'+'$3'}'    
      ;;
      esac
    注:这个方法就是在执行的时候输入参数
  • 相关阅读:
    团队选题与评审
    消息管家
    团队展示
    功能规格说明书
    测试与优化
    git分支管理
    MVC小结
    .Net基础加强
    结对编程
    个人作业1_软件工程
  • 原文地址:https://www.cnblogs.com/fengkui/p/6122394.html
Copyright © 2020-2023  润新知