"首次提交"

This commit is contained in:
2024-06-08 15:01:12 +08:00
parent 6e0f708d0a
commit 84349a2469
112 changed files with 3272 additions and 0 deletions

1
learn_test_8/.gitignore vendored Executable file
View File

@ -0,0 +1 @@
/target

8
learn_test_8/Cargo.toml Executable file
View File

@ -0,0 +1,8 @@
[package]
name = "learn_test_8"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

126
learn_test_8/src/main.rs Executable file
View File

@ -0,0 +1,126 @@
use std::{collections::HashMap, fmt::Debug};
fn main() {
println!("Hello, world!");
sta_array();
pig_latin();
company_empolyer_mgn()
}
fn sta_array () {
let numbers = vec![-6,2,2,4,5,6,6,100,8,20,10];
let mut avg = 0;
//统计平均値
for num in &numbers {
avg = (avg + num) / 2;
}
println!("the average of {:?} is {}", numbers, avg);
//统计出现次数最多的数字
let mut map = HashMap::new();
#[derive(Debug)]
struct MaxNumInfo{
num:i32,
cnt:i32,
}
let mut max_num = MaxNumInfo {
num:0,
cnt:0,
};
impl MaxNumInfo{
fn from(other:&MaxNumInfo) -> MaxNumInfo{
MaxNumInfo{
num: other.num,
cnt: other.cnt,
}
}
}
let mut max_numv: Vec<MaxNumInfo> = Vec::new();
for num in &numbers {
let cnt = map.entry(num).or_insert(0);
*cnt += 1;
if *cnt >= max_num.cnt {
if *cnt > max_num.cnt {
max_numv.clear();
}
max_num.cnt = *cnt;
max_num.num = *num;
max_numv.push(MaxNumInfo::from(&max_num));
}
}
println!("the max count in {:?} is {:?}", numbers, max_numv);
//统计中位数
let mut order = numbers.clone();
order.sort();
let size = order.len() / 2;
let num = order[size];
println!("median of {:?} is {:?}", numbers, num);
}
fn pig_latin () {
let vowel = vec!['a','e','i','o','u'];
let words = vec!["apple", "first"];
let mut new_words = Vec::new();
for word in &words {
for v in &vowel {
if let Some(first_char) = word.chars().next() {
if first_char == *v {
new_words.push(String::from(*word) + "-hay");
} else {
new_words.push(String::from(&(*word)[1..]) + "-" + &first_char.to_string() + "ay");
}
break;
}
}
}
println!("after process {:?} to {:?}", words, new_words);
}
fn company_empolyer_mgn() {
struct empolyer_mgn{
map:HashMap<String,Vec<String>>,
}
impl empolyer_mgn {
fn new() -> empolyer_mgn {
empolyer_mgn {
map:HashMap::new(),
}
}
fn add(&mut self, depart:String, name:String) {
let employers = self.map.entry(depart).or_insert( Vec::new());
employers.push(name);
}
fn get_all_emplyer(&mut self) {
print!("total empolyer is ");
for (key, value) in self.map.iter() {
print!("{:?} ", value);
}
println!("");
}
fn get_emplyer(&mut self, depart:String) {
match self.map.entry(depart) {
std::collections::hash_map::Entry::Occupied(entry) => {
println!("in {} empolyer is {:?}", entry.key(), entry.get());
},
std::collections::hash_map::Entry::Vacant(entry) => {
println!("in {} has no emplyer found", entry.key())
},
}
}
}
let mut mgn = empolyer_mgn::new();
mgn.add(String::from("项目部门"), String::from("Sally"));
mgn.add(String::from("销售部门"), String::from("Amir"));
mgn.get_all_emplyer();
mgn.get_emplyer(String::from("研发部门"));
mgn.get_emplyer(String::from("销售部门"));
mgn.get_emplyer(String::from("项目部门"));
}