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?
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
@dark777 Not so much. And the conversion the
(char)
has to come right like thisminhavar = (char)"message"
but it won’t work either because"message"
is a pointer and not achar
, 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.– Isac
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
@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 achar*
, and the guy you’re converting is aint
and not achar*
as in your case.– Isac
then as I understood it could only be done with a single character...
– dark777
@dark777 Yes conversions work between specific types and for
char
only for a single character. Examples of conversions like thisint x = (int)'A';
in whichx
would be 65. Orchar c = (char)65;
in whichc
got into wordsA
– Isac
legal thanks for the explanations..
– dark777