Hi,

I did write a script to add 2 natural numbers, and send it to the mailing
list of sed.

Because I found no list for sed developers, I send this script to this list.
I did send it to sed-us...@yahoogroups.com, but that one is a private list,
and cannot access it unless you subscribe the group.

It is not for sure the best place to send the script that adds 2 numbers :)
, but probably one of you recomment me the good place where to send it.

The Hold space is used to add 2 digits . The Pattern Space is used to keep
the numbers.

The general steps of the algorithm are the followings

* read both numbers in PS, and keep them separated by a space
* LOOP as time as there are still digits to add
*   remove the last digit of every number, and move them to the end of PS
*   append PS to HS
*   commute to HS
*   if there is only 1 digit, append a '0' (a number is longer than the
other)
*   transform every digit into points (N points for the digit N)
*   if there are more than 10 points, transform the last 10 points to a SIGN
('+')
*   convert the rest of N points into the digit N
*   print the result:
*     for a void result, print 0
*     for a digit print and remove it
*     for a '+', print 0, and convert '+' into a point
*     for a digit and '+', print and remove the digit, and convert '+' into
a point
*   go back to Pattern Space
*   remove the last processed digits
* END_LOOP
* if there is a point in HS, print a '1'

For a better display of the result, you can pipe the resulting stream via
this:

sed ':a N ; s:\(.*\)\n\(.*\):\2\1:g ; ta;'      # pipe the output via this

Example:

sed -f ./add.sed <<X | sed ':a N ; s:\(.*\)\n\(.*\):\2\1:g ; ta;'
10
101
X
111

I followed the principle kiss (keep it simple. stupid). The initial
zeros are displayed, as I did not want to complicate the code.

A colleague of mine found a bug (the void set,
id est the number 0 was not displayed sometimes).
If you find other errors, please report me.


Alin

Attachment: add.sed
Description: Binary data

Reply via email to