PDA

View Full Version : محاسبه مقدر ورودی



syncmasteyr
یک شنبه 16 مهر 1391, 23:54 عصر
سلام دوستان
بنده درون متغیری مثل x عبارتی محاسباتی مثل 3*2 یا 3+2 قرار دادم
حالا میخوام ببینم چیکار باید بکنم تا حاصل 2*3 یعنی 6 و یا 2+3 یعنی 5 توی x ذخیره بشه

شرمنده به خدا خیلی وقته از برنامه نویسی فاصله گرفتم کلا یادم رفته:لبخندساده: که همچین سوال ابتدایی می پرسم

امین مستانی
دوشنبه 17 مهر 1391, 00:32 صبح
سلام علیکم

باید از تابع Val استفاده کنید مانند :


x=Val(2 + 3)
یا
x = Val(2) + Val(3)


موفق باشید

syncmasteyr
دوشنبه 17 مهر 1391, 00:57 صبح
درود بر شما
بنده جای عدد عبارت زیررو قرار دادم

X=val(text1.text(
و وقتی درون text box مثلا مینویسم 2+3 عددی که در x ذخیره میشود 3 هست مشکل من دقیقا اینجاست

syncmasteyr
دوشنبه 17 مهر 1391, 01:26 صبح
من همچنان منتظر جواب هستم :ناراحت:

امین مستانی
دوشنبه 17 مهر 1391, 01:53 صبح
اینجوری شما نمیتونید استفاده کنید

یا باید دو تا تکست بذارید یا یک تکست با حافظه

البته نظر منه شاید بشه

امین مستانی
دوشنبه 17 مهر 1391, 01:56 صبح
الان یک تابع پیدا کردم که برای شما این کار رو میکنه

بفرمایید :


Function Calc(exp As String) As Double
'start searching for ()
'everything between "(" and ")"
'will calculate first

Dim startpos As Integer
Dim Count As Integer

Dim lexp As String ' left expression
Dim rexp As String ' right expression

Dim cmid As String

Dim exp2 As String
Dim answer2 As Double

' \ and / are same division
exp = Replace(exp, "\", "/")
' "[" and "(" are the same also "]" and ")"
exp = Replace(exp, "[", "(")
exp = Replace(exp, "]", ")")

startpos = InStr(1, exp, "(")

'we have found "("
If startpos > 0 Then
'searching for ")"
For endpos = (startpos + 1) To Len(exp)
Select Case Mid(exp, endpos, 1)
Case "("
Count = Count + 1
Case ")"
If Count = 0 Then Exit For
Count = Count - 1
End Select
Next endpos

' if there no ")" we will create ")" automatic
If Count > 0 Then
exp = exp & Replace(Space(Count + 1), " ", ")")
endpos = endpos + Count

End If

' check if () is empty or not
If endpos <= startpos + 1 Then
' if it empty it mean 0
exp2 = "0"
exp2 = Replace(exp, "()", "0")
Else
' get expression between "(" and ")"
exp2 = Mid(exp, startpos + 1, endpos - (startpos + 1))

' calculate expression between "(" and ")"
answer2 = Calc(exp2)

' replace the expression between "(" and ")"
' with the result that we have calculate
exp2 = Replace(exp, "(" & exp2 & ")", answer2)
End If

Calc = Calc(exp2)

Exit Function

ElseIf InStr(1, exp, "abs", vbTextCompare) > 0 Then

' get absolute start position
startpos = InStr(1, exp, "abs", vbTextCompare)
' clear all the text in exp2
exp2 = ""

' get number
For endpos = startpos + 3 To Len(exp)
cmid = Mid(exp, endpos, 1)
If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
exp2 = exp2 & cmid
Else
Exit For
End If
Next endpos

If exp2 <> "" Then
' convert it into absolute
answer2 = Abs(CDbl(exp2))

' replace the adsolute number with the answer
exp2 = Replace(exp, "abs" & exp2, answer2)
Calc = Calc(exp2)
Else
Mid(exp, startpos, 3) = "000"
Calc = Calc(exp)
End If

Exit Function

ElseIf InStr(1, exp, "cos", vbTextCompare) > 0 Then

' get cosines start position
startpos = InStr(1, exp, "cos", vbTextCompare)
' clear all the text in exp2
exp2 = ""

' get cosines angle
For endpos = startpos + 3 To Len(exp)
cmid = Mid(exp, endpos, 1)
If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
exp2 = exp2 & cmid
Else
Exit For
End If
Next endpos

If exp2 <> "" Then
' calculate cosines
answer2 = Cos(CDbl(exp2))

' replace cosines with the answer
exp2 = Replace(exp, "cos" & exp2, answer2)
Calc = Calc(exp2)
Else
Mid(exp, startpos, 3) = "000"
Calc = Calc(exp)
End If

Exit Function

ElseIf InStr(1, exp, "sin", vbTextCompare) > 0 Then

' get sines start position
startpos = InStr(1, exp, "sin", vbTextCompare)
' clear all the text in exp2
exp2 = ""

' get sines angle
For endpos = startpos + 3 To Len(exp)
cmid = Mid(exp, endpos, 1)
If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
exp2 = exp2 & cmid
Else
Exit For
End If
Next endpos

If exp2 <> "" Then
' calculate sines
answer2 = Sin(CDbl(exp2))

' replace cosines with the answer
exp2 = Replace(exp, "sin" & exp2, answer2)
Calc = Calc(exp2)
Else
Mid(exp, startpos, 3) = "000"
Calc = Calc(exp)
End If

Exit Function

ElseIf InStr(1, exp, "log", vbTextCompare) > 0 Then

' get logarithmic start position
startpos = InStr(1, exp, "log", vbTextCompare)
' clear all the text in exp2
exp2 = ""

' get number
For endpos = startpos + 3 To Len(exp)
cmid = Mid(exp, endpos, 1)
If IsNumeric(cmid) = True Or cmid = "." Or cmid = "," Then
exp2 = exp2 & cmid
Else
Exit For
End If
Next endpos

If exp2 <> "" Then
' calculate logarithmic
answer2 = Log(CDbl(exp2))

' replace logarithmic with the answer
exp2 = Replace(exp, "log" & exp2, answer2)
Calc = Calc(exp2)
Else
Mid(exp, startpos, 3) = "000"
Calc = Calc(exp)
End If

Exit Function

ElseIf InStr(1, exp, "mod") > 1 Then

' modulo

startpos = InStr(1, exp, "mod")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - (startpos + 2))

Calc = Calc(lexp) Mod Calc(rexp)

Exit Function

ElseIf InStr(1, exp, "+") > 1 Then

' addition

startpos = InStr(1, exp, "+")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)

Calc = Calc(lexp) + Calc(rexp)

Exit Function

ElseIf InStr(1, exp, "-") > 1 Then

' subtract

startpos = InStr(1, exp, "+")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)

