linux加减乘除的命令 linux 加减运算

在linux里 如何将 date'%j' 赋值给a 在对a进行四则运算?题主linux加减乘除的命令你好,
你这个问题其实涉及的是linux中的两个概念: 一个是"将命令的输出结果赋值给变量", 另一个是"linux如何进行四则运算".
-----将命令的输出结果赋值给变量-----
linux下可以使用:变量名=`语句` 的语句将"语句"的输出结果,赋值给"变量名", 见例子:
*.需要注意的是将语句包围起来的是反引号,即键盘左上角Esc下面那个键.
-----linux如何进行四则运算-----
linux下, 四则运算的语句是: $((表达式)), 见例子:
*.还有一点需要注意的是,linux下的变量赋值时,等号两边都不能有空格.
-----
有linux加减乘除的命令了上面的知识作铺垫,题主的问题也有好解决了:
=====
希望可以帮到题主, 欢迎追问.
Linux 里面命令行打开计算器 怎么输入linux命令行计算器
详细文档请 man bc
在windows下,大家都知道直接运行calc,(c:\windows\system32\calc.exe),可以打开计算器 。
注:
calculate vt.vi.
计算;calculator n. 计算器 。calc就是这个计算的简写 。
Ca在化学中是代表钙元素,calcium
钙(20号元素,符号Ca) 。两者有什么联系呢?
calculate 来自calculus,原义是做算术运算的小石子 , 是calx(石灰石)的小称 。
那么在linux系统下,有无与windows下calc.exe类似的计算器呢?
下面总结linux下的三个命令,来介绍一下linux下的计算方法:
1)bc
bc在默认的情况下是个交互式的指 令 。在bc工作环境下,可以使用以下计算符号:
加法
- 减法
* 乘法
/ 除法
^ 指数
% 余数
如:
#
bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation,
Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
3 6
=加法
9
4 2*3=加法、乘法
10
(4 2)*3=加法、乘法(优先)
18
4*6/8
=乘法、除法
3
10^3
=指数
1000
18%5=余数
3 4;5*2;5^2;18/4
=一行输入多个计算,用;相隔 。
7
10
25
4
quit=退出
# bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation,
Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
scale=3
=设小数位
1/3
.333
quit
以上是交互的计算 , 那到也可以 不进行交互而直接计算出结果 。
A.用echo和|法,如:
# echo "(6 3)*2" |bc
18
# echo 15/4 |bc
3
# echo "scale=2;15/4"
|bc
3.75
# echo "3 4;5*2;5^2;18/4"
|bc
7
10
25
4
另外,
bc除了scale来设定小数位之外,还有ibase和obase来其它进制的运算 。
如:
//将16进制的A7输出为10进制, 注意,英文只能大写
# echo "ibase=16;A7"
|bc
167
//将2进制的11111111转成10进制
# echo "ibase=2;11111111"
|bc
255
//输入为16进制,输出为2进制
# echo "ibase=16;obase=2;B5-A4"
|bc
10001
对于bc还有补充,在bc --help中还可以发现:bc后可以接文件名 。如:
# more calc.txt
3 2
4 5
8*2
10/4
# bc calc.txt
5
9
16
2
2)expr
expr命令可不光能计算加减乘除哦,还有很多表达式,都可以计算出结果,不过有一点需要注意 , 在计算加减乘除时,不要忘了使用空格和转义 。下
面直接用实例来介绍一下expr的运算,如:
# expr 6
3
(有空格)
9
# expr 2 \*
3
(有转义符号)
6
# expr 14 % 9
5
# a=3
# expr
$a 5
(无空格)
3 5
# expr $a
5
(变量 , 有空格)
8
#
a=`expr 42`
echo $a
6
# expr $a3
9
另外 , expr对于字串的操作(计算)也是很方便的,如:
//字串长度
# expr length
"yangzhigang.cublog.cn"
21
//从位置处抓取字串
# expr substr "yangzhigang.cublog.cn"
1 11
yangzhigang
//字串开始处
# expr index
"yangzhigang.cublog.cn" cu
13
3)dc
用dc来进行计算的人可以不多,因为dc与bc相比要复杂 , 但是在进行简单的计划时,是差不多的,不算难 。dc为压栈操作,默认也是交互的,但
也可以用echo和|来配合打算 。
如:
# dc
3
2
p
5
4*
p
20
quit
# echo 3 24* p |dc
20
4)echo
echo用来进行回显,是周知的事 。上面也配合bc来进行计算 。其实echo也可以单独进行简单的计算 , 如:
# echo $((3 5))
8
# echo $(((3 5)*2))
16
echo还可以进行变量的计算,如:
# a=10
# b=5
# echo $(($a $b))
15
# echo $a $b
10 5
# echo $a $b |bc
15
//计算前天的日期
#echo `date
%Y%m%d`
20090813
#echo `date%Y%m%d`-2
20090813-2
#echo `date%Y%m%d`-2
|bc
20090811
5)AWK
awk在处理文件的时,可以进行运算 , 那当然也可以单单用来计算了,如:
# awk 'BEGIN{a=3 2;print
a}'
5
# awk 'BEGIN{a=(3 2)*2;print
a}'
10
Awk 支持常见的运算符,如(加),- (减) , * (乘),/ (除),^ 或 ** (乘方),% (取模)
等等 。此外 , awk 也提供了一些常用的数学函数, 比如 sin(x), cos(x), exp(x), log(x),
sqrt(x), rand() 。使用这些运算符和函数可以直接进行一些简单的运算:
# echo | awk '{print
8 6}'
14
# echo | awk '{print
8/6}'
1.33333
# echo | awk '{print
9%5}'
4
请放心使用
有问题的话请追问
满意请及时采纳,谢谢
在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 3210
#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 $12# 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):
#PIpi, i.e. 3.1415926545897931
#Ee, 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]3220 / 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
【linux加减乘除的命令 linux 加减运算】[[ $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
Linux用c语言编程一个计算机程序,用户输入两个数后,输出加减乘除的各个结果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;
}
如何在Linux 命令行里做数学运算可以考虑使用$(())进行数学运算
例如:
[WtbmWdgz@localhost 桌面]$ a=5;b=3
[WtbmWdgz@localhost 桌面]$ echo $((a*7-b))
32
这个可以执行加减乘除和取模(就是取余数)的数学计算还有逻辑运算的与、或、非、还有异或运算
与运算是两个条件同时成立结果才成立 。或运算是两个条件有一个成立结果就成立 。非运算只需要一个条件 。条件成立,结果就是否定 。条件不成立,结果就肯定 。比如:下雨就不出门,不下雨就出门 。异或也叫半加 。它是 两个条件相同,结果是0,就是否定,两条件不同,结果才是肯定或者是1
关于linux加减乘除的命令和linux 加减运算的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读