Go Back   Hardware Forums > Software Support Forums > Web Development

Reply
 
LinkBack Thread Tools
Old 18-11-2006, 10:44 PM   #1 (permalink) Top
out of date
 
zeus's Avatar
 
Join Date: May 2003
Age: 27
Posts: 1,643
Times Helpful: 105
My Mood: Twisted
Status: Online

My Computer

Default day from date calculator

Well ive finished my first c++ program. Its a program which converts a date into a weekday name.

It took all day and im sure ive done things the hard way but im happy with it.

Heres the program...
day from date calculator

There are a few things I want to change but I dont think I know the code id need to use. Ive only done the basics of c++ section on C++ Language Tutorial

I want it to ask for a solid date, ie 1289, instead of 12 then 89. I cant get my program to split the first two digits from the second two. Again, its just because i haven't read far enough!

Here is the code
Code:
//This program will find the day from any date between 45BC to 9999AD.
//It uses the Julian Calendar between the dates 45BC and 1752AD. 
//It assumes 45BC (the year the Julian calendar was introduced) is a leap year.
//The year does not exist, 3BC, 2BC, 1BC, 1AD....
//Type 'config' to change the date the Gregorian calendar takes effect.

#include <iostream>
#include <string>
using namespace std;

int main()
{
//Collect Data
int england=1752;
start:
cout << "\nDDDDD           tt";                                     
cout << "\nDD  DD    aa aa tt      eee";
cout << "\nDD   DD  aa aaa tttt  ee   e";  
cout << "\nDD   DD aa  aaa tt    eeeee";       
cout << "\nDDDDDD   aaa aa  tttt  eeeee\n";   
    
cout << "\n CCCCC          lll                lll         tt";
cout << "\nCC    C   aa aa lll   cccc uu   uu lll   aa aa tt     oooo  rr rr";
cout << "\nCC       aa aaa lll cc     uu   uu lll  aa aaa tttt  oo  oo rrr  r";   
cout << "\nCC    C aa  aaa lll cc     uu   uu lll aa  aaa tt    oo  oo rr";
cout << "\n CCCCC   aaa aa lll  ccccc  uuuu u lll  aaa aa  tttt  oooo  rr";
cout << "\n\nby Rik Potts";   
    
    string start;
    cout << "\n\nEnter 'go' to begin or type 'config' for configuration: ";
    cin >> start;
    
    if (start!="go" && start!="config") 
{
    cout << start << " is not a valid entry. Please try again\n\n";
    goto start;
}
    
    if (start=="go") goto findday; else goto config;
    
    //Configuration     
    config:    
    cout << "\nPlease enter the date you want the Gregorian (modern) calendar to take effect.";
    cout << "\n\nDefault is 1752 which is when England began using the Gregorian";
    cout << "\ncalendar. The Gregorian calendar was first introduced in 1582";
    cout << "\nin Venice, France, Spain, Portugal and Netherlands. Germany in 1583";
    cout << ",\nPrussia and Denmark in 1700, Sweden in 1753 and Russia in 1918!\n\n";
    cout << "Please enter the date you want the Gregorian calendar to take effect: ";
    cin >> england;
    cout << "NOTE: You will have to set config each time you run the program.\n\n";
    
    //Find the day (day)   
findday:
    cout << "The Gregorian (modern) calendar will take effect from " << england;
    
    int day;
    cout << "\nPlease enter the day (##): " ;
    cin >> day;
    
    if (day>31 || day<1)
{   
    cout << "\n" << day << " is not a valid day!\nPlease try again\n\n"; 
    goto findday;
}
    
    //Find the month (month)...
month:
    string month;
    cout << "Please enter the Month (abc): ";
    cin >> month;
    
    if (month!="feb" && month!="aug" && month!="mar" && month!="nov" 
    && month!="jun" && month!="sep" && month!="dec" && month!="apr" 
    && month!="jul" && month!="jan" && month!="oct" && month!="may")
{   
    cout << "\n" << month << " is not a valid month!\nPlease try again...\n\n";
    goto month;
}
    
    if (month=="feb" && day>29) 
{
    cout << "\nIt is not possible to have " << day << " days in February!";
    cout << "\nPlease try again\n\n";
    goto findday;
}

     //Find the year (date)...
cent:
    cout << "\nNOTE: If you need a BC date you DO ";
    cout << "NOT need to use a minus sign (-)\n";
    
    //Get the century (cent)
    int cent;
    cout << "Please enter the first two digits of the year (##__): ";
    cin >> cent;
    
    if (cent>99) 
{
    cout << "\n" << cent << " is not a valid entry.\nYou must enter a number below 99.\n";
    goto cent;
}
    
    //Get the year (year)
year:
    int year;
    cout << "Please enter the last two digits of the year (__##): ";
    cin >> year;
    
    if (year>99 || year<0) 
{
    cout << "\n" << year << " is not a valid entry.\nYou must enter a number below 99\n\n";
    goto year;
}

    //make the full year (date)
    int date, numdate;
    date=(100*cent) + year;
    numdate=date;
    
    if (date==0)
{
    cout << "\nThe date 0000 is not valid because the Julian calendar uses 1BC";
    cout << " instead\n\n";
    goto cent;
}

    //Is it a negative or positive year?...
norp:
    string bc;
    cout << "Is the year BC? (y/n) : ";
    cin >> bc;
    
    if (bc!="y" && bc!="n") 
{
    cout << bc << " is not a valid option. Please try again...";
    goto norp;
}
    
    if (bc=="y") date=-date;
    
    if (date<=-46) 
{
    cout << "\nYou entered " << numdate << "BC. The Julian calendar was introduced ";
    cout << "in 45BC.";
    cout << "\nThis makes " << numdate << "BC invalid!\n";
    goto cent;
}
    
    int year2;
    year2=year/4;
    
//Process and Validate Data
    //Is the year a leap year?...
    //Is the year on the gregorian or julian calendar?
    int yl=1, nl=0;
    if (date>=england) goto gregorian;
    
    //Julian calendar
    int jleap;  
    if (date>0 && (date%4==0)) jleap=yl; else jleap=nl;
    if (date<0 && (date%4==-1)) jleap=yl; else jleap=nl;
    goto validation;
   
    //Gregorian calendar
gregorian:
    int gleap;
    gleap = ((date%4==0 && date%100!=0) || date % 400 == 0) ? yl : nl;

    //Is the Month and Day combination correct?
validation:
    if ((jleap==1 || gleap==1) && month=="feb" && day<=29) goto notfeb;
    
    if ((jleap!=1 || gleap!=1) && month=="feb" && day==29) 
{
    cout << "\nThe 29th February is not a valid date because " << date;
    cout << " \nisnt a leap year!\nPlease try again from the beginning\n\n"; 
    goto findday;
}
    
    if (month!="feb") goto notfeb;
    
    if (!(day<=28 && day>=1)) 
{
    cout << day << "\n is not a valid day!\nPlease try again\n\n"; 
    goto findday;
}

notfeb:
    if (month!="sep" && month!="apr" && month!="jun" && month!="nov") goto nsajn;
    
    if (!(day<=30 && day>=1)) 
{
    cout << day << "\n is not a valid day!\nPlease try again\n\n"; 
    goto findday;
}

nsajn:
    if (!(day<=31 && day>=1))
{
    cout << day << "\n is not a valid day!\nPlease try again\n\n"; 
    goto findday;
}

//Interpret Data
    //Calculate the Monthcode
    int monthcode;
    if (month=="aug") monthcode=0;
    if (month=="feb" || month=="mar" || month=="nov") monthcode=1;
    if (month=="jun") monthcode=2;
    if (month=="sep" || month=="dec") monthcode=3;
    if (month=="apr" || month=="jul") monthcode=4;
    if (month=="jan" || month=="oct") monthcode=5;
    if (month=="may") monthcode=6;
    
    //Which calendar to use?
    if (date>=england) goto gregoriansum;

    //Is the leap year compensation required?
    int ujleap, ugleap;
    ujleap=jleap;
    ugleap=gleap;
    if (month!="jan" && month!="feb") ujleap=nl;
    if (month!="jan" && month!="feb") ugleap=nl;
    
    //Julian Formula...
    //(Day + MonthCode + Year + Year/4 + 5 – Century – isLeapJanFeb?) % 7
    int jresult;
    if (date<0) jresult=(day + monthcode - year - (year2) + 5 - cent )%(-7);
    else
    jresult=(day + monthcode + year + (year2) + 5 - cent - ujleap)%7;
    
    goto weekday;
    
    //Gregorian Formula...
gregoriansum:
    //(Day + MonthCode + Year + Year/4 – 2×(Century%4) – isLeapJanFeb?) % 7
    int gresult;
    gresult=(day + monthcode + year + (year2) - (2*(cent%4)) - ugleap)%7;
    
    //Assign day code to word.
weekday:
    int weekday;
    string result;
    weekday = (date<england) ? jresult : gresult;
    if (weekday==0) result="Monday";
    if (weekday==1 || weekday==-6) result="Tuesday";
    if (weekday==2 || weekday==-5) result="Wednesday";
    if (weekday==3 || weekday==-4) result="Thursday";
    if (weekday==4 || weekday==-3) result="Friday";
    if (weekday==5 || weekday==-2) result="Saturday";
    if (weekday==6 || weekday==-1) result="Sunday";
    
//Print results
    if (date<0)
    cout << "\nThe day on " << day << " " << month << " " << numdate << "BC is a " 
    << result << "!\n";

    else 
    cout << "\nThe day on " << day << " " << month << " " << date << "AD is a " 
    << result << "!\n";

    if (date==england) 
{
    cout << "\nYou entered the year 1752!";
    cout << "\nThis was the year the Gregorian calendar was first ";
    cout << "\nproposed by Dr Aloysius Lilius!\n";
}
    
    if (date==(-45)) 
{
    cout << "\nYou entered the year 45BC!";
    cout << "\nThis was the year the Julian calendar was first ";
    cout << "\nintroduced by Julius Caesar!\n";
}
    
    if (date<0 && (jleap==1))
{
    cout << "The year " << numdate << "BC is a leap year!" << "\n\n";
    goto rerun;
}
    
    if (date>0 && (jleap==1 || gleap==1))
{   
    cout << "The year " << date << "AD is a leap year!" << "\n\n";
    goto rerun;
}
    
    if (date<0 && (jleap!=1))
{   
    cout << "The year " << numdate << "BC isnt a leap year!" << "\n\n";
    goto rerun;
}
    
    if (date>0 && (jleap==0 || gleap==0))
    cout << "The year " << date << "AD isnt a leap year!" << "\n\n"; 
    goto rerun;

rerun:
    cout << "Do you want to run the program again? (y/n): ";
    string startagain;
    cin >> startagain;
    cout << "\n";
    if (startagain=="y") goto findday; else goto close;
    
    close:
    return 0;
}


