Chinaunix

标题: 求出 小弟初学 [打印本页]

作者: hzy12359    时间: 2011-02-25 02:32
标题: 求出 小弟初学
Write a shell script to simulate a simple calculator. Sample output is as follows.
建一个脚本 简单的计算器 运行如下
tsaiy:/./calculator
enter the first number
6
enter the second number
8
enter operand
*
the result is 48
tsaiy:/./calculator
enter the first number
50
enter the second number
25
enter operand
/
the result is 2
作者: lkk2003rty    时间: 2011-02-25 09:08
做作业?
作者: hzy12359    时间: 2011-02-25 09:26
不是呀 练习题
作者: lkk2003rty    时间: 2011-02-25 09:42
  1. #!/bin/bash
  2. echo "enter the first number"
  3. read a
  4. echo "enter the second number"
  5. read b
  6. echo "enter operand"
  7. read oper
  8. case "$oper" in
  9. "+" )
  10.     ((b=a+b))
  11.    echo "the result is $b"
  12.    ;;
  13. "-" )
  14.     ((b=a-b))
  15.     echo "the result is $b"
  16.     ;;
  17. "*" )
  18.     ((b=a*b))
  19.     echo "the result is $b"
  20.     ;;
  21. "/" )
  22.     if [ "$b" -eq "0" ]
  23.     then
  24.         echo "Error"
  25.     else
  26.         ((b=a/b))
  27.         echo "the result is $b"
  28.     fi
  29.     ;;
  30. *   )
  31.     echo "Error"
  32.     ;;
  33. esac
复制代码
回复 3# hzy12359
作者: wtuter    时间: 2011-02-25 12:51
回复 1# hzy12359


    好好做作业吧,出来了有用,别浪费了学习的机会




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2