Shell Calculation Tool Source code

Source: Internet
Author: User
Tags greatest common divisor

#!/bin/bash# defines greatest common divisor and the minimum number of conventions gcd_result= "lcm_result=" "#定义一个相加的函数function  sum ()  { result=$ (  num1+num2 )   echo -e  "\033[31m  two number of $result \033[0m"}# Define a greatest common divisor function function divisor ()  { if [  "$num 1"  -eq  "$num 2"  ];     then        s_great= $num 1       s_small= $num 2       #export  s_great        #export  s_small fi   if [  "$num 1"  -gt  "$num 2"  ];     then        s_great= $num 1        s_small= $num 2   else       s_great=$ Num2       s_small= $num 1   fi  i=1    gcd_result=1   greattmp=1&nbSp;  smalltmp=1   lcm_result=1   export gcd_result while  [  $i  -le  "$s _small"  ]   do       greattmp= ' expr  $s _great %  $i '       smalltmp= ' expr  $s _small  %  $i '       if [ ${greattmp} -eq 0 ];  Then           if [ ${smalltmp} -eq  0 ]; then               gcd_result=${i}          fi       fi      i= ' expr ${i} + 1 '    done    lcm_result= ' expr  $s _small /  $GCD _result '  lcm_result= ' expr  $LCM _result \*   $s _great '     #echo -e  "\033[31m  two numbers greatest common divisor for $s_great \033[0m"} function say ()  {    divisor   echo -e  "\033[31m  two numbers greatest common divisor for $gcd_result \033[0m"}# Define a least common multiple function function multiple ()  {   divisor      echo  -e  "\033[31m  Two numbers least common multiple for $lcm_result \033[0m"}function main ()  {  echo   "---------------------------"   echo  "Enter the method you want to perform"   echo  "Input 1: Perform the Add function and return the sum of two"   echo  "Input 2: Execute greatest common divisor function, return two number greatest common divisor"   echo  "Input 3: Execute least common multiple function, return two number least common multiple"   echo   "Input 4: Exit"   read input  case  $input  in  1)   sum;;   2)   say;;   3)   multiple;;   4)   exit;;   esac }if [ $# != 2 ];then   echo  "command format is not,  Command format:   SH D.SH NUM1&NBSp;num2 "   exitfi# Determines whether the input is a number num= ' Expr $1 + 0 &>/dev/null ' if the returned state  [ $? -ne 0 ];then   echo  "num1  must be a number format"     exitfinum= ' Expr $2 + 0 &>/dev/null ' If [ $? -ne 0 ];then    echo  "num2  must be number format" fi# Display selection menu while truedo  num1=$1  num2=$ 2  maindone


Shell Calculation Tool Source code

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.