shell脚本编写简单计算器(编写一个SHELL脚本程序计算1到100的和)

本文目录
- 编写一个SHELL脚本程序计算1到100的和
- 在Linux下,用shell编写一个简单的计算器,要实现加减乘除4个功能就行了
- 编写Linux 的 shell语言 要求设计一个计算器,输入2个数可以计算 +
- 如何用shell编写一个简单的计算器
- 怎么用shell编写计算1+2+3…+n
编写一个SHELL脚本程序计算1到100的和
#!/bin/bash
j=0
for ((i=1;i《=100;i++));do
j=$(($i+$j))
done
echo $j
在Linux下,用shell编写一个简单的计算器,要实现加减乘除4个功能就行了
不用写吧,本来有个 bc 命令可用,没有下载就成.
非要写一个,zsh 的function里有一个,名 zcalc,
贴上来给你
#!/usr/bin/zsh -i
#
# Zsh calculator. Understands most ordinary arithmetic expressions.
# Line editing and history are available. A blank line or `q’ quits.
#
# Runs as a script or a function. If used as a function, the history
# is remembered for reuse in a later call (and also currently in the
# shell’s own history). There are various problems using this as a
# script, so a function is recommended.
#
# The prompt shows a number for the current line. The corresponding
# result can be referred to with $《line-no》, e.g.
# 1》 32 + 10
# 42
# 2》 $1 ** 2
# 1764
# The set of remembered numbers is primed with anything given on the
# command line. For example,
# zcalc ’2 * 16’
# 1》 32 # printed by function
# 2》 $1 + 2 # typed by user
# 34
# 3》
# Here, 32 is stored as $1. This works in the obvious way for any
# number of arguments.
#
# If the mathfunc library is available, probably understands most system
# mathematical functions. The left parenthesis must be adjacent to the
# end of the function name, to distinguish from shell parameters
# (translation: to prevent the maintainers from having to write proper
# lookahead parsing). For example,
# 1》 sqrt(2)
# 1.4142135623730951
# is right, but `sqrt (2)’ will give you an error.
#
# You can do things with parameters like
# 1》 pi = 4.0 * atan(1)
# too. These go into global parameters, so be careful. You can declare
# local variables, however:
# 1》 local pi
# but note this can’t appear on the same line as a calculation. Don’t
# use the variables listed in the `local’ and `integer’ lines below
# (translation: I can’t be bothered to provide a sandbox).
#
# Some constants are already available: (case sensitive as always):
# PI pi, i.e. 3.1415926545897931
# E e, i.e. 2.7182818284590455
#
# You can also change the output base.
# 1》
# 1》
# Changes the default output to hexadecimal with numbers preceded by `16#’.
# Note the line isn’t remembered.
# 2》
# 2》
# Change the default output base to hexadecimal with no prefix.
# 3》
# Reset the default output base.
#
# This is based on the builtin feature that you can change the output base
# of a given expression. For example,
# 1》 32 + 20 / 2
# 2A
# 2》
# prints the result of the calculation in hexadecimal.
#
# You can’t change the default input base, but the shell allows any small
# integer as a base:
# 1》 2#1111
# 15
# 2》 13#6 * 13#9
# 42
# and the standard C-like notation with a leading 0x for hexadecimal is
# also understood. However, leading 0 for octal is not understood --- it’s
# too confusing in a calculator. Use 8#777 etc.
#
# Options: -#《base》 is the same as a line containing just `,
# similarly -##《base》; they set the default output base, with and without
# a base discriminator in front, respectively.
#
#
# To do:
# - separate zcalc history from shell history using arrays --- or allow
# zsh to switch internally to and from array-based history.
emulate -L zsh
setopt extendedglob
local line ans base defbase forms match mbegin mend psvar optlist opt arg
local compcontext="-math-"
integer num outdigits outform=1
# We use our own history file with an automatic pop on exit.
history -ap "${ZDOTDIR:-$HOME}/.zcalc_history"
forms=( ’%2$g’ ’%.*g’ ’%.*f’ ’%.*E’ )
zmodload -i zsh/mathfunc 2》/dev/null
: ${ZCALCPROMPT="%1v》 "}
# Supply some constants.
float PI E
(( PI = 4 * atan(1), E = exp(1) ))
# Process command line
while ; do
optlist=${1}
shift
&& break
while ; do
opt=${optlist}
optlist=${optlist}
case $opt in
(’#’) # Default base
if ; then
arg=$optlist
optlist=
elif ; then
arg=$1
shift
else
print "-# requires an argument" 》&2
return 1
fi
if ; then
print - "-# requires a decimal number as an argument" 》&2
return 1
fi
defbase=""
;;
esac
done
done
for (( num = 1; num 《= $#; num++ )); do
# Make sure all arguments have been evaluated.
# The `$’ before the second argv forces string rather than numeric
# substitution.
(( argv ))
print "$num》 $argv"
done
psvar=$num
while vared -cehp "${(%)ZCALCPROMPT}" line; do
&& break
# special cases
# Set default base if `’ etc. on its own.
# Unset it if `’.
if ; then
if ; then
if ; then
defbase=
else
defbase=$match
fi
print -s -- $line
line=
continue
else
base=$match
fi
else
base=$defbase
fi
print -s -- $line
case ${${line###} in
q) # Exit if `q’ on its own.
return 0
;;
norm) # restore output format to default
outform=1
;;
sci#(#b)(《-》)(#B))
outdigits=$match
outform=2
;;
fix#(#b)(《-》)(#B))
outdigits=$match
outform=3
;;
eng#(#b)(《-》)(#B))
outdigits=$match
outform=4
;;
local(##*|))
eval $line
line=
continue
;;
*)
# Latest value is stored as a string, because it might be floating
# point or integer --- we don’t know till after the evaluation, and
# arrays always store scalars anyway.
#
# Since it’s a string, we’d better make sure we know which
# base it’s in, so don’t change that until we actually print it.
eval "ans=\$(( $line ))"
# on error $ans is not set; let user re-edit line
|| continue
argv=$ans
psvar=$num
;;
esac
if ; then
print -- $(( $base $ans ))
elif || (( outdigits )); then
printf "$forms\n" $outdigits $ans
else
printf "%d\n" $ans
fi
line=
done
return 0
支援小数点,+ - * / , ok
编写Linux 的 shell语言 要求设计一个计算器,输入2个数可以计算 +
printf "Please input the first number:"
read number1
printf "Please input the second number:"
read number2
echo "result = $"
这样写比较好看点,输入第一个数 之后 回车 会提示你输入第二个数,再回车就输出结果了!
如何用shell编写一个简单的计算器
如果只做四个简单的运算这应应该可以
shell脚本代码如下:
#!/bin/bash
read -p "input num1:" a
read -p "input num2:" b
read -p "input operator:" o
case $o in
+) let "res=a + b"
echo $res;;
-) let "res=a - b"
echo $res;;
/) awk ’BEGIN{printf "%.2f\n",’$a’/’$b’}’;;
*) let "res=a * b"
echo $res;;
esac
怎么用shell编写计算1+2+3…+n
shell脚本部分:
执行:
ojbk
#!/bin/bash
read -p "请输入数字n;" n
j=0
for ((i=1;i《=$n;i++));do
j=$(($i+$j))
done
echo $j

