#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define LIST struct list
#define Nm 100
#define Nmax 30
#define M 8
LIST
 {
	char Name[Nmax];
	int year;
	int month;
	int day;
 };
LIST studentu[Nm];
int maxday[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int n;
void FormList()        /*formirovanie spiska*/
 {
	int i;
	printf("Vvedite kolichestvo studentov: ");
	do
	 scanf("%d",&n);
	while(n<0 || n>Nm);
	for (i=0; i<n; i++)
	 {
		printf("%d.Vvedite familiu i inicialu studenta: ",i+1);
		scanf("%s",studentu[i].Name);
		printf("Vvedite god rojdeniya: ");
		scanf("%d",&studentu[i].year);
		printf("Vvedite mesyac rojdeniya: ");
		scanf("%d",&studentu[i].month);
		printf("Vvedite den' rojdeniya: ");
		scanf("%d",&studentu[i].day);
	 }
 }
int Nomercifru(int k, int m, int lp)  /*k-число, m-номер цифры в числе, lp-максимальное количество цифр*/
 {
	int lpm,q,i0,it;
	float d;
	lpm=lp-m+1;
	q=10;
	for (it=1; it<lpm; it++)
	 {
		q=q*10;
	 }
	i0=(int)k/q;
	d=1.0*k/q;
	return((int)((d-i0)*10));
 }
void Sortirovka(LIST a[], int n, int p)
 {
	int i,j,j0,l,m;
	int s[10];
	LIST b[Nm];
	for (m=p; m>0; m--)
	 {
		for(i=0; i<10; i++)
		 {
			s[i]=0;
		 }
		for(j=0; j<n; j++)
		 {
			l=Nomercifru(a[j].year,m,p);
			s[l]++;
		 }
		for (i=1; i<10; i++)
		 {
			s[i]+=s[i-1];
		 }
		for(j0=1; j0<=n; j0++)
		 {
			j=n-j0+1;
			l=Nomercifru(a[j-1].year,m,p);
			i=s[l]-1;
			b[i]=a[j-1];
			s[l]--;
		 }
		for (j=0; j<n; j++)
		 {
			a[j]=b[j];
		 }
	 }
	return;
 }
void PrintList(int n)  /*pechat' spiska*/
 {
	int i;
	printf(" #  Familiya i inicialu God rojdeniya Mesyac rojdeniya Den' rojdeniya\n");
	for (i=0; i<n; i++)
	 printf(" %-6d%-22s%-16d%-16d%-2d\n", i+1, studentu[i].Name, studentu[i].year, studentu[i].month, studentu[i].day);
 }
main()
 {
	int c,p;
	clrscr();
	do
	 {
		window(1,1,80,80);
		gotoxy(1,1);puts("1.Formirovanie spiska studentov odnogo kursa");
		gotoxy(1,2);puts("2.Pechat' spiska");
		gotoxy(1,3);puts("3.Sortirovka");
		gotoxy(1,4);puts("4.Vuhod");
		c=getch();
		window(1,6,80,80);
		clrscr();
		switch(c)
		 {
			case '1':
			 FormList();     /*formirovanie spiska*/
			 clrscr();
			 break;
			case '2':
			 PrintList(n);   /*pechat' spiska*/
			 getch();
			 clrscr();
			 break;
			case '3':        /*sortirovka*/
			 Sortirovka(studentu,n,p);
			 break;
			case '4':			   /*vuhod*/
			 break;
		 }
	 }while (c>'0' && c<'4');
	return c;
 }
