Κυριακή 27 Οκτωβρίου 2024

Another SuperClass Example

This is a simple program looking the core, we get a number, then add 3 and then multiply by 0.86, and do that for each new result until the new result is equal to old result.

Because I would like to show a program which use the SuperClass, I expand the program to hold results in a document object, and works for 4 different types of number which hold decimal digits.

The SuperClass is a special object which give each time a different object which can use the unique properties/methods of superclass. So here the unique parts are: the document and the module to put something in it formated as we wish. All other parts are specific for each object we make from this SuperClass. So the function Result  get from SuperClass the common Document and return it (as string).

The decimal point is a dot here. We can change it using LOCALE 1032 or GREEK (this also change the language for Error messages, and labels on dialogue forms), to a comma (eg 1.334 turn to 1,334).

For code the decimal point is always the point. Also we can use .23 without the 0 before. 

The Ouput of the program (a(0) to a(3) are four objects, with own Count propertu, and common Result() function which return the Doc$ from SuperClass unique property.

The Print #=2 redirect output formated for file to screen. We need this to get the document processing the new line properly. A simple Print the characters bellow 32 are shown with letters, eg. the new line is ␍␊, changing character code from 0x0D 0x0A to 0x240D and 0x240A, and thats the reason to not process the new line. But Print #FileNumber send all bytes to output, without altering, so the -2 file number send all bytes to screen processing them. All print statements add a new line unless we use a collon  ; at the end.

	test on a(0) so far:2
test on a(1) so far:2
test on a(2) so far:2
test on a(3) so far:2
Results
Single           4.00  87 18.42854
Single        1.23E+8 194 18.4286
Double           4.00 209 18.4285714285711
Double   123456789.00 316 18.4285714285717
Decimal          4.00 423 18.428571428571428571428571426
Decimal  123456789.00 528 18.428571428571428571428571432
Currency         4.00  72 18.4283
Currency 123456789.00 176 18.4289



This is the program

superclass MainTest {
unique:
document Doc$
module PutValue {
.Doc$<=letter$+{
}
}
public:
property counter{value}=0
function result {
for superclass {
=.Doc$
}
}
module Test1 (thisType, ThisValue) {
select case thistype
case 0
single a
case 1
double a
case 3
currency a
case else
decimal a
end select
a=ThisValue
aa=a
.[counter]++
long long i=1
do
o=a
a+=3@ ' decimal literal value
a*=.86@
if o=a then exit
i++
always
for superclass {
.PutValue format$("{0:8} {1:2:-12} {2::-3} {3}",type$(aa), aa, i, a)
}
}
}
document result$
dim a(0 to 3)=MainTest
for i=0 to 3
a(i).test1 i, 4
a(i).test1 i, 123456789
result$ = "test on a("+i+") so far:"+a(i).counter+{
}
next
result$ = "Results" + {
}+a(1).result()


clipboard result$
Print #-2, result$

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.