Skip to content

Commit eee8655

Browse files
committedJan 23, 2021
Finished chapter 4
1 parent b39c8bf commit eee8655

File tree

10 files changed

+321
-0
lines changed

10 files changed

+321
-0
lines changed
 
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
// IntelliSense を使用して利用可能な属性を学べます。
3+
// 既存の属性の説明をホバーして表示します。
4+
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "lldb",
9+
"request": "launch",
10+
"name": "Debug executable 'problem_1'",
11+
"cargo": {
12+
"args": [
13+
"build",
14+
"--bin=problem_1",
15+
"--package=problem_1"
16+
],
17+
"filter": {
18+
"name": "problem_1",
19+
"kind": "bin"
20+
}
21+
},
22+
"args": [],
23+
"cwd": "${workspaceFolder}"
24+
},
25+
{
26+
"type": "lldb",
27+
"request": "launch",
28+
"name": "Debug unit tests in executable 'problem_1'",
29+
"cargo": {
30+
"args": [
31+
"test",
32+
"--no-run",
33+
"--bin=problem_1",
34+
"--package=problem_1"
35+
],
36+
"filter": {
37+
"name": "problem_1",
38+
"kind": "bin"
39+
}
40+
},
41+
"args": [],
42+
"cwd": "${workspaceFolder}"
43+
}
44+
]
45+
}

‎chapter_4/problem_1/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chapter_4/problem_1/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "problem_1"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <yohei.osawa.318.niko8@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

‎chapter_4/problem_1/src/main.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
struct Stack {
2+
stack_point: i32,
3+
stack: Vec<i32>,
4+
}
5+
6+
impl Stack{
7+
fn add(&mut self){
8+
let a = self.stack.pop().unwrap();
9+
let b = self.stack.pop().unwrap();
10+
self.stack.push(a+b);
11+
self.stack_point -= 1;
12+
}
13+
14+
fn sub(&mut self){
15+
let a = self.stack.pop().unwrap();
16+
let b = self.stack.pop().unwrap();
17+
self.stack.push(a - b);
18+
self.stack_point -= 1;
19+
}
20+
21+
fn mul(&mut self){
22+
let a = self.stack.pop().unwrap();
23+
let b = self.stack.pop().unwrap();
24+
self.stack.push(a * b);
25+
self.stack_point -= 1;
26+
}
27+
28+
fn push(&mut self, value: i32){
29+
self.stack.push(value);
30+
self.stack_point += 1;
31+
}
32+
}
33+
34+
fn main() {
35+
let contents: Vec<String> = input::read_numline();
36+
let mut stack:Stack = Stack{stack_point:0, stack: Vec::new()};
37+
for index in 0..contents.len() {
38+
let content = &contents[index];
39+
if content == "+" {
40+
stack.add();
41+
} else if content == "-" {
42+
stack.sub();
43+
} else if content == "*" {
44+
stack.mul();
45+
} else {
46+
let value = content.parse::<i32>().ok().unwrap();
47+
stack.push(value);
48+
}
49+
}
50+
51+
println!("result is {}", stack.stack[0]);
52+
}

‎chapter_4/problem_2/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chapter_4/problem_2/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "problem_2"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <yohei.osawa.318.niko8@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

‎chapter_4/problem_2/src/main.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#[derive(Debug)]
2+
struct Proc{
3+
name: String,
4+
proctime: i32,
5+
}
6+
7+
8+
fn main() {
9+
let info: Vec<i32> = input::read_numline();
10+
let mut procs: Vec<Proc> = Vec::new();
11+
let size = info[0];
12+
let time = info[1];
13+
for _ in 0..size {
14+
let mut line = String::new();
15+
std::io::stdin().read_line(&mut line).ok().unwrap();
16+
let mut iter = line.split_whitespace();
17+
let proc_name = match iter.next() {
18+
Some(name) => name,
19+
_ => "none",
20+
};
21+
let proc_time = match iter.next() {
22+
Some(time) => time.parse::<i32>().ok().unwrap(),
23+
_ => 0,
24+
};
25+
procs.push(Proc{name: proc_name.to_string(), proctime: proc_time});
26+
}
27+
28+
let mut total_time = 0;
29+
while procs.len() > 0 {
30+
let mut proc = procs.remove(0);
31+
if proc.proctime <= time {
32+
total_time += proc.proctime;
33+
println!("{} finished at {}", proc.name, total_time);
34+
} else {
35+
total_time += time;
36+
proc.proctime -= time;
37+
procs.push(proc);
38+
}
39+
}
40+
}
41+

