Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1706747
  • 博文数量: 171
  • 博客积分: 11553
  • 博客等级: 上将
  • 技术积分: 3986
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-25 20:28
文章分类

全部博文(171)

文章存档

2012年(2)

2011年(70)

2010年(9)

2009年(14)

2008年(76)

分类: Python/Ruby

2011-08-07 21:47:18

#!/bin/bash
#【程序71】
#题目:编写input()和output()函数输入,输出5个学生的数据记录。
#1.程序分析:
#2.程序源代码:
#array=(num0,name0,score0[0..2],num1,name1,score1[0..2]...)

N=5
student=()

function input(){
    for i in $(seq 0 $(($N - 1)));do
        read -p "input student number: " student[$((i*5 + 0))]
        eval "student$i[0]=$num"
        read -p "input student name: " student[$((i*5 + 1))]
        eval "student$i[1]=$name"
        for j in {0..2};do
           read -p "input score: " student[$((i*5 + 2 + j))]
        done
    done
}

function output(){
    for i in $(seq 0 $(($N - 1)));do
        echo "${student[$((i*5 + 0))]} ${student[$((i*5 + 1))]} \
        ${student[$((i*5 + 2))]} ${student[$((i*5 + 3))]} ${student[$((i*5+4))]}"
    done
}
function main(){
    input
    output
}

main#!/bin/bash
#【程序72】
#题目:创建一个链表。
#1.程序分析:           
#2.程序源代码:

#list=(id,name,value)
list=()
function add_node(){
   local id=$1
   local name=$2
   local value=$3

   len=${#list[*]}
   list[$(($len + 0))]=$id
   list[$(($len + 1))]=$name
   list[$(($len + 2))]=$value
   len=${#list[*]}
}

function del_node(){
    local id=$1
    for i in $(seq 0 $((${#list[*]}/3 - 1)) );do
        if [ ${list[$(($i*3))]} -eq $id ];then
            unset list[$((3*$i))]
            unset list[$((3*$i))]
            unset list[$((3*$i))]
            break
        fi
    done
}

function print_node(){
    local id=$1
    for i in $(seq 0 $((${#list[*]}/3 - 1)) );do
        if [ ${list[$(($i*3))]} -eq $id ];then
            echo "${list[$(($i*3))]} ${list[$(($i*3 + 1))]} ${list[$(($i*3 + 2))]}"
        fi
    done
}

function main(){
    add_node 1 book 1000
    add_node 2 river 20
    add_node 3 paper 300
    
    print_node 1
    print_node 3
    print_node 2

    del_node 2
    echo "${list[*]}"
}

main

#!/bin/bash
#【程序73】
#题目:反向输出一个链表。   
#1.程序分析:
#2.程序源代码
#list=(id name value)
list=(1 book 100 2 paper 200 3 river 300)

for i in $(seq $((${#list[*]}/3 - 1)) -1 0 );do
    echo "${list[$(($i * 3 + 0))]} ${list[$(($i * 3 + 1))]} ${list[$(($i * 3 + 2))]}"
done

#!/bin/bash
#【程序74】
#题目:连接两个链表。
#1.程序分析:
#2.程序源代码:

a=(1 2 3 4 5 6 7 8 9 0)
b=(a b c d e f g h i j)

a_len=${#a[*]}
b_len=${#b[*]}

echo "before:${a[*]}"

for i in $(seq 0 $(($b_len - 1)) );do
   a[$(($a_len + $i))]=${b[$i]}
done

echo "after:${a[*]}"

#!/bin/bash
#【程序75】
#题目:放松一下,算一道简单的题目。
#1.程序分析:
#2.程序源代码:

function main(){
    for i in {0..4};do
        n=0
        if [ $i -ne 1 ];then ((n++));fi
        if [ $i -eq 3 ];then ((n++));fi
        if [ $i -eq 4 ];then ((n++));fi
        if [ $i -ne 4 ];then ((n++));fi
        if [ $n -eq 3 ];then echo $((64 + $i)); fi
    done
}

main


阅读(803) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

网络安全服务2011-08-16 10:49:05