ComplexNumbers/
lib.rs

1use std::io;
2pub fn readCom()->(f64,f64){
3
4    let mut x = String::new();
5    let mut y = String::new();
6
7    println!("Real Part : ");
8    io::stdin().read_line(&mut x).expect("invalid input");
9    println!("Imaginary Part : ");
10    io::stdin().read_line(&mut y).expect("invalid input");
11
12    let x :f64 = x.trim().parse().expect("invalid input");
13    let y :f64 = y.trim().parse().expect("invalid input");
14    (x,y)
15}
16
17pub mod Operations{   
18     pub fn ComAdd(c1:&(f64,f64), c2:&(f64,f64))->(f64,f64){
19        (c1.0 + c2.0, c1.1 + c2.1)
20    }
21
22    pub fn ComSubstract(c1:&(f64,f64), c2:&(f64,f64))->(f64,f64){
23        (c1.0 - c2.0, c1.1 - c2.1)
24    }
25
26    pub fn ComDistance(c1:&(f64,f64), c2:&(f64,f64))->f64{
27        let x= c1.0 - c2.0;
28        let y= c1.1 - c2.1;
29        let d= x*x + y*y;
30        d.sqrt()
31    }
32
33    pub fn ComMult(c1:&(f64,f64), c2:&(f64,f64))-> (f64, f64){
34
35        let x = (c1.0*c2.0)-(c1.1*c2.1);
36        let y = (c1.1*c2.0)+(c1.0*c2.1);
37        (x,y)
38    }
39
40    pub fn ComDiv(c1:&(f64,f64), c2:&(f64,f64))-> (f64, f64){
41        let x_num = (c1.0*c2.0)+(c1.1*c2.1);
42        let y_num = (c1.1*c2.0)-(c1.0*c2.1);
43        let den = c2.0*c2.0 + c2.1*c2.1;
44        (x_num/den,y_num/den)
45    }
46
47    pub fn ComAdd_Inv(c1: &(f64,f64))->(f64,f64){
48        (-c1.0,-c1.1)
49    }
50
51    pub fn ComMult_Inv(c1: &(f64,f64)) ->(f64,f64){
52
53        let x_num = c1.0;
54        let y_num = -c1.1;
55        let den = c1.0*c1.0 + c1.1*c1.1;
56        (x_num/den,y_num/den)
57
58    }
59
60    pub fn ComMod(c1: &(f64,f64))->f64{
61
62        let x = c1.0*c1.0;
63        let y = c1.1*c1.1;
64        let m = x+y;
65        m.sqrt()
66    }
67
68    pub fn CompCompare(c1: &(f64,f64),c2 :&(f64,f64)){
69
70        let d1 = ComMod(c1) ;
71        let d2 = ComMod(c2);
72        if d1>d2      {   println!("{:?} is greater than {:?}.",c1,c2);   }
73        else if d2>d1 {  println!("{:?} is greater than {:?}.",c2,c1);    }
74        else         {  println!("{:?} is equal to {:?}",c1,c2);     }
75    }
76
77}