用iced库写一个计算器
时间: 2024-05-11 22:17:30 浏览: 112
好的,以下是用 Rust 语言和 Iced 库实现的简单计算器:
```rust
use iced::{button, Button, Column, Element, Sandbox, Settings, Text};
pub struct Calculator {
first_operand: String,
second_operand: String,
operator: Operator,
result: String,
calculate_button_state: button::State,
}
#[derive(Debug, Clone, Copy)]
pub enum Operator {
Add,
Subtract,
Multiply,
Divide,
}
impl Default for Calculator {
fn default() -> Self {
Self {
first_operand: String::new(),
second_operand: String::new(),
operator: Operator::Add,
result: String::new(),
calculate_button_state: button::State::new(),
}
}
}
#[derive(Debug, Clone)]
pub enum Message {
FirstOperandChanged(String),
SecondOperandChanged(String),
OperatorChanged(Operator),
CalculateButtonPressed,
}
impl Sandbox for Calculator {
type Message = Message;
fn new() -> Self {
Self::default()
}
fn title(&self) -> String {
String::from("Calculator")
}
fn update(&mut self, message: Message) {
match message {
Message::FirstOperandChanged(value) => {
self.first_operand = value;
}
Message::SecondOperandChanged(value) => {
self.second_operand = value;
}
Message::OperatorChanged(operator) => {
self.operator = operator;
}
Message::CalculateButtonPressed => {
let first_operand = self.first_operand.parse::<f32>().unwrap_or_default();
let second_operand = self.second_operand.parse::<f32>().unwrap_or_default();
let result = match self.operator {
Operator::Add => first_operand + second_operand,
Operator::Subtract => first_operand - second_operand,
Operator::Multiply => first_operand * second_operand,
Operator::Divide => first_operand / second_operand,
};
self.result = result.to_string();
}
}
}
fn view(&mut self) -> Element<Message> {
let first_operand_input = Text::new("First operand:").size(20);
let second_operand_input = Text::new("Second operand:").size(20);
let calculate_button = Button::new(
&mut self.calculate_button_state,
Text::new("Calculate").size(20),
)
.on_press(Message::CalculateButtonPressed);
let result_text = Text::new(&self.result).size(20);
Column::new()
.push(first_operand_input)
.push(
Text::new(&self.first_operand)
.size(20)
.padding(10)
.width(iced::Length::Units(200))
.border_width(1)
.border_radius(5),
)
.push(second_operand_input)
.push(
Text::new(&self.second_operand)
.size(20)
.padding(10)
.width(iced::Length::Units(200))
.border_width(1)
.border_radius(5),
)
.push(
Column::new()
.push(
Button::new(
&mut button::State::new(),
Text::new("Add").size(20),
)
.on_press(Message::OperatorChanged(Operator::Add)),
)
.push(
Button::new(
&mut button::State::new(),
Text::new("Subtract").size(20),
)
.on_press(Message::OperatorChanged(Operator::Subtract)),
)
.push(
Button::new(
&mut button::State::new(),
Text::new("Multiply").size(20),
)
.on_press(Message::OperatorChanged(Operator::Multiply)),
)
.push(
Button::new(
&mut button::State::new(),
Text::new("Divide").size(20),
)
.on_press(Message::OperatorChanged(Operator::Divide)),
),
)
.push(calculate_button)
.push(result_text)
.spacing(20)
.padding(20)
.into()
}
}
fn main() -> iced::Result {
Calculator::run(Settings::default())
}
```
这个计算器允许用户输入两个操作数和选择一个操作符,然后计算结果。请在 Rust 环境中运行这个代码,然后使用键盘或鼠标输入操作数和选择操作符,最后按下“Calculate”按钮计算结果。
阅读全文