diff --git a/lab2/zad3/package.xml b/lab2/zad3/package.xml new file mode 100644 index 0000000..85095be --- /dev/null +++ b/lab2/zad3/package.xml @@ -0,0 +1,9 @@ + + GF + GF + + test/Test.st + + src/GF.st + src/main.st + diff --git a/lab2/zad3/run.st b/lab2/zad3/run.st new file mode 100644 index 0000000..287e7c6 --- /dev/null +++ b/lab2/zad3/run.st @@ -0,0 +1 @@ +PackageLoader fileInPackage: #GF. diff --git a/lab2/zad3/src/GF.st b/lab2/zad3/src/GF.st new file mode 100644 index 0000000..ae3b88b --- /dev/null +++ b/lab2/zad3/src/GF.st @@ -0,0 +1,115 @@ +Object subclass: GF [ + | characteristic value | + + GF class >> create: newCharacteristic [ + ^self new initialize: newCharacteristic + withValue: 0; + yourself + ] + + GF class >> create: newCharacteristic withValue: newValue [ + ^self new initialize: newCharacteristic + withValue: newValue; + yourself + ] + + initialize: newCharacteristic withValue: newValue [ + characteristic := newCharacteristic. + value := newValue. + ] + + verifyCharacteristics: rhs [ + (characteristic ~= rhs characteristic) + ifTrue: [Error signal: 'both arguments must have the same characteristic'] + ] + + + rhs [ + self verifyCharacteristics: rhs. + ^GF create: characteristic + withValue: ((value + rhs value) \\ characteristic) + ] + + - rhs [ + self verifyCharacteristics: rhs. + "^GF create: characteristic + withValue: ((value < (rhs value)) + ifTrue: [ ^(characteristic - ((rhs value) - value)) ] + ifFalse: [ ^(value - (rhs value)) ])" + (value < (rhs value)) + ifTrue: [ + ^GF create: characteristic + withValue: (characteristic - ((rhs value) - value)) + ] + ifFalse: [ + ^GF create: characteristic + withvalue: (value - (rhs value)) + ]. + ] + + * rhs [ + self verifyCharacteristics: rhs. + ^GF create: characteristic + withValue: ((value * rhs value) \\ characteristic) + ] + + / rhs [ + | inverse | + self verifyCharacteristics: rhs. + (rhs value = 0) ifTrue: [ZeroDivide signal: 'division by 0 is illegal']. + + 1 to: (characteristic - 1) do: [ :i | + (((rhs value * i) \\ characteristic) = 1) ifTrue: [ + inverse := i. + ] + ]. + + ^GF create: characteristic + withValue: ((value * inverse) \\ characteristic) + ] + + = rhs [ + self verifyCharacteristics: rhs. + ^value = rhs value + ] + + ~= rhs [ + self verifyCharacteristics: rhs. + ^value ~= rhs value + ] + + > rhs [ + self verifyCharacteristics: rhs. + ^value > rhs value + ] + + < rhs [ + self verifyCharacteristics: rhs. + ^value < rhs value + ] + + >= rhs [ + self verifyCharacteristics: rhs. + ^value >= rhs value + ] + + <= rhs [ + self verifyCharacteristics: rhs. + ^value <= rhs value + ] + + characteristic [ + ^characteristic + ] + + value [ + ^value + ] + + displayOn: stream [ + 'GF<' displayOn: stream. + characteristic displayOn: stream. + '>(' displayOn: stream. + value displayOn: stream. + ')' displayOn: stream. + ] +] diff --git a/lab2/zad3/src/main.st b/lab2/zad3/src/main.st new file mode 100644 index 0000000..ee234e2 --- /dev/null +++ b/lab2/zad3/src/main.st @@ -0,0 +1,32 @@ +FileStream fileIn: 'src/GF.st'. + +a := GF create: 1234577 withValue: ((stdin nextLine) asInteger). +b := GF create: 1234577 withValue: ((stdin nextLine) asInteger). + +a displayOn: stdout. +' + ' displayOn: stdout. +b displayOn: stdout. +' = ' displayOn: stdout. +(a + b) displayOn: stdout. +'' displayNl. + +a displayOn: stdout. +' - ' displayOn: stdout. +b displayOn: stdout. +' = ' displayOn: stdout. +(a - b) displayOn: stdout. +'' displayNl. + +a displayOn: stdout. +' * ' displayOn: stdout. +b displayOn: stdout. +' = ' displayOn: stdout. +(a * b) displayOn: stdout. +'' displayNl. + +a displayOn: stdout. +' / ' displayOn: stdout. +b displayOn: stdout. +' = ' displayOn: stdout. +(a / b) displayOn: stdout. +'' displayNl. diff --git a/lab2/zad3/test/Test.st b/lab2/zad3/test/Test.st new file mode 100644 index 0000000..782462d --- /dev/null +++ b/lab2/zad3/test/Test.st @@ -0,0 +1,50 @@ +FileStream fileIn: 'src/GF.st'. + +a := GF create: 5 withValue: 2. +b := GF create: 5 withValue: 4. + +(a + b) = (GF create: 5 withValue: 1) + ifFalse: [self error: 'failed on the addition test!']. + +(a - b) = (GF create: 5 withValue: 3) + ifFalse: [self error: 'failed on the subtraction test!']. + +a := GF create: 6 withValue: 2. +b := GF create: 6 withValue: 4. + +(a * b) = (GF create: 6 withValue: 2) + ifFalse: [self error: 'failed on the multiplication test!']. + +a := GF create: 7 withValue: 2. +b := GF create: 7 withValue: 4. + +(a / b) = (GF create: 7 withValue: 4) + ifFalse: [self error: 'failed on the division test!']. + +a := GF create: 5 withValue: 2. +b := GF create: 5 withValue: 0. + +exceptionRaised := false. + +(a / b) on: ZeroDivide do: [ :sig | + exceptionRaised := true. + ((sig errorString) = 'division by 0 is illegal') + ifFalse: [self error: 'division by 0 error string doesn''t match!']. +]. + +exceptionRaised + ifFalse: [self error: 'failed on the division by 0 test!']. + +a := GF create: 10 withValue: 5. +b := GF create: 8 withValue: 5. + +exceptionRaised := false. + +(a + b) on: Error do: [ :sig | + exceptionRaised := true. + ((sig errorString) = 'both arguments must have the same characteristic') + ifFalse: [self error: 'different characteristics error string doesn''t match!']. +]. + +exceptionRaised + ifFalse: [self error: 'failed on the different characteristics test!'].