2019独角兽企业重金招聘Python工程师标准>>>

bc -- The Linux Command-line Calculator

Probably every distro of desktop Linux comes with its own built-in, graphical calculator. And, if you don't like it, you can always install another one from the distro's repository. So, why would you want to learn about a calculator that has no graphical interface, and that you work simply by entering formulas on the command-line?

Well, you might be working with a computer that isn't running a graphical interface. Or, you might just want something that works a bit faster than what a graphical-type calculator would. Most importantly, you might want something that's more flexible, and more powerful than any run-of-the-mill graphical-type calculator.

And if you really do want speed, flexibility, and power, have we got a deal for you!

The "bc" calculator comes as a part of your Linux distro, so there's no need for you to install anything extra. In addition to performing simple math functions, it can also perform conversions between different number systems, perform a number of scientific math functions, and can even run programs that you write and save in a text file. Here's how it works.

At the command-prompt, enter:

bc -l

(You'll need the "-l" switch to perform anything other than the simplest of functions. It's what pulls in the "standard math libraries".)

Your screen should now look something like:

bc -l

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'.

Now, just type in your formula at the blinking cursor, and hit "Enter". You can type in something simple, or you can type in complex statements with grouped operators.

((83.12 + 32.13) * 37.3)

4298.825

In addition to the normal math functions--addition, subtraction, multiplication, division, modulus, and exponents--bc also has functions to calculate sines, cosines, arctangents, and logarithms. So, for example, if you need to find the natural logarithm of 4,332, it's as simple as:

l (4332)

8.37378460812088123024

The "ibase" function allows you to set what numbering system that you want to use for input. "obase" allows you to set what numbering system to use for output. You can mix-n'-match, here, so that you can have a handy way of converting numbers from one system to another.

Let's say that you want to convert numbers from decimal to hexadecimal. First, set your "obase".

obase = 16

Now, multiply the number that you want to convert by 1:

14 * 1

E

Oh, but wait, you really wanted that answer in binary. No problem, just change the obase.

obase = 2

14 * 1

1110

To return to normal, set the obase back to 10.

obase = 10

14 * 1

14

To convert from another number system back to decimal, set the "ibase".

ibase = 16

E * 1

14

You can perform calculations in other number systems by setting both the ibase and the obase.

ibase = 2

obase = 2

101 + 1

110

Now, here's the real beauty of bc. You can write programs for it, save them to text files, and then run the programs by using the name of the program text file as an argument. (The programming syntax is somewhat similar to that of bash scripting.) Let's start by looking at a program that demonstrates the use of the "if" construct. We'll save the program to a file that we'll name "simple_if".

print "Input x\n"; x = read()

print "Input y\n"; y = read()

if (x > y) {

print "x > y\n";

}

if (x < y) {

print "x < y\n";

}

if (x == y) {

print "x = y\n";

}

quit

Now, let's run it, and see what happens.

bc -l simple_if

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'.

Input x

25

Input y

4

x > y

The "read()" functions in the first two lines allowed us to interactively assign values to the "x" and "y" variables, and the "if" constructs chose the appropriate output.

For something more practical, let's look at the "checkbook" example program from the bc man page.

scale = 2

print "\nCheck Book Program!\n"

print "Remember, deposits are negative transactions.\n"

print "Exit by a 0 transaction.\n\n"

print "Initial balance?"; bal = read()

bal /= 1

print "\n"

while (1) {

"current balance ="; bal

"transaction?"; trans = read()

if (trans == 0) break;

bal -= trans

bal /= 1

}

quit

The "scale" function on the first line limits input and output to two decimal places. When you run it, it'll first ask you to enter an initial account balance. The "while" construct will allow you to enter transactions until you're done, at which time you'll enter a transaction of "0".

bc -l checkbook

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'.

Check Book Program!

Remember, deposits are negative transactions.

Exit by a 0 transaction.

Initial balance?5578.80

current balance =5578.80

transaction?-1230

current balance =6808.80

transaction?45

current balance =6763.80

transaction?34.89

current balance =6728.91

transaction?5089.90

current balance =1639.01

transaction?-55.15

current balance =1694.16

transaction?1608.97

current balance =85.19

transaction?0

We've just scratched the surface of what you can do with bc. Give it a try, and see what it can do for you.

