Gambas/数学
外观
< Gambas
返回 Gambas
这个小程序计算一个整数的交叉求和。你需要两个文本框和一个命令按钮来运行程序。
请注意符号 / 和 \。在这个程序中,符号 \ 用于无余数除法。
代码
PUBLIC SUB Button1_Click()
DIM z AS Integer
DIM crosssum AS Integer
z = Val(TextBox1.Text)
DO WHILE z <> 0
crosssum = crosssum + z MOD 10
z= z \ 10
LOOP
TextBox2.Text = Str$(crosssum)
END
这个小程序计算一列整数的总和。你需要一个文本区域、一个文本框和一个命令按钮来运行程序。你也可以添加负整数。非常有帮助的是 Split 命令和字符串数组 String[]
代码
PUBLIC SUB Button1_Click()
DIM text AS String
DIM summe AS Float
DIM Elt AS String[]
DIM Sb AS String
text = textarea1.Text
Elt = Split(text,Chr(10))
FOR EACH Sb IN Elt
summe = summe + Val(sb)
NEXT
textbox1.Text = summe
END
这个小程序计算一列整数的平均值。你需要一个文本区域、两个文本框和一个命令按钮来运行程序。
代码
PUBLIC SUB Button1_Click()
DIM text AS String
DIM summe AS Float
DIM mw AS Float
DIM varianz AS Float
DIM sigma AS Float
DIM Liste AS String[]
DIM Einzelwert AS String
text = textarea1.Text
Liste = Split(text,Chr(10))
FOR EACH Einzelwert IN Liste
summe = summe + Val(Einzelwert)
NEXT
mw = summe / Liste.Length
textbox1.Text = mw
varianz = 0
FOR EACH Einzelwert IN Liste
varianz = varianz + ((Val(Einzelwert) - mw)^2)
PRINT Einzelwert,mw,((Val(Einzelwert) - mw)^2)
PRINT varianz
NEXT
varianz = varianz / (Liste.Length - 1)
PRINT varianz
sigma = Sqr(varianz)
textbox2.Text = sigma
END
一组值的**中位数**位于这组值的两等份之间。50% 的值大于或等于中位数。50% 的值小于或等于中位数。与平均值不同的是,中位数不会受到极端值的影响太大。
如果你想编程计算中位数,你需要:
- 1. 对你的值列表进行排序
- 2. 检查列表中是否有偶数个值还是奇数个值
- 3. 选择中位数的正确公式
- 对于奇数个值
xmedian = x[(n+1)/2]
- 对于偶数个值
xmedian = 1/2*(x[n/2] + x[n/2+1])
示例 1
排序后的值列表
11 12 13 14 15
值的个数是奇数,n = 5
(n+1)/2 = 6/2 = 3
xmedian = x[3] = 13
示例 2
排序后的值列表
11 12 13 14 15 16
值的个数是偶数,n = 6
n/2 = 6/2 = 3 x[3] = 13 x[4] = 14 xmedian = 1/2*(13+14) = 13,5
你可以从上面获取列表,并将其复制到 Gambas 中位数程序。使用计算机的剪贴板来操作。Ctrl+C = 复制,Ctrl + V = 粘贴
示例程序
你需要两个文本区域、三个命令按钮和一个文本框来运行程序
' Gambas CLASS file
PUBLIC liste AS String[]
PUBLIC SUB Form_Open()
ME.Text = "Computate the Median "
END
PUBLIC SUB Button1_Click()
'sort
Dim c AS Integer
Dim j AS Integer
Dim n AS Integer
Dim y AS Variant
Dim liste AS String[]
Dim element AS String
Dim txt AS String
Dim text AS String
text = Textarea1.Text
liste = Split(text,Chr(10))
y = 0
n = liste.length
REPEAT
c = 0
FOR j = 0 TO n - 2
'PRINT j,y,liste[0],ar[1],ar[2],ar[3],ar[4]
IF Val(liste[j]) > Val(liste[j + 1]) THEN
y = Val(liste[j])
liste[j] = liste[j + 1]
liste[j + 1] = Str(y)
c = 1
ENDIF
NEXT
UNTIL c = 0
FOR EACH element IN liste
txt = txt & Str(element) & Chr(10)
NEXT
PRINT txt
textarea2.Text = ""
txt = Mid$(txt,1,-1)
'the last CR has to be killed chr(10)
textarea2.Text = txt
END
PUBLIC SUB Button2_Click()
'computate median, but sort first !!
Dim text AS String
Dim median AS Float
Dim liste AS String[]
Dim posten AS String
text = Textarea2.Text
liste = Split(text,Chr(10))
'count the length of your list and choose the right formula
IF liste.Length MOD 2 THEN
'PRINT liste.Length MOD 2 & " odd"
'PRINT (liste.length + 1)/2
'PRINT liste[(liste.length + 1)/2 - 1]
median = Val(liste[(liste.length + 1)/2 - 1])
'the array starts with the element 0 not with the element 1 !
ELSE
'PRINT liste.Length MOD 2 & " even"
median = (Val(liste[liste.length/2 - 1]) + Val(liste[liste.length/2]))/2
ENDIF
textbox1.Text = Str(median)
END
PUBLIC SUB Button3_Click()
'list of values as an example
textarea1.Text = "114,3"
textarea1.Text = textarea1.Text & Chr(10) & "135,7"
textarea1.Text = textarea1.Text & Chr(10) & "104,8"
textarea1.Text = textarea1.Text & Chr(10) & "118,5"
textarea1.Text = textarea1.Text & Chr(10) & "125,7"
textarea1.Text = textarea1.Text & Chr(10) & "121,4"
textarea1.Text = textarea1.Text & Chr(10) & "122,4"
textarea1.Text = textarea1.Text & Chr(10) & "96,8"
textarea1.Text = textarea1.Text & Chr(10) & "118,9"
textarea1.Text = textarea1.Text & Chr(10) & "120"
textarea1.Text = textarea1.Text & Chr(10) & "112,2"
textarea1.Text = textarea1.Text & Chr(10) & "127,9"
textarea1.Text = textarea1.Text & Chr(10) & "122,8"
textarea1.Text = textarea1.Text & Chr(10) & "128,9"
textarea1.Text = textarea1.Text & Chr(10) & "120,3"
'median = 120,3
END