add calculator
This commit is contained in:
parent
7da5c9392e
commit
f5eef42300
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1 @@
|
|||||||
/target
|
target
|
||||||
|
|||||||
7
calculator/Cargo.lock
generated
Normal file
7
calculator/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "calculator"
|
||||||
|
version = "0.1.0"
|
||||||
6
calculator/Cargo.toml
Normal file
6
calculator/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "calculator"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
111
calculator/src/main.rs
Normal file
111
calculator/src/main.rs
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
use std::io;
|
||||||
|
|
||||||
|
struct Calculator {
|
||||||
|
first_number: i32,
|
||||||
|
second_number: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Calculator {
|
||||||
|
/**
|
||||||
|
* 加法
|
||||||
|
*/
|
||||||
|
fn add(&self) -> i32 {
|
||||||
|
self.first_number + self.second_number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 减法
|
||||||
|
*/
|
||||||
|
fn sub(&self) -> i32 {
|
||||||
|
self.first_number - self.second_number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 乘法
|
||||||
|
*/
|
||||||
|
fn mul(&self) -> i32 {
|
||||||
|
self.first_number * self.second_number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 除法
|
||||||
|
*/
|
||||||
|
fn div(&self) -> i32 {
|
||||||
|
self.first_number / self.second_number
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut calculator = Calculator {
|
||||||
|
first_number: 0,
|
||||||
|
second_number: 0,
|
||||||
|
};
|
||||||
|
loop {
|
||||||
|
println!("Please Enter Your Algorithm");
|
||||||
|
println!("1. add");
|
||||||
|
println!("2. sub");
|
||||||
|
println!("3. mul");
|
||||||
|
println!("4. div");
|
||||||
|
println!("5. return");
|
||||||
|
let mut algorithm_input = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut algorithm_input)
|
||||||
|
.expect("Failed to a line");
|
||||||
|
let algorithm_trim = algorithm_input.trim();
|
||||||
|
let algorithm: i32;
|
||||||
|
if let Ok(parsed_value) = algorithm_trim.parse() {
|
||||||
|
algorithm = parsed_value
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if algorithm == 5 {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Please Enter First Number");
|
||||||
|
let mut first_number_input = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut first_number_input)
|
||||||
|
.expect("Failed to a line");
|
||||||
|
let first_number_trim = first_number_input.trim();
|
||||||
|
let first_number: i32;
|
||||||
|
if let Ok(parsed_value) = first_number_trim.parse() {
|
||||||
|
first_number = parsed_value
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Please Enter Second Number");
|
||||||
|
let mut second_number_input = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut second_number_input)
|
||||||
|
.expect("Failed to a line");
|
||||||
|
let second_number_trim = second_number_input.trim();
|
||||||
|
let second_number: i32;
|
||||||
|
if let Ok(parsed_value) = second_number_trim.parse() {
|
||||||
|
second_number = parsed_value
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
calculator.first_number = first_number;
|
||||||
|
calculator.second_number = second_number;
|
||||||
|
|
||||||
|
match algorithm {
|
||||||
|
1 => {
|
||||||
|
println!("add: {}", calculator.add())
|
||||||
|
}
|
||||||
|
2 => {
|
||||||
|
println!("sub: {}", calculator.sub())
|
||||||
|
}
|
||||||
|
3 => {
|
||||||
|
println!("mul: {}", calculator.mul())
|
||||||
|
}
|
||||||
|
4 => {
|
||||||
|
println!("div: {}", calculator.div())
|
||||||
|
}
|
||||||
|
_ => return,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
206
main.rs
206
main.rs
@ -1,162 +1,58 @@
|
|||||||
|
struct Car {
|
||||||
|
color: String,
|
||||||
|
transmission: Transmission,
|
||||||
|
convertible: bool,
|
||||||
|
age: (Age, u32),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
enum Age {
|
||||||
|
New,
|
||||||
|
Used,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug)]
|
||||||
|
enum Transmission {
|
||||||
|
Manual,
|
||||||
|
SemiAuto,
|
||||||
|
Automatic,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn car_factory(color: String, transmission: Transmission, convertible: bool, miles: u32) -> Car {
|
||||||
|
Car {
|
||||||
|
color,
|
||||||
|
transmission,
|
||||||
|
convertible,
|
||||||
|
age: car_quality(miles),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn car_quality(miles: u32) -> (Age, u32) {
|
||||||
|
if miles > 100 {
|
||||||
|
return (Age::Used, miles);
|
||||||
|
}
|
||||||
|
|
||||||
|
(Age::New, miles)
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let guess: u32 = "42".parse().expect("Not a number!");
|
println!("Hello, world");
|
||||||
println!("You guessed: {}", guess);
|
let colors = ["Blue", "Green", "Red", "Silver"];
|
||||||
let heart_eyed_cat = '😻';
|
let mut car = car_factory(String::from(colors[0]), Transmission::Automatic, false, 0);
|
||||||
println!("{}", heart_eyed_cat);
|
|
||||||
|
|
||||||
let cc = another_function(3, 'a');
|
|
||||||
println!("The value of cc is: {}", cc);
|
|
||||||
|
|
||||||
// Statements and Expressions
|
|
||||||
let x = {
|
|
||||||
let y = 5;
|
|
||||||
y + 1
|
|
||||||
};
|
|
||||||
println!("The value of x is: {}", x);
|
|
||||||
|
|
||||||
for number in 1..4 {
|
|
||||||
println!("{}!", number);
|
|
||||||
}
|
|
||||||
|
|
||||||
let result = fibonacci_function(3);
|
|
||||||
println!("The result is: {}", result);
|
|
||||||
// 相互转换摄氏与华氏温度
|
|
||||||
let celsius = 30.0;
|
|
||||||
let fahrenheit = celsius_to_fahrenheit(celsius);
|
|
||||||
println!("{} celsius is {} fahrenheit.", celsius, fahrenheit);
|
|
||||||
let fahrenheit = 86.0;
|
|
||||||
let celsius = fahrenheit_to_celsius(fahrenheit);
|
|
||||||
println!("{} fahrenheit is {} celsius.", fahrenheit, celsius);
|
|
||||||
|
|
||||||
let mut s = String::from("hello");
|
|
||||||
s.push_str(", world!");
|
|
||||||
println!("{}", s);
|
|
||||||
|
|
||||||
let s2 = s;
|
|
||||||
println!("{}", s2);
|
|
||||||
// println!("{}", s);
|
|
||||||
|
|
||||||
let t = String::from("hello");
|
|
||||||
takes_ownership(t);
|
|
||||||
// println!("{}", t);
|
|
||||||
let y = 5;
|
|
||||||
makes_copy(y);
|
|
||||||
println!("{}", y);
|
|
||||||
|
|
||||||
let s4 = String::from("hello");
|
|
||||||
let len = calculate_length(&s4);
|
|
||||||
println!("The length of '{}' is {}.", s4, len);
|
|
||||||
|
|
||||||
// let s5 = String::from("hello");
|
|
||||||
// change(&s5);
|
|
||||||
|
|
||||||
let mn = String::from("hello world");
|
|
||||||
let word = first_word(&mn);
|
|
||||||
// mn.clear();
|
|
||||||
println!("The first word is: {}", word);
|
|
||||||
|
|
||||||
// let width1 = 30;
|
|
||||||
// let height1 = 50;
|
|
||||||
|
|
||||||
let rect1 = Rectangle {
|
|
||||||
width: 30,
|
|
||||||
height: 50,
|
|
||||||
};
|
|
||||||
dbg!(&rect1);
|
|
||||||
// println!("rect1 is {:#?}", rect1);
|
|
||||||
// println!(
|
|
||||||
// "The area of the rectangle is {} square pixels.",
|
|
||||||
// area(&rect1)
|
|
||||||
// );
|
|
||||||
println!(
|
println!(
|
||||||
"The area of the rectangle is {} square pixels.",
|
"Car order 1: {:?}, Hard top = {}, {:?}, {}, {} miles",
|
||||||
rect1.area()
|
car.age.0, car.color, car.convertible, car.color, car.age.1
|
||||||
);
|
);
|
||||||
|
|
||||||
let rect2 = Rectangle::square(3);
|
car = car_factory(String::from(colors[1]), Transmission::SemiAuto, false, 1);
|
||||||
println!(
|
println!(
|
||||||
"The area of the rectangle is {} square pixels.",
|
"Car order 1: {:?}, Hard top = {}, {:?},{:?}, {}, {} miles",
|
||||||
rect2.area()
|
car.age.0, car.color, car.convertible, car.transmission, car.color, car.age.1
|
||||||
|
);
|
||||||
|
|
||||||
|
car = car_factory(String::from(colors[2]), Transmission::Manual, false, 101);
|
||||||
|
println!(
|
||||||
|
"Car order 1: {:?}, Hard top = {}, {:?}, {}, {} miles",
|
||||||
|
car.age.0, car.color, car.convertible, car.color, car.age.1
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn another_function(a: i32, char: char) -> i32 {
|
|
||||||
println!("Another function.");
|
|
||||||
println!("The value of a is: {}", a);
|
|
||||||
println!("The value of char is: {}", char);
|
|
||||||
5
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fibonacci_function(n: i32) -> i32 {
|
|
||||||
if n == 0 {
|
|
||||||
return 0;
|
|
||||||
} else if n == 1 {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return fibonacci_function(n - 1) + fibonacci_function(n - 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn celsius_to_fahrenheit(celsius: f64) -> f64 {
|
|
||||||
celsius * 1.8 + 32.0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fahrenheit_to_celsius(fahrenheit: f64) -> f64 {
|
|
||||||
(fahrenheit - 32.0) / 1.8
|
|
||||||
}
|
|
||||||
|
|
||||||
fn takes_ownership(some_string: String) {
|
|
||||||
// some_string 进入作用域
|
|
||||||
println!("{some_string}");
|
|
||||||
} // 这里,some_string 移出作用域并调用 `drop` 方法。
|
|
||||||
// 占用的内存被释放
|
|
||||||
|
|
||||||
fn makes_copy(some_integer: i32) {
|
|
||||||
// some_integer 进入作用域
|
|
||||||
println!("{some_integer}");
|
|
||||||
} // 这里,some_integer 移出作用域。没有特殊之处
|
|
||||||
|
|
||||||
fn calculate_length(s: &String) -> usize {
|
|
||||||
s.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
// fn change(some_string: &String) {
|
|
||||||
// some_string.push_str(", world");
|
|
||||||
// }
|
|
||||||
|
|
||||||
fn first_word(s: &String) -> &str {
|
|
||||||
let bytes = s.as_bytes();
|
|
||||||
|
|
||||||
for (i, &item) in bytes.iter().enumerate() {
|
|
||||||
if item == b' ' {
|
|
||||||
return &s[..i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
&s[..]
|
|
||||||
}
|
|
||||||
|
|
||||||
// fn area(width: u32, height: u32) -> u32 {
|
|
||||||
// width * height
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Rectangle {
|
|
||||||
width: u32,
|
|
||||||
height: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Rectangle {
|
|
||||||
fn area(&self) -> u32 {
|
|
||||||
self.width * self.height
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Rectangle {
|
|
||||||
fn square(size: u32) -> Rectangle {
|
|
||||||
Rectangle {
|
|
||||||
width: size,
|
|
||||||
height: size,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user