正整数计算游戏:设计个给幼儿玩的计算器,只有+-*/功能,只要求计算部分,不要求控件设计
1、数字和运算符号间可能有空格,如“5 + 2 ”
2、可能出现 12 56 或 + - 或 45A67,去掉不合理的内容,变成1256 或 + 或 4567
3、数值计算是按先后顺序如 2+3*2-4=6
4、要处理的数据首尾可能是空格或运算符,可去掉不合理的运算符
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As String = " -2 + -13 * 6A2 -*104 /+ 5/" '这个也能计算,相当于2+13*62-104/5
a = a.Trim(New Char() {"+", "-", "*", "/", " "})
'防止错顷辩误
a = Regex.Replace(a, "[^0-9\+\-\*/]", "") '清除非法字符
a = Regex.Replace(a, "(?<=[\+\-\*/])([\+\-\*/])", "") '去除多余的连续+-*/
'分隔数值
Dim g() As String = (From mt As Match In Regex.Matches(a, "[0-9]+") Select mt.Value).ToArray
'提取运算符
Dim g_m() As String = (From mt As Match In Regex.Matches(a, "[\+\-\*/]") Select mt.Value).ToArray
'数值计算
Dim My_Sum As Integer = Trim(g(0))
For i = 1 To g.GetUpperBound(0)
My_Sum= New DataTable().Compute(My_Sum &g_m(i - 1) &Trim(g(i)), "")
Next
End Sub
End Class
My_Sum是计算结果
Option ExplicitPrivate Sub Form_Click()
On Error Resume Next
Dim i As Integer, a As Integer
Randomize
Me.Caption = "正在生拆含哪成随老档机旅码数中 ...."
Do While Not a = 10000
a = a + 1
Print Int(Rnd * (800 - 200 + 1)) + 1
Loop
Cls
Print "520"
Me.Caption = "生成结束,最后得出:520"
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)