Last edited by zeus; 03-12-2006 at 02:28 AM.
  Reply With Quote
Whats this? G15 Gaming Keyboard
G15 Gaming Keyboard
Seller Price (inc. VAT) Delivery Total Price Availability Seller Rating
Tekheads.co.uk £54.99 £4.95 £59.94 In Stock Rated: 3 out of 5 - Number of votes: 310
Misco.co.uk £53.99 £4.69 £58.68 In Stock Rated: 4 out of 5 - Number of votes: 1354
inkcartridgedirect.org.uk £50.63 £4.99 £55.62 In Stock Rated: 1 out of 5 - Number of votes: 1
Old 24-11-2006, 12:10 AM   #2 (permalink) Top
It's D Grav80 Of Luv
 
Karanislove's Avatar
 
Join Date: Jan 2006
Age: 23 Male
Posts: 3,460
Times Helpful: 160
My Mood: Cool
Status: Offline

My Computer

nice lill program! lol...i was born on Sunday...neva bothered to knw this before!
__________________
__________________________________________________ ___


Troubleshoot a Dead/Unbootable PC / Microsoft Events Viewer

Troubleshoot Printer / Most Common Home Audio Problems_
  Reply With Quote
Old 24-11-2006, 10:50 AM   #3 (permalink) Top
out of date
 
zeus's Avatar
 
Join Date: May 2003
Age: 27
Posts: 1,643
Times Helpful: 105
My Mood: Twisted
Status: Online

My Computer

Ive started this again.
My heads frying. Different dates use different calendars and different calendars use different leap year rulres. Year 0000 is really year 1BC, no-one on earth can give a definitive answer to whether 45BC is a leap year or not and for some reason my calculator is multiply the date by 10!

I cant find a single calculator which does everything correctly so I can check mine against it. Most use the gregorian calendar before 1582, most say 0000 is a date too. The ones that say 0000 isnt a date say 1BC isnt a leap year... Its a pain in the arse.

It has made me realise how so many people do a half arse job on stuff.

The only day calculator on the whole of the net which id put trust in is this one.... And mine when it stops saying the date is 16660 for 1666 etc!
Calendar Converter
All the other ones which come up in google are incomplete and accept dates which are beyond the scope of the formula the calculator uses.
  Reply With Quote
Old 24-11-2006, 11:03 AM   #4 (permalink) Top
Geek
 
daisycutter's Avatar
 
Join Date: Jun 2006
Posts: 94
Times Helpful: 4
My Mood: Mellow
Status: Offline
fun little distraction! at least i know now that my birthday falls on a saturday in 3577!
  Reply With Quote
