
What you did not expect from a calculator. Solitaire on bc
- Tutorial
Innocent entertainment with bash no longer seems to me something special. As my next goal for experiments, I chose GNU bc - a console calculator and a scripting mathematical programming language in one box. This article will certainly be useful to all Linuxoids and Unixoids who are not very familiar with bc.

So, a short introduction to bc programming,kawaii and neki solitaire on pure bc and poetry .
I don’t remember when I first learned about bc, but for many years I have been using only it as a calculator. The simple and convenient console calculator, available by default on most Linux distributions, captivates with its convenience.
You can start the calculator with the obvious bc command . After that, you can enter expressions for calculation. Pressing Enter immediately produces the result.
In expressions, you can use brackets, signs of addition, subtraction, division and multiplication, as well as && (logical “and”), || (logical "or") and! (logical "not").
Variables can and should be used in the bc calculator. To define a variable, you need to write its name, put an equal sign and then write the value of the variable. Everything is like everywhere.
The most attentive managed to find that the calculator rounds all numbers to integers. The reason is as follows: the special variable scale, which determines the accuracy of calculations, is equal to zero by default. If you want to get the result with an accuracy of ten characters, then assign the number 10 to the variable.
There are three more special variables in bc. Last stores the last result of calculations, obase and ibase are used to work with various number systems.
It's time to learn how to work with the standard library and connect third-party functions. To use the standard bc language library, you need to run it with the -l option:
The following functions are defined in the standard library:
To use third-party libraries, you should download them, for example, from the sites phodd.net/gnu-bc and http://marcmmw.freeshell.org/esp/programacion/bc.html . You will find procedures for working with arrays, integrals, derivatives, logic, random number generators and much more. My favorite library is for working with ppm images. It is possible on bash that you want to generate - at least bmp, at least jpeg, but here everything is more severe. Not EGGOGology, of course, but still.
Run bc with third-party libraries as follows:

Mandelbrot fractal calculated in bc and stored in ppm.
The bc syntax is very similar to the C syntax. Therefore, you can safely use the if, for, while, break, continue, return commands and place curly braces wherever you go. Only after the commands you do not need to put semicolons, use the print command to display on the screen, and read to enter. Define functions with the define command. See:
In bc, unfortunately, you cannot work with file I / O. All input through read, all output through print. Still in bc it is impossible to work with string variables. Wait a minute though! You can use the 36-decimal number system, or work with arrays of numbers as with strings.
In addition, in bc, the print command does not support the full set of escape sequences.
When I found out about this sad limitation, I thought that I was waiting for the pink Oblomingo bird, because for any serious toy you need accurate cursor positioning, and it is desirable to use different colors. However, I took the hex editor in my hands and circumvented the restriction as follows: I inserted the character with the code 1Bh (escape) as an argument to the print command, and then used all the joys of Escape sequences.
Ah, yes, so that the called functions do not litter on the screen, their return value should be assigned to some variable as I did in the previous example.
And finally, the most dull fact: in bc there is only one way to catch keystrokes - read. And it is absolutely not suitable for interactive games. It’s a pity, because on my computer the raccasting algorithm launched in bc works ten times faster than the same algorithm in bash and produces about 30-40 frames per second.
Now you can write a simple solitaire in bc. I chose Turkish Shawl Solitaire. Download the source code archive , unpack it, run it like this:
After starting, you need to enter seed - the initial value of the random number generator. After that, you will see a shuffled deck of cards laid out in ten columns. Enter the column numbers, and if the last column cards have the same picture (for example, two clubs and two tambourines), then they will be removed. Once all the cards can be dealt, the program will congratulate you on your victory and complete bc. If you decide that you should stop unraveling the scarf, press Ctrl + C twice, and then Enter.
And there will be no information about him. Why? Because I still don’t feel like a master ...
PS Link to solitaire again, especially for those who always first download all the files and then read the article: narod.ru/disk/31656937001/platok.tar.html

