Custom Search

Calender Program In C#


Beginning of Calendar.cs

 This program prints out the calendar for a full year, specified by the user.


using System;

class Calendar
{
const int Sunday    = 0;
const int Monday    = 1;
const int Tuesday   = 2;
const int Wednesday = 3;
const int Thursday  = 4;
const int Friday    = 5;
const int Saturday  = 6;

static void Main (string[] args)
{
    int year;

    GiveInstructions();
    year = GetYearFromUser();
    PrintCalendar(year);
}

static void GiveInstructions ()
{
    Console.WriteLine("This program displays a calendar for a full");
    Console.WriteLine("year.  The year must not be before 1900.");

}
static int GetYearFromUser ()
{
    int year = 1900;

    while (true)
    {
        Console.Write("Which year? ");
        year = Int32.Parse(Console.ReadLine());
        if (year >= 1900) return year;
        Console.WriteLine("The year must be at least 1900.");
    };
}

static void PrintCalendar(int year)
{
    int month;

    for (month = 1; month <= 12; month++) 
    {
        PrintCalendarMonth(month, year);
        Console.WriteLine();
    }
}

static void PrintCalendarMonth(int month, int year)
{
    int weekday, nDays, day;

    Console.WriteLine("    " + (MonthName(month)) + " " + year);
    Console.WriteLine(" Su Mo Tu We Th Fr Sa");
    nDays = MonthDays(month, year);
    weekday = FirstDayOfMonth(month, year);
    IndentFirstLine(weekday);
    for (day = 1; day <= nDays; day++)
    {
       if (day > 9) Console.Write(" " + day);
       else Console.Write("  " + day);
       if (weekday == Saturday) Console.WriteLine();
       weekday = (weekday + 1) % 7;
    }
    if (weekday != Sunday) Console.WriteLine();
}

static void IndentFirstLine(int weekday) 
{
    int i;

    for (i = 0; i < weekday; i++) 
    {
        Console.Write("   ");
    }
}      


static int MonthDays(int month, int year)
 {

    switch (month) 
    {
      case 2:
        if (IsLeapYear(year)) return (29);
        return (28);
      case 4: case 6: case 9: case 11:
        return (30);
      default:
        return (31);
    }
}

static int FirstDayOfMonth(int month, int year)
{
    int weekday, i;

    weekday = Monday;
    for (i = 1900; i < year; i++) 
    {
        weekday = (weekday + 365) % 7;
        if (IsLeapYear(i)) weekday = (weekday + 1) % 7;
    }
    for (i = 1; i < month; i++)
    {
        weekday = (weekday + MonthDays(i, year)) % 7;
    }
    return (weekday);
}

static string MonthName(int month)
{
    switch (month)
   {
      case  1: return ("January");
      case  2: return ("February");
      case  3: return ("March");
      case  4: return ("April");
      case  5: return ("May");
      case  6: return ("June");
      case  7: return ("July");
      case  8: return ("August");
      case  9: return ("September");
      case 10: return ("October");
      case 11: return ("November");
      case 12: return ("December");
      default: return ("Illegal month");
    }
}

static bool IsLeapYear(int year)
{

    return ( ((year % 4 == 0) && (year % 100 != 0))
             || (year % 400 == 0) );


0 comments:

Post a Comment

Back to TOP