C++ Learning Community Forum
August 01, 2010, 03:20:17 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: The calendar calculator  (Read 673 times)
geft
N00b!!1
*
Posts: 5


View Profile
« on: December 17, 2008, 10:32:41 AM »

Input a year and a number. The calculator will tell you what day, date and month that number falls on that year.

Code:
#include <iostream>
#include <string>

using namespace std;
int normal(), leap(), key();
int num, day, date, year, yearc;
int last2digits, keyvalue, centurycode;
string dayc, month;

int main()
{

    cout<< "Enter a year: ";
    cin>> year;

    if (year%4!=0)
    {
        normal();
    }
    else
    {
        leap();
    }

    key();

    cout<< "Day " << num << " falls on " << dayc << ", " << date << " " << month << " " << year<<"\n";
    main();

return 0;
}

int normal()
{
    cout<< "Enter a number between 1 to 365: ";
    cin>> num;

    if (num>=1 && num<=31)
    {
    date=num;
    month="January";
    keyvalue=1;
    }

else if (num>=32 && num<=59)
    {
    date=num-31;
    month="February";
    keyvalue=4;
    }

else if (num>=60 && num<=90)
    {
    date=num-59;
    month="March";
    keyvalue=4;
    }

else if (num>=91 && num<=120)
    {
    date=num-90;
    month="April";
    keyvalue=0;
    }

else if (num>=121 && num<=151)
    {
    date=num-120;
    month="May";
    keyvalue=2;
    }

else if (num>=152 && num<=181)
    {
    date=num-151;
    month="June";
    keyvalue=5;
    }

else if (num>=182 && num<=212)
    {
    date=num-181;
    month="July";
    keyvalue=0;
    }

else if (num>=213 && num<=243)
    {
    date=num-212;
    month="August";
    keyvalue=3;
    }

else if (num>=244 && num<=273)
    {
    date=num-243;
    month="September";
    keyvalue=6;
    }

else if (num>=274 && num<=304)
    {
    date=num-273;
    month="October";
    keyvalue=1;
    }

else if (num>=305 && num<=334)
    {
    date=num-304;
    month="November";
    keyvalue=4;
    }

else if (num>=335 && num<=365)
    {
    date=num-334;
    month="December";
    keyvalue=6;
    }
}

int leap()
{
    cout<< "Enter a number between 1 to 366: ";
    cin>> num;

    if (num>=1 && num<=31)
    {
    date=num;
    month="January";
    keyvalue=0;
    }

else if (num>=32 && num<=60)
    {
    date=num-31;
    month="February";
    keyvalue=3;
    }

else if (num>=61 && num<=91)
    {
    date=num-60;
    month="March";
    keyvalue=4;
    }

else if (num>=92 && num<=121)
    {
    date=num-91;
    month="April";
    keyvalue=0;
    }

else if (num>=122 && num<=152)
    {
    date=num-121;
    month="May";
    keyvalue=2;
    }

else if (num>=153 && num<=182)
    {
    date=num-152;
    month="June";
    keyvalue=5;
    }

else if (num>=183 && num<=213)
    {
    date=num-182;
    month="July";
    keyvalue=0;
    }

else if (num>=214 && num<=244)
    {
    date=num-213;
    month="August";
    keyvalue=3;
    }

else if (num>=245 && num<=274)
    {
    date=num-244;
    month="September";
    keyvalue=6;
    }

else if (num>=275 && num<=305)
    {
    date=num-274;
    month="October";
    keyvalue=1;
    }

else if (num>=306 && num<=335)
    {
    date=num-305;
    month="November";
    keyvalue=4;
    }

else if (num>=336 && num<=366)
    {
    date=num-335;
    month="December";
    keyvalue=6;
    }

}

int key()
{
    last2digits=year;

    for (; last2digits>=100;)
        last2digits-=100;

    yearc=year;
    for (; yearc<1700;)
        yearc+=400;

    for (; yearc>2099;)
        yearc-=400;

    if (yearc>=1700 && yearc<= 1799)
    centurycode = 4;

    else if (yearc>=1800 && yearc<= 1899)
    centurycode = 2;

    else if (yearc>=1900 && yearc<= 1999)
    centurycode = 0;

    else if (yearc>=2000 && yearc<= 2099)
    centurycode = 6;

    day = ((last2digits + last2digits/4 + date + keyvalue + centurycode)%7);


    if (day==0)
    dayc = "Saturday";
    else if (day==1)
    dayc = "Sunday";
    else if (day==2)
    dayc = "Monday";
    else if (day==3)
    dayc = "Tuesday";
    else if (day==4)
    dayc = "Wednesday";
    else if (day==5)
    dayc = "Thursday";
    else if (day==6)
    dayc = "Friday";
}
Logged

A beginner in programming. Started learning C++ some time in December 08. Any constructive criticism is very much appreciated.
mleveill
1337 L0LCoder
C++ Freak
***
Posts: 430


IM IN UR PROGRAMZ WRITIN UR CODEZ!


View Profile
« Reply #1 on: December 18, 2008, 01:44:18 AM »

cool, i didn't run it though. next, try combining leap() and normal()
e.g.
Code:
get_month(bool leap_year) { ... }
Logged

Quote from: kohlrak
Common interests aren't always the best thing. Especially programming wise.
I could just picture a couple fighting over weather to "xor eax by eax" or "and eax by 0"...
Then kids'll come home and argue with their parents that readabilty is better than size and that "mov eax, 0" would be best because that's "more readable." (Even if it really isn't...)

LOLCode - http://lolcode.com/home
adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #2 on: December 18, 2008, 03:21:47 AM »

Code:
    if (year%4!=0)
    {
        normal();
    }
    else
    {
        leap();
    }

Almost, you need to check whether the year is divisible by 400 as well
Quote
    if (year%4!=0 || year % 400 != 0)
1800 wasn't a leap year, but with your current code it is.
Logged

geft
N00b!!1
*
Posts: 5


View Profile
« Reply #3 on: December 18, 2008, 04:47:07 AM »

Thanks for the input. Smiley I've always thought a leap year is a year that's divisible by 4.

@mleveill: Thanks for that, but I haven't get to arguments yet. Not sure how to use it.
« Last Edit: December 18, 2008, 06:16:35 AM by geft » Logged

A beginner in programming. Started learning C++ some time in December 08. Any constructive criticism is very much appreciated.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!