Benutzer:Rotkaeppchen68/vanity.c

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
#pragma warning( disable : 4032)
//#include <conio.h>
#pragma warning( default : 4032)
#include <stdio.h>

#pragma warning( disable : 4201 4214 4115)
#include <windows.h> //ok
#pragma warning( default : 4201 4214 4115; disable: 4514)

#define MAXNUMLEN 80
static int screen_width = 80;
int todigit(int c)
{
	int d;
    if(!(isalpha(c)&&!(c&~0x7f)))
		return c;
	c = toupper(c);
	d = (c - 'A') / 3 + '2';
	if(d<'8')
		return d;
	d = (c - 'Q') / 3 + '7';
	if(d<='9')
		return d;
	return '9';
}
int toletter(int c, int ndx)
{
	char xlat[]="ABC2DEF3GHI4JKL5MNO6PQRSTUV8WXYZ";
	if((c<'2')||(c>'9'))
		return c;
	if((ndx<0)||(ndx>3))
		return c;
	return xlat[(c - '2') * 4 + ndx];
}
int vanity(char *n)
{
	/*int poss[10]={1,1,4,4,4,4,4,5,4,5};*/
	int poss[10]={1,1,3,3,3,3,3,4,3,4};
	int total,i,r,q,d, npl, len, j;
	char *s, *t;
	char res[MAXNUMLEN+1];
	printf("Original Number: %s\n", n);
	s = n;
	while(*s)
	{
		*s = (char)todigit(*s);
		s++;
	}
	printf("All Digits: %s\n", n);
	s = n;
	t = n;
	while(*s)
	{
        if(isascii(*s)&&isdigit(*s))
			*t++=*s;
		s++;
	}
	*t=0;
	printf("Shrunk: %s\n", n);
	total=1;
	len = strlen(n);
	for(i=0;i<len;i++)
	{
		total *= poss[n[i]- '0'];
	}
	printf("%d possibilities.\n%s\n",total,n);
	for(i=0;i<4;i++)
	{
		for(j=0;j<len;j++)
		{
			if(i<poss[n[j]- '0'])
				putchar(toletter(n[j],i));
			else
				putchar(' ');
		}
		putchar('\n');
	}
	getchar();
	npl = screen_width / (1 + len);
	res[len]=0;
	for(i=0;i<total;i++)
	{
		r = i;
		for(j=len-1;j>=0;j--)
		{
			q = poss[n[j] - '0'];
			d = r % q;
			r /= q;
			res[j] = (char)toletter(n[j],d);
		}
		if((i+1)%npl)
			printf("%s ",res);
		else
			printf("%s\n",res);
	}
	if(i%npl)
		putchar('\n');
	return 0;
}
int main(int argc, char **argv)
{
	int i;
	char number[MAXNUMLEN+1];
	CONSOLE_SCREEN_BUFFER_INFO  ConsoleScreenBufferInfo;    /* address of screen buffer info.*/
	GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &ConsoleScreenBufferInfo);
	screen_width = ConsoleScreenBufferInfo.dwSize.X;
	if(argc>1)
		for(i=1;i<argc;i++)
			vanity(argv[i]);
	else
	{
		printf("Enter Phone Number: ");
		gets(number);
		vanity(number);
	}
	getchar();
	return 0;
}