Last active
June 3, 2020 01:31
-
-
Save dhirabayashi/7e64aea40a1f7b6e265258df022f14c6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
use std::io::{stdout, Write}; | |
struct AddCalculator{} | |
struct SubtractCalculator{} | |
struct MultiplyCalculator{} | |
struct DivideCalculator{} | |
trait Calculator { | |
fn execute(&self, x: i32, y: i32) -> i32; | |
fn get_operator(&self) -> &str; | |
fn calc_surplus(&self, _x: i32, _y: i32) -> Option<i32> { | |
None | |
} | |
} | |
impl Calculator for AddCalculator { | |
fn execute(&self, x: i32, y: i32) -> i32 { | |
x + y | |
} | |
fn get_operator(&self) -> &str { | |
"+" | |
} | |
} | |
impl Calculator for SubtractCalculator { | |
fn execute(&self, x: i32, y: i32) -> i32 { | |
x - y | |
} | |
fn get_operator(&self) -> &str { | |
"-" | |
} | |
} | |
impl Calculator for MultiplyCalculator { | |
fn execute(&self, x: i32, y: i32) -> i32 { | |
x * y | |
} | |
fn get_operator(&self) -> &str { | |
"*" | |
} | |
} | |
impl Calculator for DivideCalculator { | |
fn execute(&self, x: i32, y: i32) -> i32 { | |
x / y | |
} | |
fn get_operator(&self) -> &str { | |
"/" | |
} | |
fn calc_surplus(&self, x: i32, y: i32) -> Option<i32> { | |
Some(x % y) | |
} | |
} | |
fn main() { | |
println!("プログラムを開始します。終了の際は999を入力して下さい。"); | |
loop { | |
print_with_flush("番号を入力して下さい。(1:加算 2:減算 3:乗算 4:除算 999:終了) > "); | |
let menu: &str = &read_line(); | |
let calc: &dyn Calculator = match menu { | |
"1" => { | |
println!("1:加算を行います。"); | |
&AddCalculator{} | |
} | |
"2" => { | |
println!("2:減算を行います。"); | |
&SubtractCalculator{} | |
} | |
"3" => { | |
println!("3:乗算を行います。"); | |
&MultiplyCalculator{} | |
} | |
"4" => { | |
println!("4:除算を行います。"); | |
&DivideCalculator{} | |
} | |
"999" => { | |
println!("お疲れ様でした。"); | |
break; | |
} | |
_ => { | |
println!("番号は1から4の間で選択して下さい。"); | |
continue; | |
} | |
}; | |
print_with_flush("一番目の数字を入力して下さい。 > "); | |
let x: i32 = read_line().trim().parse().unwrap(); | |
print_with_flush("二番目の数字を入力して下さい。 > "); | |
let y: i32 = read_line().trim().parse().unwrap(); | |
println!("{}{}{}", x, calc.get_operator(), y); | |
match calc.calc_surplus(x, y) { | |
Some(surplus) => println!("答えは[{}] 余り[{}]です。", &pretty_sprint_int(calc.execute(x, y)), &pretty_sprint_int(surplus)), | |
None => println!("答えは[{}]です。", &pretty_sprint_int(calc.execute(x, y))), | |
} | |
} | |
} | |
fn read_line() -> String { | |
let mut raw_input = String::new(); | |
stdin().read_line(&mut raw_input).unwrap(); | |
raw_input.trim().to_string() | |
} | |
fn print_with_flush(s: &str) { | |
print!("{}", s); | |
stdout().flush().unwrap(); | |
} | |
fn pretty_sprint_int(i: i32) -> String { | |
let mut s = String::new(); | |
let i_str = i.to_string(); | |
let a = i_str.chars().rev().enumerate(); | |
for (idx, val) in a { | |
if idx != 0 && idx % 3 == 0 { | |
s.insert(0, ','); | |
} | |
s.insert(0, val); | |
} | |
s | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
use std::io::{stdout, Write}; | |
use regex::Regex; | |
fn main() { | |
loop { | |
print!("文字を入力してください(999で終了) > "); | |
stdout().flush().unwrap(); | |
let mut raw_input = String::new(); | |
stdin().read_line(&mut raw_input).unwrap(); | |
let input: &str = &raw_input.trim(); | |
if input == "999" { | |
println!("お疲れ様でした。"); | |
return; | |
} | |
let v: Vec<char> = input.chars().collect(); | |
if v.len() > 1 { | |
println!("入力可能な文字は一文字です"); | |
continue; | |
} | |
let lower_alphabet_re = Regex::new(r"[a-z]").unwrap(); | |
let upper_alphabet_re = Regex::new(r"[A-Z]").unwrap(); | |
let hiragana_re = Regex::new(r"[あ-ん]").unwrap(); | |
let digit_re = Regex::new(r"\d+").unwrap(); | |
let chars: Vec<char> = input.chars().collect(); | |
if lower_alphabet_re.is_match(input) { | |
let upper_chars: Vec<char> = input.to_uppercase().chars().collect(); | |
println!("{}: {}, {}: {}", input, chars[0] as u32, input.to_uppercase(), upper_chars[0] as u32); | |
} else if upper_alphabet_re.is_match(input) { | |
let lower_chars: Vec<char> = input.to_lowercase().chars().collect(); | |
println!("{}: {}, {}: {}", input, chars[0] as u32, input.to_lowercase(), lower_chars[0] as u32); | |
} else if hiragana_re.is_match(input) || digit_re.is_match(input) { | |
println!("{}: {}", input, chars[0] as u32); | |
} else { | |
println!("アルファベット[a-z][A-Z]、ひらがな[あ-ん]、数字[0-9]のいずれかを入力して下さい"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
use std::io::{stdout, Write}; | |
fn main() { | |
println!("プログラムを開始します。終了の際は999を入力して下さい。"); | |
loop { | |
print!("番号を入力して下さい。(1~4) > "); | |
stdout().flush().unwrap(); | |
let mut raw_input = String::new(); | |
stdin().read_line(&mut raw_input).unwrap(); | |
let input: &str = &raw_input.trim(); | |
match input { | |
"1" => { println!("一が選択されました。") } | |
"2" => { println!("二が選択されました。") } | |
"3" => { println!("三が選択されました。") } | |
"4" => { println!("四が選択されました。") } | |
"999" => { | |
println!("お疲れ様でした。"); | |
return; | |
} | |
_ => println!("番号は1から4の間で選択して下さい。") | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
use std::io::{stdout, Write}; | |
fn main() { | |
let lower_alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']; | |
print_with_flush("何をしますか?(0:暗号化 1:複合化) --> "); | |
let input_operation: &str = &read_line(); | |
let operation = match input_operation { | |
"0" => Operation {name: "暗号化".to_string(), is_encription: true}, | |
"1" => Operation {name: "復号化".to_string(), is_encription: false}, | |
_ => { | |
println!("入力可能な値は「0」「1」です。"); | |
return; | |
}, | |
}; | |
print_with_flush("暗号化する文字列を入れて下さい --> "); | |
let input_target: &str = &read_line(); | |
print_with_flush("第1暗号キーはいくつ(1~9)? --> "); | |
let ipnut_key1: &str = &read_line(); | |
let key1: i32 = ipnut_key1.parse().unwrap(); | |
if key1 > 9 || key1 < 1 { | |
println!("入力可能な値は「1〜9」です。"); | |
return; | |
} | |
print_with_flush("第2暗号キーはいくつ(1~9)? --> "); | |
let ipnut_key2: &str = &read_line(); | |
let key2: i32 = ipnut_key2.parse().unwrap(); | |
if key2 > 9 || key2 < 1 { | |
println!("入力可能な値は「1〜9」です。"); | |
return; | |
} | |
println!("文字列を{}します", operation.name); | |
println!("{}文字は[{}]です", operation.name, operation.enc_dec(lower_alphabet, input_target, key1, key2)); | |
} | |
fn print_with_flush(s: &str) { | |
print!("{}", s); | |
stdout().flush().unwrap(); | |
} | |
fn read_line() -> String { | |
let mut s = String::new(); | |
stdin().read_line(&mut s).unwrap(); | |
s.trim().to_string() | |
} | |
struct Operation { | |
name: String, | |
is_encription: bool, | |
} | |
impl Operation { | |
fn enc_dec(&self, lower_alphabet: [char;26], target_str: &str, input_key1: i32, input_key2: i32) -> String { | |
let key1 = if self.is_encription { | |
input_key1 | |
} else { | |
-input_key1 | |
}; | |
let key2 = if self.is_encription { | |
input_key2 | |
} else { | |
-input_key2 | |
}; | |
let mut result = String::new(); | |
for (i, c) in target_str.chars().collect::<Vec<char>>().iter().enumerate() { | |
if c == &' ' { | |
result.push('*'); | |
continue; | |
} | |
let key = if (i+1) % 2 != 0 { key1 } else { key2 }; | |
result.push(shift_chars(lower_alphabet, *c, key)); | |
} | |
return result; | |
} | |
} | |
fn shift_chars(lower_alphabet: [char; 26], chr: char, key: i32) -> char { | |
let index = match lower_alphabet.iter().position(|&c| c == chr) { | |
Some(i) => i, | |
None => return chr, | |
}; | |
let pos = (index as i32) + key; | |
if pos >= 26 { | |
return lower_alphabet[(pos - 26) as usize]; | |
} else if pos < 0 { | |
return lower_alphabet[(pos + 26) as usize]; | |
} else { | |
return lower_alphabet[pos as usize]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::{stdout, Write}; | |
fn main() { | |
let ast = "*"; | |
let spc = " "; | |
for i in 0..10 { | |
for _ in 0..i { | |
print_with_flush(spc); | |
} | |
if i == 0 { | |
for _ in 0..20 { | |
print_with_flush(ast); | |
} | |
} else { | |
print_with_flush(ast); | |
print_with_flush(ast); | |
for _ in (i*2)..16 { | |
print_with_flush(spc); | |
} | |
if i != 9 { | |
print_with_flush(ast); | |
print_with_flush(ast); | |
} | |
} | |
println!(); | |
} | |
} | |
fn print_with_flush(s: &str) { | |
print!("{}", s); | |
stdout().flush().unwrap(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
println!("Hello World"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
let n: i32 = args[1].parse().unwrap(); | |
if (n % 100 == 0 && n % 400 != 0) || n % 4 != 0 { | |
println!("うるう年でない"); | |
} else { | |
println!("うるう年"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
for _ in 0..10 { | |
println!("あいうえお"); | |
} | |
let mut i = 1; | |
while i < 11 { | |
println!("かきくけこ"); | |
i += 1; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
use std::io::{stdout, Write}; | |
use rand::Rng; | |
fn main() { | |
println!("***** 数当てゲームを始めます *****"); | |
print_with_flush(" いくつまでの数に挑戦しますか-->"); | |
let input_limit: &str = &read_line(); | |
let limit: u32 = input_limit.parse().unwrap(); | |
println!("それではスタートします"); | |
println!("(1~{} までの数を当ててください)", limit); | |
let secret_number = rand::thread_rng().gen_range(1, limit); | |
let mut count = 0; | |
loop { | |
count += 1; | |
print_with_flush("さていくつ?-->"); | |
let input: &str = &read_line(); | |
let number: u32 = input.parse().unwrap(); | |
if number == secret_number { | |
println!("*** おめでとう! 当たりました ***"); | |
println!("{} 回目でした。", count); | |
if count == 1 { | |
println!("その強運をもっとましなことに使ったら?"); | |
} else if count < 6 { | |
println!("なかなかやりますね。"); | |
} else if count < 11 { | |
println!("微妙ですね。"); | |
} else { | |
println!("全然ダメですね。"); | |
} | |
break; | |
} | |
if number < secret_number { | |
println!("もっと大きいですよ"); | |
continue; | |
} | |
if number > secret_number { | |
println!("もっと小さいですよ"); | |
} | |
} | |
} | |
fn print_with_flush(s: &str) { | |
print!("{}", s); | |
stdout().flush().unwrap(); | |
} | |
fn read_line() -> String { | |
let mut s = String::new(); | |
stdin().read_line(&mut s).unwrap(); | |
s.trim().to_string() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
let n: i32 = args[1].parse().unwrap(); | |
for i in 1..(n+1) { | |
if is_prime(i) { | |
println!("{}", i); | |
} | |
} | |
} | |
fn is_prime(n: i32) -> bool { | |
if n < 0 || n == 1{ | |
return false; | |
} | |
if n == 2 { | |
return true; | |
} | |
if n % 2 == 0 { | |
return false; | |
} | |
for i in (3..(n+1)).step_by(2) { | |
if n % i == 0 && i != n { | |
return false; | |
} | |
} | |
true | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
use std::io::{stdout, Write}; | |
use std::i32; | |
fn main() { | |
loop { | |
my_print("入力する数字の基数を選択してください(2進数、10進数、16進数) > "); | |
let input_radix: &str = &input(); | |
let from_radix: u32 = match input_radix { | |
"2" | "10" | "16" => input_radix.parse().unwrap(), | |
_ => { | |
println!("入力可能な値は「2」「10」「16」です。"); | |
continue; | |
} | |
}; | |
my_print("変換したい数字を入力してください > "); | |
let input_from_num: &str = &input(); | |
let from_num: i32 = match i32::from_str_radix(input_from_num, from_radix) { | |
Ok(n) => n, | |
Err(_err) => { | |
println!("数字以外は入力できません。"); | |
continue; | |
}, | |
}; | |
my_print("変換先の基数を選択してください(2進数、8進数、10進数、16進数) > "); | |
let input_to_num: &str = &input(); | |
match input_to_num { | |
"2" => println!("{:0>b}", from_num), | |
"8" => println!("{:0>o}", from_num), | |
"10" => println!("{}", from_num), | |
"16" => println!("{:0>x}", from_num), | |
_ => { | |
println!("入力可能な値は「2」「8」「10」「16」です。"); | |
continue; | |
} | |
}; | |
my_print("続行する場合は「1」を入力してください(それ以外は終了) > "); | |
let input_continue: &str = &input(); | |
if input_continue != "1" { | |
break; | |
} | |
} | |
println!("お疲れ様でした。"); | |
} | |
fn my_print(s: &str) { | |
print!("{}", s); | |
stdout().flush().unwrap(); | |
} | |
fn input() -> String { | |
let mut s = String::new(); | |
stdin().read_line(&mut s).unwrap(); | |
s.trim().to_string() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
use std::io::{stdout, Write}; | |
fn main() { | |
println!("テストの点数を入力して下さい(999 で終了)"); | |
let mut examinees: Vec<Examinee> = Vec::new(); | |
let mut sum = 0; | |
for i in 1..11 { | |
let input_score: &str = &input(i); | |
if input_score == "999" { | |
break; | |
} | |
let score: u32 = input_score.parse().unwrap(); | |
examinees.push(Examinee{number: i as u32, score: score}); | |
sum += score; | |
} | |
let count = examinees.len(); | |
println!("受験者{}名", count); | |
if count == 0 { | |
return; | |
} | |
examinees.sort_by(|a, b| b.score.cmp(&a.score)); | |
let max = &examinees[0]; | |
let min = &examinees[count - 1]; | |
println!("最高点 {} 点(受験 No.{:03})", max.score, max.number); | |
println!("最低点 {} 点(受験 No.{:03})", min.score, min.number); | |
println!("平均点 {} 点", sum as f32 / count as f32); | |
println!("――――点数順位――――"); | |
for examinee in examinees { | |
examinee.print(); | |
} | |
println!(); | |
println!("―――――――――――― "); | |
} | |
fn input(i: i32) -> String { | |
print!("No.{:03}-->", i); | |
stdout().flush().unwrap(); | |
let mut s = String::new(); | |
stdin().read_line(&mut s).unwrap(); | |
s.trim().to_string() | |
} | |
struct Examinee { | |
number: u32, | |
score: u32, | |
} | |
impl Examinee { | |
fn print(&self) { | |
print!("{}(受験 No.{:03}) ", self.score, self.number); | |
stdout().flush().unwrap(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::stdin; | |
use std::io::{stdout, Write}; | |
fn main() { | |
print_with_flush("数値を3つ入力してください(1つ目) > "); | |
let a = read_float(); | |
print_with_flush("数値を3つ入力してください(2つ目) > "); | |
let b = read_float(); | |
print_with_flush("数値を3つ入力してください(3つ目) > "); | |
let c = read_float(); | |
if !(a < b+c && b < a+c && c < a+b) { | |
println!("これは三角形にはならない"); | |
return; | |
} | |
if a == b && b == c { | |
println!("正三角形"); | |
return; | |
} | |
let is_isosceles = a == b || b == c; | |
let is_right = a*a + b*b == c*c; | |
if is_isosceles && is_right { | |
println!("直角二等辺三角形"); | |
return; | |
} else if is_isosceles { | |
println!("二等辺三角形"); | |
return; | |
} else if is_right { | |
println!("直角三角形"); | |
return; | |
} | |
println!("普通の三角形"); | |
} | |
fn print_with_flush(s: &str) { | |
print!("{}", s); | |
stdout().flush().unwrap(); | |
} | |
fn read_float() -> f32 { | |
let mut s = String::new(); | |
stdin().read_line(&mut s).unwrap(); | |
let f: f32 = s.trim().parse().unwrap(); | |
f | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment