linux的程序员计算器,linux中的计算器

windows系统安装时会自带计算器,在cmd中运行calc即可打开。那么linux系统中有没有计算器呢?答案是肯定的。

linux下的图形计算器

linux系统一般也会默认安装一个图形界面的计算器,例如红帽系统默认安装的是gcalctool。假如在一个图形桌面环境中需要一个计算器时,在终端中运行gcalctool即可打开。

如果系统没有运行桌面环境,那是否有一个支持命令行的计算器呢?

linux下的命令行计算器

bc是任意精度计算器,官方对它的描述是“an arbitrary precision calculator language”,一种支持任意精度计算的计算器语言,它支持变量、数组、输入输出、分支结构、循环结构、函数等基本的编程元素。bc可以在交互环境下使用,也可以再shell编程中使用。

* 交互模式下使用bc

在终端中输入bc --help,可以查看bc的使用说明:

[release@redhat59-211 ~]$ bc -h

usage: bc [options] [file ...]

-h --help print this usage and exit,显示帮助

-i --interactive force interactive mode,强制进入交互模式

-l --mathlib use the predefine math routnes,加载数学库

-q --quiet don't print initial banner,不打印版本信息

-s --standard non-standard bc constructs are errors

-w --warn warn about non-standard bc constructs

-v --version print version information and exit,显示版本

bc有四个常用的内置变量,如下表所示:

变量名

作用

scale

指定精度,即小数点后的位数,默认为0

ibase

指定输入的数字的进制,如ibase=2

obase

指定输入数字的进制,如果需要设置ibase和obase,应先设置obase

last

表示最近打印的数字

在终端中输入bc,可以进行bc的交互模式,然后输入想要计算的表达式即可。示例如下图,可见bc支持变量、函数,功能还是挺强大的。

[release@redhat59-211 ~]$ 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'.

1+1

2

n=2

n+1

3

sqrt(9)

3

define myadd(a,b){

return a+b;

}

myadd(1,2)

3

last

3

此外,如果想计算正弦、余弦、正切等,可以在启动bc时加-l参数,如bc -l,这样就引入了数学库,可以使用s(1)计算1的正弦值。

函数名

作用

s(x)

计算正弦值,x为弧度值

c(x)

计算余弦值

a(x)

计算反正切值

l(x)

计算x的自然对数

e(x)

计算e的x次方

j(n,x)

贝塞尔函数,计算从n到x的阶数

[root@localhost bin]# bc -l

bc 1.06.95

Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.

This is free software with ABSOLUTELY NO WARRANTY.

