# 一.简单项目
# 1.摄氏度华氏度转换
use std::{cmp::Ordering, io};
fn main() {
println!("摄氏度转华氏度,华氏度转摄氏度"); // ! 表示宏
println!("请输入 1或2 数字,然后按回车键");
let mut guess = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut guess).expect("输入错误"); // expect 错误处理
let guess: u32 = guess.trim().parse().expect("输入数据有误"); // . 调用方法
println!("请输入度数数字,然后按回车键");
let mut num = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut num).expect("输入错误"); // expect 错误处理
let num: f32 = num.trim().parse().expect("输入数据有误"); // . 调用方法
let res = if guess == 1 {
(num - 32.0) / 1.8
} else {
num * 1.8 + 32.0
};
println!("你的数据是: {res}")
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 2.猜数字游戏
use std::{cmp::Ordering, io}; // use 使用模块 :: 模块下的 方法
use rand::Rng;
fn main() {
println!("100以内猜数字游戏"); // ! 表示宏
let num_f = rand::thread_rng().gen_range(1..=100);
loop {
println!("请输入 1-100 数字,然后按回车键");
let mut guess = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut guess).expect("输入错误");// expect 错误处理
let guess: u32 = guess.trim().parse().expect("输入数据有误"); // . 调用方法
match guess.cmp(&num_f) {
Ordering::Less => println!("你猜小了"),
Ordering::Equal => {
println!("你猜对了");
break;
}
Ordering::Greater => println!("你猜大了"),
}
println!("你猜的数据是: {guess}")
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 3.数组累加
use std::{cmp::Ordering, io};
fn main() {
println!("输入整数,用空格分隔");
let mut guess = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut guess).expect("输入错误"); // expect 错误处理
let mut arr: [u32; 5] = [0, 0, 0, 0, 0];
let v: Vec<&str> = guess.split(' ').collect();
let mut index = 0;
for num in v {
arr[index] = num.trim().parse().expect("输入数据有误");
index += 1;
if index == 5 {
break;
}
}
let mut res: u32 = 0;
for r in arr {
res += r;
}
println!("你的输入是: {:?},你的结果是: {}",arr,res)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 4.斐波那契数列
- loop
use std::{cmp::Ordering, io};
fn main() {
println!("输入整数");
let mut guess = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut guess).expect("输入错误"); // expect 错误处理
let num: u32 = guess.trim().parse().expect("输入数据有误");
let mut i: u32 = 0;
let mut res: u32 = 1;
let mut pre: u32 = 1;
let mut total: u32 = 1;
loop {
total = res + pre;
pre = res;
res = total;
i += 1;
if i == 0 || i == 1 {
total = 1;
break; // 用于跳出循环
}
if(i != num){
break;
}
}
println!("你的结果是: {}", res)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- while
use std::{cmp::Ordering, io};
fn main() {
println!("输入整数");
let mut guess = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut guess).expect("输入错误"); // expect 错误处理
let index: u32 = guess.trim().parse().expect("输入数据有误");
let mut i: u32 = 0;
let mut cur: u32 = 1;
let mut pre: u32 = 1;
let mut total: u32 = 1;
loop {
if index == 0 || index == 1 || index == 2 {
total = 1;
break; // 用于跳出循环
} else if (i != index - 1) {
total = cur + pre;
pre = cur;
cur = total;
i += 1;
} else {
break;
}
}
println!("你的结果是: {}", pre)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- match
use std::{cmp::Ordering, io};
fn main() {
println!("输入整数");
let mut guess = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut guess).expect("输入错误"); // expect 错误处理
let index: u32 = guess.trim().parse().expect("输入数据有误");
let mut i: u32 = 0;
let mut cur: u32 = 1;
let mut pre: u32 = 1;
let mut total: u32 = 1;
total = match index {
0 | 1 | 2 => 1,
_ => {
while i != index-1 {
total = cur + pre;
pre = cur;
cur = total;
i += 1;
}
pre
}
};
println!("你的结果是: {}", total)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 5.质数判断
use std::io;
fn main() {
println!("输入整数");
let mut guess = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut guess).expect("输入错误"); // expect 错误处理
let num: u32 = guess.trim().parse().expect("输入数据有误");
let mut res: u32 = 0;
for item in 2..num {
res = num % item;
if res == 0 {
println!("{num}不是质数");
break;
}
}
if num == 0 || num == 1 {
return println!("{num}不是质数");
}
if res != 0 {
println!("{num}是质数");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 6.冒泡排序
use rand::Rng;
fn main(){
println!("随机10个数");
let mut numbers = [0u32;10];
for i in 0..10 {
numbers[i] = rand::thread_rng().gen_range(1..10);
}
println!("ERRORE:{:?}",numbers);
let len = numbers.len();
for i in 0..len {
for j in 0..len -i -1{
if numbers[j]>numbers[j+1]{
numbers.swap(j, j+1)
}
}
}
println!("AFTER:{:?}",numbers)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 7.字符次数
- a-z 26 个字符,其余字符舍弃
- 如果输入的文本中有大写的 A-Z,计数到对应的小写字母上,也就是说大小写不敏感
- 最终输入出的时候一行一个结果,总共输出 26 行,例如 a => 1 这样的格式
use std::{collections::HashMap, fmt::format, io}; // use 使用模块 :: 模块下的 方法
fn main() {
println!("请输入字符,然后按回车键,计算字符次数");
let str = String::from("abcdefghigklmnopqrstuvwxyz");
let mut obj = HashMap::new();
let mut s = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut s).expect("输入错误"); // expect 错误处理
s = s.trim().to_lowercase();
for c in s.chars() {
if str.contains(c) {
if obj.contains_key(&c) {
let count = obj.get(&c).unwrap() + 1;
obj.insert(c, count);
} else {
obj.insert(c, 1);
}
}
}
for w in str.chars() {
if obj.contains_key(&w){
let target = format(format_args!("{} => {}", w, obj.get(&w).unwrap()));
println!("{target}")
}else {
let target = format(format_args!("{} => {}", w, 0));
println!("{target}")
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 8.最大公约数
- 用户输入 2 个 2^31 - 1 以内的正整数,计算其最大公约数(推荐使用辗转相除法)
use std::io;
fn main() {
println!("输入 2 个 2^31 - 1 以内的正整数用空格隔开然后回车,计算其最大公约数");
let mut s = String::new(); // :: 类型下的静态方法
io::stdin().read_line(&mut s).expect("输入错误"); // expect 错误处理
let arr: Vec<&str> = s.trim().split(" ").collect();
let num_a = arr[0].trim().parse().expect("数据有误");
let num_b = arr[1].trim().parse().expect("数据有误");
let mut max: i32;
let mut min: i32;
if num_a > num_b {
max = num_a;
min = num_b;
} else {
max = num_b;
min = num_a;
}
while max % min != 0 {
let res = max % min;
max = min;
min = res;
}
println!("最大公约数:{}", min)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 9.数组工具
编写一个数组工具,实现以下函数(具体是传入引用还是值,传入哪种引用,自行决定)
- 传入一个 i32 切片,返回其中最大的值
- 传入一个 i32 切片,返回其中最小的值
- 传入一个 i32 切片,返回其中最大的值的引用
- 传入一个 i32 切片,返回其中最小的值的引用。
- 传入一个 i32 切片,返回元素的平均值。
- 传入一个 i32 切片,将其中的每个元素都放大到原来的 2 倍
// 编写一个数组工具,实现以下函数(具体是传入引用还是值,传入哪种引用,自行决定)
// - 传入一个 i32 切片,返回其中最大的值
// - 传入一个 i32 切片,返回其中最小的值
// - 传入一个 i32 切片,返回其中最大的值的引用
// - 传入一个 i32 切片,返回其中最小的值的引用。
// - 传入一个 i32 切片,返回元素的平均值。
// - 传入一个 i32 切片,将其中的每个元素都放大到原来的 2 倍
fn main() {
struct Utils<'a> {
slice: &'a mut [i32],
}
impl<'a> Utils<'a> {
fn new(slice: &'a mut [i32]) -> Self {
Self { slice }
}
}
trait SliceUtils {
fn get_max(&self) -> i32;
fn get_min(&self) -> i32;
fn get_max_ref(&self) -> &i32;
fn get_min_ref(&self) -> &i32;
fn get_avg(&self) -> f64;
fn double_change(&mut self);
}
impl<'a> SliceUtils for Utils<'a> {
fn get_max(&self) -> i32 {
let mut max = &self.slice[0];
for v in self.slice.iter() {
if max < v {
max = v
}
}
*max
}
fn get_min(&self) -> i32 {
let mut min = &self.slice[0];
for v in self.slice.iter() {
if min > v {
min = v
}
}
*min
}
fn get_max_ref(&self) -> &i32 {
let mut max = &self.slice[0];
for v in self.slice.iter() {
if max < v {
max = v
}
}
max
}
fn get_min_ref(&self) -> &i32 {
let mut min = &self.slice[0];
for v in self.slice.iter() {
if min > v {
min = &v
}
}
min
}
fn get_avg(&self) -> f64 {
let sum = self.slice.iter().map(|&n| n as f64).sum::<f64>();
let len = self.slice.len() as f64;
let avg = sum / len;
avg
}
fn double_change(&mut self) {
let length = self.slice.len();
for i in 0..length {
self.slice[i] *= 2; // 将每个数乘以2
}
}
}
let mut slice = [2, 3, 4, 5, 6, 7, 8];
let mut slice_utils = Utils::new(&mut slice[..]);
println!("最大值 {}", slice_utils.get_max());
let mut slice2 =[1, 3, 4, 5, 6, 7, 8];
let mut slice_utils2 = Utils::new(&mut slice2[..]);
println!("最小值 {}", slice_utils2.get_min());
println!("最大值引用 {}", slice_utils.get_max_ref());
println!("最小值引用 {}", slice_utils.get_min_ref());
println!("平均值 {}", slice_utils.get_avg());
slice_utils.double_change();
println!("最大值 {}", slice_utils.get_max());
println!("最小值 {}", slice_utils.get_min());
println!("最大值引用 {}", slice_utils.get_max_ref());
println!("最小值引用 {}", slice_utils.get_min_ref());
println!("平均值 {}", slice_utils.get_avg());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92