Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Задача 2
- ――――――――
- Съставете програма mcal, която по зададени на командния ред две цели числа – месец (1÷12)
- и ден от седмицата, в който започва годината (1 за понеделник, 2 за вторник и т.н.)
- извежда на стандартния изход календар за посочения месец във вида, посочен в примера
- по-долу. Ако годината е високосна, на командния ред се добавя още и знакът + като отделен
- аргумент. Например, като повикаме програмата с mcal 9 5, тя трябва да изведе
- Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
- 1 2 3 4 5 1 2 3 4
- 6 7 8 9 10 11 12 5 6 7 8 9 10 11
- 13 14 15 16 17 18 19, а повикана с mcal 9 5 +: 12 13 14 15 16 17 18
- 20 21 22 23 24 25 26 19 20 21 22 23 24 25
- 27 28 29 30 26 27 28 29 30 .
- */
- #include <cstring>
- #include <stdio.h>
- void mcalc(int keyMonth, int firstDayIndex, char isLeap = ' ')
- {
- int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int indexKeyMonth = firstDayIndex;
- if (isLeap == '+') // Feb's days are 29 if the year is leap
- days[1] = 29;
- int daysToKeyMonth = 0;
- for (int i = 0; i < keyMonth - 1; i++)
- {
- daysToKeyMonth += days[i];
- }
- // find which day of the week is the first day of the set month 1 - for Mon and etc
- while (daysToKeyMonth--)
- {
- firstDayIndex++;
- if (firstDayIndex > 7)
- firstDayIndex = 1;
- }
- printf("\n Mo Tu We Th Fr Sa Su \n");
- int spacing = firstDayIndex * 2 + 2; // proper amount of spaces for the first days
- if (isLeap == '+')
- spacing++; // formating fix
- printf(" %*d", spacing, 1);
- int currentIndex = firstDayIndex;
- int spacing2Digits = 2;
- // print the days
- for (int i = 2; i < days[keyMonth]; i++)
- {
- currentIndex++;
- if (currentIndex == 8)
- {
- printf("\n");
- currentIndex = 0;
- printf(" %2d", i);
- currentIndex++;
- }
- else
- printf(" %2d", i);
- }
- }
- int main()
- {
- char str[10];
- int i;
- int month, day;
- char leapSign = ' ';
- fgets(str, 10, stdin);
- // remove newline if present
- i = strlen(str) - 1;
- if (str[i] == '\n')
- str[i] = '\0';
- // optional agrument '+' for the sscanf
- if (strstr(str, "+"))
- sscanf_s(str, "%d %d %c", &month, &day, &leapSign);
- else
- sscanf_s(str, "%d %d", &month, &day);
- mcalc(month, day, leapSign);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement