Discuz!官方免费开源建站系统

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索

一个shell写的加法机,请高手帮忙修改一下

[复制链接]
xlink 发表于 2006-4-8 03:32:28 | 显示全部楼层 |阅读模式
shell初学者,不要见笑。请高手帮忙修正一下:
已知问题:
1.不能屏蔽英文字符和符号的输入。
2.减法错误。

一共两个文件,addrobot和numf
addrobot内容:
  1. #!/bin/sh
  2. clear
  3. cat /dev/null > num.txt
  4. cat /dev/null > numf.txt
  5. while [ TRUE ]
  6. do
  7. clear
  8. line=`wc -l num.txt|awk '{ print $1 }'`
  9. sum=`awk 'BEGIN { sum = 0 } { sum = sum + $1} END { OFMT = "%.2f"; print sum/100; }' num.txt`
  10. tput cup 25 20
  11. echo $line行                        总计:$sum
  12. tput cup 1 8
  13. echo  ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
  14. echo -----------------------------------------------------xlink-----------------
  15. for x in 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  16. do
  17. tput cup $x 4
  18. echo 数据录入:
  19. tput cup $x 14
  20. read num
  21. if [ $num = q 2>/dev/null ]
  22. then
  23. exit
  24. elif [ -z $num ]
  25. then
  26. tput cup $x 14
  27. echo "非法数值"
  28. elif [ $num -eq 0 2>/dev/null ]
  29. then
  30. tput cup $x 14
  31. echo "非法数值"
  32. else
  33. tput cup $x 40
  34. echo $num>tmp
  35. echo $num>>num.txt
  36. No=`wc -l num.txt|awk '{print $1}'`
  37. echo No.$No
  38. line=`wc -l num.txt|awk '{ print $1 }'`
  39. sum=`awk 'BEGIN { sum = 0 } { sum = sum + $1} END { OFMT = "%.2f"; print sum/100; }' num.txt`
  40. tput cup 25 20
  41. echo 共$line行                        总计:$sum
  42. tput cup $x 48
  43. ./numf
  44. tput cup $x 14
  45. fi
  46. done
  47. done
复制代码


numf内容:
  1. #!/bin/sh
  2. n=`awk '{print length($1)}' tmp`
  3. if [ $n = 1 ]
  4. then read num<tmp
  5. echo 0.0$num
  6. echo 0.0$num>>numf.txt
  7. elif [ $n = 2 ]
  8. then read num<tmp
  9. echo 0.$num
  10. echo 0.$num>>numf.txt
  11. elif [ $n = 3 ]
  12. then read num<tmp
  13. na=`echo $num|cut -c1`
  14. nb=`echo $num|cut -c2-3`
  15. echo ${na}.${nb}
  16. echo ${na}.${nb}>>numf.txt
  17. elif [ $n = 4 ]
  18. then read num<tmp
  19. na=`echo $num|cut -c1-2`
  20. nb=`echo $num|cut -c3-4`
  21. echo ${na}.${nb}
  22. echo ${na}.${nb}>>numf.txt
  23. elif [ $n = 5 ]
  24. then read num<tmp
  25. na=`echo $num|cut -c1-3`
  26. nb=`echo $num|cut -c4-5`
  27. echo ${na}.${nb}
  28. echo ${na}.${nb}>>numf.txt
  29. elif [ $n = 6 ]
  30. then read num<tmp
  31. na=`echo $num|cut -c1`
  32. nb=`echo $num|cut -c2-4`
  33. nc=`echo $num|cut -c5-6`
  34. echo ${na},${nb}.${nc}
  35. echo ${na},${nb}.${nc}>>numf.txt
  36. elif [ $n = 7 ]
  37. then read num<tmp
  38. na=`echo $num|cut -c1-2`
  39. nb=`echo $num|cut -c3-5`
  40. nc=`echo $num|cut -c6-7`
  41. echo ${na},${nb}.${nc}
  42. echo ${na},${nb}.${nc}>>numf.txt
  43. elif [ $n = 8 ]
  44. then read num<tmp
  45. na=`echo $num|cut -c1-3`
  46. nb=`echo $num|cut -c4-6`
  47. nc=`echo $num|cut -c7-8`
  48. echo ${na},${nb}.${nc}
  49. echo ${na},${nb}.${nc}>>numf.txt
  50. elif [ $n = 9 ]
  51. then read num<tmp
  52. na=`echo $num|cut -c1`
  53. nb=`echo $num|cut -c2-4`
  54. nc=`echo $num|cut -c5-7`
  55. nd=`echo $num|cut -c8-9`
  56. echo ${na},${nb},${nc}.${nd}
  57. echo ${na},${nb},${nc}.${nd}>>numf.txt
  58. elif [ $n = 10 ]
  59. then read num<tmp
  60. na=`echo $num|cut -c1-2`
  61. nb=`echo $num|cut -c3-5`
  62. nc=`echo $num|cut -c6-8`
  63. nd=`echo $num|cut -c9-10`
  64. echo ${na},${nb},${nc}.${nd}
  65. echo ${na},${nb},${nc}.${nd}>>numf.txt
  66. elif [ $n = 11 ]
  67. then read num<tmp
  68. na=`echo $num|cut -c1-3`
  69. nb=`echo $num|cut -c4-6`
  70. nc=`echo $num|cut -c7-9`
  71. nd=`echo $num|cut -c10-11`
  72. echo ${na},${nb},${nc}.${nd}
  73. echo ${na},${nb},${nc}.${nd}>>numf.txt
  74. elif [ $n = 12 ]
  75. then read num<tmp
  76. na=`echo $num|cut -c1`
  77. nb=`echo $num|cut -c2-4`
  78. nc=`echo $num|cut -c5-7`
  79. nd=`echo $num|cut -c8-10`
  80. ne=`echo $num|cut -c11-12`
  81. echo ${na},${nb},${nc},${nd}.${ne}
  82. echo ${na},${nb},${nc},${nd}.${ne}>>numf.txt
  83. elif [ $n = 13 ]
  84. then read num<tmp
  85. na=`echo $num|cut -c1-2`
  86. nb=`echo $num|cut -c3-5`
  87. nc=`echo $num|cut -c6-8`
  88. nd=`echo $num|cut -c9-11`
  89. ne=`echo $num|cut -c12-13`
  90. echo ${na},${nb},${nc},${nd}.${ne}
  91. echo ${na},${nb},${nc},${nd}.${ne}>>numf.txt
  92. elif [ $n = 14 ]
  93. then read num<tmp
  94. na=`echo $num|cut -c1-3`
  95. nb=`echo $num|cut -c4-6`
  96. nc=`echo $num|cut -c7-9`
  97. nd=`echo $num|cut -c10-12`
  98. ne=`echo $num|cut -c13-14`
  99. echo ${na},${nb},${nc},${nd}.${ne}
  100. echo ${na},${nb}.${nc},${nd}.${ne}>>numf.txt
  101. else
  102. :
  103. fi
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Discuz! 官方站 ( 皖ICP备16010102号 )star

GMT+8, 2024-12-28 11:04 , Processed in 0.020981 second(s), 4 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表