0% found this document useful (0 votes)
49 views

CSharp Rational Assignment

The document describes a C# program that defines a Rational class to represent rational numbers. The Rational class contains methods to initialize rational numbers, reduce them to lowest terms, perform arithmetic operations like addition and multiplication, and print rational numbers to the console. The Main method demonstrates how to use the Rational class by initializing some rational numbers, performing operations on them, and printing the results.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

CSharp Rational Assignment

The document describes a C# program that defines a Rational class to represent rational numbers. The Rational class contains methods to initialize rational numbers, reduce them to lowest terms, perform arithmetic operations like addition and multiplication, and print rational numbers to the console. The Main method demonstrates how to use the Rational class by initializing some rational numbers, performing operations on them, and printing the results.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Midterm Assignment Date: 8/16/2011, Mon C# Name: Tangpus, Mark Vincent B.

5:30pm, M-W-F Year&Section: BSIT 4B Ortega


using System; using System.Collections.Generic; using System.Text; namespace RationalNumbers { class Rational { private int numerator; private int denominator; //constructors public Rational() { numerator = 1; denominator = 1; } public Rational(int a, int b) { numerator = a; denominator = b; ReduceForm(); } //getters public int GetNumerator() { return numerator; } public int GetDenominator() { return denominator; } //setters public void SetNumerator(int a) { numerator = a; } public void SetDenominator(int b) { denominator = b; } //other methods public int ComputeGCF() { int x=numerator,y=denominator; if (numerator < 0) x = numerator * -1; if (denominator < 0) y = denominator * -1; int[] a = new int[x]; int[] b = new int[y]; int i, ii, j, jj; int counter1 = 0, counter2 = 0;

Subject: Schedule: 4:30-

IT-413

Instructor: Mr. Eric

Page 1 of 5

Midterm Assignment Date: 8/16/2011, Mon C# Name: Tangpus, Mark Vincent B. 5:30pm, M-W-F Year&Section: BSIT 4B Ortega
int gcf = 1; Boolean found = false; //get factors of numerator for (i = 1; i <= a.Length; i++) { if (numerator % i == 0) a[counter1++] = i; } //get factors of denominator for (ii = 1; ii <= b.Length; ii++) { if (denominator % ii == 0) b[counter2++] = ii; } //get Greatest Common Factor for (j = counter1 - 1; j >= 0; j--) { for (jj = counter2 - 1; jj >= 0; jj--) { if (a[j] == b[jj]) { gcf = a[j]; found = true; break; } } if (found) break; } return gcf;

Subject: Schedule: 4:30-

IT-413

Instructor: Mr. Eric

} public void ReduceForm() { int gcf = ComputeGCF(); numerator = numerator / gcf; denominator = denominator / gcf; } public void Print() { Console.WriteLine("{0}/{1}", numerator, denominator); } public void Print(int d)//1-4 decimal places { double x,y,f; x = numerator; y = denominator; f = x / y; switch (d) { case 1: Console.WriteLine("{0:f1}", f); break;

Page 2 of 5

Midterm Assignment Date: 8/16/2011, Mon C# Name: Tangpus, Mark Vincent B. 5:30pm, M-W-F Year&Section: BSIT 4B Ortega
case 2: Console.WriteLine("{0:f2}", f); break; case 3: Console.WriteLine("{0:f3}", f); break; case 4: Console.WriteLine("{0:f4}", f); break; default: break;

Subject: Schedule: 4:30-

IT-413

Instructor: Mr. Eric

} } public Rational Add(Rational addend) { return new Rational(((numerator * addend.denominator)+ (denominator * addend.numerator)),denominator * addend.denominator); } public Rational Subtract(Rational subtrahend) { int lcd; int a=1, b=1, x=1, y=1; if (denominator > subtrahend.denominator) { lcd = denominator / subtrahend.denominator; x = subtrahend.numerator * lcd; y = subtrahend.denominator * lcd; } else { lcd = subtrahend.denominator / denominator; a = numerator * lcd; b = denominator * lcd; } return new Rational(a-x, b); } public Rational Multiply(Rational factor) { return new Rational(numerator * factor.numerator, denominator * factor.denominator); } public Rational Divide(Rational divisor) { return new Rational(numerator * divisor.denominator, denominator * divisor.numerator); } } }

Page 3 of 5

Midterm Assignment Date: 8/16/2011, Mon C# Name: Tangpus, Mark Vincent B. 5:30pm, M-W-F Year&Section: BSIT 4B Ortega

Subject: Schedule: 4:30-

IT-413

Instructor: Mr. Eric

using System; using System.Collections.Generic; using System.Text; namespace RationalNumbers { class Program { static void Main(string[] args) { Rational a = new Rational(); Console.Write("a.print() = "); a.Print(); Console.Write("Enter numerator for a: "); a.SetNumerator(int.Parse(Console.ReadLine())); Console.Write("Enter denominator for a: "); a.SetDenominator(int.Parse(Console.ReadLine())); Console.Write("a.print() = "); a.Print(); Console.WriteLine("a.ComputeGCF() = {0}", a.ComputeGCF()); a.ReduceForm(); Console.Write("a.ReduceForm() = "); a.Print(); Console.Write("a.Print(1) = "); a.Print(1); Console.Write("a.Print(2) = "); a.Print(2); Console.Write("a.Print(3) = "); a.Print(3); Console.Write("a.Print(4) = "); a.Print(4); Rational b = new Rational(); Console.Write("Enter numerator for b: "); b.SetNumerator(int.Parse(Console.ReadLine())); Console.Write("Enter denominator for b: "); b.SetDenominator(int.Parse(Console.ReadLine())); Console.Write("b.print() = "); b.Print(); Rational c; c = a.Add(b); Console.Write("c = a.Add(b) \t\t-> {0}/{1} + {2}/{3} = ", a.GetNumerator(), a.GetDenominator(), b.GetNumerator(), b.GetDenominator()); c.Print(); Rational d; d = a.Subtract(b); Console.Write("c = a.Subtract(b) \t-> {0}/{1} - {2}/{3} = ", a.GetNumerator(), a.GetDenominator(), b.GetNumerator(), b.GetDenominator());

Page 4 of 5

Midterm Assignment Date: 8/16/2011, Mon C# Name: Tangpus, Mark Vincent B. 5:30pm, M-W-F Year&Section: BSIT 4B Ortega
d.Print();

Subject: Schedule: 4:30-

IT-413

Instructor: Mr. Eric

Rational e; e = a.Multiply(b); Console.Write("c = a.Multiply(b) \t-> {0}/{1} * {2}/{3} = ", a.GetNumerator(), a.GetDenominator(), b.GetNumerator(), b.GetDenominator()); e.Print(); Rational f; f = a.Divide(b); Console.Write("c = a.Divide(b) \t-> {0}/{1} / {2}/{3} = ", a.GetNumerator(), a.GetDenominator(), b.GetNumerator(), b.GetDenominator()); f.Print(); } } } Console.ReadLine();

Page 5 of 5

You might also like