So, a short introduction to bc programming,
I don’t remember when I first learned about bc, but for many years I have been using only it as a calculator. The simple and convenient console calculator, available by default on most Linux distributions, captivates with its convenience.
Ninth Kyu (White Belt)
You can start the calculator with the obvious bc command . After that, you can enter expressions for calculation. Pressing Enter immediately produces the result.
c
carrot@ubuntu:~$ bc
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'.
>2*2
4
>3*(6+1)
21
>5||(1&&0)
1
>5/2
2
In expressions, you can use brackets, signs of addition, subtraction, division and multiplication, as well as && (logical “and”), || (logical "or") and! (logical "not").
Sixth Kyu (Green Belt)
Variables can and should be used in the bc calculator. To define a variable, you need to write its name, put an equal sign and then write the value of the variable. Everything is like everywhere.
>raw=3116
>raw+1
3117
The most attentive managed to find that the calculator rounds all numbers to integers. The reason is as follows: the special variable scale, which determines the accuracy of calculations, is equal to zero by default. If you want to get the result with an accuracy of ten characters, then assign the number 10 to the variable.
>5/2
2
>scale=10
>5/2
2.5000000000
>7/191
.0366492146
There are three more special variables in bc. Last stores the last result of calculations, obase and ibase are used to work with various number systems.
>1
1
>last+10
11
>obase=16
>last
B
>255+1
100
>ibase=16
>FE+1
FF
Fifth Kyu (blue belt)
It's time to learn how to work with the standard library and connect third-party functions. To use the standard bc language library, you need to run it with the -l option:
carrot@ubuntu:~$ 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(3)
.14112000805986722210
The following functions are defined in the standard library:
- s (x) - sine x
- c (x) - cosine x
- a (x) - arc tangent x
- l (x) is the natural logarithm of x
- e (x) is the exponent of x
- j (n, x) is the n-th order Bessel function of x
To use third-party libraries, you should download them, for example, from the sites phodd.net/gnu-bc and http://marcmmw.freeshell.org/esp/programacion/bc.html . You will find procedures for working with arrays, integrals, derivatives, logic, random number generators and much more. My favorite library is for working with ppm images. It is possible on bash that you want to generate - at least bmp, at least jpeg, but here everything is more severe. Not EGGOGology, of course, but still.
Run bc with third-party libraries as follows:
carrot@ubuntu:~$ bc rand.bc
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'.
>rand(96)
21

Mandelbrot fractal calculated in bc and stored in ppm.
Fourth Kyu (Blue Belt)
The bc syntax is very similar to the C syntax. Therefore, you can safely use the if, for, while, break, continue, return commands and place curly braces wherever you go. Only after the commands you do not need to put semicolons, use the print command to display on the screen, and read to enter. Define functions with the define command. See:
>define func () {
>for (i=0;i<10;i++) {
>a=a+i
>print a, «\n»
>}
>return 1}
>t=func ()
0
1
3
6
10
15
21
28
36
45
>print t
1
Third Kyu (brown belt)
In bc, unfortunately, you cannot work with file I / O. All input through read, all output through print. Still in bc it is impossible to work with string variables. Wait a minute though! You can use the 36-decimal number system, or work with arrays of numbers as with strings.
In addition, in bc, the print command does not support the full set of escape sequences.
The special characters recognized by bc are "a" (alert or bell), "b" (backspace), "f" (form feed), "n" (newline), "r" (carriage return), "q" (double quote), "t" (tab), and "\e" (backslash). Any other character following the backslash will be ignored.
When I found out about this sad limitation, I thought that I was waiting for the pink Oblomingo bird, because for any serious toy you need accurate cursor positioning, and it is desirable to use different colors. However, I took the hex editor in my hands and circumvented the restriction as follows: I inserted the character with the code 1Bh (escape) as an argument to the print command, and then used all the joys of Escape sequences.
Ah, yes, so that the called functions do not litter on the screen, their return value should be assigned to some variable as I did in the previous example.
And finally, the most dull fact: in bc there is only one way to catch keystrokes - read. And it is absolutely not suitable for interactive games. It’s a pity, because on my computer the raccasting algorithm launched in bc works ten times faster than the same algorithm in bash and produces about 30-40 frames per second.
Second Kyu (still brown belt)
Now you can write a simple solitaire in bc. I chose Turkish Shawl Solitaire. Download the source code archive , unpack it, run it like this:
bc rand.bc printcard.bc
After starting, you need to enter seed - the initial value of the random number generator. After that, you will see a shuffled deck of cards laid out in ten columns. Enter the column numbers, and if the last column cards have the same picture (for example, two clubs and two tambourines), then they will be removed. Once all the cards can be dealt, the program will congratulate you on your victory and complete bc. If you decide that you should stop unraveling the scarf, press Ctrl + C twice, and then Enter.
First Dan (black belt)
And there will be no information about him. Why? Because I still don’t feel like a master ...
PS Link to solitaire again, especially for those who always first download all the files and then read the article: narod.ru/disk/31656937001/platok.tar.html