linux脚本 输出两个数字,按照数字从小到大排序,比如输入:31,12,输出结果就是12,31

linux脚本 输出两个数字,按照数字从小到大排序,比如输入:31,12,输出结果就是12,31,第1张

[root@localhost ~]# a=""\

>b=""\

>tmp=""\

>read x\

>a=${x%%,*}\

>b=${x##*,}\

>if [ $a >$b ] then \

> tmp=$a\

> a=$b\

> b=$tmp\

>fi\

>echo $a $b

34,12

12 34

[root@localhost ~]#

可以看出我是直接把代码粘到终端上运行的,所以每一行后都有分号作语句分隔用,然后又用\断行,其实上面这么多行,是在同一行内。

每行前的 >是bash shell自动给出,因为它知道代码还没输完。你只要把下面这样的代码复制上去也就可以:

a=""\

b=""\

tmp=""\

read x\

a=${x%%,*}\

b=${x##*,}\

if [ $a >$b ] then \

tmp=$a\

a=$b\

b=$tmp\

fi\

echo $a $b

用history命令可以查询到我确实就相当于输入在同一行:

1022 a=""b=""tmp=""read xa=${x%%,*}b=${x##*,}if [ $a >$b ] thentmp=$a a=$b b=$tmpfiecho $a $b

1023 history

[root@localhost ~]#

int main () {

float a, b, res

char op[] = "+-*/", *p =op

scanf("%f %f", &a, &b)

while (*p) {

switch(*p) {

case '+': res = a+b break

case '-': res = a+b break

case '*': res = a+b break

case '/': if (b)res = a/b break

}

printf("%f %c %f = %f\n", a, *p, b, res)

p++

}

return 0

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8797687.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-21
下一篇 2023-04-21

发表评论

登录后才能评论

评论列表(0条)

保存