Advertisement
tsounakis

rpncalc

Nov 11th, 2020
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 4.49 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "strconv"
  5.     "strings"
  6.  
  7.     "fyne.io/fyne"
  8.     "fyne.io/fyne/app"
  9.     "fyne.io/fyne/layout"
  10.     "fyne.io/fyne/widget"
  11. )
  12.  
  13. type stack []int
  14.  
  15. func (s *stack) Push(str int) {
  16.     *s = append(*s, str)
  17. }
  18.  
  19. func (s *stack) Pop() int {
  20.     if s.IsEmpty() {
  21.         return 0
  22.     } else {
  23.         index := len(*s) - 1
  24.         element := (*s)[index]
  25.         *s = (*s)[:index]    
  26.         return element
  27.     }
  28. }
  29.  
  30. func (s *stack) IsEmpty() bool {
  31.     return len(*s) == 0
  32. }
  33.  
  34. type calc struct {
  35.     equation string
  36.     input    *widget.Entry
  37.     output   *widget.Entry
  38.     window   fyne.Window
  39. }
  40.  
  41. func (c *calc) loadGUI(app fyne.App, s stack) {
  42.     var selectedOutput []string
  43.     var selectedInput []string
  44.     c.window = app.NewWindow("Calc")
  45.     c.window.SetFixedSize(true)
  46.  
  47.     c.input = widget.NewEntry()
  48.     c.output = widget.NewEntry()
  49.     c.output.Disable()
  50.     c.output.SetText("Output:")
  51.  
  52.     c.window.SetContent(fyne.NewContainerWithLayout(layout.NewGridLayout(1),
  53.         c.input,
  54.         c.output,
  55.         fyne.NewContainerWithLayout(layout.NewGridLayout(4),
  56.             widget.NewButton("1", func() {
  57.                 selectedInput = []string{c.input.Text, "1"}
  58.                 c.input.SetText(strings.Join(selectedInput, ""))
  59.             }),
  60.             widget.NewButton("2", func() {
  61.                 selectedInput = []string{c.input.Text, "2"}
  62.                 c.input.SetText(strings.Join(selectedInput, ""))
  63.             }),
  64.             widget.NewButton("3", func() {
  65.                 selectedInput = []string{c.input.Text, "3"}
  66.                 c.input.SetText(strings.Join(selectedInput, ""))
  67.             }),
  68.             widget.NewButton("+", func() {
  69.                 selectedInput = []string{c.input.Text, "+"}
  70.                 c.input.SetText(strings.Join(selectedInput, ""))
  71.             })),
  72.  
  73.         fyne.NewContainerWithLayout(layout.NewGridLayout(4),
  74.             widget.NewButton("4", func() {
  75.                 selectedInput = []string{c.input.Text, "4"}
  76.                 c.input.SetText(strings.Join(selectedInput, ""))
  77.             }),
  78.             widget.NewButton("5", func() {
  79.                 selectedInput = []string{c.input.Text, "5"}
  80.                 c.input.SetText(strings.Join(selectedInput, ""))
  81.             }),
  82.             widget.NewButton("6", func() {
  83.                 selectedInput = []string{c.input.Text, "6"}
  84.                 c.input.SetText(strings.Join(selectedInput, ""))
  85.             }),
  86.             widget.NewButton("-", func() {
  87.                 selectedInput = []string{c.input.Text, "-"}
  88.                 c.input.SetText(strings.Join(selectedInput, ""))
  89.             })),
  90.  
  91.         fyne.NewContainerWithLayout(layout.NewGridLayout(4),
  92.             widget.NewButton("7", func() {
  93.                 selectedInput = []string{c.input.Text, "7"}
  94.                 c.input.SetText(strings.Join(selectedInput, ""))
  95.             }),
  96.             widget.NewButton("8", func() {
  97.                 selectedInput = []string{c.input.Text, "8"}
  98.                 c.input.SetText(strings.Join(selectedInput, ""))
  99.             }),
  100.             widget.NewButton("9", func() {
  101.                 selectedInput = []string{c.input.Text, "9"}
  102.                 c.input.SetText(strings.Join(selectedInput, ""))
  103.             }),
  104.             widget.NewButton("/", func() {
  105.                 selectedInput = []string{c.input.Text, "/"}
  106.                 c.input.SetText(strings.Join(selectedInput, ""))
  107.             })),
  108.         fyne.NewContainerWithLayout(layout.NewGridLayout(4),
  109.             widget.NewButton("*", func() {
  110.                 selectedInput = []string{c.input.Text, "*"}
  111.                 c.input.SetText(strings.Join(selectedInput, ""))
  112.             }),
  113.             widget.NewButton(" ", func() {
  114.                 selectedInput = []string{c.input.Text, " "}
  115.                 c.input.SetText(strings.Join(selectedInput, ""))
  116.             }),
  117.             widget.NewButton("C", func() {
  118.                 c.input.SetText("")
  119.             }),
  120.             widget.NewButton("=", func() {
  121.                 c.equation = c.input.Text
  122.                 selectedOutput = []string{"Output:", c.calculate(c.equation, s)}
  123.                 c.output.SetText(strings.Join(selectedOutput, " "))
  124.             }))))
  125.  
  126.     c.window.Resize(fyne.NewSize(300, 400))
  127.     c.window.ShowAndRun()
  128. }
  129.  
  130. func (c *calc) calculate(equation string, s stack) string {
  131.     e := strings.Fields(c.equation)
  132.     for iterations, num := range e {
  133.         if iterations == len(e) {
  134.             break
  135.         }
  136.         if IsLetter(num) {
  137.             switch num {
  138.             case "+":
  139.                 p1 := s.Pop()
  140.                 p2 := s.Pop()
  141.                 s.Push(p1 + p2)
  142.             case "-":
  143.                 p1 := s.Pop()
  144.                 p2 := s.Pop()
  145.                 s.Push(p2 - p1)
  146.             case "*":
  147.                 p1 := s.Pop()
  148.                 p2 := s.Pop()
  149.                 s.Push(p1 * p2)
  150.             case "/":
  151.                 p1 := s.Pop()
  152.                 p2 := s.Pop()
  153.                 s.Push(p2 / p1)
  154.             default:
  155.                 c.output.SetText("Output: Error!")
  156.             }
  157.         } else {
  158.             n2, _ := strconv.Atoi(num)
  159.             s.Push(n2)
  160.         }
  161.         //log.Println("stack is", s)
  162.     }
  163.     result := s.Pop()
  164.     resultStr := strconv.Itoa(result)
  165.     return resultStr
  166.  
  167. }
  168.  
  169. func IsLetter(v string) bool {
  170.     if _, err := strconv.Atoi(v); err == nil {
  171.         return false
  172.     } else {
  173.         return true
  174.     }
  175. }
  176.  
  177. func newCalculator() *calc {
  178.     c := &calc{}
  179.     return c
  180. }
  181.  
  182. func main() {
  183.     var s stack
  184.     c := newCalculator()
  185.     c.loadGUI(app.New(), s)
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement