n2实例

2022-04-17 18:51:15   第一文档网     [ 字体: ] [ 阅读: ] [ 文档下载 ]
说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。下载word有问题请添加QQ:admin处理,感谢您的支持与谅解。点击这里给我发消息

#第一文档网# 导语】以下是®第一文档网的小编为您整理的《n2实例》,欢迎阅读!
实例

实例

ryj@ubuntu:~/test/lab2$ cat 2_1_1.tcl set foo "john"

puts "my name is $foo"

ryj@ubuntu:~/test/lab2$ ns 2_1_1.tcl my name is john

ryj@ubuntu:~/test/lab2$ cat 2_1_2.tcl set month 2 set day 3 set year 97

set date "$month:$day:$year" puts $date

ryj@ubuntu:~/test/lab2$ ns 2_1_2.tcl 2:3:97

ryj@ubuntu:~/test/lab2$ cat 2_1_3.tcl set foo "puts hi" eval $foo

ryj@ubuntu:~/test/lab2$ ns 2_1_3.tcl hi

ryj@ubuntu:~/test/lab2$ cat 2_2_1.tcl set value [expr 0==1] puts $value

ryj@ubuntu:~/test/lab2$ ns 2_2_1.tcl 0

ryj@ubuntu:~/test/lab2$ cat 2_2_2.tcl set value [expr 2>=1] puts $value

ryj@ubuntu:~/test/lab2$ ns 2_2_2.tcl 1

ryj@ubuntu:~/test/lab2$ cat 2_2_3.tcl set value [expr 2+3] puts $value

ryj@ubuntu:~/test/lab2$ ns 2_2_3.tcl



5

ryj@ubuntu:~/test/lab2$ cat 2_3_1.tcl

puts "I am [expr 10 * 2] years old, and my I.Q. is [expr 100 - 25]"

ryj@ubuntu:~/test/lab2$ ns 2_3_1.tcl I am 20 years old, and my I.Q. is 75

ryj@ubuntu:~/test/lab2$ cat 2_3_2.tcl set my_height 6.0

puts "If I was 2 inches taller, I would be [expr $my_height + (2.0 / 12.0)] feet tall"

ryj@ubuntu:~/test/lab2$ ns 2_3_2.tcl

If I was 2 inches taller, I would be 6.166666666666667 feet tall

ryj@ubuntu:~/test/lab2$ cat 2_4_1.tcl set my_planet "earth"

if {$my_planet == "earth"} { puts "I feel right at home."

} elseif {$my_planet == "venus"} { puts "This is not my home." } else {

puts "I am neither from Earth, nor from Venus." }

set temp 95

if {$temp < 80} {

puts "It's a little chilly." } else {

puts "Warm enough for me." }

ryj@ubuntu:~/test/lab2$ ns 2_4_1.tcl I feel right at home. Warm enough for me.

ryj@ubuntu:~/test/lab2$ cat 2_4_2.tcl set num_legs 6 switch $num_legs {

2 {puts "It could be a human."}

1 / 3


4 {puts "It could be a cow."} 6 {puts "It could be an ant."} 8 {puts "It could be a spider."}

default {puts "It could be anything."} }

ryj@ubuntu:~/test/lab2$ ns 2_4_2.tcl It could be an ant.

ryj@ubuntu:~/test/lab2$ cat 2_4_3.tcl for {set i 0} {$i < 5} {incr i 1} {

puts "In the for loop, and i == $i" }

ryj@ubuntu:~/test/lab2$ ns 2_4_3.tcl In the for loop, and i == 0 In the for loop, and i == 1 In the for loop, and i == 2 In the for loop, and i == 3 In the for loop, and i == 4

ryj@ubuntu:~/test/lab2$ cat 2_4_4.tcl set i 0

while {$i < 5} {

puts "In the while loop, and i == $i" incr i 1 }

ryj@ubuntu:~/test/lab2$ ns 2_4_4.tcl In the while loop, and i == 0 In the while loop, and i == 1 In the while loop, and i == 2 In the while loop, and i == 3 In the while loop, and i == 4

ryj@ubuntu:~/test/lab2$ cat 2_4_5.tcl foreach vowel {a e i o u} {

puts "$vowel is a vowel" }

ryj@ubuntu:~/test/lab2$ ns 2_4_5.tcl a is a vowel



e is a vowel i is a vowel o is a vowel u is a vowel

ryj@ubuntu:~/test/lab2$ cat 2_5_1.tcl proc sum_proc {a b} { return [expr $a + $b] }

proc magnitude {num} { if {$num > 0} { return $num }

set num [expr $num * (-1)] return $num }

set num1 12 set num2 14

set sum [sum_proc $num1 $num2]

puts "The sum is $sum"

puts "The magnitude of 3 is [magnitude 3]" puts "The magnitude of -2 is [magnitude -2]"

ryj@ubuntu:~/test/lab2$ ns 2_5_1.tcl The sum is 26

The magnitude of 3 is 3 The magnitude of -2 is 2

ryj@ubuntu:~/test/lab2$ cat 2_5_2.tcl proc dumb_proc {} { set myvar 4

puts "The value of the local variable is $myvar"

global myglobalvar

puts "The value of the global variable is $myglobalvar" }

set myglobalvar 79 dumb_proc

2 / 3




ryj@ubuntu:~/test/lab2$ ns 2_5_2.tcl The value of the local variable is 4 The value of the global variable is 79

ryj@ubuntu:~/test/lab2$ cat 2_6_1.tcl set myarray(0) "Zero" set myarray(1) "One" set myarray(2) "Two"

for {set i 0} {$i < 3} {incr i 1} { puts $myarray($i) }

ryj@ubuntu:~/test/lab2$ ns 2_6_1.tcl Zero One Two

ryj@ubuntu:~/test/lab2$ cat 2_6_2.tcl set person_info(name) "Fred Smith" set person_info(age) "25"

set person_info(occupation) "Plumber"

foreach thing {name age occupation} { puts "$thing == $person_info($thing)" }

ryj@ubuntu:~/test/lab2$ ns 2_6_2.tcl name == Fred Smith age == 25

occupation == Plumber

ryj@ubuntu:~/test/lab2$ cat 2_6_3.tcl set person_info(name) "Fred Smith" set person_info(age) "25"

set person_info(occupation) "Plumber"

foreach thing [array names person_info] { puts "$thing == $person_info($thing)" }

ryj@ubuntu:~/test/lab2$ ns 2_6_3.tcl occupation == Plumber



age == 25

name == Fred Smith

ryj@ubuntu:~/test/lab2$ cat 2_7_1.tcl set str "This is a string" puts "The string is: $str"

puts "The length of the string is: [string length $str]"

puts "The character at index 2 is: [string index $str 2]"

puts "The characters from index 4 through 8 are: [string range $str 4 8]"

puts "The index of the first occurrence of letter \"i\" is: [string first i $str]"

ryj@ubuntu:~/test/lab2$ ns 2_7_1.tcl The string is: This is a string The length of the string is: 16 The character at index 2 is: i

The characters from index 4 through 8 are: is a

The index of the first occurrence of letter "i" is: 2

ryj@ubuntu:~/test/lab2$ cat 2_8_1.tcl set f [open "/tmp/myfile" "w"]

puts $f "We live in Texas. It's already 110 degrees out here." puts $f "456" close $f

ryj@ubuntu:~/test/lab2$ ns 2_8_1.tcl

3 / 3


本文来源:https://www.dywdw.cn/866cf8f8770bf78a6529544e.html

相关推荐
推荐阅读