Innocent entertainment with bash no longer seems special to me. As my next goal for experiments, I chose GNU bc, a console calculator and a scripted mathematical programming language in one box. This article will certainly be useful to all linuksoids and unixoids who are not very familiar with bc.

So, a short introduction to bc programming,
kawai and neki solitaire on pure bc
and poetess .
')
I do not remember when I first learned about bc, but for many years I have been using only it as a calculator. Simple and convenient console calculator, available by default in most Linux distributions, captivates with its convenience.
Ninth kyu (white belt)
You can run the calculator with the obvious
bc command. After that, you can enter expressions to calculate. Clicking on
Enter will immediately give 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 the expressions, you can use brackets, addition signs, subtraction, division and multiplication, as well as && (logical "and"), || (logical "or") and! (logical "not").
Sixth Kyu (Green Belt)
In the bc calculator, you can and should use variables. To define a variable, you need to write down its name, put an equal sign and then write the value of the variable. Just like everywhere else.
>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 a result with an accuracy of ten characters, then assign the variable number 10.
>5/2 2 >scale=10 >5/2 2.5000000000 >7/191 .0366492146
There are three more special variables in bc. Last stores in itself the last result of calculations, obase and ibase are used to work with different 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 standard library defines the following functions:
- s (x) - sine x
- c (x) is the cosine of x
- a (x) - arctangent x
- l (x) is the natural logarithm of x
- e (x) is an exponent of x
- j (n, x) is the nth 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 more. My favorite library is for working with images in ppm format. You can generate it on bash — you want to generate it — at least bmp, at least jpeg, but here everything is more severe. Not EGOGOGology, 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 saved 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 brackets anywhere. Only after the commands do not need to put a semicolon, to display the print command, and for input - read. Functions define 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 is via read, all output is via print. Even in bc you cannot work with string variables. Wait a minute though! You can use the 36-year 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 learned about this sad restriction, I thought that the pink bird was waiting for me, because for any serious toy you need precise positioning of the cursor, and the use of different colors is desirable. However, I took the hex editor in my hands and bypassed the restriction as follows: I inserted a character with the code 1Bh (escape) as an argument to the print command, and then used all the joys of the Escape sequences.
Oh yes, so that the called functions are not littered on the screen, their return value should be assigned to a variable in the way I did in the last example.
And, finally, the saddest fact: in bc there is only one way to catch keystrokes - read. And it is absolutely not suitable for interactive games. And it’s a pity, because on my computer, the raycasting algorithm running in bc works ten times faster than the same algorithm in bash, and it gives out about 30-40 frames per second.
Second kyu (still brown belt)
Now you can write a simple solitaire on bc. I chose Turkish Scarf solitaire. Download the
archive with the source code , unpack, run like this:
bc rand.bc printcard.bc
After starting, you must enter the seed - the initial value of the random number generator. After that you will see a shuffled deck of cards, arranged in ten columns. Enter the column numbers, and if the last column cards have the same picture (for example, a deuce of clubs and a deuce of tambourines), they will be removed. As soon as all the cards can be disassembled, the program will congratulate you on your victory and complete bc. If you decide that you should stop unraveling a handkerchief, press Ctrl + C twice, and then Enter.
First dan (black belt)
And there will be information about him. Why?
Because I still do not feel like a master ...PS Solitaire link again, especially for those who always download all the files first, and then read the article:
narod.ru/disk/31656937001/platok.tar.html