Palendromic Numbers

jnawrocki 0 Tallied Votes 245 Views Share

Programming just for fun.

// Given a palendromic start date 2001/10/02 
// calculate the next 10 future date palendromes.

#include "stdafx.h" 
#include<conio.h>

#define STRINGLENGTH 19
 
long jd(int y, int m, int d);
void gd (long JD, int *y, int *m, int *d);
int IsPallendrome (char Date[]);

void main() 
{

	int Year, Month, Day, count;
	long Julian;

	Year = 2001;
	Month = 10;
	Day = 02;

	char d[20];
	count = 1;
	Julian = jd(Year, Month, Day); // Seed the start date with the Julian date
	for(;;)
	{
		gd(Julian,&Year, &Month, &Day);  // convert Julian to Gregorian
		sprintf_s(d,"%4d%02d%02d\n", Year, Month, Day); // make date string
		if(IsPallendrome(d))		// test for palendromw
		{
			printf(" %d: %s is a Palendrome\n",count,d); // yay we found one
			count += 1;
			if (count > 10) break; // found enough?
		}
		Julian += 1;  // bump julian and look more
	}
	_getch();
}

//Gregorian to Julian
 long jd(int y, int m, int d)
 {
    y+=8000;
    if(m<3) { y--; m+=12; }
    return (y*365) +(y/4) -(y/100) +(y/400) -1200820
    +(m*153+3)/5-92
    +d-1;
 }

// Julian to Gregorian
void gd (long JD, int *y, int *m, int *d)
{
    int I,J,K,L,N;

    L  = JD + 68569;
    N  = 4*L/146097;
    L  = L-(146097*N+3)/4;
    I  = 4000*(L+1)/1461001;
    L  = L-1461*I/4+31;
    J  = 80*L/2447;
    K  = L-2447*J/80;
    L  = J/11;
    J  = J+2-12*L;
    I  = 100*(N-49)+I+L;
    *y = I;
    *m = J;
    *d = K;
}
   
// is it a palendrome
int IsPallendrome (char Date[]) 
{
	int y = 7;
	for (int x = 0; x < 4; ++x, --y)
	{
		if (Date[x] != Date[y])
			return(false);
	}
	return(true);
}
WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

Code Snippets should not use anything in conio.h. They should be written so anyone can comple the program.

jnawrocki -3 Light Poster

Remove conio.h and _getch and it should compile for you.

deceptikon 1,790 Code Sniper Team Colleague Featured Poster

Remove conio.h and _getch and it should compile for you.

If you can remove parts of a program without affecting it's functionality or usefulness, were those parts really needed in the first place? ;)

But no, it won't compile for anyone who's not using Visual Studio. Even then, Visual Studio users would need to have precompiled headers enabled for the project (note that I always disable them). Another compiler-specific part of the code is sprintf_s(), which is part of Microsoft's "safe" strong library. Your code is closely tied to your compiler, but it's also easily made more portable:

/* Given a palendromic start date 2001/10/02 
   calculate the next 10 future date palendromes. */

#include <stdio.h>

#define STRINGLENGTH 19

long jd(int y, int m, int d);
void gd (long JD, int *y, int *m, int *d);
int IsPallendrome (char Date[]);

int main(void) 
{
    int Year, Month, Day, count;
    long Julian;
    char d[20];

    Year = 2001;
    Month = 10;
    Day = 02;

    count = 1;
    Julian = jd(Year, Month, Day); /* Seed the start date with the Julian date */
    for(;;)
    {
        gd(Julian,&Year, &Month, &Day);  /* convert Julian to Gregorian */
        sprintf(d,"%4d%02d%02d\n", Year, Month, Day); /* make date string */
        if(IsPallendrome(d))        /* test for palendromw */
        {
            printf(" %d: %s is a Palendrome\n",count,d); /* yay we found one */
            count += 1;
            if (count > 10) break; /* found enough? */
        }
        Julian += 1;  /* bump julian and look more */
    }

    return 0;
}

/* Gregorian to Julian */
long jd(int y, int m, int d)
{
    y+=8000;
    if(m<3) { y--; m+=12; }
    return (y*365) +(y/4) -(y/100) +(y/400) -1200820
        +(m*153+3)/5-92
        +d-1;
}

/* Julian to Gregorian */
void gd (long JD, int *y, int *m, int *d)
{
    int I,J,K,L,N;

    L  = JD + 68569;
    N  = 4*L/146097;
    L  = L-(146097*N+3)/4;
    I  = 4000*(L+1)/1461001;
    L  = L-1461*I/4+31;
    J  = 80*L/2447;
    K  = L-2447*J/80;
    L  = J/11;
    J  = J+2-12*L;
    I  = 100*(N-49)+I+L;
    *y = I;
    *m = J;
    *d = K;
}

/* is it a palendrome */
int IsPallendrome (char Date[]) 
{
    int y = 7;
    int x;
    for (x = 0; x < 4; ++x, --y)
    {
        if (Date[x] != Date[y])
            return(0);
    }
    return(1);
}

I make your code C90 conformant. See if you can find my changes and figure out why I made them. :) On a side note, due to the obvious fact that you're using Visual Studio, and the less obvious fact that Visual Studio doesn't presently support C99, I'd recommend you look at your project settings to make sure that you're compiling as C and not C++. There are subtle differences between the two languages when it comes to the C subset. You were using a few features of C++ that aren't legal C90, which Visual Studio should have caught if compiling the code as C.

jnawrocki -3 Light Poster

There's a reason I called it programming for fun.

WaltP 2,905 Posting Sage w/ dash of thyme Team Colleague

What reason? So anyone who doesn't have your exact compiler and O/S can scream in frustration because they can't try your program? Other people don't think getting punk'd is fun. ;o)

jnawrocki -3 Light Poster

The important parts of the progtam are the conversion functions. If you can't include them in any code for any compiler and use as is, maybe programming for fun is something you will never understand.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.