NWJ619 2022-07-06 06:28 采纳率: 66.7%
浏览 323
已结题

用unix模拟一个简单的计算器

用unix模拟一个简单的计算器,包括(+,-,x,/)和(MS, M+,MC, MR,X)功能。

while true
do
echo " Enter the operand1"
read operand1

echo " Enter the operator"
read operator
echo " Enter the operand2"
read operand2
if [ "operand1" == "$result" ]
then
echo "operand1 is: $result"
echo ""
fi

   if [ "$operator" == "+" ]
     then
       result=$(($operand1 + $operand2))

   elif [ "$operator" == "-" ]
    then
       result=$(($operand1 - $operand2))

   elif [ "$operator" == "x" ]
     then
       result=$(($operand1 x $operand2))

   elif [ "$operator" == "/" ]
     then
        if [ $operand2 -eq 0]
           then
             echo "Can't divide by zero. please re-enter divisor."
        else
            result=$(($operand1 / $operand2))
        fi

   elif [ "$operator" == "X" ]
       then
        exit
   elif [ "$operator" == "MS" ]
       then
         MS -> $result

   elif [ "$operator" == "MR" ]
       then
         operand2=$result
         MR -> $operand2

   elif [ "$operator" == "M+" ]
       then
         M+ -> $((result+MS))

   elif [ "$operator" == "MC" ]
       then
         $result=0
         MC -> 0
   else
       echo "Sorry, is not a valid operator. plrase re-enter operator. "
   fi

echo "$operand1 $operator $operand2 = $result"
done

只能计算(+,-, x , /),并且当数值不符合条件时不能做到重复,而且(MS, M+,MC, MR,X)功能并不能使用。
运用 while loop和if,只限一个小时内解决
可以模拟成正常我们日常生活中的计算器

$ ./calc
[ user instruction displayed ]
Enter operand: 3
Enter operator: +
Enter operand: 4
3 + 4 = 7
Enter operator: -
enter operand: 12
7 - 12 = -5
Enter operator: MS
-5 -> M
Enter operator: x
enter operand: 3
-5 x 3 = -15
Enter operator: C
results cleared(清除结果)
enter operand: MR
M -> -5
Enter operator: x
enter operand: MR
M -> -5
-5 x -5 = 25
Enter operator: M+
25 + -5 = 20 -> M
Enter operator: /
enter operand: 0
can't divide by zero. plesae re-enter divisor.(不能除以0,请重新输入)
enter operand: 0
can't divide by zero. plesae re-enter divisor.(不能除以0,请重新输入)
enter operand: -6
25 / -6 = -4
Enter operator: +
enter operand: C
results cleared(清除结果)
enter operand: -9
Enter operator: K
Sorry, k, is not a valid operator.please re-enter the operator. (k不是一个有效的operator,请重新输入)
Enter operator: Q
Sorry, Q, is not a valid operator.please re-enter the operator. (Q 不是一个有效的operator,请重新输入)
Enter operator: MC
0 -> M
Enter operator: +
enter operand:3
-9 + 3 = -6
Enter operator: X
Good bye.(再见)

  • 写回答

2条回答 默认 最新

  • 赵4老师 2022-07-06 09:40
    关注

    在sh中添加echo和pause语句调试
    ……
    虽然当时时间来不及了,我还是补上完美答案吧:

    #!/bin/bash
    echo "[ user instrution displayed ]"
    echo "number + - x / C MS M+ MR MC supported, X exit"
    result=0
    memory=0
    while true
    do
        read -p "Enter the operand:" operand1
        if [ "$operand1" == "MR" ]
        then
            operand1=$memory
            echo "    M -> $operand1"
        fi
        while true
        do
            read -p "    Enter the operator:" operator
            if [ "$operator" == "+" ]
            then
                read -p "Enter the operand:" operand2
                if [ "$operand2" == "MR" ]
                then
                    operand2=$memory
                    echo "    M -> $operand2"
                elif [ "$operand2" == "C" ]
                then
                    result=0
                    echo "    results cleared."
                    break
                fi
                result=$(($operand1 + $operand2))
                echo "    $operand1 + $operand2 = $result"
                operand1=$result
            elif [ "$operator" == "-" ]
            then
                read -p "Enter the operand:" operand2
                if [ "$operand2" == "MR" ]
                then
                    operand2=$memory
                    echo "    M -> $operand2"
                    echo "$operand2"
                elif [ "$operand2" == "C" ]
                then
                    result=0
                    echo "    results cleared."
                    break
                fi
                result=$(($operand1 - $operand2))
                echo "    $operand1 - $operand2 = $result"
                operand1=$result
            elif [ "$operator" == "x" ]
            then
                read -p "Enter the operand:" operand2
                if [ "$operand2" == "MR" ]
                then
                    operand2=$memory
                    echo "    M -> $operand2"
                elif [ "$operand2" == "C" ]
                then
                    result=0
                    echo "    results cleared."
                    break
                fi
                result=$(($operand1 * $operand2))
                echo "    $operand1 x $operand2 = $result"
                operand1=$result
            elif [ "$operator" == "/" ]
            then
                while true
                do
                    read -p "Enter the operand:" operand2
                    if [ "$operand2" == "MR" ]
                    then
                        operand2=$memory
                        echo "    M -> $operand2"
                    elif [ "$operand2" == "C" ]
                    then
                        result=0
                        echo "    results cleared."
                        break
                    fi
                    if [ $operand2 -eq 0 ]
                    then
                        echo "Can't divide by zero. please re-enter divisor."
                    else
                        result=$(($operand1 / $operand2))
                        echo "    $operand1 / $operand2 = $result"
                        operand1=$result
                        break
                    fi
                done
                if [ "$operand2" == "C" ]
                then
                    break
                fi
            elif [ "$operator" == "X" ]
            then
                echo "Good bye."
                exit
            elif [ "$operator" == "MS" ]
            then
                memory=$result
                echo "    $result -> M"
            elif [ "$operator" == "C" ]
            then
                result=0
                echo "    results cleared."
                break
            elif [ "$operator" == "MR" ]
            then
                operand1=$memory
                result=$operand1
                echo "    M -> $result"
            elif [ "$operator" == "M+" ]
            then
                oldmemory=$memory
                memory=$(($operand1 + $oldmemory))
                echo "    $operand1 + $oldmemory = $memory -> M"
            elif [ "$operator" == "MC" ]
            then
                memory=0
                echo "    0 -> M"
            else
                echo "Sorry, $operator is not a valid operator. please re-enter the operator."
            fi
        done
    done
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月8日
  • 赞助了问题酬金71元 7月6日
  • 修改了问题 7月6日
  • 赞助了问题酬金10元 7月6日
  • 展开全部

悬赏问题

  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问
  • ¥15 初学者如何快速上手学习stm32?
  • ¥15 如何自动更换布娃娃图片上的衣服