Basic procedural exercises under the shell

Source: Internet
Author: User

1. Simple progress bar Program

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/F2/wKiom1dmiCfizPKcAABUqdm5Mxw224.png-wh_500x0-wm_3 -wmp_4-s_2159390602.png "title=" bar. png "alt=" wkiom1dmicfizpkcaabuqdm5mxw224.png-wh_50 "/>

Lable array is equivalent to the progress bar forward, the refreshed circle, progress bar to show progress, with count% display

Program run Result: 650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/82/F2/wKiom1dmmevzR36jAAAK-J8K68U715.png-wh_ 500x0-wm_3-wmp_4-s_2478296629.png "title=" PROCB. PNG "width=" 952 "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:952px;height:40px; "alt=" Wkiom1dmmevzr36jaaak-j8k68u715.png-wh_50 "/>

The summation between the 2.1-100

1> with while implementation 2>

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/82/F2/wKiom1dmnRfTv5ZMAAAjgZ6mYNU881.png-wh_500x0-wm_3 -wmp_4-s_1941125326.png "title=" While1. PNG "alt=" wkiom1dmnrftv5zmaaajgz6mynu881.png-wh_50 "/> 650" this.width=650; "src=" http://s4.51cto.com/wyfs02/ M00/82/f2/wkiom1dmnldxnh-2aaazliuqrqi406.png-wh_500x0-wm_3-wmp_4-s_1567897641.png "title=" While1. PNG "alt=" Wkiom1dmnldxnh-2aaazliuqrqi406.png-wh_50 "/>

Program Run Result:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/82/F1/wKioL1dmnVbiRqQoAAA46TfWdYM822.png-wh_500x0-wm_3 -wmp_4-s_2390456978.png "title=" 1-100while. PNG "alt=" Wkiol1dmnvbirqqoaaa46tfwdym822.png-wh_50 "/>

3> Recursive implementation

Digui

function Add ()

{

Local val=$1

Local sum=0

Local add1=0

Local add2=0

If [$val-eq 1];then

Echo 1

Return

Fi

add1= $val

Let val--

add2=$ (add $val)

Let sum= $add 1+ $add 2

Echo $sum

}

function Main ()

{

Read n

Add $n

}

Main

Program Run Result:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/82/F1/wKioL1dmnsfzqUEcAAARNfe6nAQ125.png-wh_500x0-wm_3 -wmp_4-s_2237051162.png "title=" 1-100 Digui. PNG "alt=" Wkiol1dmnsfzquecaaarnfe6naq125.png-wh_50 "/>

4> using SEQ and BC for summation

Seq N: Generate all numbers to this number

|BC: Use |BC to sum

For example:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/82/F2/wKiom1dmoA-RYiXQAAAaecb78dQ855.png-wh_500x0-wm_3 -wmp_4-s_532952954.png "title=" BC seq. PNG "alt=" Wkiom1dmoa-ryixqaaaaecb78dq855.png-wh_50 "/>

So 1-100 sums can be written like this:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/F1/wKioL1dmoFyz2SPpAABaQjNVLQU285.png-wh_500x0-wm_3 -wmp_4-s_158030029.png "title=" 1-100BC. PNG "alt=" Wkiol1dmofyz2sppaabaqjnvlqu285.png-wh_50 "/>

Where tr represents substitution , seq comes out of all the numbers have newline characters, by TR will ' \ n ' replaced by ' + '. The next sed is to replace the last ' + ' with ' \ n ' and then sum it through BC.

3. To find the nth Fibonacci number

1> Non-recursive implementation

Locally modified variables are local variables

the function fib ()

87 {

Val=$1 Local

[$val-eq 0-o $val-eq 1]

Do

Echo 1

Return

Do

94 I=1

J=1

[$val-ge 2]

Do

fibn= $i + $j

I= $j

101 j= $fibn

102 ((val--))

103 Done

105}

106 function Main ()

107 {

108 Read n

109 Fib $n

Echo $fibn

112}

113 Main

2> Recursive implementation

Thought: When N=1 or n=0, return 1. Since the nth Fibonacci number is the sum of the number of n-1 and n-2, the value of the N value passed is first minus 1 in the recursive save values, and then the N minus 1 recursively save the value, Nth is the sum of two of the depositary.


The function fib ()

116 {

117 Local Val=$1

118 Local Fib1=0

119 Local Fib2=0

Fibc=0

121 If [$val-eq 0-o $val-eq 1];then

122 Echo 1

123 return

124 fi

val--

126 fib1=$ (fib $val)

127 Let val--

fib2=$ (fib $val)

129 let fibc= $fib 1+ $fib 2

Echo $FIBC

131

132}

133

134 Function Main ()

135 {

136 Read N

137 fib $n

138

139}

Main

Program Run Result:

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/82/F1/wKioL1dmpEfAmhS9AAA6h8dQagE616.png-wh_500x0-wm_3 -wmp_4-s_239409028.png "title=" fib d.png "alt=" Wkiol1dmpefamhs9aaa6h8dqage616.png-wh_50 "/>





This article is from the "Output Diamond pattern" blog, so be sure to keep this source http://10541571.blog.51cto.com/10531571/1790879

Basic procedural exercises under the shell

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.