1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use crate as cm;
use cm::number;

enum Operation {
	Addition,
	Soustraction,
	Multiplication,
	Division,
}


pub trait Equation {
	fn make_addition(&mut self) -> f64;
	fn make_soustraction(&mut self) -> f64;
	fn make_multiplication(&mut self) -> f64;
	fn make_division(&mut self) -> f64;
	fn print(&self) -> Result<String, &str>;
	fn result(&self) -> Option<f64>;
}

//2 factors equation base
pub struct TwoFactor {
	x: f64,
	y: f64,
	result: Option<f64>,
	operation: Option<Operation>,
}

impl TwoFactor {
	pub fn new(random_buffer: &[i32; 100]) -> TwoFactor {
		let x_random_index = random_number::random_ranged(0..random_buffer.len());
		let y_random_index = random_number::random_ranged(0..random_buffer.len());

		TwoFactor {
			x: f64::from(*random_buffer.get(x_random_index).unwrap()),
			y: f64::from(*random_buffer.get(y_random_index).unwrap()),
			result: None,
			operation: None,
		}
	}

	//todo
	// fn add_factor(self, random_buffer: &mut [i32; 100]) -> ThreeFactor {
	// 	ThreeFactor {
	// 		x: self.x,
	// 		y: self.y,
	// 		z: f64::from(*random_buffer.get(cm::number::r_index()).unwrap()),
	// 		result: 0.0,
	// 	}
	// }
}

impl Equation for TwoFactor {

	fn make_addition(&mut self) -> f64 {
		let result = self.x + self.y;
		self.operation = Some(Operation::Addition);
		self.result = Some(result);

		result
	}

	fn make_soustraction(&mut self) -> f64 {
		let result = self.x - self.y;
		self.operation = Some(Operation::Soustraction);
		self.result = Some(result);

		result
	}

	fn make_multiplication(&mut self) -> f64 {
		let result = self.x * self.y;
		self.operation = Some(Operation::Multiplication);
		self.result = Some(result);

		result
	}

	fn make_division(&mut self) -> f64 {
		let result = number::change_float_part(self.x / self.y, 3);
		self.operation = Some(Operation::Division);
		self.result = Some(result);

		result
	}

	fn print(&self) -> Result<String, &str> {
		let operation = match &self.operation {
			Some(operation) => match operation {
				Operation::Addition => "+",
				Operation::Soustraction => "-",
				Operation::Multiplication => "*",
				Operation::Division => "/",
			}
			None => return Err("There is no operation on this equation"),
		};

		let expression = format!("{} {} {}", self.x, operation, self.y);

		Ok(expression)
	}

	fn result(&self) -> Option<f64> {
		self.result
	}
}





//todo 
pub struct _ThreeFactor { }