still broken
This commit is contained in:
parent
c7075e57eb
commit
086b5b58e4
53
src/main.rs
53
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<u32>,
|
||||
right: Option<u32>,
|
||||
|
||||
struct Calc {
|
||||
left: String,
|
||||
right: String,
|
||||
operator: Option<Operators>,
|
||||
}
|
||||
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::<u32>().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::<u32>().unwrap();
|
||||
let right = self.right.parse::<u32>().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");
|
||||
|
|
Loading…
Reference in New Issue