For details type `warranty'.

s(1)

.84147098480789650665

c(1)

.54030230586813971740

s(1)*s(1)+c(1)*c(1)

.99999999999999999999

* shell脚本中使用bc计算器

在shell脚本中可以借助管道或者输入重定向来使用bc计算器。

借助管道使用bc

echo "expression"|bc,其中expression是计算的数学表达式。

示例:var=$(echo "1+1"|bc),将1+1的计算结果赋值给var变量

[release@redhat59-211 ~]$ echo "10^2"|bc

100

[release@redhat59-211 ~]$ var=$(echo "10^2"|bc)

[release@redhat59-211 ~]$ echo $var

100

[release@redhat59-211 ~]$ echo "$var*2"|bc

200

[release@redhat59-211 ~]$ echo "obase=2;10"|bc 10转换为2进制

1010

借助重定向使用bc

使用方法:

var=$(bc <

expressions

EOF

)

示例:

[release@redhat59-211 ~]$ var=$(bc <

> obase=10;

> ibase=16;

> F

> EOF

> )

[release@redhat59-211 ~]$ echo $var

15

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441286.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 薛猫猫杯程序设计网络赛】【题解】

A 爬山 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0 Accepted Submission(s): 0 Problem Description 小Z准备去爬山&#xff0c;在他的面前有N座山&#xff0c;每座山都有对应的高度。他想选择两座高…

win10 linux安卓模拟器,genymotion安卓模拟器在Window10中使用的问题

最近一段时间&#xff0c;把系统升级到了win10&#xff0c;然后悲催的事情出现了&#xff0c;genymotion挂了&#xff0c;根本就不能启动&#xff0c;而且还是2.6版本的genymotion&#xff0c;下面我把遇到的问题总结一下&#xff1a;首先&#xff0c;在我的win10系统中&#x…

【CodeForces - 706C】Hard problem(dp,字典序)

题干&#xff1a; Vasiliy is fond of solving different tasks. Today he found one he wasnt able to solve himself, so he asks you to help. Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical orde…

cross_compile = arm-linux-,cross compile grpc for arm

8种机械键盘轴体对比本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f;This post will tell you how to cross compile gPRC static lib for ARM.前段时间尝试交叉编译gRPC遇到了不少的麻烦&#xff0c;写篇post记录一下。gRPCPreparat…

【CodeForces - 349B】Color the Fence (贪心,填数)

题干&#xff1a; Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanyas house. Igor thinks that the larger the number is, the more chance to win Tanyas heart he has. Unfortunately, Igor …

linux键盘映射默认,Linux 中的键盘映射

前面提到&#xff0c;X Window 直接处理了键盘的输入输出端口&#xff0c;因此&#xff0c;在 Linux 虚拟控制台下和 X Window 下使用不同的键盘映射方法。在 Lin对于英语来说&#xff0c;键盘上的字母键直接和英语字母表中的字母对应&#xff0c;但是对于非英语的语种来说&…

【POJ - 2301 】Beat the Spread! (简单数学)

题干&#xff1a; Superbowl Sunday is nearly here. In order to pass the time waiting for the half-time commercials and wardrobe malfunctions, the local hackers have organized a betting pool on the game. Members place their bets on the sum of the two final s…

Linux x8664汇编,Linux Udis86 反汇编引擎使用

前两篇说了capstone/beaengine,这节一起用一用经典的udis86;github:https://github.com/vmt/udis860x01:udis86相比于前面两个&#xff0c;用起来还是比较简单的&#xff0c;使用文档如下所示&#xff1a;Getting StartedBuilding and Installing udis86----------------------…

【HDU - 2200】Eddy's AC难题(简单组合数学)

题干&#xff1a; Eddy是个ACMer,他不仅喜欢做ACM题,而且对于Ranklist中每个人的ac数量也有一定的研究,他在无聊时经常在纸上把Ranklist上每个人的ac题目的数量摘录下来&#xff0c;然后从中选择一部分人(或者全部)按照ac的数量分成两组进行比较&#xff0c;他想使第一组中的最…

C语言用字符串sex储存,2005年计算机等级考试二级C语言全真标准预测试卷(2)

一、选择题(1&#xff5e;40题每题1分&#xff0c;41&#xff5e;50题每题2分&#xff0c;共60分)1.微型计算机的运算器、控制器及内存储器组合在一起&#xff0c;称之为()(本题分值&#xff1a;1分)A.ALUB.CPUC.MPUD.主机【正确答案】D2.下列存储器中&#xff0c;存取速度最快…

【POJ - 3320 】Jessica's Reading Problem (尺取,哈希)

题干&#xff1a; Jessicas a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The aut…

c语言全局变量SQR,c语言a/=SQR(k+m);是什么意思?

满意答案NightmareJJ2013.03.16采纳率&#xff1a;47% 等级&#xff1a;12已帮助&#xff1a;11233人先看第一个main() // 主函数{printf("%ld\n", fun(3));} //输出fun(3)这个函数的返回值&#xff0c;3就是下一个//函数中的n 值。long fun (int n) //定义fun函数…

【POJ - 1850】Code (组合数学,字符串另类排序)

题干&#xff1a; Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is considered that …

c语言蓝牙接收6,终于搞定了通过两路蓝牙接收数据

一直想做无线传感器&#xff0c;通过蓝牙来接收数据&#xff0c;无奈因为arduino接收串口数据的一些问题&#xff0c;一直搁到现在。因为学校里给学生开了选修课&#xff0c;所以手边有一些nano和mega可以使用&#xff0c;所以就做了用两个nano加上两个蓝牙模块来发射数据&…

【POJ - 1942 】Paths on a Grid (组合数学,求组合数的无数种方法)

题干&#xff1a; Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time hes explaining that (ab) 2a 22abb 2). So you decide to waste your time with d…

编译原理last集c语言,编译原理作业集-第七章.doc

编译原理作业集-第七章第七章 语义分析和中间代码产生本章要点1. 中间语言&#xff0c;各种常见中间语言形式&#xff1b;2. 说明语句赋值语句布尔表达式控制语句的翻译&#xff1b;3. 过程调用的处理&#xff1b;4. 类型检查&#xff1b;本章目标掌握和理解中间语言&#xff0…

【CodeForces - 244A 】Dividing Orange (构造,水题)

题干&#xff1a; One day Ms Swan bought an orange in a shop. The orange consisted of nk segments, numbered with integers from 1 to nk. There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided…

兔子吃萝卜的c语言编程,狼追兔子的c语言实现

满意答案16guoyuming2013.03.05采纳率&#xff1a;49% 等级&#xff1a;13已帮助&#xff1a;8005人用单链表实现#include #includetypedef struct node{int cave;struct node * next;}node,*LinkList;void main(){int i0,j,count1; // 初始值为1&#xff1b;LinkList L,p,h…

【CodeForces - 244B】Undoubtedly Lucky Numbers (dfs打表 + 二分)

题干&#xff1a; Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x  4, and y  7, then numbers 47, 74…

c语言二叉树构造与输出,C语言数据结构树状输出二叉树,谁能给详细的解释一下...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼void TranslevelPrint(Bitree bt){struct node{Bitree vec[MAXLEN]; //存放树结点int layer[MAXLEN]; //结点所在的层int locate[MAXLEN]; //打印结点的位置int front,rear;}q;int i,j,k;int nLocate;j 1;k 0;q.front 0;q.rear …