• R/O
  • SSH
  • HTTPS

コミット

タグ
未設定

よく使われているワード(クリックで追加)

javaandroidc++linuxc#objective-ccocoa誰得qtrubypythonbathyscaphephpgamewindowsguic翻訳omegattwitterframeworktestbtronarduinovb.net計画中(planning stage)directxpreviewerゲームエンジンdom

練習用です。いろんなものがごちゃまぜです。


コミットメタ情報

リビジョン390 (tree)
日時2018-05-10 16:21:09
作者bellyoshi

ログメッセージ

変更サマリ

差分

--- Calc/Calc/Form1.vb (revision 389)
+++ Calc/Calc/Form1.vb (revision 390)
@@ -1,9 +1,16 @@
11 Public Class Form1
22
33 Private r As New calcModel
4+ Private s As String = String.Empty
45
56 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, ButtonPlus.Click, Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button14.Click, Button13.Click, Button12.Click, Button11.Click, Button0.Click
6- Dim t = DirectCast(sender, Button).Text
7+ Dim t = DirectCast(sender, Button).Text(0)
78 r.add(t)
9+ TextBox1.Text = r.LastValue()
10+ ListBox1.Items(0) = r.Last
811 End Sub
12+
13+ Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
14+ ListBox1.Items.Add(s)
15+ End Sub
916 End Class
--- Calc/Calc/calcModel.vb (revision 389)
+++ Calc/Calc/calcModel.vb (revision 390)
@@ -1,10 +1,38 @@
11 Public Class calcModel
22
33
4+ Public history As List(Of String)
45
6+ Public Last As String
57
6- Public Sub add(ByVal c As String)
8+ Private isNew As Boolean = False
79
10+ Private e As New evaluator
11+
12+ Public Function LastValue() As String
13+ Return e.ValueStr(Last)
14+ End Function
15+
16+
17+ Public Sub add(ByVal c As Char)
18+
19+ If isNew Then
20+ isNew = False
21+
22+ If Not ("0"c <= c AndAlso c <= "9"c) Then
23+ Last = e.ValueStr(Last)
24+ Else
25+ Last = ""
26+ End If
27+
28+ End If
29+
30+ Last &= c
31+
32+ If c = "="c Then
33+ isNew = True
34+ End If
35+
836 End Sub
937
1038 End Class
--- Calc/Calc/evaluator.vb (revision 389)
+++ Calc/Calc/evaluator.vb (revision 390)
@@ -1,7 +1,58 @@
11 Public Class evaluator
22
3- Public Function ValueStr(ByVal expression) As String
4- Return "0"
3+ Private num As Decimal = 0
4+ Private ans As Decimal = 0
5+ Private ope As String = ""
6+
7+ Public Function ValueStr(ByVal expression As String) As String
8+ num = 0
9+ ans = 0
10+ ope = "+"
11+ Dim isDigit = True
12+ For Each c In expression
13+ If "0"c <= c AndAlso c <= "9"c Then
14+ num *= 10
15+ num += Asc(c) - Asc("0")
16+ isDigit = True
17+ Else
18+ isDigit = False
19+ End If
20+ If c = "×" OrElse c = "*" Then
21+ NewOpe("*")
22+ ElseIf c = "/" OrElse c = "÷" Then
23+ NewOpe("/")
24+ ElseIf c = "-" Then
25+ NewOpe("-")
26+ ElseIf c = "+" Then
27+ NewOpe("+")
28+ ElseIf c = "=" Then
29+ NewOpe("=")
30+ End If
31+ Next
32+ If isDigit Then
33+ Return num.ToString()
34+ Else
35+ Return ans.ToString()
36+ End If
537 End Function
638
39+ Public Sub NewOpe(ByVal ope As String)
40+ DoOpe()
41+ Me.ope = ope
42+ num = 0
43+ End Sub
44+
45+ Private Sub DoOpe()
46+ Select Case ope
47+ Case "+"
48+ ans += num
49+ Case "-"
50+ ans -= num
51+ Case "/"
52+ ans /= num
53+ Case "*"
54+ ans *= num
55+ End Select
56+ End Sub
57+
758 End Class
--- Calc/UnitTestProject1/evaluatorTest.vb (revision 389)
+++ Calc/UnitTestProject1/evaluatorTest.vb (revision 390)
@@ -5,8 +5,16 @@
55
66 <TestMethod()> Public Sub ValueStrTest()
77 Dim e As New Calc.evaluator
8-
98 Assert.AreEqual("0", e.ValueStr(""))
9+ Assert.AreEqual("1", e.ValueStr("1"))
10+ Assert.AreEqual("10", e.ValueStr("10"))
11+ Assert.AreEqual("17", e.ValueStr("5+12="))
12+ Assert.AreEqual("123", e.ValueStr("5+123"))
13+ Assert.AreEqual("-10", e.ValueStr("-10="))
14+ Assert.AreEqual("4", e.ValueStr("1+2+3+4"))
15+ Assert.AreEqual("10", e.ValueStr("1+2+3+4="))
16+ Assert.AreEqual("8", e.ValueStr("-1+2+3+4="))
17+ Assert.AreEqual("16", e.ValueStr("-1+2+3*4+"))
1018 End Sub
1119
1220 End Class
\ No newline at end of file