编写shell脚本以打印菱形图 [英] Write a shell script to print a diamond figure
本文介绍了编写shell脚本以打印菱形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是家庭作业,并不是要欺骗任何人,但我没有在原始帖子中对此进行详细说明.
This is homework, didn't mean to trick anybody, but I didn't specify that in my original post.
写一个shell脚本来打印下图(不要硬编码).
Write a shell script to print the following figure (Do not hardcode).
http://cl.ly/1K0C1b2x3W2N3o3m1Y2y
推荐答案
一方面,我不喜欢给出答案.另一方面,我认为我已经使这个问题变得非常复杂,以至于试图弄清楚它的工作方式可能具有一定的教育价值.祝你好运!
On the one hand, I don't like to just give out an answer. On the other hand, I think I've made this sufficiently convoluted that, perhaps, there is some educational value in trying to figure out how it works. Good luck!
#!/bin/sh
while i=$( expr ${i-0} ${op-+} 1 ); do
j=$i
t=$( expr 2 \* $i - 1 )
printf %$( expr \( $( expr 2 \* ${1-6} - 1 ) - $t \) / 2 )s
while test $(( j-- )) -gt 0; do printf '* '; done
echo
test $i = ${1-6} && op=-
done
这是一个不错的变体:
#!/bin/sh
while i=$( expr ${i-0} ${op-+} 1 ); do
printf %$( expr $(( 2 * $(( ${1-6} - $i )) )) / 2 )s
printf \*%$(( $i - 1 ))s | sed 's/ / */g'
test $i = ${1-6} && op=-
done
这篇关于编写shell脚本以打印菱形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文