#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'tk'

root = TkRoot.new { title "計算プログラム"  }
puts "計算内容を+-*/を使って入力してください。"
naiyou=gets.chomp
def numload(n,j)
  k=""
  while n[j]!="+" && n[j]!="-" && n[j]!="*" && n[j]!="/" && n[j]!=nil
    k+=n[j]
    j+=1
  end
  k
end
i=0
l="n"
while i<naiyou.length
  if l=="n"
    num1=numload(naiyou,i)
    i+=num1.length
    l="t"
  elsif l=="t" && (naiyou[i]=="*" || naiyou[i]=="/")
    l=naiyou[i]
    i+=1
  elsif l=="t"
    i+=1
    l="n"
  else
    num2=numload(naiyou,i)
    naiyou2=""
    i-=1
    m=0
    num3=num1.to_f
    num4=num2.to_f
    while m<naiyou.length
      if m<i-num1.length || m>i+num2.length
        naiyou2+=naiyou[m]
        m+=1
      elsif l=="*"
        num3*=num4
        naiyou2+=num3.to_s
        m+=num1.length+num2.length+1
      else
        num3/=num4
        naiyou2+=num3.to_s
        m+=num1.length+num2.length+1
      end
    end
    naiyou=naiyou2
    i-=num1.length
    num1=num3.to_s
    i+=num1.length
    l="t"
  end
end
i=0
l="n"
while i<naiyou.length
  if l=="n"
    num1=numload(naiyou,i)
    i+=num1.length
    l="t"
  elsif l=="t"
    l=naiyou[i]
    i+=1
  else
    num2=numload(naiyou,i)
    naiyou2=""
    i-=1
    m=0
    num3=num1.to_f
    num4=num2.to_f
    while m<naiyou.length
      if m>i+num2.length
        naiyou2+=naiyou[m]
        m+=1
      elsif l=="+"
        num3+=num4
        naiyou2+=num3.to_s
        m+=num1.length+num2.length+1
      else
        num3-=num4
        naiyou2+=num3.to_s
        m+=num1.length+num2.length+1
      end
    end
    naiyou=naiyou2
    i-=num1.length
    num1=num3.to_s
    i+=num1.length
    l="t"
  end
end
puts naiyou
TkLabel.new(root) {
  text naiyou
  font "Helvetica 50 bold')
  pack
}

Tk.mainloop