Old 24-11-2006, 08:55 PM   #5 (permalink) Top
out of date
 
zeus's Avatar
 
Join Date: May 2003
Age: 27
Posts: 1,643
Times Helpful: 105
My Mood: Twisted
Status: Online

My Computer

I think ive finally cracked it!
As far as I know its only the second program easily found on Google (actually this probably isnt easily found!) which uses the correct formula for the correct date!

The link I posted above is the only other place. Though have a look at the year 24BC. There is a one day discrepancy. Ive checked it by hand and im sure mine is correct... not unless there is a third formula for dates including 24BC. Ive found no reference to a third one though.

I want to add a few bits here and there.... ie "On this day......" etc
Anyway, on with the turorial.

Last edited by zeus; 25-11-2006 at 01:16 PM.
  Reply With Quote
Old 26-11-2006, 12:12 PM   #6 (permalink) Top
MiCrO$oFt $uK$ :D
 
Willz's Avatar
 
Join Date: Jun 2005
Age: 19 Male
Posts: 4,234
Times Helpful: 150
My Mood: Lurking
Status: Offline

My Computer

Nice program , i wish at college i learned C++ than vB, i mean, i could create programs for my keyboard when it comes :p, and C++ seems more intresting than vB.
__________________
Send a message via AIM to Willz Send a message via MSN to Willz Send a message via Yahoo to Willz   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Package List out of date. DavidNW Linux and Other OSes 8 07-11-2006 06:28 PM
PSU calculator zeus Power Supplies and UPS's 17 15-07-2006 11:25 AM
keeping up to date zeus CPU, Motherboards and Memory 4 01-07-2006 01:58 PM
Shell calculator? Addis Linux and Other OSes 1 20-02-2006 11:55 PM
Final date for sig competition? matttibb Graphics Design 4 22-12-2004 07:27 PM


All times are GMT +1. The time now is 11:12 AM.


Copyright © 2000 - 2008 · HARDWAREFORUMS.COM · All rights reserved