![]() |
|
|
#1 |
|
Banned
Join Date: Dec. 30th, 2003
Posts: 1,223
|
c++ help
ok I have an assignment that I don't understand.
I have to write a program that prompts the user for a date, and then displays how days ago it was from todays date. In julian days. It also has to continue to prompt for dates and process until the user wants to stop. The algorithm for julian day number : Set jd, jm, jy to the day, month, and year. If the year is negative add 1 to jy (there is no year 0, year 1 BC was immediately followed by year AD 1). If the month is larger than february add 1 to jm. Otherwise, add 13 to jm and subtract 1 from jy. Then compute. long jul = floor(365.25 * jy) + floor(30.6001 * jm) + d + 1720995.0 Store the result in a variable of type long (simple integers may not have enough digits to hold the value). If the date was before October 15, 1582 return this value, otherwise the following correction needs to be made: int ja = 0,01* jy; jul = jul + 2 -ja + 0.25 * ja ; So I know I need to write a function long julian(int year, int month, int day) that converts the date into a julian day number. That's part of it. I just have no idea how to do this whole program.
__________________
It takes 2 people to have an argument, both are part of the problem. Repeating the same opinion over and over again, will not make it a fact. If you will only listen to opinions that enforce your own, than there is no point in voicing yours to begin with. |
|
|
|
|
|
#2 |
|
You're algorithm looks frickin too complicated.
Id use a switch to test for the month and calculate the date based off that. Then add a feature to either test for leap year or have the user enter that it's leap year. If you generate and algorithm to test for leap year (i.e. leap years, assuming started 0 BC - (probably not) should be divisible by 4) so... if (year%4 == 0) leapyear = 1 else leapyear = 0 You could actually calculate back to the first leap year by starting backwards from this year to get the actaul start date - iI just used 0 BC as example. Then test what month it is - put in switch Ex. Code:
switch(month)
{
case January:
{
days = day;
break;
}
case February:
{
days = day + 31;
break;
}
etc, ...
}
maybe brutish - but it'll work just fine just be sure to have something to test leapyear and you should be good. |
|
|
|
|
|
|
#3 | ||
|
Saito, your original method is fine.
here is a function using your method: Code:
long julian(int year, int month, int day)
{
double jy = year;
double jm = month;
double jd = day;
if(year < 0)
jy += 1;
if(month > 2)
jm += 1;
else
{
jm += 13;
jy -= 1;
}
long jul = floor(365.25 * jy) + floor(30.6001 * jm) + d + 1720995.0
if(month < 10 && day < 15 && year < 1582)
return jul;
else
{
int ja = 0,01* jy;
jul = jul + 2 -ja + 0.25 * ja ;
return jul;
}
}
you can make the switch much easier: Code:
int day;
...
edit: these needed to be shifted by 1:
switch(month)
{
case December:
day += days in november
case November:
days += days in october
case October:
days += days in september
case Feburary:
days += days in january
// don't need to do anything for january
}
Quote:
Quote:
Last edited by bobtheking; 31st Mar 2004 at 02:59 AM. |
|||
|
|
|
|
|
#4 | |
|
Oops, you're right - sorry.
Quote:
|
||
|
|
|
|
|
#5 |
|
Banned
Join Date: Dec. 30th, 2003
Posts: 1,223
|
thx
__________________
It takes 2 people to have an argument, both are part of the problem. Repeating the same opinion over and over again, will not make it a fact. If you will only listen to opinions that enforce your own, than there is no point in voicing yours to begin with. |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|