# 日历制作

该程序有如下功能:

  • 输入年和月,输出日历。
  • 输入年月日,输出这是该年的第几天。
using System;

namespace MyBase
{
    public class calendar
    {
        int year, month;
        char[]  weekday=new char[7]{'日','一','二','三','四','五','六'};
        int dayofmonth(int year,int month)
        {
            if (month == 2)
            {
                if (isleapyear(year) == true) return 29; else return 28;
            }
            else
                if (month == 4 || month == 6 || month == 9 || month == 11)
                {
                    return 30;
                }else{
                    return 31;
                }
        }
        public static bool isleapyear(int year)
        {
            if(year%400 ==0 )return true;
            if (year % 4 == 0 && year % 100 != 0) return true;
            return false;
        }
        int calweekofday(int day)
        {
            DateTime timetemp = new DateTime(year, month, day);
            return (int)timetemp.DayOfWeek;
        }
        public calendar()
        {
            print("Input year:");
            year = int.Parse(read());
            print("Input month:");
            month = int.Parse(read());
            string keyinput;
            do
            {
                printcalendar();
                print("input q to quit!");
                keyinput = read();
                nextmonth();
            } while (keyinput != "q");

            computedayofyear();
        }
        void nextmonth()
        {
            if (month == 12) {
                year++;
                month = 1;
            }
            else
            {
                month++;
            }
        }
        void print(object mstr)
        {
            Console.WriteLine(mstr);
        }
        string read()
        {
            return Console.ReadLine();
        }
        //打印出日历来
        void printcalendar()
        {
            printyear();
            printweekday();
            printday3();
        }
        void printyear()
        {
            print(year+"年"+month+"月");
        }
        void printweekday()
        {
            string mix="";
            for (int i = 0; i < 7; i++)
                {
                    mix += weekday[i] + "\t";
                }
            print(mix);
        }
        void printday3()
        {
            string tempstr = "";
            int monthday = dayofmonth(year, month);
            int firstlinebegin = calweekofday(1);
            for (int i = 0; i < 7; i++)
            {
                if (i < firstlinebegin)
                    tempstr += "\t";
            }
            Console.Write(tempstr);
            for (int i = 1; i <= monthday; i++)
            {
                Console.Write(i+"\t");
                if (calweekofday(i) == 6) Console.WriteLine();
            }
            Console.WriteLine();
        }
        void printday2()
        {
            string tempstr = "";
            int monthday = dayofmonth(year, month);
            int pointpos = calweekofday(1);
            int pos = 1;
            do{

                if (pos < pointpos+1)
                {
                    tempstr += "\t";
                }
                else
                {
                    tempstr += (pos-pointpos) + "\t";
                }
                
                if (pos % 7 == 0)
                {
                    print(tempstr);
                    tempstr = "";
                }
                pos++;
            }while(pos<monthday+pointpos+1);
            print(tempstr);
        }
        void printday()
        {
            string tempstr="";
            int monthday = dayofmonth(year, month);
            int firstlinebegin = calweekofday(1);
            int firstlineday = 1;
           
            for (int i = 0; i < 7; i++)
            {
                if (i < firstlinebegin)
                    tempstr += "\t";
                else
                    tempstr += firstlineday++ + "\t";
            }
            print(tempstr);

            int tempfinal= (monthday - firstlineday) / 7;
            for (int i = 0; i <tempfinal; i++)
            {
                tempstr = "";
                for(int j=0;j<7;j++){
                    tempstr += firstlineday++ + "\t";
                }
                print(tempstr);
            }
           

            tempstr="";
            tempfinal = (monthday - firstlineday) % 7;
            for (int i = 0; i <= tempfinal ; i++)
            {
                tempstr += firstlineday++ + "\t";
            }
            print(tempstr);
        }
        //输入年月日,计算这是一年中的第几天
        public void computedayofyear()
        {
            int year, month, day;
            Console.WriteLine("input year:");
            year = int.Parse(Console.ReadLine());
            Console.WriteLine("input month:");
            month = int.Parse(Console.ReadLine());
            Console.WriteLine("input day:");
            day = int.Parse(Console.ReadLine());
            int[] dayofmonth = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            if (isleapyear(year) == true) dayofmonth[1] = 29;
            int tempday = 0;
            for (int i = 0; i < month - 1; i++)
            {
                tempday += dayofmonth[i];
            }
            tempday += day;
            Console.WriteLine("this day is in {0} day in this year", tempday);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190