4 Replies - 16016 Views - Last Post: 05 June 2012 - 01:26 AM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Challenge: Mission Operator Overlord.

Post icon  Posted 31 May 2012 - 05:58 AM

Challenge: Mission Operator Overlord

Challenge: The standard operator are so boring, that it time to implement some let's say more creative ones.

Your task is to implement working example(s) of your more creative use of operators.

This post has been edited by AdamSpeight2008: 31 May 2012 - 06:07 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Challenge: Mission Operator Overlord.

#2 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 342
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: Challenge: Mission Operator Overlord.

Posted 31 May 2012 - 06:14 AM

Someone make an operator, I have never needed it in my life yet it takes up a key on my mac keyboard :(
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: Challenge: Mission Operator Overlord.

Posted 31 May 2012 - 06:16 AM

Sadly there is no § operator in .net to overload, Unfortunately.

I really would to see someone overload the LIKE operator.

This post has been edited by AdamSpeight2008: 31 May 2012 - 06:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,135
  • Joined: 12-June 08

Re: Challenge: Mission Operator Overlord.

Posted 31 May 2012 - 11:44 AM

Quote

I really would to see someone overload the LIKE operator.

Challenge accepted.

The code to use the class's "like".
    Sub Main()

        Dim foo As New Twelve
        foo.Beibs = 20
        Dim bar As New Twelve
        bar.Beibs = 21

        Dim aaa As New Twelve
        aaa.Beibs = 69

        For i As Int32 = 0 To 10
            If foo Like bar Then
                Console.WriteLine("Like!")
            Else
                Console.WriteLine("No Like!")
            End If

            If aaa Like bar Then
                Console.WriteLine("Like!")
            Else
                Console.WriteLine("No Like!")
            End If
        Next
        Stop
    End Sub



The class and structures involved in it.

''' <summary>
    ''' A simple class to hold numbers relevant to the Beibs property, and used for the 'like' comparison overload.
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Twelve
        Private Structure Nuts
            Public sVal As String
            Public lVal As Int32
            Public Sub New(ByVal s As String, ByVal l As Int32)
                sVal = s
                lVal = l
            End Sub
        End Structure

        Public Beibs As Int32

        ''' <summary>
        ''' Like is based off a grueling game of MASH.
        ''' </summary>
        ''' <param name="left"></param>
        ''' <param name="right"></param>
        ''' <returns></returns>
        ''' <remarks>Through rigerous testing of houses, future mates, cars, and number of children a composite score is greated and a boolean summary of that is returned.</remarks>
        Public Shared Operator Like(ByVal left As Twelve, ByVal right As Twelve)
            System.Threading.Thread.Sleep(11) '-- back to back it gets abit.. similar

            Dim myRand As New Random(Now.Hour + Now.Minute + Now.Second + Now.Millisecond + Now.Day + Now.Month + Now.Year)

            Dim lVal As Int32 = 0

            Dim sArrayBoyNames() As String
            Dim sArrayGirlNames() As String
            Dim sTempListOfNames As New List(Of String)

            Dim lWordsNumberOfKids As New List(Of Int32)

            Dim oHouses As New List(Of Nuts)
            Dim oNames As New List(Of Nuts)
            Dim oCars As New List(Of Nuts)
            Dim oListOfCars As New List(Of Nuts)

            '-- houses with assigned weights.
            oHouses.AddRange({New Nuts("Mansion", 4), New Nuts("Apartment", 1), New Nuts("Shack", -3), New Nuts("House", 3)})

            '-- thirty boy names
            sArrayBoyNames = {"William", "James", "John", "Robert", "Sean", "Nathan", "Andrew", "Jesse", "Ross",
                              "Gregory", "Josh", "Jacob", "Jamal", "Malcolm", "Reese", "Dewey", "Francis", "Hal", "Jack", "Humphrey",
                              "Eric", "Poindexter", "Harry", "Aaron", "Daniel", "Ethan", "Morgan", "Tyler", "Stephen", "Joseph"}
            '-- thirty boy names
            sArrayGirlNames = {"Sophia", "Isabella", "Emma", "Olivia", "Ava", "Emily", "Abigail", "Madison", "Mia", "Chloe",
                                "Elizabeth", "Ella", "Addison", "Natalie", "Lily", "Grace", "Samantha", "Avery", "Sofia", "Aubrey",
                                "Brooklyn", "Lillian", "Victoria", "Evelyn", "Hannah", "Alexis", "Charlotte", "Zoey", "Leah", "Amelia"}
            '-- important list of cars and assigned weights.
            oListOfCars.AddRange({New Nuts("porche", 15), New Nuts("ferrari", 16), New Nuts("station wagon", -4), New Nuts("mini van", -5), New Nuts("pinto", -10), New Nuts("jeep", 15), New Nuts("taurus", 1), New Nuts("truck", 15), New Nuts("SUV", 10), New Nuts("bug", 7), New Nuts("motorcycle", 12), New Nuts("el camino", -15)})

            '-- random flip if we are dealing with a boy or girl this time.
            If myRand.Next(2) = 0 Then
                '-- boy list
                sTempListOfNames.AddRange(sArrayBoyNames)

                '-- randomly select five names and assign five weights
                For i As Int32 = 0 To 4
                    lVal = myRand.Next(sTempListOfNames.Count - 1)
                    oNames.Add(New Nuts(sTempListOfNames(lVal), myRand.Next(-20, 20)))
                    sTempListOfNames.RemoveAt(lVal)
                Next

            Else
                '-- girl list
                sTempListOfNames.AddRange(sArrayGirlNames)

                '-- randomly select five names and assign five weights
                For i As Int32 = 0 To 4
                    lVal = myRand.Next(sTempListOfNames.Count - 1)
                    oNames.Add(New Nuts(sTempListOfNames(lVal), myRand.Next(-20, 20)))
                    sTempListOfNames.RemoveAt(lVal)
                Next
            End If

            '-- select five cars, at random, and add them to the list.
            For i As Int32 = 0 To 4
                lWordsNumberOfKids.Add(myRand.Next(21))
                lVal = myRand.Next(oListOfCars.Count - 1)
                oCars.Add(oListOfCars(lVal))
                oListOfCars.RemoveAt(lVal)
            Next

            ''-- uncomoment if you caer to see the list.
            'Console.WriteLine("House:")
            'Console.WriteLine("------")
            'For i As Int32 = 0 To oHouses.Count - 1
            '    Console.WriteLine(String.Format("{0} ({1})", oHouses(i).sVal, oHouses(i).lVal))
            'Next
            'Console.WriteLine(Environment.NewLine)
            'Console.WriteLine("Names:")
            'Console.WriteLine("------")
            'For i As Int32 = 0 To oNames.Count - 1
            '    Console.WriteLine(String.Format("{0} ({1})", oNames(i).sVal, oNames(i).lVal))
            'Next
            'Console.WriteLine(Environment.NewLine)
            'Console.WriteLine("Cars:")
            'Console.WriteLine("------")
            'For i As Int32 = 0 To oCars.Count - 1
            '    Console.WriteLine(String.Format("{0} ({1})", oCars(i).sVal, oCars(i).lVal))
            'Next
            'Console.WriteLine(Environment.NewLine)
            'Console.WriteLine("Kids:")
            'Console.WriteLine("------")
            'For i As Int32 = 0 To lWordsNumberOfKids.Count - 1
            '    Console.WriteLine(lWordsNumberOfKids(i))
            'Next

            '-- randomly determine how many numbers to cycle through.
            lVal = myRand.Next(1, 23)
            '-- keep track of which number we are on when counting.
            Dim lCounter As Int32 = 0
            '-- basic loop control for the internal wheres
            Dim a As Int32 = 0

            '-- only stop that train when we have one of each category.
            While Not (oHouses.Count = 1 AndAlso oNames.Count = 1 AndAlso oCars.Count = 1 AndAlso lWordsNumberOfKids.Count = 1)
                If oHouses.Count > 1 Then
                    a = 0
                    While a < oHouses.Count - 1
                        lCounter += 1
                        If lCounter = lVal Then
                            oHouses.RemoveAt(a)
                            lCounter = 0
                        End If
                        a += 1
                    End While
                End If

                If oNames.Count > 1 Then
                    a = 0
                    While a < oNames.Count - 1
                        lCounter += 1
                        If lCounter = lVal Then
                            oNames.RemoveAt(a)
                            lCounter = 0
                        End If
                        a += 1
                    End While
                End If

                If oCars.Count > 1 Then
                    a = 0
                    While a < oCars.Count - 1
                        lCounter += 1
                        If lCounter = lVal Then
                            oCars.RemoveAt(a)
                            lCounter = 0
                        End If
                        a += 1
                    End While
                End If

                If lWordsNumberOfKids.Count > 1 Then
                    a = 0
                    While a < lWordsNumberOfKids.Count - 1
                        lCounter += 1
                        If lCounter = lVal Then
                            lWordsNumberOfKids.RemoveAt(a)
                            lCounter = 0
                        End If
                        a += 1
                    End While
                End If
            End While

            '-- uncomment if you want to see the returned value.
            'Console.WriteLine(Environment.NewLine)
            'Console.WriteLine(String.Format("House: {0} ({1})", oHouses(0).sVal, oHouses(0).lVal))
            'Console.WriteLine(String.Format("Name: {0} ({1})", oNames(0).sVal, oNames(0).lVal))
            'Console.WriteLine(String.Format("Car: {0} ({1})", oCars(0).sVal, oCars(0).lVal))
            'Console.WriteLine("Kids: " + lWordsNumberOfKids(0).ToString)

            '-- basic evaluation.. add up all the positive and negative numbers.
            '-- after intense scientific study - any children over three become a problem.

            Dim lTotal As Int32 = 0
            lTotal += oHouses(0).lVal
            lTotal += oNames(0).lVal
            lTotal += oCars(0).lVal
            lTotal += (3 - lWordsNumberOfKids(0))

            ''-- uncomment if you want to see the returned value.
            'Console.WriteLine("Composit score of: " + lTotal.ToString)
            'Console.WriteLine(String.Format("Like?: {0}", lTotal > 0))

            '  Console.WriteLine(String.Format("House: {0} ({1})  Name: {2} ({3})  Car: {4} ({5}) Kids: ", oHouses(0).sVal, oHouses(0).lVal, oNames(0).sVal, oNames(0).lVal, oCars(0).sVal, oCars(0).lVal, lWordsNumberOfKids(0)))
            Return lTotal > 0
        End Operator
    End Class



Was This Post Helpful? 1
  • +
  • -

#5 pradeep1210  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 56
  • Joined: 08-May 12

Re: Challenge: Mission Operator Overlord.

Posted 05 June 2012 - 01:26 AM

Please accept something from me too :)

The class:
Public Class Thing
    Public Enum ThingProperties
        [Class]
        Color
        Size
        Shape
        'TODO: Define your property types here
    End Enum

    Public Property Properties As New Dictionary(Of ThingProperties, String)
    Public Property Name As String

    Public Sub New(ByVal name As String)
        Me.Name = name
    End Sub

    Public Overloads Shared Operator Like(ByVal a As Thing, ByVal b As Thing) As Boolean
        'TODO: Incease or decrease this depending on what precision of match you want to have
        Return MatchPercent(a,B)/> >= 50
    End Operator

    Public Overloads Shared Operator =(ByVal a As Thing, ByVal b As Thing) As Boolean
        Return MatchPercent(a,B)/> = 100
    End Operator

    Public Overloads Shared Operator <>(ByVal a As Thing, ByVal b As Thing) As Boolean
        Return MatchPercent(a,B)/> <> 100
    End Operator

    Public Overrides Function ToString() As String
        Return Me.Name
    End Function

    Private Shared Function MatchPercent(ByVal a As Thing, ByVal b As Thing) As Integer
        Dim matches, found, notFound As Integer

        If a Is b Then Return 100

        For Each key In a.Properties.Keys
            If b.Properties.ContainsKey(key) Then
                If a.Properties(key).ToLower = b.Properties(key).ToLower Then matches += 1
                found += 1
            Else
                notFound += 1
            End If
        Next
        If found = 0 OrElse found < notFound Then
            Return 0
        Else
            Return CInt(matches * 100 / found)
        End If
    End Function
End Class



Sample Usage:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim apple As New Thing("Apple")
    Dim mango As New Thing("Mango")
    Dim orange As New Thing("Orange")

    apple.Properties.Add(ThingProperties.Class, "Apple")
    mango.Properties.Add(ThingProperties.Class, "Mango")
    orange.Properties.Add(ThingProperties.Class, "Orange")

    apple.Properties.Add(ThingProperties.Color, "Red")
    mango.Properties.Add(ThingProperties.Color, "Yellow")
    orange.Properties.Add(ThingProperties.Color, "Orange")

    apple.Properties.Add(ThingProperties.Size, "10")
    mango.Properties.Add(ThingProperties.Size, "10")
    orange.Properties.Add(ThingProperties.Size, "10")

    apple.Properties.Add(ThingProperties.Shape, "Round")
    mango.Properties.Add(ThingProperties.Shape, "Oval")
    orange.Properties.Add(ThingProperties.Shape, "Round")

    Dim msg As New StringBuilder
    msg.AppendFormat("{0} is {2} {1}" & vbCrLf, apple, mango, If(apple Like mango, "like", "not like"))
    msg.AppendFormat("{0} is {2} {1}" & vbCrLf, apple, orange, If(apple Like orange, "like", "not like"))
    msg.AppendFormat("{0} is {2} {1}" & vbCrLf, mango, orange, If(mango Like orange, "like", "not like"))

    MessageBox.Show(msg.ToString)
End Sub

This post has been edited by pradeep1210: 05 June 2012 - 01:35 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1