๊ด€๋ฆฌ ๋ฉ”๋‰ด

IT’s Portfolio

[Rust] Start Rust (Day 7) - Rust Example Script 1 [Temperature Converter] ๋ณธ๋ฌธ

Development Study/Rust

[Rust] Start Rust (Day 7) - Rust Example Script 1 [Temperature Converter]

f1r3_r41n 2023. 1. 9. 16:10
728x90
๋ฐ˜์‘ํ˜•

๐Ÿฆ€ Rust Day 7

๐Ÿณ๏ธ Rust Example Script 1 - Temperature Converter

1๏ธโƒฃ Description

  • ํ™”์”จ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์„ญ์”จ๋กœ, ์„ญ์”จ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ™”์”จ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ

2๏ธโƒฃ how it works

  • ์„ญ์”จ๋Š” C, ํ™”์”จ๋Š” F ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. (๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š์Œ)
  • ์˜จ๋„๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.
  • ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณ€ํ™˜ ๋‹จ์œ„์— ๋งž์ถฐ ์ถœ๋ ฅํ•œ๋‹ค. (๊ฒฐ๊ณผ๊ฐ€ ์†Œ์ˆ˜์ ์„ ๊ฐ€์งˆ ๋•Œ ํ•ด๋‹น ๊ฐ’์—์„œ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ์ •์ˆ˜๋กœ ์ถœ๋ ฅ)

3๏ธโƒฃ Code

use std::io;

// ์˜จ๋„ ์ž…๋ ฅ ํ•จ์ˆ˜
fn input_temperature() -> f64 {
    loop{
        println!("์˜จ๋„๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”...");

        let mut tem = String::new();
        io::stdin().read_line(&mut tem)
            .expect("Unable to read input!");

        let tem: f64 = match tem.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("Error! You have to input number!");
                continue
            },
        };

        break tem;
    }
}

// ๊ณ„์‚ฐ ํ•จ์ˆ˜
fn calculation(u: &str, t: f64) -> f64 {
    let mut result: f64 = 0.0;

    if u=="C"||u=="c" {
        result = (t*1.8)+32.0;
    } else if u=="F"||u=="f" {
        result = (t-32.0)/1.8;
    }   

    return result;
}

// ๋ฉ”์ธ ํ•จ์ˆ˜
fn main() {
    loop{
        println!("๋‹จ์œ„ ์ž…๋ ฅ(C & F)...");

        // ์ž…๋ ฅํ•œ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”
        let mut unit = String::new();

        // ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ์œ„ํ•œ ์ฝ”๋“œ
        io::stdin().read_line(&mut unit)
            .expect("Unable to read input!");

        let unit: &str = unit.trim();

        if unit=="C"||unit=="c" {
            println!("Celsius");
            let result: f64 = calculation(unit, input_temperature()).round();
            println!("{}F", result);
            break;
        } else if unit=="F"||unit=="f" {
            println!("Fahrenheit");
            let result: f64 = calculation(unit, input_temperature()).round();
            println!("{}C", result);
            break;
        } else {
            println!("๋‹ค์‹œ ์ž…๋ ฅํ•˜์„ธ์š”...");
            continue;
        }
    }
}

4๏ธโƒฃ Result

  • C to F - 1

      ๋‹จ์œ„ ์ž…๋ ฅ(C & F)...
      C // input
      Celsius // output
      ์˜จ๋„๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”...
      100 // input
      212F // output
  • C to F - 2

      ๋‹จ์œ„ ์ž…๋ ฅ(C & F)...
      C // input
      Celsius // output
      ์˜จ๋„๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”...
      37 // input
      99F // output
    
      // 37C๋Š” 98.6F
  • F to C - 1

      ๋‹จ์œ„ ์ž…๋ ฅ(C & F)...
      F // input
      Fahrenheit // output
      ์˜จ๋„๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”...
      212 // input
      100C // output
  • F to C - 2

      ๋‹จ์œ„ ์ž…๋ ฅ(C & F)...
      F // input
      Fahrenheit // output
      ์˜จ๋„๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”...
      100 // input
      38C // output
    
      // 100F๋Š” 37.777...C

์œ ์ตํ•œ ๊ธ€ - Rust์˜ String๊ณผ str

728x90
๋ฐ˜์‘ํ˜•
Comments