Error in Pascoa calendar algorithm

Asked

Viewed 385 times

1

I was playing with programs that calculate dates, but I found a very precise python algorithm that returns me correctly the day that will fall and that already fell the holiday of Paris.

The python algorithm 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 went to convert the same to c++ but it’s giving me 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 website: http://www.webcid.com.br/calendario/2018/brasil

the python algorithm is precisely calculating the days which is the error?

1 answer

2


There are some points in your converted code that need to fix:

  • mes='Janeiro'

    String delimiter are double quotes " and so your assignment of the months is not correct and should be mes="Janeiro";. The single quote ' sometimes call of plica is used for the type char one-character.

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

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

    Soon it would never work. Remember that in C a variable is set with a type and cannot change unlike python.

  • printf("A pascoa caiu no dia: %s.",&dia); The printf does not lead & 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++ it has a structure that can use different for many linked ifs that is the switch which allows you to further organize the code. Combining this with correcting 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);
}

Note that each case of switch, which corresponds to each of the ifI needed to have the break to be correct.

Note also that the type used to represent a text was a char* which may be a 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, with the need to include the corresponding library, which for C++ would be <cstring>.

  • so I had even tried to use double quotes only changed, char* mes1 = "Invalid month"; for char* mes1 = "Invalid month"; but I found that even the variable I feel an entire variable I thought if I used (char)minhavar="message"; it should return the string on it shouldn’t it? regarding the & printf was careless of me and carelessness after I posted that I went to see..

  • @dark777 Not so much. And the conversion the (char) has to come right like this minhavar = (char)"message" but it won’t work either because "message" is a pointer and not a char, I mean, it’s a little more complicated. Transitioning from python or any other language to C will be more complicated mainly due to pointers and memory. And so my final recommendation is that you study that part in particular.

  • so I did this not because I wanted to imitate the python conversation in c++, it turns out that one day I saw a program that had a function that received an integer but gave char return by holding the whole variable and did: Return (char)variable; and variable was whole...

  • @dark777 Gives perfectly to do, and converts a number to the corresponding character using the ascii table. I not long ago answered a question using this. But it should be noted that it is convert to a char and not a char*, and the guy you’re converting is a int and not a char* as in your case.

  • then as I understood it could only be done with a single character...

  • @dark777 Yes conversions work between specific types and for char only for a single character. Examples of conversions like this int x = (int)'A'; in which x would be 65. Or char c = (char)65; in which c got into words A

  • legal thanks for the explanations..

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.