it maths! what more could u want?
This commit is contained in:
parent
086b5b58e4
commit
73a0d968c3
121
src/main.rs
121
src/main.rs
|
@ -1,6 +1,8 @@
|
||||||
extern crate gtk;
|
extern crate gtk;
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use gtk::{Button, Window, WindowType};
|
use gtk::{Button, Window, WindowType};
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
enum Operators {
|
enum Operators {
|
||||||
PLUS,
|
PLUS,
|
||||||
|
@ -13,6 +15,7 @@ struct Calc {
|
||||||
left: String,
|
left: String,
|
||||||
right: String,
|
right: String,
|
||||||
operator: Option<Operators>,
|
operator: Option<Operators>,
|
||||||
|
label: gtk::Label,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Calc {
|
impl Calc {
|
||||||
|
@ -20,21 +23,41 @@ impl Calc {
|
||||||
match self.operator {
|
match self.operator {
|
||||||
None => self.left.push_str(&button),
|
None => self.left.push_str(&button),
|
||||||
Some(_) => self.right.push_str(&button),
|
Some(_) => self.right.push_str(&button),
|
||||||
|
};
|
||||||
|
self.display();
|
||||||
}
|
}
|
||||||
}
|
fn eval(&mut self) {
|
||||||
fn eval(&mut self) -> u32 {
|
|
||||||
let left = self.left.parse::<u32>().unwrap();
|
let left = self.left.parse::<u32>().unwrap();
|
||||||
let right = self.right.parse::<u32>().unwrap();
|
let right = self.right.parse::<u32>().unwrap();
|
||||||
return match self.operator {
|
let ans = match self.operator {
|
||||||
Some(Operators::PLUS) => left + right,
|
Some(Operators::PLUS) => left + right,
|
||||||
Some(Operators::MINUS) => left - right,
|
Some(Operators::MINUS) => left - right,
|
||||||
Some(Operators::DIVIDE) => left / right,
|
Some(Operators::DIVIDE) => left / right,
|
||||||
Some(Operators::MULTIPLY) => left * right,
|
Some(Operators::MULTIPLY) => left * right,
|
||||||
None => panic!("oh noes")
|
None => panic!("oh noes"),
|
||||||
}
|
};
|
||||||
|
self.right = String::new();
|
||||||
|
self.operator = None;
|
||||||
|
self.left = ans.to_string();
|
||||||
|
self.display();
|
||||||
}
|
}
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Calc{left: String::new(), right: String::new(), operator: None}
|
Calc {
|
||||||
|
left: String::new(),
|
||||||
|
right: String::new(),
|
||||||
|
operator: None,
|
||||||
|
label: gtk::Label::new(Some("")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn display(&self) {
|
||||||
|
let text = match self.operator {
|
||||||
|
Some(Operators::PLUS) => self.left.clone() + "+" + &self.right,
|
||||||
|
Some(Operators::MINUS) => self.left.clone() + "-" + &self.right,
|
||||||
|
Some(Operators::DIVIDE) => self.left.clone() + "/" + &self.right,
|
||||||
|
Some(Operators::MULTIPLY) => self.left.clone() + "*" + &self.right,
|
||||||
|
None => self.left.clone(),
|
||||||
|
};
|
||||||
|
self.label.set_label(&text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,51 +66,133 @@ fn main() {
|
||||||
println!("failed to init GTK");
|
println!("failed to init GTK");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let calc = Calc::new();
|
let calc = Rc::new(RefCell::new(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);
|
||||||
|
// row 0
|
||||||
|
let cal0 = calc.clone();
|
||||||
|
container.add(&cal0.borrow().label);
|
||||||
// row 1
|
// row 1
|
||||||
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| calc.handle_number(but.get_label().unwrap()));
|
let cal1 = calc.clone();
|
||||||
|
button1.connect_clicked(move |but| {
|
||||||
|
cal1.borrow_mut().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 cal2 = calc.clone();
|
||||||
|
button2.connect_clicked(move |but| {
|
||||||
|
cal2.borrow_mut().handle_number(but.get_label().unwrap());
|
||||||
|
});
|
||||||
|
|
||||||
let button3 = Button::new_with_label("3");
|
let button3 = Button::new_with_label("3");
|
||||||
row1.add(&button3);
|
row1.add(&button3);
|
||||||
|
let cal3 = calc.clone();
|
||||||
|
button3.connect_clicked(move |but| {
|
||||||
|
cal3.borrow_mut().handle_number(but.get_label().unwrap());
|
||||||
|
});
|
||||||
|
|
||||||
// row 2
|
// row 2
|
||||||
let row2 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
let row2 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||||
let button4 = Button::new_with_label("4");
|
let button4 = Button::new_with_label("4");
|
||||||
row2.add(&button4);
|
row2.add(&button4);
|
||||||
|
let cal4 = calc.clone();
|
||||||
|
button4.connect_clicked(move |but| {
|
||||||
|
cal4.borrow_mut().handle_number(but.get_label().unwrap());
|
||||||
|
});
|
||||||
|
|
||||||
let button5 = Button::new_with_label("5");
|
let button5 = Button::new_with_label("5");
|
||||||
row2.add(&button5);
|
row2.add(&button5);
|
||||||
|
let cal5 = calc.clone();
|
||||||
|
button5.connect_clicked(move |but| {
|
||||||
|
cal5.borrow_mut().handle_number(but.get_label().unwrap());
|
||||||
|
});
|
||||||
|
|
||||||
let button6 = Button::new_with_label("6");
|
let button6 = Button::new_with_label("6");
|
||||||
row2.add(&button6);
|
row2.add(&button6);
|
||||||
|
let cal6 = calc.clone();
|
||||||
|
button6.connect_clicked(move |but| {
|
||||||
|
cal6.borrow_mut().handle_number(but.get_label().unwrap());
|
||||||
|
});
|
||||||
|
|
||||||
// row 3
|
// row 3
|
||||||
let row3 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
let row3 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||||
let button7 = Button::new_with_label("7");
|
let button7 = Button::new_with_label("7");
|
||||||
row3.add(&button7);
|
row3.add(&button7);
|
||||||
|
let cal7 = calc.clone();
|
||||||
|
button7.connect_clicked(move |but| {
|
||||||
|
cal7.borrow_mut().handle_number(but.get_label().unwrap());
|
||||||
|
});
|
||||||
|
|
||||||
let button8 = Button::new_with_label("8");
|
let button8 = Button::new_with_label("8");
|
||||||
row3.add(&button8);
|
row3.add(&button8);
|
||||||
|
let cal8 = calc.clone();
|
||||||
|
button8.connect_clicked(move |but| {
|
||||||
|
cal8.borrow_mut().handle_number(but.get_label().unwrap());
|
||||||
|
});
|
||||||
|
|
||||||
let button9 = Button::new_with_label("9");
|
let button9 = Button::new_with_label("9");
|
||||||
row3.add(&button9);
|
row3.add(&button9);
|
||||||
|
let cal9 = calc.clone();
|
||||||
|
button9.connect_clicked(move |but| {
|
||||||
|
cal9.borrow_mut().handle_number(but.get_label().unwrap());
|
||||||
|
});
|
||||||
|
|
||||||
// row 4
|
// row 4
|
||||||
let row4 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
let row4 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||||
let button0 = Button::new_with_label("0");
|
let button0 = Button::new_with_label("0");
|
||||||
row4.add(&button0);
|
row4.add(&button0);
|
||||||
|
let cal0 = calc.clone();
|
||||||
|
button0.connect_clicked(move |but| {
|
||||||
|
cal0.borrow_mut().handle_number(but.get_label().unwrap());
|
||||||
|
});
|
||||||
|
|
||||||
let button_sub = Button::new_with_label("-");
|
let button_sub = Button::new_with_label("-");
|
||||||
row4.add(&button_sub);
|
row4.add(&button_sub);
|
||||||
|
let cal_sub = calc.clone();
|
||||||
|
button_sub.connect_clicked(move |_| {
|
||||||
|
let mut cal_sub_borrowed = cal_sub.borrow_mut();
|
||||||
|
cal_sub_borrowed.operator = Some(Operators::MINUS);
|
||||||
|
cal_sub_borrowed.display();
|
||||||
|
});
|
||||||
|
|
||||||
let button_add = Button::new_with_label("+");
|
let button_add = Button::new_with_label("+");
|
||||||
row4.add(&button_add);
|
row4.add(&button_add);
|
||||||
|
let cal_add = calc.clone();
|
||||||
|
button_add.connect_clicked(move |_| {
|
||||||
|
let mut cal_add_borrowed = cal_add.borrow_mut();
|
||||||
|
cal_add_borrowed.operator = Some(Operators::PLUS);
|
||||||
|
cal_add_borrowed.display();
|
||||||
|
});
|
||||||
// row 5
|
// row 5
|
||||||
let row5 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
let row5 = gtk::Box::new(gtk::Orientation::Horizontal, 1);
|
||||||
let button_mult = Button::new_with_label("*");
|
let button_mult = Button::new_with_label("*");
|
||||||
row5.add(&button_mult);
|
row5.add(&button_mult);
|
||||||
|
let cal_mult = calc.clone();
|
||||||
|
button_mult.connect_clicked(move |_| {
|
||||||
|
let mut cal_mult_borrowed = cal_mult.borrow_mut();
|
||||||
|
cal_mult_borrowed.operator = Some(Operators::MULTIPLY);
|
||||||
|
cal_mult_borrowed.display();
|
||||||
|
});
|
||||||
let button_div = Button::new_with_label("/");
|
let button_div = Button::new_with_label("/");
|
||||||
row5.add(&button_div);
|
row5.add(&button_div);
|
||||||
|
let cal_div = calc.clone();
|
||||||
|
button_div.connect_clicked(move |_| {
|
||||||
|
let mut cal_div_borrowed = cal_div.borrow_mut();
|
||||||
|
cal_div_borrowed.operator = Some(Operators::DIVIDE);
|
||||||
|
cal_div_borrowed.display();
|
||||||
|
});
|
||||||
let button_equals = Button::new_with_label("=");
|
let button_equals = Button::new_with_label("=");
|
||||||
row5.add(&button_equals);
|
row5.add(&button_equals);
|
||||||
|
let cal_eq = calc.clone();
|
||||||
|
button_equals.connect_clicked(move |_| {
|
||||||
|
let mut cal_eq_borrowed = cal_eq.borrow_mut();
|
||||||
|
cal_eq_borrowed.eval();
|
||||||
|
});
|
||||||
// add rows to window
|
// add rows to window
|
||||||
container.add(&row1);
|
container.add(&row1);
|
||||||
container.add(&row2);
|
container.add(&row2);
|
||||||
|
|
Loading…
Reference in New Issue