0% found this document useful (0 votes)
50 views3 pages

Class Mangps

The document defines classes to represent rational numbers (fractions) and arrays of those rational numbers. The MANG_PS class initializes arrays of random fractions, searches arrays for a given fraction, sorts arrays, finds the maximum fraction, and prints arrays. The PHANSO class represents individual fractions with numerator and denominator fields and methods for operations like addition, comparison, and simplification. The main method creates an array of fractions, sorts it, prints it, and finds the maximum element.

Uploaded by

NHULEE
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views3 pages

Class Mangps

The document defines classes to represent rational numbers (fractions) and arrays of those rational numbers. The MANG_PS class initializes arrays of random fractions, searches arrays for a given fraction, sorts arrays, finds the maximum fraction, and prints arrays. The PHANSO class represents individual fractions with numerator and denominator fields and methods for operations like addition, comparison, and simplification. The main method creates an array of fractions, sorts it, prints it, and finds the maximum element.

Uploaded by

NHULEE
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

class MANG_PS { int dodai; PHANSO[] a; public MANG_PS(int n) { Random rand = new Random(); dodai = n; a = new PHANSO[20]; for

(int i = 0; i < dodai; i++) a[i] = new PHANSO(rand.Next(10) + 1, rand.Next(10) + 1); } public bool timKiem(PHANSO ps) { ps.rutgon(); for (int i = 0; i < dodai; i++) { a[i].rutgon(); if (ps.get_ts() == a[i].get_ts() && ps.get_ms() == a[i].get_ms()) return true; } return false; } public bool timKiem1(PHANSO ps) { for (int i = 0; i < dodai; i++) if (ps.get_ts() * a[i].get_ms() == ps.get_ms() * a[i].get_ts()) return true; return false; } public void sapxep() { for(int i = 0; i < dodai - 1; i++) for(int j = i+1; j < dodai; j++) if (a[i].LonHon(a[j])) { PHANSO temp = new PHANSO(); temp.set_ts(a[i].get_ts()); temp.set_ms(a[i].get_ms()); a[i].set_ts(a[j].get_ts()); a[i].set_ms(a[j].get_ms()); a[j].set_ts(temp.get_ts()); a[j].set_ms(temp.get_ms()); } } public PHANSO get_max() { PHANSO max = a[0]; for(int i = 1; i < dodai; i++) if (a[i].LonHon(max))

max = a[i]; return max; } public void InMangPS() { for (int i = 0; i < dodai; i++) Console.WriteLine("PS {0}: {1}/{2}", i, a[i].get_ts(), a[i].get_ms()); } }

public class PHANSO { int ts, ms; public PHANSO() { ts = 1; ms = 0; } public int get_ts() { return ts; } public void set_ts(int t) { ts = t; } public int get_ms() { return ms; } public void set_ms(int m) { ms = m; } public PHANSO(int t, int m) { ts = t; ms = m; } public void cong(PHANSO ps) { ts = ts * ps.ms + ms * ps.ts; ms = ms * ps.ms; } public bool LonHon(PHANSO ps) { if (ts * ps.ms > ms * ps.ts) return true; return false; } private int usc(int x, int y) {

while (x != y) { if (x > y) x = x - y; if (x < y) y = y - x; } return x; } public void rutgon() { int temp = usc(ts, ms); ts = ts / temp; ms = ms / temp; } }

class Program { static void Main(string[] args) { MANG_PS a = new MANG_PS(8); a.sapxep(); a.InMangPS(); PHANSO max = a.get_max(); Console.WriteLine("Max la: {0}/{1}", max.get_ts(), max.get_ms()); Console.Read(); } }

You might also like