28-02-16, 03:01 PM
(آخر تعديل لهذه المشاركة : 28-02-16, 03:06 PM {2} بواسطة silverlight.)
يمكن استخدام الكلاس التالي كبداية لما تريده و يمكن التعديل عليه بتغيير الحروف و الارقام لكنه ف النهاية سيؤدي الغرض المطلوب
الإستخدام
ملحوظة الكلاس حتي الان يعمل مع الحروف الإنجليزية الصغيرة small letters فقط و لكي يعمل مع اي حروف أخري عليك ان تضيف تلك الحروف الجديدة و الأرقام الجديدة الي كل من letters Property و Numerics Property
بالتاكيد تستطيع تغيير الحروف و الارقام كما شئت و ستحصل ف النهاية علي نفس النتائج المرجوة
ملحوظة أخيرة
في المستقبل ياريت تركز انت شوية و تكتب عنوان مناسب لموضوعك
و كمان الناس هنا بتحاول تساعدك بما يستطيعون
لذلك كن راقيا في طلب المساعدة تنال اجابة راقية
PHP كود :
Public Class Accumulator
Public Sub New()
End Sub
Public Function Accumulate(text As String) As Integer
Return AccumulateIntegers(ConvertTextToIntegers(text))
End Function
Protected ReadOnly Property letters As List(Of String)
Get
Return New List(Of String) From {"a", "b", "c", "d", "e", "f", "J", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
End Get
End Property
Protected ReadOnly Property Numerics As List(Of String)
Get
Return New List(Of String) From {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"}
End Get
End Property
Private Function ConvertTextToIntegers(text As String) As List(Of Integer)
Dim sourceValue As String = CType(text, String)
Dim result As List(Of Integer) = CType(Nothing, List(Of Integer))
If Not (String.IsNullOrEmpty(sourceValue)) Then
result = New List(Of Integer)()
For j = 0 To sourceValue.Length - 1
Dim s As String = sourceValue(j)
If letters.Contains(s) Then
Dim index As Integer = letters.IndexOf(s)
Dim value As String = Numerics(index)
result.Add(ParseInteger(value))
End If
Next
End If
Return result
End Function
Private Function AccumulateIntegers(source As IEnumerable(Of Integer)) As Integer
Return source.Aggregate(New Func(Of Integer, Integer, Integer)(Function(a, b)
Return a + b
End Function))
End Function
Private Function ParseInteger(stringToParse As String) As Integer
Return ParseInteger(stringToParse, False)
End Function
Private Function ParseInteger(stringToParse As String, throwException As Boolean) As Integer
Dim result As Integer = 0
If throwException Then
result = Integer.Parse(stringToParse, Globalization.NumberStyles.Any, Globalization.CultureInfo.InvariantCulture)
Else
If Not Integer.TryParse(stringToParse, Globalization.NumberStyles.Any, Globalization.CultureInfo.InvariantCulture, result) Then
Integer.TryParse(stringToParse, Globalization.NumberStyles.Any, Globalization.CultureInfo.CurrentCulture, result)
End If
End If
Return result
End Function
End Class
الإستخدام
PHP كود :
Dim total As Integer = New Accumulator().Accumulate("fcx")
ملحوظة الكلاس حتي الان يعمل مع الحروف الإنجليزية الصغيرة small letters فقط و لكي يعمل مع اي حروف أخري عليك ان تضيف تلك الحروف الجديدة و الأرقام الجديدة الي كل من letters Property و Numerics Property
بالتاكيد تستطيع تغيير الحروف و الارقام كما شئت و ستحصل ف النهاية علي نفس النتائج المرجوة
ملحوظة أخيرة
في المستقبل ياريت تركز انت شوية و تكتب عنوان مناسب لموضوعك
و كمان الناس هنا بتحاول تساعدك بما يستطيعون
لذلك كن راقيا في طلب المساعدة تنال اجابة راقية

