Overview
Chicken rabbit with Cage, is one of the famous classic fun, recorded in the grandson of the book, the title of "There are several chickens and rabbits in a cage, from the top number, there are 35 heads, from the following number, there are 94 feet." Q. How many chickens and rabbits are there in the cage? ”。 Chicken rabbit with cage problem, is the primary School Olympiad number of the common questions, is also a common problem in computer programming algorithms, the beginning of the shell script learning, so it may be useful to use the script to write a calculation of the problem of scripts, to deepen their understanding of the Linux shell script.
Algorithm
Before doing a math problem, the idea is extremely important, how to solve this problem, how to follow the computer language thinking to help us calculate becomes the first priority. We understand that this is a typical two-yuan one-time equation, assuming that the rabbit is X, the chicken is y, get the equation (1) x+y=35, (2) 4x+2y=94, (2) -2* (1) can get the value of 2x=24,x=12, and thus get y=23, but how do we express the meaning in the scripting language?
The amount given in the topic is 35 head, 94 feet, asked to calculate the number of rabbits and chickens, so we can think of rabbits and chickens as a computer to calculate the number, head and feet are required to manually enter the number, not to say the title to 94, 35, the script can only be calculated according to 94 and 35, That's a waste of resources, isn't it?
There are a lot of ideas online, but we can see that the above equation (2) divided by 2 minus the equation (1) can also get a small animal number, all our idea is to set the variable feet to the number of feet, set the variable head as the number of heads, so you can have $feet/2-$head Can come out one minute the number of small animals, the rest of the value of $head to reduce, the other small number of animals with it.
Programming
After getting the above idea, the programming can begin, the first step, let someone enter the number of head and the number of feet
Read-p "Input the Sumheads:" Head
Read-p "Input the Sumfeet:" Feet
#输入一共有多少个头, how many feet
Okay, so we can figure it out next? No! Computer thinking is not like our human thinking naturally with limited scope, a little attention on the error, all the first step or check the input is not the number it!
[["$head" =~ ^[1-9][0-9]*$]] && [[$feet] =~ ^[1-9][0-9]*$] | | {echo wrong format; exit;}
#检查输入的任意一个值是否为数字, not to report the error format and exit, exit is the entire script exits, not the current command exits to continue execution, so use curly braces around
You can determine that the value is there, but it is not enough, if it is malicious input it? More head than feet? The chicken legs are the KFC old man took to make the set meal? So take another step, insurance.
["$head"-GT "$feet"] && {echo input the corrected number; exit;}
#检查输入头的数量是否大于脚的数量, if greater than, the value of the reported error
This is always the awaited Shi out of the pendulum, right? So, okay.
chicken=$[$[$feet/2]-$head]
rabbits=$[$head-$chicken]
############################
c=$[$chicken]
h=$[$rabbits]
tmp=$[$[$c + $h]/$feet]
If the words ' # ' above can be seen as a calculation based on previous ideas, what are the following? Answer: I want to go.
It is not difficult to find, ' # ' under the $tmp as a temporary variable, calculated meaning is calculated the number of feet divided by the number of input feet, do this is to find out whether the input value is in line with the normal operation, in the final analysis or is not a fault input, all code for
chicken=$[$[$feet/2]-$head]
rabbits=$[$head-$chicken]
c=$[$chicken]
h=$[$rabbits]
tmp=$[$[$c + $h]/$feet]
[ ! "$tmp" = = 1] && echo there must be some alien steal my Beasty
#检查输入, if a value of 1 indicates that the number of heads and feet is paired, otherwise a hint is given, and the calculated value may not be the answer you want.
The output is shown on the
Echo chicken= $chicken
Echo rabbits= $rabbits
OK, also is barely calculate out, shell script does not support decimal point, originally also want to separate to $tmp>1 and $tmp<1 situation make different explanation different operation, perhaps at present my level still do not know whether can exist 0< $tmp <1 situation, This is for me $tmp<1 situation is cool, finally attached to various input conditions
Linux Script Basics-chicken and rabbit cage problem