Error in easter calendar algorithm

0

I was messing around with programs that calculate dates, but I found a very precise python algorithm that correctly returns the day that will fall and the Easter holiday has fallen.

The algorithm in python is this:

#!/usr/bin/env python
#coding: utf-8
#você pode modificar o codigo e distribuir a vontade, desde quee
#nao retire o nome do Autor
#Autor: Luis Eduardo Boiko Ferreira
#e-mail: [email protected]
import time
import datetime 
print ' _______________________________________'
print '|                                       |'
print '|                                       |'
print '|       Algoritmo para calcular         |'
print '|          o dia da Pascoa              |'
print '|          Desenvolvido por             |'
print '|      Luis Eduardo Boiko Ferreira      |'
print '|_______________________________________|'
anoatual = datetime.date.today().year 
print 'O ano atual é %s.' %anoatual
ano=input('Digite o ano desejado para calcularmos o dia da páscoa: ')
a=int(ano%19)
b=int(ano/100)
c=int(ano%100)
d=int(b/4)
e=int(b%4)
f=int((b+8)/25)
g=int((b-f+1)/3)
h=((19*a+b-d-g+15)%30)
i=int(c/4)
k=int(c%4)
L=((32+2*e+2*i-h-k)%7)
m=int((a+11*h+22*L)/451)
mes=int((h+L-7*m+114)/31)
if mes==1 : mes='Janeiro'
elif mes==2 : mes='Fevereiro'
elif mes==3 : mes='Março'
elif mes==4 : mes='Abril'
elif mes==5 : mes='Maio'
elif mes==6 : mes='Junho'
elif mes==7 : mes='Julho'
elif mes ==8 : mes ='Agosto'
elif mes ==9 : mes ='Setembro'
elif mes ==10 : mes ='Outubro'
elif mes ==11 : mes ='Novembro'
else : mes ='Dezembro'
mes1=mes
dia=(((h+L-7*m+114)%31)+1)
if anoatual>ano :
  print "A pascoa caiu no dia: %s."% dia 
  print "Do mês: %s" %mes1

else :
   print "A pascoa ira cair no dia: %s."% dia 
   print "Do mês: %s" %mes1

I've been converting it to c ++ but it's giving me an error.

  

pascoa.cxx: 79: 18: error: lvalue required as left operand of assignment

My converted algorithm is this:

#include <iostream>
//http://www.webcid.com.br/calendario/2018/brasil

int main()
{

time_t now = time(0);
tm *ltm = localtime(&now);

int anoatual = ltm->tm_year+1900;


printf("O ano atual é %4d.", anoatual);

int ano;

printf("Digite o ano desejado para calcularmos o dia da páscoa: ");
scanf("%d", &ano);

int a=(ano%19);
int b=int(ano/100);
int c=(ano%100);
int d=int(b/4);
int e=(b%4);
int f=int((b+8)/25);
int g=int((b-f+1)/3);
int h=((19*a+b-d-g+15)%30);
int i=int(c/4);
int k=(c%4);

int L=((32+2*e+2*i-h-k)%7);
int m=int((a+11*h+22*L)/451);
int mes=int((h+L-7*m+114)/31);

if (mes == 1){
  (char)mes='Janeiro';
}
else 
if(mes == 2){
  (char)mes='Fevereiro';
}
else
if(mes == 3){
  (char)mes='Março';
}
else
if(mes == 4){
  (char)mes='Abril';
}
else
if(mes == 5){
    (char)mes='Maio';
}
else
if(mes == 6){
  (char)mes='Junho';
}
else
if(mes == 7){
  (char)mes='Julho';
}
else
if(mes == 8){
  (char)mes ='Agosto';
}
else
if(mes == 9){
  (char)mes ='Setembro';
}
else
if(mes == 10){
  (char)mes ='Outubro';
}
else
if(mes == 11){
  (char)mes ='Novembro';
}
else
if(mes ==12){
(char)mes ='Dezembro';
}

char mes1=(char)mes;

int dia=((h+L-7*m+114)%31)+1;

if (anoatual>ano)
{
  printf("A pascoa caiu no dia: %s.",&dia); 
  printf("Do mês: %s", &mes1);
}
else
if (anoatual<ano)
{
  printf("A pascoa ira cair no dia: %s.",&dia); 
  printf("Do mês: %s", &mes1);
}
 return 0;
}

therefore according to this site: link

The algorithm in python is calculating precisely the days what is the error?

    
asked by anonymous 13.08.2017 / 01:50

1 answer

3

There are a few points in your converted code that you need to fix:

  • mes='Janeiro'

    The string delimiter is the double quotation mark " and therefore its assignment for the months is not correct and should be mes="Janeiro"; . The single quotation mark ' sometimes called a spelling is used for type char of only one character.

    In addition, you are assigning the value to a variable of integer type:

    int mes=...;
    ...
    mes='Janeiro';    
    

    Soon it would never work. Remember that in C a variable is defined with a type and can not change unlike python.

  • printf("A pascoa caiu no dia: %s.",&dia); The printf does not carry & which is the operator to get the memory address of the variable. Only the scanf and if the variable is no longer a pointer.

In C or C ++ you have a structure that you can use differently for many chained ifs which is the switch that allows you to further organize the code. Combining this with the correction of errors could look like this:

//resto para cima igual
int mes=int((h+L-7*m+114)/31); //esta ainda igual também

char* mes1 = "Mês inválido"; //aqui nova variável para o texto do mes

switch (mes){ //agora com switch em vez de ifs
    case 1: mes1 = "Janeiro"; break;
    case 2: mes1 = "Fevereiro"; break;
    case 3: mes1 = "Março"; break;
    case 4: mes1 = "Abril"; break;
    case 5: mes1 = "Maio"; break;
    case 6: mes1 = "Junho"; break;
    case 7: mes1 = "Julho"; break;
    case 8: mes1 = "Agosto"; break;
    case 9: mes1 = "Setembro"; break;
    case 10: mes1 = "Outubro"; break;
    case 11: mes1 = "Novembro"; break;
    case 12: mes1 = "Dezembro"; break;
}

int dia=((h+L-7*m+114)%31)+1;

if (anoatual>ano) { //printfs agora sem &
    printf("A pascoa caiu no dia: %d.",dia);
    printf("Do mês: %s", mes1);
}
else if (anoatual<ano) { //printfs agora sem &
    printf("A pascoa ira cair no dia: %d.",dia);
    printf("Do mês: %s", mes1);
}

Notice that each case of switch , which corresponds to each of the if s that it had, needs to have break to be correct.

Note also that the type used to represent a text was char* which may be char[] depending on how it is used, and which represents an array of characters. You can also use the type string which in most cases simplifies, being necessary to include the corresponding library, which for C ++ would be <cstring> .

    
13.08.2017 / 03:12