Creative Commons License Foxbond's Repo

/** (c) 2012 Michał (Foxbond) Chraniuk */
#include <stdlib.h>
#include <stdio.h> 
#include <cstring> //strlen
#include <conio.h> //_getch


void cls () { system("cls"); }




bool parse (char *_buf){
	short pesel[11];
	
	if (strlen(_buf) != 11){
		return false;
	}

	for (short i = 0; i < 11; i++){
		pesel[i] = _buf[i] - 48;
		//printf("%i", pesel[i]);
	}
	

	int sum = 1*pesel[0]+3*pesel[1]+7*pesel[2]+9*pesel[3]+1*pesel[4]+3*pesel[5]+7*pesel[6]+9*pesel[7]+1*pesel[8]+3*pesel[9];
	sum %= 10;
	sum = 10-sum;
	
	if (sum != pesel[10]){
		return false;
	}

	//miesiac & rok
	int month, year;
	
	year = 10 * pesel[0];
	year += pesel[1];
	month = 10 * pesel[2];
	month += pesel[3];
	if (month > 80 && month < 93) {
		month -= 80;
		year += 1800;
	}else if (month > 20 && month < 33) {
		month -= 20;
		year += 2000;
	}else if (month > 40 && month < 53) {
		month -= 40;
		year += 2100;
	}else if (month > 60 && month < 73) {
		month -= 60;
		year += 2200;
	}else if (month > 0 && month < 13){
		year += 1900;
	}else{
		return false;
	}
	
	//dzien
	int day;
	day = 10 * pesel[4];
	day += pesel[5];
	if ((day >0 && day < 32) &&
		(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)) {
		//return 1;
	}else if ((day >0 && day < 31) &&
		(month == 2 || month == 4 || month == 6 || month == 9 || month == 11)) {
		//return 1;
	}else{
		return false;
	}


	printf("PESEL jest poprawny\nData urodzenia: %i-%i-%i\nPlec: ", day, month, year);

	
	if (pesel[9] % 2 == 1) {
		printf("Mezczyzna");
	}else {
		printf("Kobieta");
	}


	return true;
}

int main (){


	char _buf[200];
	char cont = 't';

	do {
		
		printf("Wpisz numer PESEL: ");
		scanf("%s", _buf);
		
		if (!parse(_buf)){
			printf("Niepoprawny PESEL!");
		}

		printf("\nJeszcze raz? (t/n):");
		cont = _getch();
		cls();
	}while(cont == 't' || cont == 'T');

	//system("PAUSE");
	return 0;
}

> Back