更多文章:
fontcreator怎么导入字体(如何在FontCreator中加入我想要的字)
2025年7月10日 20:00
wordpress主题二次元(怎么用wordpress搭建情侣博客怎么自己弄情侣主题求达人详细赐教!)
2025年7月4日 02:00
腾讯会议怎么向主持人询问后台记录并保存?手机储存内容会被APP浏览并上传保存APP后台服务器吗连着WIFI
2025年7月21日 04:00
正则高考复读学校(2022南京高三复读有哪些学校 南京复读学校名单)
2025年8月7日 13:30
verify例句(please-slide-to-verify是什么意思)
2025年12月5日 06:45
false的副词(英语试卷判断对错,T和F,哪个是对,那个是错)
2026年7月29日 00:00
go语言需要编译吗(Go语言能在安卓运行吗 Go是脚本语言还是汇编)
2026年3月18日 20:00
有哪些js框架(web前端三大主流框架是什么 都有哪些功能)
2025年9月22日 16:30
springfestival六年级作文(六年级过春节英语作文300字)
2026年2月27日 20:45
在java语言中下列说法正确的是(Java语言说法正确的有())
2026年9月9日 20:30
edittext下划线颜色(android 如何使EditText中不同行显示不同的颜色(两种颜色))
2026年5月15日 11:30
帝国cms手机端(请教帝国cms中的手机端同步插件bug 修复方法)
2026年1月18日 22:15











