Create an algorithm to identify days of weeks in c ++ [closed]

-3

How to create an algorithm to identify day of the week (numbered 1 to 7) and weekday, weekend, or an invalid day?

    
asked by anonymous 03.03.2017 / 08:26

2 answers

3

A simple solution using switch

int main() {
    int dia;

    printf("Entre com um numero: ");
    scanf("%d", &dia);

    system("cls");

    switch(dia) {
        case 1: printf("Segunda"); break;
        case 2: printf("Terça"); break;
        case 3: printf("Quarta"); break;
        case 4: printf("Quinta"); break;
        case 5: printf("Sexta"); break;
        case 6: printf("Sábado"); break;
        case 7: printf("Domingo"); break;
        default: printf("Numero invalido");
    }
}
    
03.03.2017 / 12:22
1

If you want to get the day of the week through a 05/03/2017 date you will have to do the year, month and day calculations.

See the algorithm I have adapted to return the names in Portuguese:

#include <stdio.h>

const char *wd(int year, int month, int day)
{
  static const char *weekdayname[] =
  {
    "Segunda",
    "Terça",
    "Quarta",
    "Quinta",
    "Sexta",
    "Sábado",
    "Domingo"
  };

  size_t JND =                                                     \
          day                                                      \
        + ((153 * (month + 12 * ((14 - month) / 12) - 3) + 2) / 5) \
        + (365 * (year + 4800 - ((14 - month) / 12)))              \
        + ((year + 4800 - ((14 - month) / 12)) / 4)                \
        - ((year + 4800 - ((14 - month) / 12)) / 100)              \
        + ((year + 4800 - ((14 - month) / 12)) / 400)              \
        - 32045;

  return weekdayname[JND % 7];
}

int main(void)
{
    printf("%d/%02d/%02d: %s", 2017, 3, 5, wd(2017, 3, 5));

    return 0;
}

Output

  

2017/03/05: Sunday

Source: link

    
05.03.2017 / 16:56