Thread: Calendar program

  1. #1
    Unregistered
    Guest

    Smile Calendar program

    Hello!!
    I'm trying to built a program and I'd like your advice!
    So, I'm trying to make a program that does the following :

    1) the user inserts a year
    2)the user enters the first day of the year(1/1/year) (eg. Monday, Friday...)
    3)The user enters the number of the days that the compurer will print (eg. 100 days)
    4) The computer prints the (100) first days if the year.

    I also, want to check if the year is leap (365 days or 366 days).

    I have made the program, however I would like your opinion. Is this the correct solution? Is there any other (better) solution??


    -------------------------
    My code :
    -------------------------


    #include <iostream.h>
    #include <stdlib.h>

    int myear;
    int i = 1;
    int week=1;
    int hd;
    unsigned short int proti_mera;

    bool disekto(unsigned short int year)
    {
    if(year % 100 != 0)
    return (year % 4 == 0);
    else
    return (year % 400 == 0);
    }

    void menu()
    {
    cout<<"Insert the year : \n";
    cin>>myear;
    cout<<endl;
    cout<<"Which is the first day of the year ?\n";
    cout<<"1. Monday\n";
    cout<<"2. Tuesday\n";
    cout<<"3. Wednesday\n";
    cout<<"4. Thursday\n";
    cout<<"5. Friday\n";
    cout<<"6. Saturday\n";
    cout<<"7. Sunday\n";
    cout<<endl;
    do
    {
    cout<<"Press (1..7) : ";
    cin>>proti_mera;
    if (!((proti_mera > 0) && (proti_mera < 8)))
    continue;
    }
    while (!((proti_mera > 0) && (proti_mera < 8)));
    cout<<endl;
    cout<<"How many days would you like me to print ?\n";
    do
    {
    if (disekto(myear))
    {
    cout<<"Press (1..366) : ";
    cin>>hd;
    if (!((hd > 0) && (hd <= 366)))
    continue;
    else
    break;
    }
    if (!disekto(myear))
    {
    cout<<"Press (1..365) : ";
    cin>>hd;
    if (!((hd > 0) && (hd <= 365)))
    continue;
    else
    break;
    }
    }
    while (1);
    cout<<endl;
    }

    void display_week()
    {
    cout.width(15);
    cout<<week++<<" Week\n";
    cout<<endl;
    }

    void pause_page()
    {
    if (i % 7 == 0)
    {
    cout<<endl;
    system("PAUSE");
    cout<<endl;
    display_week();
    }
    }

    int main()
    {
    char mdays[7][10] = {"Monday","Tuesday","Wednesday","Thursday","Friday ","Saturday","Sunday"};
    char mmonths[12][12] = {"January","February","March","April","May","June" ,"July","August","September","October","November", "December"};
    int disdays[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
    int udisdays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    int dday = 1;
    int mmonth=0;
    menu();
    int mday = proti_mera-1;

    display_week();

    for(i;i<=hd;i++)
    {
    cout<<i<<" ";
    cout.width(5);
    cout<<mdays[mday++]<<" "<<dday++<<" "<<mmonths[mmonth]<<" "<<myear<<endl;

    if (disekto(myear))
    {
    if(dday == disdays[mmonth]+1)
    {
    dday=1;
    mmonth++;
    }
    }
    if (!disekto(myear))
    {
    if(dday == udisdays[mmonth]+1)
    {
    dday=1;
    mmonth++;
    }
    }

    if (mday >= 7)
    mday=0;

    pause_page();
    }
    cout<<endl;
    return 0;
    }


    ----------------------------------------------

    Thank you in advance!!

    Apostolis

  2. #2
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    here's my solution

    here's my solution..
    this one also saves it to a text file if you want..

    PHP Code:
    #include <stdio.h>
    #include <iostream.h>
    #include <conio.h>

    int leapyear(int year)
    {
        if ( (
    year%4==0) && (year%400==0) )
        {
            return 
    1;
        }

        if ( (
    year%4==0) && (year%100!=0) )
        {
            return 
    1;
        }
        
        return 
    0;
    }


    void getweekday(int monthint dayint yearint *week)
    {
        
    int centurycodemonthcode[13], CD;

        
    monthcode[0]=1;
        
    monthcode[1]=1;
        
    monthcode[2]=4;
        
    monthcode[3]=4;
        
    monthcode[4]=0;
        
    monthcode[5]=2;
        
    monthcode[6]=5;
        
    monthcode[7]=0;
        
    monthcode[8]=3;
        
    monthcode[9]=6;
        
    monthcode[10]=1;
        
    monthcode[11]=4;
        
    monthcode[12]=6;

        
    C=year/100;
        
    D=year%100;


        if (
    C==17)
        {
         
    centurycode=4;
        }
        if (
    C==18)
        {
         
    centurycode=2;
        }
        if (
    C==19)
        {
         
    centurycode=0;
        }
        if (
    C==20)
        {
         
    centurycode=6;
        }

        *
    week = (centurycode + (D/D)%+monthcode[month] + day)%7;


    }

    void drawcalendar(int daysint firstday)
    {

        
    int idaythen;
        
    char space ' ';

        if (
    firstday == 0)
        {
            
    firstday 7;
        }

        
    daythen=firstday;
        
    printf("SUN MON TUE WED THU FRI SAT\n");

        for (
    i=1i<firstdayi++)
        {
            
    printf("%3c "space);
        }

        for (
    i=1i<=daysi++)
        {
            
    printf("%3d "i);
            
    daythen++;

            if (
    daythen>&& != days)
            {
                
    daythen=1;
                
    printf("\n");
                
            }

        }
        
        
    printf("\n\n\n");
    }

    void writecalendar(int daysint firstdayFILE *fp)
    {

        
    int idaythen;
        
    char space ' ';

        if (
    firstday == 0)
        {
            
    firstday 7;
        }

        
    daythen=firstday;
        
    fprintf(fp"SUN MON TUE WED THU FRI SAT\n");

        for (
    i=1i<firstdayi++)
        {
            
    fprintf(fp"%3c "space);
        }

        for (
    i=1i<=daysi++)
        {
            
    fprintf(fp"%3d "i);
            
    daythen++;

            if (
    daythen>&& != days)
            {
                
    daythen=1;
                
    fprintf(fp"\n");
                
            }

        }
        
        
    fprintf(fp"\n\n\n");
    }

    void getdays(int yeartypeint monthint *days)
    {
        if (
    yeartype == && month == 2)
        {
            *
    days 28;
        }
        
        if (
    yeartype == && month == 2)
        {
            *
    days 29;
        }
        
        if ( (
    month==4) || (month==6) || (month==9) || (month==11) )
        {
            *
    days 30;
        }

        if ( (
    month==1) || (month==3) || (month==5) || (month == 7) || (month==8) || (month==10) || (month==12) )
        {
            *
    days 31;
        }
    }

    void writefile(int monthint yearint daysint firstdayint yeartype)
    {
        
    FILE *fp;
        
    fp fopen("CALENDAR.TXT""w");
        for (
    month =1month <=12month++)
        {
            
    getweekday(month1year, &firstday);
            
    getdays(yeartypemonth, &days);

            if (
    month == 1)
            {
                
    fprintf(fp"January %d\n\n"year);
            }
            else if (
    month ==2)
            {
                
    fprintf(fp"February %d\n\n"year);
            }
            else if (
    month ==3)
            {
                
    fprintf(fp"March %d\n\n"year);
            }
            else if (
    month ==4)
            {
                
    fprintf(fp"April %d\n\n"year);
            }
            else if (
    month ==5)
            {
                
    fprintf(fp"May %d\n\n"year);
            }
            else if (
    month ==6)
            {
                
    fprintf(fp"June %d\n\n"year);
            }
            else if (
    month ==7)
            {
                
    fprintf(fp"July %d\n\n"year);
            }
            else if (
    month ==8)
            {
                
    fprintf(fp"August %d\n\n"year);
            }
            else if (
    month ==9)
            {
                
    fprintf(fp"September %d\n\n"year);
            }
            else if (
    month ==10)
            {
                
    fprintf(fp"October %d\n\n"year);
            }
            else if (
    month ==11)
            {
                
    fprintf(fp"November %d\n\n"year);
            }
            else if (
    month ==12)
            {
                
    fprintf(fp"December %d\n\n"year);
            }
            
    writecalendar(daysfirstdayfp);
        }
        
    fprintf(fp"Program written by Maharshi Akilla\n");
        
    fprintf(fp"http://mahurshi.tripod.com");
        
    fclose(fp);
    }

    main()
    {
        
    int monthyeardaysfirstdayyeartype;
        
    char answer;

        
    cout << "Calendar" << endl << "Generates the calendar for any year between 1700 and 2099" << endl << endl;
        do
        {
            
    cout << "Enter the year: ";
            
    cin >> year;
        } while (
    year<1700 || year>2099);

        
    clrscr();

        
    yeartype leapyear(year);

        for (
    month =1month <=12month++)
        {
            
    getweekday(month1year, &firstday);
            
    getdays(yeartypemonth, &days);

            if (
    month == 1)
            {
                
    cout << "January " << year << endl << endl;
            }
            else if (
    month ==2)
            {
                
    cout << "February " << year << endl << endl;
            }
            else if (
    month ==3)
            {
                
    cout << "March " << year << endl << endl;
            }
            else if (
    month ==4)
            {
                
    cout << "April " << year << endl << endl;
            }
            else if (
    month ==5)
            {
                
    cout << "May " << year << endl << endl;
            }
            else if (
    month ==6)
            {
                
    cout << "June " << year << endl << endl;
            }
            else if (
    month ==7)
            {
                
    cout << "July " << year << endl << endl;
            }
            else if (
    month ==8)
            {
                
    cout << "August " << year << endl << endl;
            }
            else if (
    month ==9)
            {
                
    cout << "September " << year << endl << endl;
            }
            else if (
    month ==10)
            {
                
    cout << "October " << year << endl << endl;
            }
            else if (
    month ==11)
            {
                
    cout << "November " << year << endl << endl;
            }
            else if (
    month ==12)
            {
                
    cout << "December " << year << endl << endl;
            }
            
    drawcalendar(daysfirstday);
        }

        do
        {
            
    cout << "Do you want to write to a file? (Y/N)" ;
            
    cin >> answer;
        } while (
    answer != 'y' && answer != 'Y' && answer != 'n' && answer != 'N');

        if (
    answer == 'y' || answer == 'Y')
        {
            
    writefile(monthyeardaysfirstdayyeartype);
            
    cout << endl << "A file 'CALENDAR.TXT' has been created which contains the calendar" << endl;
        }

        
    cout << endl << "Program written by Maharshi Akilla" << endl;
        
    cout << "http://mahurshi.tripod.com" << endl;
        while (!
    kbhit())
        {
        }



    The above code has a mixture of couts and printfs,
    (i was too lazy to use printfs all the time) you might
    want to edit that. It doesn't cause any harm anyway.
    You can modify this to fit your requirements.

    GOOD LUCK!
    Last edited by moonwalker; 07-22-2002 at 08:47 PM.

  3. #3
    Unregistered
    Guest
    Thank you my friend!!
    Your code was very helpfull for me!

  4. #4
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    you're welcome

    you're welcome

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yet another Calendar Program.
    By duffmckagan in forum C Programming
    Replies: 9
    Last Post: 07-14-2006, 10:29 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. calendar program
    By dharmastum in forum C Programming
    Replies: 3
    Last Post: 01-25-2002, 05:35 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM