A simple calculator implemented with a shell

Source: Internet
Author: User


#!/bin/bash
#echo "1.$12.$23.$3"
If ["$" = "+"]
Then
Res= ' expr $ + $ '
echo ">>the result is: $RES"

elif ["$" = "-"]
Then
Res= ' expr $-$ '
echo ">>the result is: $RES"

#这里说明下, using "x" instead of "*"

#是因为在命令行输入 *,shell parsing problem,

#* cannot be passed to the script as an argument, so it's replaced with X,

#这样能规避这个问题, but I still don't know * the command line cannot be passed to the script,

#那位大侠能帮忙解释下, thank you very much ~ elif ["$" = "X"]
Then
res= ' Expr $ \* $
echo ">>the result is: $RES"

elif ["$" = "%"]

Then

If ["$" = "0"] # #判断除数是否为0
Then
echo "Input error,division by 0"
Exit
Fi
big= ' expr $/$ ' # #取整
Small= ' expr $ ' # #取余
Long=6 # #小数点后保留的位数
Count=0 # #循环的计数器, to be less than long
Res1=0 # #


# #无限循环, the goal is that if the small is not zero, the method of calculating the remainder is

# #在SMALL后加上LONG个0, divide by divisor, long is the exact number of digits after the decimal point
While:
Todo
Small= ' expr $SMALL \* 10 '
Count= ' expr $COUNT + 1 '

if [$COUNT = = $LONG]
Then
Break
Fi
Done

res1= ' expr $SMALL/$ ' # #到此余数就计算好了
echo ">>the result is: $BIG. $RES 1" # #把整数部分和余数部分拼接起来输出就是结果了

elif ["$" = "--help"]
Then
echo "Input format:"
echo "calculator.sh x + y"
echo "calculator.sh xy"
echo "calculator.sh x x Y"
echo "calculator.sh x y"
echo "Calculator.sh--help"

Else
echo "Input format error!"

Fi





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.