Calc = Calc(lexp) - Calc(rexp)

Exit Function

ElseIf InStr(1, exp, "*") > 1 Then

' multiply

startpos = InStr(1, exp, "*")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)

Calc = Calc(lexp) * Calc(rexp)

Exit Function

ElseIf InStr(1, exp, "/") > 1 Then

' divide

startpos = InStr(1, exp, "/")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)

answer2 = Calc(rexp)
If answer2 = 0 Then answer2 = 1

Calc = Calc(lexp) / answer2

Exit Function

ElseIf InStr(1, exp, "^") > 1 Then

' exponent

startpos = InStr(1, exp, "^")
lexp = Left(exp, startpos - 1)
rexp = Right(exp, Len(exp) - startpos)

Calc = Calc(lexp) ^ Calc(rexp)

Exit Function
Else

If IsNumeric(exp) = True Then
Calc = CDbl(exp)
Else
Calc = 0
End If
Exit Function

End If
End Function

Private Sub Command1_Click()
Print Calc(Text1.Text)
End Sub

MohammadGh2011
دوشنبه 17 مهر 1391, 11:39 صبح
درود بر شما
بنده جای عدد عبارت زیررو قرار دادم

X=val(text1.text(
و وقتی درون text box مثلا مینویسم 2+3 عددی که در x ذخیره میشود 3 هست مشکل من دقیقا اینجاست

سلام عليکم
براي اينکار شما بايد از دستور زير استفاده کنيد ولي دستور زير فقط محاسبه ي جمع دو عدد رو ميده مثلا 3+2 يا 9+4 ،

x = Mid(Text1, 1, 1)
y = Mid(Text1, 3, 1)
Print Val(x) + Val(y)


موفق باشيد

syncmasteyr
دوشنبه 17 مهر 1391, 15:18 عصر
واقعا نمیدونم آقای مستانی من چجوری از شما تشکر کنم ،تابعی که معرفی کردید تمام نیاز های نرم افزار منو بر طرف کرد:قلب:
MohammadGh2011 (http://barnamenevis.org/member.php?189627-MohammadGh2011) تشکر ولی بنده قصد انجام عملیات مختلف تو textbox رو داشتم نه فقط جمع کردن رو جناب امین مستانی تابعی رو معرفی کردند که کار منو تماما راه انداخت

MohammadGh2011
دوشنبه 17 مهر 1391, 15:39 عصر
واقعا نمیدونم آقای مستانی من چجوری از شما تشکر کنم ،تابعی که معرفی کردید تمام نیاز های نرم افزار منو بر طرف کرد:قلب:
MohammadGh2011 (http://barnamenevis.org/member.php?189627-MohammadGh2011) تشکر ولی بنده قصد انجام عملیات مختلف تو textbox رو داشتم نه فقط جمع کردن رو جناب امین مستانی تابعی رو معرفی کردند که کار منو تماما راه انداخت
البته کدي رو که گذاشتم رو هم ميشه روش کار کرد و تمام محسبات رو انجام بده به هرحال موفق باشيد دوست گرامي.