I found one exercise with Ratioal Numbers and the result will be in rational number too.Can anyone of u Please help me.
1)Do we have any Specific Data type for represent Rational Numbers in C#?
2)How will i calculate Age of the Person?(Any Clues)
I have searched in the Net.But I didnt find an Answer for it.
Ur Reply is Valuable to me.
Thanks.
jaguRitaPosted Aug 7, 2007, 5:20 PM
Hmm.....Thanks...
Reading, & Keep on Reading..........
AlanPosted Aug 7, 2007, 5:09 PM
Keep reading and, above all, writing your own code.
I've been programming .NET since it started more than 6 years ago, but I'm still learning and there are some exciting developments on the horizon to look forward to :)
jaguRitaPosted Aug 7, 2007, 4:53 PM
Yeah,I think this is the one i understood very easily.And I understood the rational number program,because i had done GCD already.Ur code is easy.Thanks.
But the previous one(Age) is,difficult to understand for me.I hope its because of lack of my Programming skill.Am finding a way to improve my skill.Any Guidence from u to improve skill?
Thanks Again.
AlanPosted Aug 7, 2007, 4:16 PM
Here's another way of calculating someone's age that you might find easier to understand:
string dob = "01/01/1980"; // or whatever
DateTime dt1 = DateTime.Parse(dob);
DateTime dt2 = DateTime.Now;
int age = dt2.Year - dt1.Year;
if (dt2.Month < dt1.Month ||(dt2.Month == dt1.Month && dt2.Day < dt1.Day)) age-- ;
AlanPosted Aug 7, 2007, 2:18 PM
Because the months having a varying number of days and you've also got the leap year problem, the idea behind this algorithm is to find an easy and consistent way of converting a given date into a number.
To do this, you assume that every month has 31 days and hence every year has 12 * 31 = 372 days. It doesn't matter that this is fictitious when all you're doing is working out the number of complete years between two dates and in certain other applications :)
jaguRitaPosted Aug 7, 2007, 2:00 PM
int d1 = dt1.Year * 372 + (dt1.Month - 1) * 31 + dt1.Day - 1;
int d2 = dt2.Year * 372 + (dt2.Month - 1) * 31 + dt2.Day - 1;
int age = (d2 - d1) / 372;
Can u please tell me how do u multiply year by 372 ? Is there any specific reason?
jaguRitaPosted Aug 7, 2007, 1:24 PM
Thanks a lot 4 ur both answers.
I need some time to analyze it.
AlanPosted Aug 7, 2007, 1:04 PM
If it helps you with the rational number exercise, I've dug out some code I wrote a while back which contains methods to convert decimals and doubles to fractions in their lowest terms:
using System;
class Test
{
static void Main()
{
string result = DecimalToFraction(0.235M, false);
Console.WriteLine(result);
string result2 = DoubleToFraction(0.72, false);
Console.WriteLine(result2);
Console.ReadKey();
}
public static string DecimalToFraction(decimal number, bool mixed)
{
string sign = "";
if(number < 0.0M)
{
sign = "-";
number = -number;
}
long numerator = (long)number;
long denominator = 1;
while (numerator != number)
{
number *= 10.0M;
denominator *= 10L;
numerator = (long)number;
}
if (denominator == 1)
return sign + numerator.ToString();
long gcd = GetGCD(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
if (!mixed)
return sign + numerator.ToString() + "/" + denominator.ToString();
long whole = numerator / denominator;
numerator %= denominator;
string sign2 = (sign == "-") ? " - " : " + ";
return sign + whole.ToString() + sign2 + numerator.ToString() + "/" + denominator.ToString();
}
public static string DecimalToFraction(decimal number)
{
return DecimalToFraction(number, true);
}
public static string DoubleToFraction(double number, bool mixed)
{
if (number > (double)Decimal.MaxValue || number < (double)Decimal.MinValue)
return "Out of range";
return DecimalToFraction((decimal)number, mixed);
}
public static string DoubleToFraction(double number)
{
return DoubleToFraction(number, true);
}
public static long GetGCD(long numerator, long denominator)
{
long gcd = 0L;
while (numerator != 0L)
{
gcd = numerator;
numerator = denominator % numerator;
denominator = gcd;
}
return gcd;
}
}
AlanPosted Aug 7, 2007, 12:42 PM
To my knowledge, there is no specific class for dealing with rational numbers in .NET.
There are various ways of calculating a person's age but here's a method which I've always liked:
string dob = "01/01/1980"; // or whatever
DateTime dt1 = DateTime.Parse(dob);
DateTime dt2 = DateTime.Now;
int d1 = dt1.Year * 372 + (dt1.Month - 1) * 31 + dt1.Day - 1;
int d2 = dt2.Year * 372 + (dt2.Month - 1) * 31 + dt2.Day - 1;
int age = (d2 - d1) / 372;
If a person born on 29 February is considered, where you live, to celebrate their birthday on 28 February in a non-leap year, rather than 1 March, then change the code to this:
string dob = "01/01/1980"; // or whatever
DateTime dt1 = DateTime.Parse(dob);
DateTime dt2 = DateTime.Now;
int d1 = dt1.Year * 372 + (dt1.Month - 1) * 31 + dt1.Day - 1;
int d2 = dt2.Year * 372 + (dt2.Month - 1) * 31 + dt2.Day - 1;
if (dt1.Month == 2 && dt1.Day == 29 && dt2.Month == 2 && dt2.Day == 28 && !DateTime.IsLeapYear(dt2.Year))
d2++;
int age = (d2 - d1) / 372;