‎chapter_4/problem_3/Cargo.lock

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎chapter_4/problem_3/Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "problem_3"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <yohei.osawa.318.niko8@gmail.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

‎chapter_4/problem_3/src/main.rs

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use std::rc::{Rc, Weak};
2+
use std::cell::RefCell;
3+
4+
#[derive(Debug)]
5+
struct Node<T: Clone> {
6+
value: T,
7+
prev: Option<Weak<RefCell<Node<T>>>>,
8+
next: Option<Rc<RefCell<Node<T>>>>,
9+
}
10+
11+
impl<T: Clone> Node<T> {
12+
fn new(value: T) -> Rc<RefCell<Self>> {
13+
Rc::new(RefCell::new(Node {
14+
value: value,
15+
prev: None,
16+
next: None,
17+
}))
18+
}
19+
}
20+
21+
#[derive(Debug)]
22+
struct LinkedList<T: Clone> {
23+
head: Option<Rc<RefCell<Node<T>>>>,
24+
tail: Option<Rc<RefCell<Node<T>>>>,
25+
size: usize,
26+
}
27+
28+
impl<T: Clone> LinkedList<T> {
29+
fn new() -> Self {
30+
LinkedList {
31+
head: None,
32+
tail: None,
33+
size: 0,
34+
}
35+
}
36+
37+
fn len(&self) -> usize {
38+
self.size
39+
}
40+
41+
fn append(&mut self, value: T) {
42+
let node = Node::new(value);
43+
match self.tail.take() {
44+
// first append
45+
None => {
46+
self.head = Some(Rc::clone(&node));
47+
}
48+
Some(oldtail) => {
49+
oldtail.borrow_mut().next = Some(Rc::clone(&node));
50+
node.borrow_mut().prev = Some(Rc::downgrade(&oldtail));
51+
}
52+
};
53+
self.size += 1;
54+
self.tail = Some(Rc::clone(&node));
55+
}
56+
57+
fn pop(&mut self) -> Option<T> {
58+
match self.tail.take(){
59+
Some(tail_node) => {
60+
if let Some(prev) = tail_node.borrow_mut().prev.take() {
61+
let prev = prev.upgrade().unwrap();
62+
prev.borrow_mut().next = None;
63+
self.tail = Some(prev);
64+
} else {
65+
self.head = None;
66+
}
67+
self.size -= 1;
68+
// must be exactly one strong reference
69+
let value = Rc::try_unwrap(tail_node).ok().unwrap().into_inner().value;
70+
Some(value)
71+
}
72+
None => None
73+
}
74+
}
75+
76+
fn get(&self, index: usize) -> Option<T>{
77+
let mut searching_index = 0;
78+
if self.size < index {
79+
return None
80+
}
81+
82+
let mut node = match self.head.as_ref() {
83+
Some(first_node) => {
84+
Rc::clone(first_node)
85+
}
86+
None => return None,
87+
};
88+
89+
while searching_index != index{
90+
// 一度tmp_nodeで受け取らないといけない どうすればいいのか?
91+
// let node = match node.borrow().next
92+
// とするとnodeが借用中でコンパイルできない
93+
let tmp_node = match node.borrow().next {
94+
Some(ref next_node) => Rc::clone(&next_node),
95+
None => return None,
96+
};
97+
// nodeをnode.nextにする
98+
node = tmp_node;
99+
searching_index += 1;
100+
}
101+
let value = node.borrow().value.clone();
102+
// Some(node.borrow().value.clone())とするとエラー
103+
// どうすればいいのか?
104+
Some(value)
105+
}
106+
}
107+
108+
#[derive(Clone, Debug)]
109+
struct Person{
110+
age: i32,
111+
name: String,
112+
}
113+
114+
fn main() {
115+
let mut linked_person = LinkedList::<Person>::new();
116+
let adult = Person{age: 30, name: "adult".to_string()};
117+
linked_person.append(adult);
118+
let child = Person{age: 10, name: "child".to_string()};
119+
linked_person.append(child);
120+
println!("linked list len is {}", linked_person.len());
121+
println!("2nd Person name is {}", linked_person.get(1).unwrap().name);
122+
println!("2nd Person name is {}", linked_person.pop().unwrap().name);
123+
println!("linked list len is {}", linked_person.len());
124+
println!("2nd Person name is {:?}", linked_person);
125+
}

0 commit comments

Comments
 (0)
Please sign in to comment.