#include <conio.h>
#include <stdlib.h>
#include<stdio.h>
#include<string.h>
#define ENTER 13
#define ESC 27
#define UP 72
#define LEFT 75
#define RIGHT 77
#define DOWN 80
#define BACKSPACE 8
#define F1 59
#define F2 60
#define F3 61
#define FILENAME "database.dat"
int view_cars();
int LastID=0;
int col=0;
int view_is_empty();
int write_file();
int read_file();
int error();
typedef struct
{
long id;
char year[20];
char name[20];
char surname[20];
char birth[20];
char genre[20];
char status[15];
struct node *next;
}Box;
typedef struct node
{
Box data;
struct node *next, *prev;
}ITEM;
typedef struct header
{
int number;
ITEM *first, *last;
}HIT;
HIT *head=NULL;
void mkhead();
void AddDATA(Box data);
void mkhead()
{
if((head=(HIT *)malloc(sizeof(HIT)))!=NULL)
{
head->number=0;
head->first=NULL;
head->last=NULL;
}
else
{
warning("Memory error");
}
}
int view_cars(int def)
{
Box data;
ITEM *curr=head->first;
int i,c=1,n, w=20;
n=head->number;
if(def!=1) {
while(curr->data.id!=def)
{
curr=curr->next;
}
}
for(i=0; i<n && i<w && curr!=NULL ;i++ )
{
gotoxy(2,4+i);
cprintf("%d",curr->data.id);
gotoxy(5,4+i);
cprintf("%s",curr->data.name);
gotoxy(26,4+i);
cprintf("%s",curr->data.surname);
gotoxy(37,4+i);
cprintf("%s",curr->data.birth);
gotoxy(43,4+i);
cprintf("%s",curr->data.year);
gotoxy(57,4+i);
cprintf("%s",curr->data.genre);
gotoxy(73,4+i);
cprintf("%s",curr->data.status);
curr=curr->next;
}
return c;
}
Box* edit_node(Box *head)
{
Box *cur;
int i,n;
cur=head;
if(head==NULL)
{
view_is_empty();
return head;
}
else
{
window(30,10,51,12);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
cprintf("\n NUMBER : ");
scanf("%d",&n);
}
window(1,2,80,24);
clrscr();
textbackground(BLUE);
clrscr();
textcolor(WHITE);
clrscr();
if(n>=1 && n<=col)
{
window(22,8,60,18);
textbackground(GREEN);
textcolor(WHITE);
clrscr();
for(i=0;i<n-1;i++)
{
cur=cur->next;
}
gotoxy(1,2);
printf(" NAME : ");
scanf("%s",cur->name);
gotoxy(1,4);
printf(" SURNAME : ");
scanf("%s",cur->surname);
gotoxy(1,6);
printf(" BIRTHPLACE : ");
scanf("%s",cur->birth);
gotoxy(1,8);
printf(" YEAR OF BIRTH : ");
scanf("%s",cur->year);
gotoxy(1,10);
printf(" GENRE : ");
scanf("%s",cur->genre);
return head;
}
else
{
error();
return head;
}
}
Box* find_node(Box *head)
{
Box *cur;
char *n,*s;
int col=1;
if(head==NULL)
{
view_is_empty();
return head;
}
window(30,10,51,12);
clrscr();
textbackground(WHITE);
textcolor(BLACK);
clrscr();
cprintf("\n NAME : ");
scanf("%s",n);
window(30,10,51,12);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
cprintf("\n SURNAME : ");
scanf("%s",s);
cur=head;
while(cur!=NULL)
{
if(strcmp(cur->name,n)==0)
{
if(strcmp(cur->surname,s)==0)
{
window(22,8,60,20);
textbackground(GREEN);
textcolor(WHITE);
clrscr();
gotoxy(10,6);
cprintf("Element %s %s is found\n",n,s);
gotoxy(10,8);
cprintf(" at number %d",col);
return head;
}
}
col++;
cur=cur->next;
}
window(22,8,60,20);
textbackground(RED);
textcolor(WHITE);
clrscr();
gotoxy(6,7);
cprintf("Element %s %s is not found\n",n,s);
return head;
}
int show_cur();
int show_cur()
{
_setcursortype(_NORMALCURSOR);
}
int add();
int add()
{
Box data;
ITEM *curr=NULL, *val=head->last;
show_cur();
if((curr=(ITEM *)malloc(sizeof(ITEM)))==NULL)
{
warning("Oshibka pamyati");
}
else
{
curr->data.id=++LastID;
gotoxy(5,4);
gets(curr->data.name);
gotoxy(26,4);
gets(curr->data.surname);
gotoxy(37,4);
gets(curr->data.birth);
gotoxy(43,4);
gets(curr->data.year);
gotoxy(57,4);
gets(curr->data.genre);
gotoxy(73,4);
gets(curr->data.status);
if(head->number==NULL)
{
curr->prev=NULL;
head->first=curr;
}
else
{
val->next=curr;
curr->prev=val;
}
curr->next=NULL;
head->number++;
head->last=curr;
}
}
Box* delete_node(Box* head)
{
Box *cur,*prev;
int i,n;
cur=head;
prev=NULL;
if(head==NULL)
{
view_is_empty();
return head;
}
else
{
window(30,10,51,12);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
cprintf("\n NUMBER : ");
scanf("%d",&n);
}
window(1,2,80,24);
clrscr();
textbackground(BLUE);
clrscr();
textcolor(WHITE);
clrscr();
if(n==1)
{
window(22,8,60,20);
textbackground(GREEN);
textcolor(WHITE);
clrscr();
gotoxy(15,7);
printf("DELETE 1");
head=head->next;
free(cur);
col--;
return head;
}
if(n>1 && n<=col)
{
for(i=0;i<n-1;i++)
{
prev=cur;
cur=cur->next;
}
window(22,8,60,20);
textbackground(GREEN);
textcolor(WHITE);
clrscr();
gotoxy(15,7);
printf("DELETE %d",n);
prev->next=cur->next;
free(cur);
col--;
return head;
}
error();
return head;
}
int cors[8][2]={{1,1},{10,1},{20,1},{29,1},{40,1},{50,1},{60,1},{70,1}};
int pos1[8][2]={{7,1},{0,2},{1,3},{2,4},{3,5},{4,6},{5,7},{6,0}};
char name[8][7]={"VIEW","EDIT","ADD","DELETE","SEARCH","SORT","HELP","EXIT"};
int w=20;
int pos=0;
int del();
int warning(char*str);
int win(int x,int y,int xw,int yw,char*str);
int print_topmenu();
int background();
int val();
int downhelp();
int tophelp();
int print_help();
int chaction();
int table();
int view2();
int view2()
{int i,m,n,l,def,w,lol=1;
char ch;
ITEM *current=head->first;
n=head->number;
if (n==0) return;
i=1;def=1;w=20;
textcolor(15);
gotoxy(10,10);
cprintf("%d",current->data.id);
gotoxy(20,10);
cprintf("%d",head->number);
do
{ current=head->first;
while(current->data.id!=i)
{
current=current->next;
}
gotoxy(20,10);
cprintf("%d",current->data.id);
if (i+def>n+1) def=n;
view_cars(def);
textbackground(13);
gotoxy(2,3+i);
cprintf("%d",current->data.id);
gotoxy(5,3+i);
cprintf("%s",current->data.name);
gotoxy(26,3+i);
cprintf("%s",current->data.surname);
gotoxy(37,3+i);
cprintf("%s",current->data.birth);
gotoxy(43,3+i);
cprintf("%s",current->data.year);
gotoxy(57,3+i);
cprintf("%s",current->data.genre);
gotoxy(73,3+i);
cprintf("%s",current->data.status);
fflush(stdin);
ch=getch();
if (lol)
{
textbackground(0);
gotoxy(2,3+i);
cprintf("%d",current->data.id);
gotoxy(5,3+i);
cprintf("%s",current->data.name);
gotoxy(26,3+i);
cprintf("%s",current->data.surname);
gotoxy(37,3+i);
cprintf("%s",current->data.birth);
gotoxy(43,3+i);
cprintf("%s",current->data.year);
gotoxy(57,3+i);
cprintf("%s",current->data.genre);
gotoxy(73,3+i);
cprintf("%s",current->data.status);
ch=getch();
if (ch==72)
{
if ((i==1 )&&(def>2))
{
def--;
view_cars(def);
}
if ((i>1)&&((def+i)>1))
{
i--;
}
}
if (ch==80)
{
if ((i==19)&&(i+def<n+1))
{
def++;
view_cars(def);
}
if ((i<20)&&((def+i)<n+1)&&(i<w)) i++;
}
/*if (ch==62) edit(i+def);
if (ch==66) if (n!=0)
{
del(i);
if (i>0) i--;
clean();
view_cars();
if (n==0) return;
} */
}
}while(ch!=ESC);
/* l=i;
textbackground(0);
gotoxy(11,2+i);
cprintf(base[i].brand);
gotoxy(31,2+i);
cprintf(base[i].year);
gotoxy(48,2+i);
cprintf(base[i].price); */
}
int win(int x,int y,int xw,int yw,char*str)
{int i,j;
textcolor(0);
textbackground(3);
gotoxy(x,y);
cprintf("|");
gotoxy(x+xw,y);
cprintf("|");
gotoxy(x,y+yw-1);
cprintf("|");
gotoxy(x+xw,y+yw-1);
cprintf("|");
for(j=0;j<yw;j++)
{
if ((j!=0)&&(j!=yw-1))
{gotoxy(x,y+j);
cprintf("|");
gotoxy(x+xw,y+j);
cprintf("|");
}
if ((j==0)||(j==yw-1))
{
for(i=1;i<xw;i++)
{gotoxy(x+i,y+j);
cprintf("|");
}
}
}
x=x+(xw-strlen(str))/2+1;
textcolor(0);
textbackground(7);
gotoxy(x,y);
cprintf(str);
textcolor(7);
textbackground(0);
}
int table()
{int i;
clrscr();
gotoxy(0,0);
puts("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°");
puts("° N° Model ° Color ° Year ° Body type ° Price per day °Status°");
puts("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°");
for(i=0;i<20;i++)
puts("° ° ° ° ° ° ° °");
puts("°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°");
win( 5,5,16,16, " nigga ");
return 0;
}
int downhelp()
{
window(1,1,80,25);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
gotoxy(1,25);
cprintf(" F1 - ");
cprintf(" F2 - ADD");
cprintf(" F3 - DELETE");
window(1,2,80,24);
textbackground(LIGHTBLUE);
textcolor(WHITE);
clrscr();
return 0;
}
int warning(char*str)
{int i,j;
win(25,10,30,10," Information ");
for (i=26;i<55;i++)
for (j=11;j<19;j++)
{gotoxy(i,j);
cprintf(" ");
}
gotoxy(26+(30-strlen(str))/2,12);
printf(str);
getch();
}
int main()
{
FILE *f;
_setcursortype(_NOCURSOR);
mkhead();
f=fopen(FILENAME,"rb");
if (f==NULL) {
warning("Bazy ne obnaryzhenno");
warning("Cozdaetsa novaja baza");
f=fopen(FILENAME,"w+b");
}
else
{
read_file();
}
fclose(f);
downhelp();
for(;;)
{
print_topmenu();
tophelp();
}
}
int print_topmenu()
{
int i;
for(i=0;i<8;i++)
{
window(cors[i][0],cors[i][1],cors[i][0]+6,cors[i][1]);
textbackground(i==pos?GREEN:WHITE);
textcolor(i==pos?BLACK:BLACK);
clrscr();
cprintf("%s",name[i]);
}
return 0;
}
int tophelp()
{
int key;
key=getch();
if(key!=0)
switch(key)
{
case RIGHT:pos=pos1[pos][1];break;
case LEFT:pos=pos1[pos][0];break;
case ENTER:chaction();break;
case F1:clrscr();
window(1,1,80,25);
textbackground(BLUE);
textcolor(WHITE);
clrscr();
print_help();
getch();
clrscr();
downhelp();
print_topmenu();
break;
case F2:add();break;
case F3:del();getch();downhelp();print_topmenu();break;
}
return 0;
}
int chaction()
{
char key;
int i,key1;
switch(pos)
{
case 0:
clrscr();
window(1,1,80,25);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
gotoxy(1,25);
cprintf(" ESC - BACK");
/*window(1,1,80,25);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
gotoxy(35,1);
puts("VIEW");
gotoxy(1,25);
cprintf(" F1 - HELP");
cprintf(" F2 - ADD");
cprintf(" F3 - DELETE");*/
window(1,1,80,24);
textcolor(WHITE);
textbackground(BLUE);
table();
view2();
do
{
key1=getch();
}
while(key1!=ESC);
clrscr(); downhelp(); print_topmenu();break;
case 1:
clrscr();
window(1,1,80,1);
clrscr();
textbackground(GREEN);
textcolor(WHITE);
clrscr();
gotoxy(37,1);
puts("EDIT");
window(1,2,80,24);
textcolor(WHITE);
textbackground(BLUE);
clrscr();
/*Head=edit_node(Head);*/
getch();
downhelp();
print_topmenu();
break;
case 2:val();break;
case 3:
del();
getch();
downhelp();
print_topmenu();
break;
case 4:
clrscr();
window(1,1,80,1);
clrscr();
textbackground(GREEN);
textcolor(WHITE);
clrscr();
gotoxy(36,1);
puts("SEARCH");
window(1,2,80,25);
textcolor(WHITE);
textbackground(BLUE);
clrscr();
/*Head=find_node(Head);*/
getch();
downhelp();
print_topmenu();
break;
case 6: background();
print_help();
getch();
clrscr();
downhelp();
print_topmenu();
break;
case 7: write_file(); exit(0);break;
}
return 0;
}
int print_help()
{
FILE *f;
char string;
if ((f = fopen("HELP.txt", "rt")) == NULL)
{
puts("help.txt not found");
}
string=getc(f);
while(!feof(f))
{
printf("%c",string);
string=getc(f);
}
fclose(f);
return 0;
}
int background()
{
window(1,1,80,25);
clrscr();
textbackground(LIGHTBLUE);
textcolor(WHITE);
clrscr();
return 0;
}
int write_file()
{
FILE *f;
ITEM *Currt;
f=fopen("FILENAME", "w+b");
fwrite(&LastID, sizeof(int), 1, f);
for(Currt=head->first; Currt!=NULL; Currt=Currt->next)
{
fwrite(&Currt->data, sizeof(Box), 1, f);
}
fclose(f);
}
int read_file()
{
FILE *f;
Box data;
f=fopen("FILENAME", "rb");
fread(&LastID, sizeof(int), 1, f);
while(fread(&data, sizeof(Box), 1, f)!=0)
{
AddDATA(data);
}
fclose(f);
}
void AddDATA(Box data)
{
ITEM *CurrLe=head->last, *new_node=NULL;
if((new_node=(ITEM *)malloc(sizeof(ITEM)))!=NULL)
{
if(head->number!=0)
{
CurrLe->next=new_node;
new_node->prev=CurrLe;
}
else
{
new_node->prev=NULL;
head->first=new_node;
}
new_node->next=NULL;
new_node->data=data;
head->number++;
head->last=new_node;
}
else
{
warning("Memory error");
}
}
int val()
{
int i;
clrscr();
window(1,1,80,1);
clrscr();
textbackground(GREEN);
textcolor(WHITE);
clrscr();
gotoxy(35,1);
puts("ADD");
window(1,2,80,25);
textcolor(WHITE);
textbackground(BLUE);
table();
add();
clrscr();downhelp();print_topmenu();
return 0;
}
int view_is_empty()
{
window(22,8,60,20);
textbackground(WHITE);
textcolor(BLACK);
clrscr();
gotoxy(13,7);
printf("VIEW IS EMPTY");
return 0;
}
int error()
{
window(22,8,60,20);
textbackground(RED);
textcolor(WHITE);
clrscr();
gotoxy(17,7);
printf("ERROR");
return 0;
}
int del()
{
clrscr();
window(1,1,80,1);
clrscr();
textbackground(GREEN);
textcolor(WHITE);
clrscr();
gotoxy(37,1);
puts("DELETE");
window(1,2,80,25);
textcolor(WHITE);
textbackground(BLUE);
clrscr();
/*Head=delete_node(Head);*/
return 0;
}