c++ calendar

I am working on a calendar project. Since this source code is extremely long, I was wondering if anybody can modify of simply this code so that it is short..


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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <stdio.h>


void getweek (int x)
{int year, month, day, weekday;
     int D, C, F, i, j;
     month = x/1000000;
     day = x%1000000/10000;
     year = x%10000;
     
     switch (month)
     {case 01: if ((day>=1) && (day <=31))	
{print("\nJanuary"); break;
}
case 02: if ((day >= 1) && (day <=28))	
{print("\nFebruary"); break;
}
case 03: if ((day >= 1) && (day <=31))	
{print("\nMarch"); break;
}
case 04: if ((day >= 1) && (day <= 30))	
{print("\nApril"); break;
}
case 05: if ((day >= 1) && (day <= 31))	
{print("\nMay"); break;
}
case 06: if ((day >= 1) && (day <=30))	
{print("\nJune"); break;
}
case 07: if ((day >= 1) && (day <= 31))	
{print("\nJuly"); break;
}
case 08: if ((day >=1) && (day <= 31))	
{print("\nAugust"); break;
}
case 09: if ((day >= 1) && (day <= 30))	
{print("\nSeptember"); break;
}
case 10: if ((day >=1) && (day <= 31))	
{print("\nOctober"); break;
}
case 11: if (( day >=1) && (day <= 30)) 
{print("\nNovember"); break;
}
case 12: if (( day >=1) && (day <=31)) 
{print("\nDecember"); break;
}
default : printf("invalid"); break;
}
      
      printf("%d\n", year); 
      printf("M  T  W  H  F  S  S\n");
      D = year%100;
      C = year/100;
      switch (month)
      { case 1: month = 11; break;
        case 2: month = 12; break;
        case 3: month = 1; break;
        case 4: month = 2; break;
        case 5: month = 3; break;
        case 6: month = 4; break;
        case 7: month = 5; break;
        case 8: month = 6; break; 
        case 9: month = 7; break; 
        case 10: month = 8; break;
        case 11: month = 9; break;
        case 12: month = 10; break;
        }
      if (month == 11 || month ==12)
      D = D-1;
      F = day + ((13*month-1)/5) + D + (D/4) + (C/4) - 2*C;
      weekday = F%7;
      if (weekday <0)
      weekday += 7;
      
      if (weekday == 0)
        { day = day - 6;
        if (day<=0 && (month==11||month==12||month==2||month==4||month==6||month==7||month==9))
            {day=day+31;
            for (i=day; i<=31; i++)
            printf("%d", i);
            day =1;
            if (month == 11 || month ==12)
            D = D-1;
             F = day + ((13*month-1)/5) + D + (D/4) + (C/4) - 2*C;
             weekday = F%7;
             if (weekday <0)
             weekday += 7;
             switch(weekday)
             {case 0: weekday = 7;break;}
            for (i=day; i<=8-weekday; i++)
            printf("%d ", i);
            printf("\n");}
        else if (day<=0 && (month==3||month==5||month==8||month==10))
             {day=day+30;
             for (i=day; i<=30; i++)
             printf("%d ", i);
             day =1; 
             F = day + ((13*month-1)/5) + D + (D/4) + (C/4) - 2*C;
             weekday = F%7;
             if (weekday <0)
             weekday += 7;
             for (i=day; i<=8-weekday; i++)  
            printf("%d ", i);
            printf("\n");}
         else if (day <=0 && month == 1 && year%4==0)
              {day=day+29;
             for (i=day; i<=29; i++)
             printf("%d ", i);
             day =1; 
             F = day + ((13*month-1)/5) + D + (D/4) + (C/4) - 2*C;
             weekday = F%7;
             if (weekday <0)
             weekday += 7;
             for (i=day; i<=8-weekday; i++)  
            printf("%d ", i);
            printf("\n");}
         else if (day<=0 && month == 1 && year%4!=0)
         {day=day+28;
             for (i=day; i<=28; i++)
             printf("%d ", i);
             day =1; 
             F = day + ((13*month-1)/5) + D + (D/4) + (C/4) - 2*C;
             weekday = F%7;
             if (weekday <0)
             weekday += 7;
             for (i=day; i<=8-weekday; i++)  
            printf("%d ", i);
            printf("\n");} 
        else if (day>0)
        {for (i=day; i<=day+6; i++)
        if(i>0 && i<10)
          printf("%d  ", i);  
          else printf("%d ", i);
          printf("\n");
          }}
      else if (weekday == 1)
        { for (i=day; i<=day+6; i++)
        if(i<10)
          printf("%d  ", i); 
        else printf ("%d ", i);
          printf("\n");
          }         
      else if (weekday == 2)
        { day = day - 1;
        if (day==0 && (month==11||month==12||month==2||month==4||month==6||month==7||month==9))
            {day=day+31;
            for (i=day; i<=31; i++)
            printf("%d ", i);
            day =1;
            for (i=day; i<=day+5; i++)
            printf("%d  ", i); 
            printf("\n");}
        else if (day==0 && month == 1 && year%4==0)
         {day=day+29;
             for (i=day; i<=29; i++)
             printf("%d ", i);
             day =1;
             for (i=day; i<=day+5; i++)
             printf("%d  ", i); 
             printf("\n");}
        else if (day==0 && month == 1 && year%4!=0)
         {day=day+28;
             for (i=day; i<=28; i++)
             printf("%d ", i);
             day=1;
             for (i=day; i<=day+5; i++)
             printf("%d  ", i); 
             printf("\n");} 
         else if (day==0 && (month==3||month==5||month==8||month==10))
             {day=day+30;
             for (i=day; i<=30; i++)
             printf("%d ", i);
             day=1;
             for (i=day; i<=day+5; i++)
             printf("%d  ", i); 
             printf("\n");}
         else if( day>0)
         for (i=day; i<=day+6; i++)
          if(i<10)
          printf("%d  ", i); 
          else printf("%d ", i);
          printf("\n");
          }
      else if (weekday == 3)
        { day = day - 2;
        if (day<=0 && (month==11||month==12||month==2||month==4||month==6||month==7||month==9))
            {day=day+31;
            for (i=day; i<=31; i++)
            printf("%d ", i);
            day =1;
            if (month == 11 || month ==12)
            D = D-1;
             F = day + ((13*month-1)/5) + D + (D/4) + (C/4) - 2*C;
             weekday = F%7;
             if (weekday <0)
             weekday += 7;
             switch(weekday)
             {case 0: weekday = 7;break;}
            for (i=day; i<=8-weekday; i++)
            printf("%d  ", i); 
            printf("\n");}
        else if (day<=0 && (month==3||month==5||month==8||month==10))
            {day=day+30;
            for (i=day; i<=30; i++)
            printf("%d ", i);
            day =1;
             F = day + ((13*month-1)/5) + D + (D/4) + (C/4) - 2*C;
             weekday = F%7;
             if (weekday <0)
             weekday += 7;
             switch(weekday)
             {case 0: weekday = 7;break;}
            for (i=day; i<=8-weekday; i++)
            printf("%d  ", i); 
            printf("\n");}
      else if (day<=0 && month == 1 && year%4!=0)
         {day=day+28;
             for (i=day; i<=28; i++)
             printf("%d ", i);
             day =1; 
             F = day + ((13*month-1)/5) + D + (D/4) + (C/4) - 2*C;
             weekday = F%7;
             if (weekday <0)
             weekday += 7;
             for (i=day; i<=8-weekday; i++)  
            printf("%d ", i);
            printf("\n");} 
      else if (day<=0 && month == 1 && year%4==0)
         {day=day+29;
             for (i=day; i<=29; i++)
             printf("%d ", i);
             day =1; 
             F = day + ((13*month-1)/5) + D + (D/4) + (C/4) - 2*C;
             weekday = F%7;
             if (weekday <0)
             weekday += 7;
             for (i=day; i<=8-weekday; i++)  
            printf("%d ", i);
            printf("\n");} 
        else if (day>0)
        for (i=day; i<=day+6; i++)
          if(i<10)
          printf("%d  ", i); 
          else printf("%d ", i);
          printf("\n");
          }


Last edited on
Please edit your post and put it all between [code]code tags[/code]. This is unreadable for the human eye - http://www.cplusplus.com/articles/jEywvCM9/
I only looked at your first function since it's so hard to read without being in [code][/code] tags.

I don't know whether or not these calculations are correct:

1
2
3
month = x/1000000;
day = x%1000000/10000;
year = x%10000;

But let's assume that they are. Let's look at the switch statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch (month)
{
    case 1: if ((day>=1) && (day <=31))	
            {
                print("\nJanuary");
                break;
            }
    //...
    case 12: if (( day >=1) && (day <=31)) 
            {
                print("\nDecember");
                break;
            }
    default: printf("invalid"); break;
}

It will only print invalid if month is wrong, but not when you have day incorrect. Maybe you hope that it'll filter down to the default case but what would happen if month was 2 and day was 30?

To shorten the code, you can use arrays:

1
2
3
4
5
6
7
8
9
char *months[] = { "January", "February", "March", "April", "May",
                   "June", "July", "August", "September", "October",
                   "November", "December" };
unsigned int maxDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

if (month < 1 || month > 12 || day < 1 || day > maxDays[month - 1])
    printf("invalid");
else
    printf("\n%s", months[month - 1]);


To shorten this switch statement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (month)
{ case 1: month = 11; break;
case 2: month = 12; break;
case 3: month = 1; break;
case 4: month = 2; break;
case 5: month = 3; break;
case 6: month = 4; break;
case 7: month = 5; break;
case 8: month = 6; break; 
case 9: month = 7; break; 
case 10: month = 8; break;
case 11: month = 9; break;
case 12: month = 10; break;
}


Just use month = (month > 2 ? month - 2 : month + 10);.
Last edited on
I have trouble executing this source code I did.
So, when the user enters V or v, a menu allows the user to choose which calendar view will be displayed: either day, week, or month.

When the user selects D or d, the user is asked to input a date value. The input
of the user for the date value is a single integer only. Assume that the format is
mmddyyyy.

The program ends if and only if the user selected the E or e option from the main
menu.

Assume the calendar is valid starting from January 1, 1900. Dates before that
considered invalid and the application will not generate calendars for them.

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
  int main ()
{
    int x, y, year, day, month;
    char v, V, e, E, d, D;
    
    year=x%10000;
    day = x%1000000/10000;
	month = x/1000000;
    
    if (year < 1900 && year > 0)
    printf("Invalid!\n");
    else
{   printf("  Calender Menu\n\n");
    printf("Display Calendar\n");
    printf("   View Format\n");
    printf("    Exit\n\n");
    printf("Enter Your Choice: ");
}

     if (y == 'd' || y =='D')
     {printf("Enter a date specified: ");
     scanf("%c", &y);}
     if (y == 'V' || y == 'v')
     {printf("Enter a date, week, or month: ");
     scanf("%c%c%c", &month, &day, &year);}
     if (y == 'E' || y == 'e')
     
     return 0;
}   
1
2
	int x, y, year, day, month;
	char v, V, e, E, d, D;


You have to initialize your variable.

When you do this

1
2
3
year=x%10000;
day = x%1000000/10000;
month = x/1000000;


"x" doesnt have a value, So how can you do these calculations?

if (y == 'd' || y =='D')

Why would "y" be equal to 'd'? You never give y a value to begin with, and you never change the value. Same with all the other if statements.
Topic archived. No new replies allowed.