calendar printing with wrong days

-1

Good morning guys, I made an announcement that generates a calendar for a given year but for years bisextos it is returning the days correctly but for the years not bisextos it after the dates for different days, I apriveitei the function calendar to generate the same for a specific month through the choice of the user and also it is not working what I Did I do it wrong?

#include <iostream>

// pega primeiro dia do mes
int primeiro_dia(int mes, int ano)
{
 int a = ((14 - mes)/12);
 int y = (ano-a);
 int m = ((mes+(12*a))-2);
 int dia = 1;

 return dia = ((dia+y+(y/4)-(y/100)+(y/400)+((31*m)/12))%7);
}

//imprime calendario de todos os meses do ano
void calendario(int ano)
{
 int month_days[12] = {31, 28, 31, 30, 31, 30, 31 ,31 ,30, 31, 30, 31};

 month_days[1] = (ano%4 == 0 || ano%400 == 0 && ano%100 != 0) ? 29 : 28;

 const char *months[12]={ "January","February","March","April","May","June","July","August","September","October","November","December" };

 std::cout<<"\n";

 for(int mes = 1; mes <=12; mes++)
 {
  std::cout<<"Month: "<<months[mes-1]<<"-"<<ano<<"\nSun Mon Tue Wed Thu Fri Sat\n";

  int primeiro = primeiro_dia(mes, ano); //pega primeiro dia do mes

  for(int i=0; i < primeiro; i++)
  std::cout<<"    ";

  for(int dia = 1; dia <= month_days[mes-1]; dia++)
  {
   printf("%3d ",dia);
   primeiro++;
   if(primeiro%7 == 0)std::cout<<"\n";
  }
  std::cout<<"\n\n";
 }
}

// imprime calendario de mes especifico
void mes_calendario(int mes, int ano)
{
 int month_days[12] = {31, 28, 31, 30, 31, 30, 31 ,31 ,30, 31, 30, 31};

 month_days[1] = (ano%4 == 0 || ano%400 == 0 && ano%100 != 0) ? 29 : 28;

 const char *months[12]={ "January","February","March","April","May","June","July","August","September","October","November","December" };

 std::cout<<"\n";

  std::cout<<"Month: "<<months[mes-1]<<"-"<<ano<<"\nSun Mon Tue Wed Thu Fri Sat\n";

  int primeiro = primeiro_dia(mes, ano); //pega primeiro dia do mes

  for(int i=0; i < primeiro; i++)
  std::cout<<"    ";

  for(int dia = 1; dia <= month_days[mes-1]; dia++)
  {
   printf("%3d ",dia);
   primeiro++;
   if(primeiro%7 == 0)std::cout<<"\n";
  }
  std::cout<<"\n\n";
}

int main()
{
 int ano;
 std::cout<<"Digite o ano: ";
 std::cin>>ano;
 calendario(ano);
 return 0;
}
    
asked by anonymous 13.03.2018 / 07:53

1 answer

1

This test of yours:

month_days[1] = (ano%4 == 0 || ano%400 == 0 && ano%100 != 0) ? 29 : 28;

is wrong. The correct one is:

month_days[1] = (((ano % 4 == 0) && (ano % 100 != 0)) || (ano % 400 == 0)) ? 29 : 28;
    
13.03.2018 / 14:42