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;
|
extern crate gtk;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::{Button, Window, WindowType};
|
use gtk::{Button, Window, WindowType};
|
||||||
|
|
||||||
enum Operators {
|
enum Operators {
|
||||||
PLUS,
|
PLUS,
|
||||||
MINUS,
|
MINUS,
|
||||||
DIVIDE,
|
DIVIDE,
|
||||||
MULTIPLY,
|
MULTIPLY,
|
||||||
}
|
}
|
||||||
struct calc {
|
|
||||||
left: Option<u32>,
|
struct Calc {
|
||||||
right: Option<u32>,
|
left: String,
|
||||||
|
right: String,
|
||||||
operator: Option<Operators>,
|
operator: Option<Operators>,
|
||||||
}
|
}
|
||||||
impl calc {
|
|
||||||
fn handle_number(&mut self, button: gtk::Button) {
|
impl Calc {
|
||||||
match self {
|
fn handle_number(&mut self, button: String) {
|
||||||
calc {
|
match self.operator {
|
||||||
left: l,
|
None => self.left.push_str(&button),
|
||||||
right: r,
|
Some(_) => self.right.push_str(&button),
|
||||||
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),
|
|
||||||
} => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn eval(&mut self) -> Self {
|
fn eval(&mut self) -> u32 {
|
||||||
calc {
|
let left = self.left.parse::<u32>().unwrap();
|
||||||
left: None,
|
let right = self.right.parse::<u32>().unwrap();
|
||||||
right: None,
|
return match self.operator {
|
||||||
operator: None,
|
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() {
|
fn main() {
|
||||||
if gtk::init().is_err() {
|
if gtk::init().is_err() {
|
||||||
println!("failed to init GTK");
|
println!("failed to init GTK");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let calc = Calc::new();
|
||||||
let window: Window = Window::new(WindowType::Toplevel);
|
let window: Window = Window::new(WindowType::Toplevel);
|
||||||
window.set_title("Rust calc");
|
window.set_title("Rust calc");
|
||||||
let container = gtk::Box::new(gtk::Orientation::Vertical, 1);
|
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 row1 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||||
let button1 = Button::new_with_label("1");
|
let button1 = Button::new_with_label("1");
|
||||||
row1.add(&button1);
|
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");
|
let button2 = Button::new_with_label("2");
|
||||||
row1.add(&button2);
|
row1.add(&button2);
|
||||||
let button3 = Button::new_with_label("3");
|
let button3 = Button::new_with_label("3");
|
||||||
|
|
Loading…
Reference in New Issue