问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

在Linux下,用shell编写一个简单的计算器,要实现加减乘除4个功能就行了...

发布网友 发布时间:2022-04-24 10:01

我来回答

2个回答

热心网友 时间:2023-01-22 04:21

不用写吧,本来有个 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> [#16]
# 1>
# Changes the default output to hexadecimal with numbers preceded by `16#'.
# Note the line isn't remembered.
# 2> [##16]
# 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> [##16] 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] 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 `[#<base>],
# 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 [[ -n $1 && $1 = -(|[#-]*) ]]; do
optlist=${1[2,-1]}
shift
[[ $optlist = (|-) ]] && break
while [[ -n $optlist ]]; do
opt=${optlist[1]}
optlist=${optlist[2,-1]}
case $opt in
('#') # Default base
if [[ -n $optlist ]]; then
arg=$optlist
optlist=
elif [[ -n $1 ]]; then
arg=$1
shift
else
print "-# requires an argument" >&2
return 1
fi
if [[ $arg != (|\#)[[:digit:]]## ]]; then
print - "-# requires a decimal number as an argument" >&2
return 1
fi
defbase="[#${arg}]"
;;
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[$num] = $argv[$num] ))
print "$num> $argv[$num]"
done

psvar[1]=$num
while vared -cehp "${(%)ZCALCPROMPT}" line; do
[[ -z $line ]] && break
# special cases
# Set default base if `[#16]' or `[##16]' etc. on its own.
# Unset it if `[#]' or `[##]'.
if [[ $line = (#b)[[:blank:]]#('[#'(\#|)(<->|)']')[[:blank:]]#(*) ]]; then
if [[ -z $match[4] ]]; then
if [[ -z $match[3] ]]; then
defbase=
else
defbase=$match[1]
fi
print -s -- $line
line=
continue
else
base=$match[1]
fi
else
base=$defbase
fi

print -s -- $line

case ${${line##[[:blank:]]#}%%[[:blank:]]#} in
q) # Exit if `q' on its own.
return 0
;;
norm) # restore output format to default
outform=1
;;
sci[[:blank:]]#(#b)(<->)(#B))
outdigits=$match[1]
outform=2
;;
fix[[:blank:]]#(#b)(<->)(#B))
outdigits=$match[1]
outform=3
;;
eng[[:blank:]]#(#b)(<->)(#B))
outdigits=$match[1]
outform=4
;;
local([[:blank:]]##*|))
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
[[ -n $ans ]] || continue
argv[num++]=$ans
psvar[1]=$num
;;
esac
if [[ -n $base ]]; then
print -- $(( $base $ans ))
elif [[ $ans = *.* ]] || (( outdigits )); then
printf "$forms[outform]\n" $outdigits $ans
else
printf "%d\n" $ans
fi
line=
done

return 0

支援小数点,+ - * / , ok

热心网友 时间:2023-01-22 05:39

#!/bin/sh
# 例如 1 + 2
# $1: 1
# $2: +
# $3: 2
# $$表示参数个数
# 使用方法: ./jsq 1 + 2

if [ $$ -lt 3 ]
then
echo "Too few args!"
exit 1
fi
case $2
"+")
a=(($1+$3))
;;
"-")
a=(($1-$3))
;;
"*")
a=(($1*$3))
;;
"/")
a=(($1/$3))
;;
*)
;;
esac
echo $a
exit 0
在Linux下,用shell编写一个简单的计算器,要实现加减乘除4个功能就行了...

local compcontext="-math-"integer num outdigits outform=1

怎样用shell语言实现小数的加减乘除运算

原来我拿shell写的计算器:[root@liuxitingtestdir]#catcalculator.sh#!/bin/bashecho"usage:1+3,qisquit"while[1]doread-p"-&gt;&gt;"str1&gt;&gt;/dev/nulla=`echo$str|awk-F'+|-|*|/''{print$1}'`if[$a==q]thenbreakfib=`echo$str|awk-F'+|-|*|/''{print$2}'`o=`echo$str|grep-...

linux除法运算linux除法

Linux网络操作系统的问题,一,编写shell脚本实现加减乘除运算。实现方法不限,但尽可能减少出错机率?setd=0;if then echo$3 d=$(($1-$2))fi echo$d linux正反斜杠的用法?1正斜杠(/)和反斜杠()都是在Linux系统中用于路径分隔符的符号。2在Linux系统中,正斜杠(/)用于分隔目录,反斜杠(...

1.linux系统下shell脚本用case语句编写四则运算 2.linux系统下shell脚 ...

原来我拿shell写的计算器:[root@liuxiting testdir]# cat calculator.sh !/bin/bash echo "usage: 1+3 &lt;Enter&gt; ,q &lt;Enter&gt; is quit"while [ 1 ]do read -p "-&gt;&gt;" str 1&gt;&gt;/dev/null a=`echo $str |awk -F '+|-|*|/' '{print $1}'`if [ $a == q ]then break fi ...

linux加减乘除命令linux加减

Linux中并没有直接的命令用于执行加减乘除操作。Linux作为一个操作系统,其核心功能并不包括直接的算术运算命令。然而,你可以通过几种不同的方法在Linux中进行基本的数学运算。使用Shell的内置算术运算:在bash shell中,你可以使用`$(( ))`结构进行算术运算。例如:bash result=$((2 + 3))echo $...

linux加减乘除命令linux加减

Linux网络操作系统的问题,一,编写shell脚本实现加减乘除运算。实现方法不限,但尽可能减少出错机率?setd=0;if then echo$3 d=$(($1-$2))fi echo$d linux常用命令有哪些?Linux是我们开发人员必不可少的系统,也是经常接触到的。然而,Linux命令比较多,有些不常用也难记住。那么,我们如何更...

编写Linux 的 shell语言 要求设计一个计算器,输入2个数可以计算 +

printf "Please input the first number:"read number1 printf "Please input the second number:"read number2 echo "result = $[$number1+$number2]"这样写比较好看点,输入第一个数 之后 回车 会提示你输入第二个数,再回车就输出结果了!

shell 脚本里面从一个文本里面读出一个数字,如何转换成整数?我需要用这...

可以参考下面几种方法:法一:echo ${var%.*} #这个是直接去除小数点及后面所有内容,只用于bash 法二:echo $var | awk -F. '{print $1}' #以小数点为分隔符取第一个字段 法三:echo $var | awk '{print int($0)}' #awk中可直接使用C函数取整 ...

linux shell简单计算器 怎样在运行文件时直接运算,怎样判断输入的值为...

变量$#是脚本参数的个数,首先判断$#是否等于3。三个参数的值分别在变量$1 $2 $3里。脚本里可以直接使用这些变量。echo 变量 | grep [^0-9.] &amp;&gt;/dev/null 返回值1的的是数字。

linux常用shell命令linux常用shell

Shell环境 Shell编程跟java、php编程一样,只要有一个能编写代码的文本编辑器和一个能解释执行的脚本解释器就可以了。Linux的Shell种类众多,常见的有:BourneShell(/usr/bin/sh或/bin/sh)BourneAgainShell(/bin/bash)CShell(/usr/bin/csh)KShell(/usr/bin/ksh)ShellforRoot(/sbin/sh)Bash,...

linux简单的shell编程 linux简单常用shell命令 创建一个简单的shell程序 linux编写shell程序 linux怎么编写shell程序 linux怎么编写shell脚本 linux编写shell脚本程序 编写一个shell ubuntu编写shell脚本
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
word里的表格怎么调整行高和列宽 怎样调整word表格的行高与列宽 一吨煤能发多少功率 微信不打开微信就收不到语音和视频提示怎么办? 微信来电不显示怎么办 ...公众号里添加文字连接?比如“点击报名”几个字,点进去就是一个报名... 顺丰快递笔记本电脑多少钱? 东莞市捷高电子材料有限公司公司简介 淘宝的购买记录保留多久? 为什么蜻蜓喜欢飞来飞去? linux shell 位置变量问题如下: 在shell里“ ` ”是什么意思? Linux命令行翻页? Linux的编程问题! 编写一个shell脚本 它把第二个位置参数及其以后的各个参数指定的文件复制 苹果6冬天电池掉电太快 嵌入式linux 下面的shell代码求解释。问题在下文中??一定采纳,跪求 如何传递参数给linux shell 脚本(当脚本从标准输入而不是从文件获取时... 满意追加50-200分!Linux中shell语句解释。。不懂的已经在代码后注释出来(共3句没看明白) linux中的shell变量的种类及各种类的用处是什么? linux shell 如何选中 linux shell 命令执行快捷键 钓鱼钩怎样绑线 周公解梦梦见前男友现在过的不好意味着什么? 科诚EZ1105条码打印机 每次开机,第一张标签都空一张不打,为什么? 为什么会梦见前男友呢,是不是代表他过的不幸福还是幸福啊 条码打印机斑玛、立像、TSC、东芝、科诚哪种打印机性价比高 昨晚梦见前男友。 科诚条码打印机不干胶三排怎么打印 热门推荐 科诚GODEX条码打印机哪个好 做梦梦见前男友 在梦里梦见他好像生了很严重的病,我就特别害怕,我就对他说,我不知道你现在能不能看见 linux(shell)题目求教,很弱智,不过我不会。。 夫妻之间没有了信任,更多的是猜疑,这样的感情是不是该趁早结束?_百度... 夫妻之间没有了信任,是不是就很难继续了? 在婚姻里,夫妻之间信任已失,还有必要继续下去吗? 夫妻不信任的说说 夫妻之间失去信任有多可怕?还能重建信任吗? 如果夫妻之间失去了信任,这段婚姻还有必要再继续吗? 夫妻之间没有了信任,这时候究竟该如何继续下去? 夫妻之间没有信任,该怎么办。 夫妻之间已经不再信任彼此,不再沟通,没有话语,是否婚姻走到尽头了? 夫妻之间没有了信任,是不是这段婚姻就不能继续了? 如果一对相爱的夫妻失去信任,是不是很难再回到从前了? 夫妻间信任已失,还能走多远? 如果夫妻或者情侣双方失去了信任,会对感情产生什么影响? 如果夫妻之间互不信任,还有必要维持这段婚姻吗? 梦见邻居生小孩是什么征兆? 梦见邻居家刚生的小孩有病是什么意思请问 梦到邻居家生了残疾孩子扔了是什么预兆 梦见别人生小孩是好是坏 梦见邻居刚出生的小孩1个月就会走了