trig_calculator_example/
trig_calculator_example.rs

1use std::io;
2use deterministic_trigonometry::DTrig;
3
4/* This is a basic command line trigonometry function calculator. It is designed as an example to show how to use the
5deterministic-trigonometry crate. */
6fn main() {
7    // This initializes the DTrig struct which writes the pre-baked trig tables into memory.
8    let d_trig = DTrig::initialize();
9
10    // This variable allows the main loop to exit.
11    let mut continue_running = true;
12
13    // This is the main loop
14    while continue_running == true {
15        println!("Welcome to the trigonometry calculator!");
16        println!("");
17
18        // This helper function takes a code for the trig function and the input value from the user.
19        let input_tuple = take_user_input();
20
21        // Just for visual spacing.
22        println!("");
23
24        // This stores a code for the trig function. 1: sine | 2: cosine | 3: tangent | 4: arcsine | 5: arccosine | 6: arctangent
25        let trig_function = input_tuple.0;
26
27        // This stores the input for the trig function.
28        let input_value = input_tuple.1;
29
30        // This converts the input value to a faction over 1000.
31        let fraction_tuple = ((input_value * 1000.0).round() as i32, 1000);
32
33        // This provides the result based on the trig function and input value.
34        match trig_function {
35            1 => {
36                println!("The answer is {}/{}", d_trig.sine(fraction_tuple).0, 1000);
37            }
38            2 => {
39                println!("The answer is {}/{}", d_trig.cosine(fraction_tuple).0, 1000);
40            }
41            3 => {
42                println!("The answer is {}/{}", d_trig.tangent(fraction_tuple).0, 1000);
43            }
44            4 => {
45                println!("The answer is {}/{}", d_trig.arcsine(fraction_tuple).0, 1000);
46            }
47            5 => {
48                println!("The answer is {}/{}", d_trig.arccosine(fraction_tuple).0, 1000);
49            }
50            6 => {
51                println!("The answer is {}/{}", d_trig.arctangent(fraction_tuple).0, 1000);
52            }
53            _ => {
54                println!("Input error. Please try again");
55            }
56        }
57
58        // This asks the user if they want to do another calculation.
59        continue_running = ask_continue_running();
60    }
61}
62
63// This is the helper function to take user input for the calculator.
64
65fn take_user_input() -> (i32, f32) {
66    // This holds the trig function code.
67    let mut trig_function: i32 = 0;
68
69    // This holds the input value.
70    let mut input_value: f32 = 0.0;
71
72    let mut input_valid = false;
73
74    // This takes and validates the trig function code.
75    while input_valid == false {
76        println!(
77            "What function do you want? 1: sine | 2: cosine | 3: tangent | 4: arcsine | 5: arccosine | 6: arctangent"
78        );
79        println!("");
80
81        let mut trig_function_string = String::new();
82        match io::stdin().read_line(&mut trig_function_string) {
83            Ok(_) => {}
84            Err(_) => {
85                println!("Input error. Try again.");
86                println!("");
87                continue;
88            }
89        }
90
91        trig_function = match trig_function_string.trim().parse() {
92            Ok(n) => n,
93            Err(_) => {
94                println!("Input error. Try again.");
95                println!("");
96                continue;
97            }
98        };
99
100        if trig_function >= 1 && trig_function <= 6 {
101            input_valid = true;
102        } else {
103            println!("Input error. Try again.");
104            println!("");
105            continue;
106        }
107    }
108
109    input_valid = false;
110
111    // This takes and validates the input value.
112    while input_valid == false {
113        println!("");
114        println!("Please enter the input value for the angle (in radians) or ratio of sides.");
115        println!("");
116
117        let mut input_value_string = String::new();
118        match io::stdin().read_line(&mut input_value_string) {
119            Ok(_) => {}
120            Err(_) => {
121                println!("Input error. Try again.");
122                println!("");
123                continue;
124            }
125        }
126
127        input_value = match input_value_string.trim().parse() {
128            Ok(n) => n,
129            Err(_) => {
130                println!("Input error. Try again.");
131                println!("");
132                continue;
133            }
134        };
135
136        input_valid = true;
137    }
138
139    return (trig_function, input_value);
140}
141
142// This checks to see if the user wishes to keep running the program.
143fn ask_continue_running() -> bool {
144    let mut clear_answer = false;
145
146    while clear_answer == false {
147        println!("");
148        println!("Do you want to do another calculation? (y/n)");
149
150        let mut answer_string = String::new();
151        match io::stdin().read_line(&mut answer_string) {
152            Ok(_) => {}
153            Err(_) => {
154                println!("Input error. Try again.");
155                println!("");
156                continue;
157            }
158        }
159
160        if answer_string.trim() == "y" {
161            println!("");
162            return true;
163        } else if answer_string.trim() == "n" {
164            clear_answer = true;
165            print!("");
166            print!("Okay! Have a nice day!");
167            print!("");
168
169        } else {
170            println!("");
171            println!("Sorry, I didn't understand that answer. Please try again.");
172        }
173    }
174
175    return false;
176}