ComplexNum/
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
17
18pub mod Operations{   
19     pub fn ComAdd(c1:&(f64,f64), c2:&(f64,f64))->(f64,f64){
20        (c1.0 + c2.0, c1.1 + c2.1)
21    }
22
23    pub fn ComSubstract(c1:&(f64,f64), c2:&(f64,f64))->(f64,f64){
24        (c1.0 - c2.0, c1.1 - c2.1)
25    }
26
27    pub fn ComDistance(c1:&(f64,f64), c2:&(f64,f64))->f64{
28        let x= c1.0 - c2.0;
29        let y= c1.1 - c2.1;
30        let d= x*x + y*y;
31        d.sqrt()
32    }
33
34    pub fn ComMult(c1:&(f64,f64), c2:&(f64,f64))-> (f64, f64){
35
36        let x = (c1.0*c2.0)-(c1.1*c2.1);
37        let y = (c1.1*c2.0)+(c1.0*c2.1);
38        (x,y)
39    }
40
41    pub fn ComDiv(c1:&(f64,f64), c2:&(f64,f64))-> (f64, f64){
42        let x_num = (c1.0*c2.0)+(c1.1*c2.1);
43        let y_num = (c1.1*c2.0)-(c1.0*c2.1);
44        let den = c2.0*c2.0 + c2.1*c2.1;
45        (x_num/den,y_num/den)
46    }
47
48    pub fn ComAdd_Inv(c1: &(f64,f64))->(f64,f64){
49        (-c1.0,-c1.1)
50    }
51
52    pub fn ComMult_Inv(c1: &(f64,f64)) ->(f64,f64){
53
54        let x_num = c1.0;
55        let y_num = -c1.1;
56        let den = c1.0*c1.0 + c1.1*c1.1;
57        (x_num/den,y_num/den)
58
59    }
60
61    pub fn ComMod(c1: &(f64,f64))->f64{
62
63        let x = c1.0*c1.0;
64        let y = c1.1*c1.1;
65        let m = x+y;
66        m.sqrt()
67    }
68
69    pub fn CompCompare(c1: &(f64,f64),c2 :&(f64,f64)){
70
71        let d1 = ComMod(c1) ;
72        let d2 = ComMod(c2);
73        if d1>d2      {   println!("{:?} is greater than {:?}.",c1,c2);   }
74        else if d2>d1 {  println!("{:?} is greater than {:?}.",c2,c1);    }
75        else         {  println!("{:?} is equal to {:?}",c1,c2);     }
76    }
77
78}