color[counter]=4;
color[++counter]=0;
}
break;
}
textbackground(4);cprintf(" ");
textcolor(WHITE);
textbackground(color[0]);cprintf(" Добавить ");
textbackground(color[1]);cprintf(" Удалить ");
textbackground(color[2]);cprintf(" Редактировать ");
textbackground(color[3]);cprintf(" Поиск ");
textbackground(color[4]);cprintf(" Сортировка ");
textbackground(color[5]);cprintf(" Назад ");
textbackground(4);cprintf(" ");
textbackground(1);
view(fn,num);
key=bioskey(0);
switch(key)
{
case UP: num--;if (num==0)
num++;
view(fn,num);
break;
case DOWN: num++;if (num+16>=get_number(fn))
num--;
view(fn,num);
break;
}
if (key==283)
return;
if (key == ENTER)
{
switch(counter) {
case 0: add(fn); clrscr(); break;
case 1: del(fn); break;
case 2: edit(fn); clrscr(); break;
case 3: find(fn); break;
case 4: sort(fn); break;
case 5: return;
}
}
}
}
void add(char *fn)
{
FILE *fp;
data st;
int pr;
while (1) {
st.num=find_max_num(fn)+1;
clrscr();
_setcursortype(_SOLIDCURSOR);
gotoxy(20,6); cout<<"Фамилия : "; cin>>st.lastName;
gotoxy(20,8); cout<<"Имя : "; cin>>st.firstName;
gotoxy(20,10); cout<<"Отчество : "; cin>>st.midName;
gotoxy(20,12); cout<<"№ группы : "; cin>>st.groupNumber;
gotoxy(20,14); cout<<"Средний балл студента : ";
fflush(stdin);scanf("%f",&st.mark);
if (!check(fn, st)) {
if ((fp = fopen(fn,"ab+")) == NULL) {
printf("File could not be opened.\n");
return;
}
fwrite(&st, sizeof(data),1,fp);
fclose(fp);
return;
}
else {
gotoxy(20,18);
cout<<"Такая запись уже существует";
getch();
}
}
}
void del(char *fn)
{
FILE *fp,*temp;
data st;
int pr=1;
int number;
gotoxy(4,25);
textcolor(11);
cprintf("Введите № записи для удаления : ");
textcolor(YELLOW);
gotoxy(36,25);
int key = 0;
cscanf("%d",&number);
textcolor(WHITE);
while(kbhit())
if (!(key=getch()))
key << 8;
if( key == '\r' ) {
if ((fp=fopen(fn,"rb")) == NULL) {
cout<<"error.file does not exist.";
return;
}
if ((temp=fopen("temp.$$$","wb")) == NULL) {
cout<<"error. temp file couldn't be create";
return;
}
while (fread(&st,sizeof(data),1,fp)) {
if (feof(fp))
break;
if (st.num != number)
fwrite(&st,sizeof(data),1,temp);
else
pr=0;
}
fclose(fp);
fclose(temp);
if (remove(fn) == -1) {
perror("remove");
return;
}
if (rename("temp.$$$",fn) == -1) {
perror("rename");
return;
}
if (pr) {
textcolor(11);
gotoxy(4,25);
cprintf("\r Ошибка. Нет записи с таким номером ");
getch();
textcolor(WHITE);
}
}
clrscr();
}
void edit(char *fn)
{
FILE *fp;
data st;
int pr=1;
int number,size=sizeof(data);
fpos_t filepos;
gotoxy(4,25);
textcolor(11);
cprintf("Введите № записи для редактирования : ");
textcolor(YELLOW);
gotoxy(44,25);
int key = 0;
cscanf("%d",&number);
textcolor(WHITE);
while( kbhit())
if( !(key = getch()))
key << 8;
if( key == '\r' )
{
if ((fp = fopen(fn,"rb+")) == NULL)
{
printf("File could not be opened.\n");
return;
}
while(fread(&st,size,1,fp))
{
if (feof(fp))
break;
if (st.num == number)
{
fseek(fp,-1*size,1);
fgetpos(fp,&filepos);
pr=1;
break;
}
else
pr=0;
}
if (pr)
{
while (1)
{
clrscr();
_setcursortype(_SOLIDCURSOR);
gotoxy(20,6);cout<<"Фамилия : ";
cin>>st.lastName;
gotoxy(20,8);cout<<"Имя : ";
cin>>st.firstName;
gotoxy(20,10);cout<<"Отчество : ";
cin>>st.midName;
gotoxy(20,12);cout<<"№ группы : ";
cin>>st.groupNumber;
gotoxy(20,14);cout<<"Средний балл студента : ";
scanf("%f",&st.mark);
if (!check(fn, st))
{
fsetpos(fp,&filepos);
fwrite(&st, sizeof(data),1,fp);
break;
}
else {
gotoxy(20,18);
cout<<"Такая запись уже существует";
getch();
}
}
}
else {
textcolor(11);
gotoxy(4,25);
cprintf("\r Ошибка... Нет записи с таким номером ");
getch();
textcolor(WHITE);
}
fclose(fp);
}
}
void view(char *fn,int n)
{
void ramka();
FILE *fp;
data st;
char c;
int counter=0;
if ((fp = fopen(fn,"rb")) == NULL) {
printf("File could not be opened.\n");
return;
}
ramka();
window(1,6,80,23);
fseek(fp,(n-1)*sizeof(data),SEEK_SET);
while (counter != 18)
{
fread(&st,sizeof(data),1,fp);
if (feof(fp))
break;
else
{
printf(" %4d %-16s %-12s %-17s %8s "
"%7.2f\n",st.num,st.lastName,st.firstName,
st.midName,st.groupNumber,st.mark);
counter++;
}
}
window(1,24,80,25);
cprintf("-----------------------------------"
"---------------------------------------------");
cprintf(" %c - вверх, %c - вниз , %c - влево,"
" %c - вправо, ESC - отмена ",0x018,0x019,0x01B,0x01A);
textcolor(10); textbackground(1); cprintf("%14s",fn);
window(1,1,80,25); textcolor(15);
fclose(fp);
}
void sort(char *fn)
{
FILE *fp;
data s1,s2;
int num=1,size=sizeof(data),counter=0,key=0;
int oldfs,newfs,color[]={0,4,4,4,4};
while(1)
{
_setcursortype(_NOCURSOR); textcolor(10);
cprintf("::::::::::::::::::::::::: М е н ю с о р т и р о в к и "
":::::::::::::::::::::::::");
switch(key)
{
case LEFT: if (counter>0) {
color[counter]=4;
color[--counter]=0;
}
break;
case RIGHT: if (counter<4) {
color[counter]=4;
color[++counter]=0;
}
break;
}
textbackground(4); cprintf(" "); textcolor(WHITE);
textbackground(color[0]);cprintf(" по № записи ");
textbackground(color[1]);cprintf(" по Ф.И.О ");
textbackground(color[2]);cprintf(" по № группы ");
textbackground(color[3]);cprintf(" по ср.балу ");
textbackground(color[4]);cprintf(" Назад ");
textbackground(4);cprintf(" "); textbackground(1);
view(fn,num);
key=bioskey(0);
switch(key)
{
case UP: num--;if (num==0)
num=1;
view(fn,num); break;
case DOWN: num++;if (num+16>=get_number(fn))
num--;
view(fn,num); break;
}
if (key==283)
return;
if (key == ENTER)
{
if ((fp=fopen(fn,"rb+")) == NULL)
{
cout<<"error.file does not exist.";
return;
}
switch(counter) {
case 1: /*-----------по Ф.И.О--------------------*/
oldfs=1; newfs=0;
fread(&s1,size,1,fp);
while(oldfs) {
fread(&s2,size,1,fp);
if(feof(fp)) {
if(!newfs)
break;
oldfs=1; newfs=0;
rewind(fp);
fread(&s1,size,1,fp);
continue;
}
if (strcmp(s1.lastName,s2.lastName) > 0) {
fseek(fp,-2*size,1);
fwrite(&s2,size,1,fp);
fwrite(&s1,size,1,fp);
newfs=1;
}
if (strcmp(s1.lastName,s2.lastName) == 0)
if (strcmp(s1.firstName,s2.firstName) > 0) {
fseek(fp,-2*size,1);
fwrite(&s2,size,1,fp);
fwrite(&s1,size,1,fp);
newfs=1;
}
if (strcmp(s1.firstName,s2.firstName) == 0)
if (strcmp(s1.midName,s2.midName) > 0) {
fseek(fp,-2*size,1);
fwrite(&s2,size,1,fp);
fwrite(&s1,size,1,fp);
newfs=1;
}
else
s1=s2;
else
s1=s2;
}
break;
case 0: /*------------ПО НОМЕРУ ЗАПИСИ-----------*/
oldfs=1; newfs=0;
fread(&s1,size,1,fp);
while(oldfs) {
fread(&s2,size,1,fp);
if(feof(fp)) {
if(!newfs)
break;
oldfs=1; newfs=0;
rewind(fp);
fread(&s1,size,1,fp);
continue;
}
if (s1.num > s2.num) {
fseek(fp,-2*size,1);
fwrite(&s2,size,1,fp);
fwrite(&s1,size,1,fp);
newfs=1;
}
else
s1=s2;
}
break;
case 2: /*----------ПО НОМЕРУ ГРУППЫ---------------*/
oldfs=1; newfs=0;
fread(&s1,size,1,fp);
while(oldfs) {
fread(&s2,size,1,fp);
if(feof(fp)) {
if(!newfs)
break;
oldfs=1; newfs=0;
rewind(fp);
fread(&s1,size,1,fp);
continue;
}
if (strcmp(s1.groupNumber,s2.groupNumber) > 0) {
fseek(fp,-2*size,1);
fwrite(&s2,size,1,fp);
fwrite(&s1,size,1,fp);
newfs=1;
}
else
s1=s2;
}
break;
case 3: /*---------ПО СР.БАЛУ----------------*/
oldfs=1; newfs=0;
fread(&s1,size,1,fp);
while(oldfs) {
fread(&s2,size,1,fp);
if(feof(fp)) {
if(!newfs)
break;
oldfs=1; newfs=0;
rewind(fp);
fread(&s1,size,1,fp);
continue;
}
if (s1.mark < s2.mark) {
fseek(fp,-2*size,1);
fwrite(&s2,size,1,fp);
fwrite(&s1,size,1,fp);
newfs=1;
}
else
s1=s2;
} break;
case 4:
fclose(fp);
return;
}
fclose(fp);
}
}
}
void find(char *fn)
{
void ramka();
FILE* fp;
data st;
char string[15];
int pr,prs;
int key=0,counter=0;
int color[]={0,4,4,4};
while (1) {
clrscr(); textcolor(10);
cprintf("::::::::::::::::::::::::::::: М е н ю П о и с к а "
":::::::::::::::::::::::::::::");
textcolor(WHITE);
textbackground(4);
cprintf(" ");
switch(key)
{
case LEFT: if (counter>0) {
color[counter]=4;
color[--counter]=0;
} break;
case RIGHT: if (counter<3) {
color[counter]=4;
color[++counter]=0;
} break;
}
textbackground(color[0]); cprintf(" по № записи ");
textbackground(color[1]); cprintf(" по Фамилии ");
textbackground(color[2]); cprintf(" по Имени ");
textbackground(color[3]); cprintf(" Отмена ");
textbackground(4);
cprintf(" ");
textbackground(1);
key=bioskey(0);
if (key==283)
return;
if (key == ENTER)
{
if((fp=fopen(fn,"rb"))==NULL) {
puts("File couldn't be open");
return;
}
switch(counter)
{
case 0:
int number;
pr=0;
prs=1;
cout<<"ВВЕДИТЕ НОМЕР ЗАПИСИ : ";
fflush(stdin);
scanf("%d",&number);
rewind(fp);
while(fread(&st,sizeof(data),1,fp)) {
if (feof(fp)) {
break;
}
if(st.num == number) {
if (prs) {
ramka();
gotoxy(1,6);
prs=0;
}
printf(" %4d %-16s %-12s %-17s %8s "
"%7.2f\n",st.num,st.lastName,st.firstName,
st.midName,st.groupNumber,st.mark);
pr=1;
}
}
if(!pr) {
cout<<"\nЗАПИСИ С ТАКИМ НОМЕРОМ НЕ СУЩЕСТВУЕТ";
}
getch();
break;
case 1:
prs=1;
pr=0;
cout<<"ВВЕДИТЕ ФАМИЛИЮ : ";
fflush(stdin);
cin>>string;
rewind(fp);
while(fread(&st,sizeof(data),1,fp)) {
if (feof(fp))
break;
if(!strcmp(st.lastName,string)) {
if (prs) {
ramka();
gotoxy(1,6);
prs=0;
}
printf(" %4d %-16s %-12s %-17s %8s "
"%7.2f\n",st.num,st.lastName,st.firstName,
st.midName,st.groupNumber,st.mark);
pr=1;
}
}
if(!pr) {
cout<<"\nНЕТ ДАННЫХ";
}
getch();
break;
case 2:
prs=1;
pr=0;
cout<<"ВВЕДИТЕ ИМЯ : ";
fflush(stdin);
cin>>string;
rewind(fp);
while(fread(&st,sizeof(data),1,fp)) {
if (feof(fp))
break;
if(!strcmp(st.firstName,string)) {
if (prs) {
ramka();
gotoxy(1,6);
prs=0;
}
printf(" %4d %-16s %-12s %-17s %8s "
"%7.2f\n",st.num,st.lastName,st.firstName,
st.midName,st.groupNumber,st.mark);
pr=1;
}
}
if(!pr) {
cout<<"\nНЕТ ДАННЫХ";
}
getch();
break;
case 3:
fclose(fp);
return;
}
fclose(fp);
}
}
}
void ramka()
{
window(1,3,80,6);
cprintf("------------------------------------"
"--------------------------------------------");
textcolor(14);
cprintf("%5s %-16s %-12s %-17s %-8s %12s ",
"№","Фамилия","Имя","Отчество","№ группы","Ср.балл");
textcolor(WHITE);
cprintf("------------------------------------"
"--------------------------------------------");
window(1,1,80,25);
}
int find_max_num(char *fn)
{
FILE *fp;
data st;
int max;
if ((fp = fopen(fn,"rb")) == NULL) {
printf("File could not be opened.\n");
return -1;
}
max=0;
while (fread(&st,sizeof(data),1,fp)) {
if (feof(fp))
break;
if (st.num>max)
max=st.num;
}
fclose(fp);
return max;
}
int get_number(char *fn)
{
FILE *fp;
int fd,n=0;
if ((fp = fopen(fn,"rb")) == NULL)
{
printf("File could not be opened.\n");
return -1;
}
fd=fileno(fp);
n=filelength(fd)/sizeof(data);
fclose(fp);
return n;
}
void create()
{
FILE *fp;
char *fn;
cout<<"Введите имя новой БД : ";
fflush(stdin);
cin>>fn;
strcat(fn,".dat");
if ((fp=fopen(fn,"wb")) == NULL) {
cout<<"Ошибка...Файл не может быть создан\n";
getch();
exit(10);
}
fclose(fp);
interface(fn);
}
void open()
{
FILE *fp;
char *fn;
cout<<"Введите имя БД : ";
fflush(stdin);
cin>>fn;
strcat(fn,".dat");
if (!(fp=fopen(fn,"rb"))) {
cout<<"Ошибка...Файл не может быть открыт\n";
getch();
exit(10);
}
fclose(fp);
interface(fn);
}
int check(char *fn, data st)
{
FILE *fp;
data temp;
int pr=0;
if ((fp = fopen(fn,"rb")) == NULL) {
printf("File could not be opened.\n");
return 0;
}
while ( fread( &temp, sizeof( data ), 1, fp)) {
if (feof(fp))
break;
if (!strcmp(st.lastName,temp.lastName)
&& !strcmp(st.firstName,temp.firstName)
&& !strcmp(st.midName,temp.midName)
&& !strcmp(st.groupNumber,temp.groupNumber)
&& (st.mark == temp.mark)) {
pr=1;
break;
}
} fclose(fp);
return pr;
}