74 lines
2.0 KiB
Plaintext
74 lines
2.0 KiB
Plaintext
DataSection
|
|
SetA:
|
|
Data.i 4
|
|
Data.s "John", "Bob", "Mary", "Serena"
|
|
; Data.i 5
|
|
; Data.s "John", "Serena", "Bob", "Mary", "Serena"
|
|
SetB:
|
|
Data.i 4
|
|
Data.s "Jim", "Mary", "John", "Bob"
|
|
; Data.i 5
|
|
; Data.s "Jim", "Mary", "John", "Jim", "Bob"
|
|
EndDataSection
|
|
|
|
Procedure addElementsToSet(List x.s())
|
|
;requires the read pointer to be set prior to calling by using 'Restore'
|
|
Protected i, count
|
|
|
|
Read.i count
|
|
For i = 1 To count
|
|
AddElement(x())
|
|
Read.s x()
|
|
Next
|
|
EndProcedure
|
|
|
|
Procedure displaySet(List x.s())
|
|
Protected i, count = ListSize(x())
|
|
FirstElement(x())
|
|
For i = 1 To count
|
|
Print(x())
|
|
NextElement(x())
|
|
If i <> count: Print(", "): EndIf
|
|
Next
|
|
PrintN("")
|
|
EndProcedure
|
|
|
|
Procedure symmetricDifference(List a.s(), List b.s(), List result.s())
|
|
Protected ACount = ListSize(a()), BCount = ListSize(b()), prev.s
|
|
|
|
;this may leave set a and b in a different order
|
|
SortList(a(),#PB_Sort_Ascending)
|
|
SortList(b(),#PB_Sort_Ascending)
|
|
|
|
FirstElement(a())
|
|
FirstElement(b())
|
|
LastElement(result()) ;add to end of result()
|
|
While ACount > 0 Or BCount > 0
|
|
If ACount <> 0 And BCount <> 0 And a() = b()
|
|
ACount - 1: NextElement(a())
|
|
BCount - 1: NextElement(b())
|
|
ElseIf BCount = 0 Or (ACount <> 0 And a() < b())
|
|
AddElement(result()): result() = a()
|
|
prev = a(): Repeat: ACount - 1: NextElement(a()): Until ACount = 0 Or (a() <> prev)
|
|
ElseIf ACount = 0 Or (BCount <> 0 And a() > b())
|
|
AddElement(result()): result() = b()
|
|
prev = b(): Repeat: BCount - 1: NextElement(b()): Until BCount = 0 Or (b() <> prev)
|
|
EndIf
|
|
Wend
|
|
EndProcedure
|
|
|
|
If OpenConsole()
|
|
NewList a.s(): Restore SetA: addElementsToSet(a())
|
|
NewList b.s(): Restore SetB: addElementsToSet(b())
|
|
Print("Set A: "): displaySet(a())
|
|
Print("Set B: "): displaySet(b())
|
|
|
|
NewList sd.s()
|
|
symmetricDifference(a(), b(), sd())
|
|
Print("Symmetric Difference: "): displaySet(sd())
|
|
|
|
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
|
|
Input()
|
|
CloseConsole()
|
|
EndIf
|