For more information, enter "man bc" at the command-line.

转载于:https://my.oschina.net/wdliming/blog/37925

bc -- The Linux Command-line Calculator相关推荐

  1. 【The Linux Command Line】学习笔记

    以下内容参考于书籍<The Linux Command Line>,中文版本翻译项目:快乐的 Linux 命令行 终端 提示符:$ 表示普通用户,# 表示超级用户 鼠标与光标:使用光标选择 ...

  2. Linux Command Line 解析

    处理模型 Linux kernel 的启动包括很多组件的初始化和相关配置,这些配置参数一般是通过command line 进行配置的.在进行后续分析之前,先来理解一下command line 的处理模 ...

  3. The Linux Command Line读书笔记(二)

    第七章: 字符展开: 通过展开,你输入的字符,在 shell 对它起作用之前,会展开成为别的字符. [me@linuxbox ~]$ echo * Desktop Documents ls-outpu ...

  4. linux bash:command,学习使用Linux Command line(Git Bash)

    了解一个新事物,一个新知识最好的方式,就是对它问问题,然后再自己寻找答案,进行解答并总结,SO...... Question: 命令行是什么? Baidu 命令提示符是在操作系统中,提示进行命令输入的 ...

  5. linux command line 利用Entrez Direct下载NCBI数据

    一.软件的安装 1.软件下载: curl    ftp://ftp.ncbi.nlm.nih.gov/entrez/entrezdirect/edirect.zip -O (熟悉curl下载文件的方法 ...

  6. matlab提示output,强制Matlab输出到命令行(Force Matlab output to command line)

    强制Matlab输出到命令行(Force Matlab output to command line) 我正在从Windows命令提示符运行MATLAB脚本: "C:\Program Fil ...

  7. linux: 命令行运行php Run PHP from the command line

    As an alternative to /opt/lampp/bin/php, to run a php script from the command line, you just need to ...

  8. Linux/Ubuntu: 命令行任务(To-Do List)管理 task - A command line todo manager

    安装: sudo apt-get install task $ task add some very important task $ task You can also install vit if ...

  9. [Linux] DSO missing from command line

    编译 FFmpeg 官方例程 gcc -o myplay demuxing_decoding.c -I /usr/local/include/ -L /usr/local/lib -lavformat ...

最新文章

  1. 【英文文本分类实战】之三——数据清洗
  2. 【查看Linux带宽】nload、iptraf 的安装与使用
  3. php限制一个函数在几分钟内不被调用_在PHP一句话木马使用过程中的种种坑点分析...
  4. Arcgis desktop 9.3的破解方法_经验版
  5. draft.js_如何使用快捷方式在Draft.js中创建有序列表和无序列表
  6. 呀~ 一个.java的源文件可以写这么多类啊
  7. 官宣!又一所新大学来了!
  8. Linux CentOS6离线安装Jupyter notebook
  9. Management reporter 2012 与AX 2012
  10. java thread already started_自定义类加载器
  11. JQuery高效制作网页特效第六章jQuery选择器课后作业
  12. Python发送电子邮件.
  13. ​九月简报 | Coinversation先行版DEX——Kaco上线碎片化,TVL最高突破1000w美金
  14. Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
  15. Python从zip文件里导入包
  16. 常见服务器类型及其简单介绍
  17. 一篇小论文:VGGish-BiGRU网络
  18. 计算机三级 计算机三级网络技术 如何两天内通过考试
  19. go 运算符
  20. Typescript.中文.接口声明.lib.es5.d.ts

热门文章

  1. 微信小程序接口测试时appid为空如何解决
  2. 计算机等级考试在校生变成社会考生,关于开展2021年9月份全国计算机等级考试(NCRE)报名工作通知...
  3. 查看php 加载.dll,无法加载PHP_OCI8.DLL的解决
  4. 高中会考access数据库_高中信息技术ACCESS数据库上机操作复习课教案
  5. opencv画线和文字
  6. android+委托列表,在Android适配器中使用委托者模式
  7. php代码正确 插不进表,在表中插入值在PHP中不工作,使用
  8. linux根目录如何改代码,linux怎样修改目录字体颜色
  9. 设备 esp32_「ESP 教程」ESP32 如何运行 TensorFlow 模型
  10. android 百叶窗动画,android 幻灯片效果之百叶窗