From 086b5b58e46480f2eca31d8202a45b6340f419e6 Mon Sep 17 00:00:00 2001 From: Raatty Date: Sun, 2 Dec 2018 15:38:03 +1300 Subject: [PATCH] still broken --- src/main.rs | 53 ++++++++++++++++++++++++++--------------------------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index e1b07fd..e019462 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,50 +1,49 @@ extern crate gtk; use gtk::prelude::*; use gtk::{Button, Window, WindowType}; + enum Operators { PLUS, MINUS, DIVIDE, MULTIPLY, } -struct calc { - left: Option, - right: Option, + +struct Calc { + left: String, + right: String, operator: Option, } -impl calc { - fn handle_number(&mut self, button: gtk::Button) { - match self { - calc { - left: l, - right: r, - operator: None, - } => match l { - Some(n) => {let s = n.to_string() + &button.get_label().unwrap(); - self.left = Some(s.parse::().unwrap()); - () - } - }, - calc { - left: Some(l), - right: r, - operator: Some(o), - } => {} + +impl Calc { + fn handle_number(&mut self, button: String) { + match self.operator { + None => self.left.push_str(&button), + Some(_) => self.right.push_str(&button), } } - fn eval(&mut self) -> Self { - calc { - left: None, - right: None, - operator: None, + fn eval(&mut self) -> u32 { + let left = self.left.parse::().unwrap(); + let right = self.right.parse::().unwrap(); + return match self.operator { + Some(Operators::PLUS) => left + right, + Some(Operators::MINUS) => left - right, + Some(Operators::DIVIDE) => left / right, + Some(Operators::MULTIPLY) => left * right, + None => panic!("oh noes") } } + fn new() -> Self { + Calc{left: String::new(), right: String::new(), operator: None} + } } + fn main() { if gtk::init().is_err() { println!("failed to init GTK"); return; } + let calc = Calc::new(); let window: Window = Window::new(WindowType::Toplevel); window.set_title("Rust calc"); let container = gtk::Box::new(gtk::Orientation::Vertical, 1); @@ -52,7 +51,7 @@ fn main() { let row1 = gtk::Box::new(gtk::Orientation::Horizontal, 1); let button1 = Button::new_with_label("1"); row1.add(&button1); - button1.connect_clicked(|but| println!("{}", but.get_label().unwrap())); + button1.connect_clicked(|but| calc.handle_number(but.get_label().unwrap())); let button2 = Button::new_with_label("2"); row1.add(&button2); let button3 = Button::new_with_label("3");