Advertisement
193030

Untitled

Nov 5th, 2021
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. /*
  2. Задача 2
  3. ――――――――
  4. Съставете програма mcal, която по зададени на командния ред две цели числа – месец (1÷12)
  5. и ден от седмицата, в който започва годината (1 за понеделник, 2 за вторник и т.н.)
  6. извежда на стандартния изход календар за посочения месец във вида, посочен в примера
  7. по-долу. Ако годината е високосна, на командния ред се добавя още и знакът + като отделен
  8. аргумент. Например, като повикаме програмата с mcal 9 5, тя трябва да изведе
  9.  
  10.        Mo Tu We Th Fr Sa Su                               Mo Tu We Th Fr Sa Su
  11.              
  12.               1  2  3  4  5                                         1  2  3  4
  13.         6  7  8  9 10 11 12                                5  6  7  8  9 10 11
  14.        13 14 15 16 17 18 19,   а повикана с mcal 9 5 +:   12 13 14 15 16 17 18
  15.        20 21 22 23 24 25 26                               19 20 21 22 23 24 25
  16.        27 28 29 30                                        26 27 28 29 30      .
  17. */
  18.  
  19. #include <cstring>
  20. #include <stdio.h>
  21.  
  22. void mcalc(int keyMonth, int firstDayIndex, char isLeap = ' ')
  23. {
  24.     int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  25.     int indexKeyMonth = firstDayIndex;
  26.     if (isLeap == '+') // Feb's days are 29 if the year is leap
  27.         days[1] = 29;
  28.  
  29.     int daysToKeyMonth = 0;
  30.     for (int i = 0; i < keyMonth - 1; i++)
  31.     {
  32.         daysToKeyMonth += days[i];
  33.     }
  34.  
  35.     // find which day of the week is the first day of the set month 1 - for Mon and etc
  36.     while (daysToKeyMonth--)
  37.     {
  38.         firstDayIndex++;
  39.         if (firstDayIndex > 7)
  40.             firstDayIndex = 1;
  41.     }
  42.     printf("\n Mo Tu We Th Fr Sa Su \n");
  43.     int spacing = firstDayIndex * 2 + 2; // proper amount of spaces for the first days
  44.     if (isLeap == '+')
  45.         spacing++; // formating fix
  46.    
  47.     printf(" %*d", spacing, 1);
  48.     int currentIndex = firstDayIndex;
  49.     int spacing2Digits = 2;
  50.  
  51.     // print the days
  52.     for (int i = 2; i < days[keyMonth]; i++)
  53.     {
  54.         currentIndex++;
  55.         if (currentIndex == 8)
  56.         {
  57.             printf("\n");
  58.             currentIndex = 0;
  59.             printf(" %2d", i);
  60.             currentIndex++;
  61.         }
  62.         else
  63.             printf(" %2d", i);
  64.     }
  65. }
  66.  
  67. int main()
  68. {
  69.     char str[10];
  70.     int i;
  71.     int month, day;
  72.     char leapSign = ' ';
  73.  
  74.     fgets(str, 10, stdin);
  75.  
  76.     // remove newline if present
  77.     i = strlen(str) - 1;
  78.     if (str[i] == '\n')
  79.  
  80.         str[i] = '\0';
  81.  
  82.     // optional agrument '+' for the sscanf
  83.     if (strstr(str, "+"))
  84.         sscanf_s(str, "%d %d %c", &month, &day, &leapSign);
  85.     else
  86.         sscanf_s(str, "%d %d", &month, &day);
  87.    
  88.     mcalc(month, day, leapSign);
  89. }
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement