XAMLab(ザムラボ)
ザムラボ - XAML研究室、WPF、Silverlight関連など。

目次

Blog 利用状況
  • 投稿数 - 202
  • 記事 - 1
  • コメント - 1193
  • トラックバック - 43
ニュース

書庫

日記カテゴリ

ギャラリ

なか-chan関連サイト

他の方へのリンク

 

第1回 『 The 祭り 』電卓アプリケーション仕様決定!

ってことで、それに向けていろいろ検討。
まずは、「逆ポーランド記法で...」といってしまった以上、
作ってみました。(条件は満たしていませんが...)
スタックを使えば簡単に実装できます。

↑Windowsって、PRNというファイル名は使えないんですね。
なんどやってもエラーになるのでおかしいと思いました(TT)

使い方

フォームのテキストボックスにたとえば、「3 1 * 4 2 / 5 + -」と入力して
リターンキーを押すと、答えが「 = -4」と表示されるだけのものです。
(3に1を掛け、4を2で割りそれに5を足したものを引く)という意味。
逆ポーランド記法(PRN)については、Wikipediaなどをご覧ください。

VB.NET

Private Sub TextBox1_KeyDown(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.KeyEventArgs) _
    Handles TextBox1.KeyDown
If e.KeyCode <> Keys.Return Then Return
Dim s As New System.Collections.Generic.Stack(Of Double)
Try
    For Each arg As String In TextBox1.Text.Split
        Select Case arg
            Case "+"
                s.Push(s.Pop + s.Pop)
            Case "-"
                s.Push(-s.Pop + s.Pop)
            Case "*"
                s.Push(s.Pop * s.Pop)
            Case "/"
                s.Push(1 / s.Pop * s.Pop)
            Case "="
            Case Else
                s.Push(Double.Parse(arg))
        End Select
    Next arg
    TextBox1.Text += " = " + s.Pop.ToString
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try
End Sub

C#

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode != Keys.Return) return;
    System.Collections.Generic.Stack<double> s =
        new System.Collections.Generic.Stack<double>();
    try
    {
        foreach (string arg in textBox1.Text.Split())
        {
            switch (arg)
            {
                case "+": s.Push(s.Pop() + s.Pop()); break;
                case "-": s.Push(-s.Pop() + s.Pop()); break;
                case "*": s.Push(s.Pop() * s.Pop()); break;
                case "/": s.Push(1 / s.Pop() * s.Pop()); break;
                case "=": break;
                default: s.Push(double.Parse(arg)); break;
            }
        }
        textBox1.Text += " = " + s.Pop();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
投稿日時 : 2007年6月28日 2:53
コメント
  • # re: 逆ポーランド記法の電卓
    なか-chan@最愛のiMac
    Posted @ 2007/06/28 6:04
    なぜかトラックバックがいかないので、リンク
    http://blogs.wankuma.com/aqua/archive/2007/06/27/82456.aspx
  • # Excellent site. A lot of helpful information here. I am sending it to some buddies ans additionally sharing in delicious. And certainly, thanks for your effort!
    Excellent site. A lot of helpful information here.
    Posted @ 2019/05/03 2:47
    Excellent site. A lot of helpful information here.
    I am sending it to some buddies ans additionally sharing in delicious.

    And certainly, thanks for your effort!
  • # Ahaa, its fastidious dialogue on the topic of this piece of writing at this place at this website, I have read all that, so at this time me also commenting at this place.
    Ahaa, its fastidious dialogue on the topic of this
    Posted @ 2019/05/12 17:33
    Ahaa, its fastidious dialogue on the topic of this piece of writing at this place at
    this website, I have read all that, so at this time me also commenting at this place.
タイトル
名前
Url
コメント