tcl编程练习1
发布日期:2022-01-31 02:52:45
浏览次数:6
分类:技术文章
本文共 1540 字,大约阅读时间需要 5 分钟。
1.用函数来实现对任意长度任意数列进行排序的功能,要求通过参数来选择从大到小排序,还是从小到大排序; 并将结果输出到一个文件里。例如,当输入2,9,4,3时,从大到小排序输出结果为:9,4,3,2。
set string { 2,9,4,3}set newlist_file [open "newlist.txt" w] #w,以只写方式写入文件set list [split $string ,]puts $listputs "please select mode:" #选择升序还是降序排列set mode [gets stdin] #从键盘读取输入if { [string match "up" $mode]} { set newlist [join [lsort -integer -increasing $list] ","]puts $newlist puts $newlist_file $newlist } elseif { [string match "down" $mode]} { set newlist [join [lsort -integer -decreasing $list] ","]puts $newlist puts $newlist_file $newlist }close $newlist_file
程序运行结果
[li@localhost test]$ tclsh test.tcl2 9 4 3please select mode:up2,3,4,9[li@localhost test]$ tclsh test.tcl2 9 4 3please select mode:down9,4,3,2[li@localhost test]$
2.在第一道题的基础上,去掉数列中重复的数字,即每个数字只能出现一次。如9,4,3,2,2的输出结果应该是9,4,3,2
set string { 2,2,9,4,3}set newlist_file [open "newlist.txt" w]set list [split $string ,]puts $listputs "please select mode:"set mode [gets stdin]if { [string match "up" $mode]} { set newlist [join [lsort -unique -integer -increasing $list] ","] #加上unique选项puts $newlist puts $newlist_file $newlist } elseif { [string match "down" $mode]} { set newlist [join [lsort -unique -integer -decreasing $list] ","]puts $newlist puts $newlist_file $newlist }close $newlist_file
程序运行结果
[li@localhost test]$ tclsh test.tcl2 2 9 4 3please select mode:up2,3,4,9[li@localhost test]$ tclsh test.tcl2 2 9 4 3please select mode:down9,4,3,2[li@localhost test]$
更多技术干货,欢迎关注微信公众号:IC小白
转载地址:https://blog.csdn.net/qq_36480087/article/details/108502354 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月10日 19时43分39秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
计算机二级C语言:大题程序修改题
2019-04-26
Android Studio 安卓手机上实现火柴人动画(Java源代码—Python)
2019-04-26
SpringMVC快速入门(2)商品列表的加载
2019-04-26
SpringMVC快速入门(3)默认组件加载
2019-04-26
Java 解决SpringMVC的post请求乱码的问题
2019-04-26
(精华)2020年7月30日 微信小程序 视图容器
2019-04-26
(精华)2020年7月30日 微信小程序 自带图标和外部图标的使用
2019-04-26
(精华)2020年7月30日 微信小程序 进度条的使用
2019-04-26
(精华)2020年7月30日 微信小程序 富文本和文本的使用
2019-04-26
(精华)2020年7月30日 微信小程序 富文本编辑器的使用
2019-04-26
(精华)2020年7月30日 微信小程序 选择器的使用
2019-04-26
(精华)2020年7月30日 微信小程序 内置插件的使用
2019-04-26
(精华)2020年7月31日 React setstate原理详解
2019-04-26
(精华)2020年7月31日 React 虚拟dom的渲染机制和性能调优
2019-04-26
(精华)2020年7月31日 React 手写ssr服务端渲染
2019-04-26
(精华)2020年7月31日 Typescript 基本配置
2019-04-26