#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
int pos_g=1, pos_x=1, pos_y=0, LastID=0;
typedef struct node
{
int id;
char *string;
//data
struct node *next, *prev;
}ITEM;
typedef struct hnode
{
int number;
ITEM *first, *last;
}HITEM;
HITEM *head=NULL;
void bg();
void print_menu();
void print_list();
void select();
void EmptyList();
void ERRORS(char *str);
void add();
ITEM AddList();
void main()
{
int key;
EmptyList();
bg();
for(;;)
{
print_menu();
print_list();
key=getch();
if(key!=0)
{
if(key==27)//ESC
{
break;
}
else
{
if(key==13)//ENTER
{
select();
}
else
{
if(key==9)//TAB
{
if(pos_g==1)
{
pos_g--; //пекреход на список
pos_x=-1;
pos_y=1;
}
else
{
pos_g++; //переход на меню
pos_y=-1;
pos_x=1;
}
}
else
{
continue;
}
}
}
}
else
{
key=getch();
if(pos_g==1)
{
if(key==77)//right
{
if(pos_x<4)
pos_x++;
}
else
{
if(key==75) //left
{
if(pos_x>1)
pos_x--;
}
else
{
continue;
}
}
}
else
{
if(pos_g==0)
{
if(key==72)//up
{
if(pos_y>1)
pos_y--;
}
else
{
if(key==80) //down
{
if(pos_y<21)
pos_y++;
}
else
{
continue;
}
}
}
}
}
}
}
void bg()
{
int i;
window(1,1,80,1); //head
textbackground(WHITE);
textcolor(GREEN);
clrscr();
window(1,2,80,25); //center
textbackground(LIGHTBLUE);
textcolor(GREEN);
clrscr();
puts("╔═════════════════════════════════════════════════════════════════════════════╗");
for(i=0;i<=20;i++)
puts("║ ║");
puts("╚═════════════════════════════════════════════════════════════════════════════╝");
window(1,25,80,25); //footer
textbackground(WHITE);
textcolor(GREEN);
clrscr();
}
void print_menu()
{
char menu[6][15]={" Добавить ", " Поиск ", " Сортировать ", " Помощь "};
int i, x=1;
for(i=1;i<5;i++,x+=13)
{
window(x,1,x+13,1);
textbackground(pos_x==i?MAGENTA:WHITE);
textcolor(GREEN);
clrscr();
puts(menu[i-1]);
}
}
void print_list()
{
int i;
for(i=1;i<22;i++)
{
window(2,(i+2),78,(i+2));
textbackground(pos_y==i?MAGENTA:BLUE);
textcolor(WHITE);
clrscr();
}
}
void select()
{
if(pos_g==0)
{
//список
}
else
{
switch(pos_x)
{
case 1:
add();
break;
case 2:
//search
break;
case 3:
//sort
break;
case 4:
//help
break;
case 5:
break;
case 6:
break;
default:
break;
}
}
}
void EmptyList()
{
if((head=(HITEM *)malloc(sizeof(HITEM)))!=NULL)
{
head->number=0;
head->first=NULL;
}
else
{
ERRORS("Memory error");
}
}
void ERRORS(char *str)
{
window(29,12,51,14);
textbackground(LIGHTRED);
textcolor(BLACK);
clrscr();
cputs(" ╔═══════════════════╗ ");
cputs(" ║ ║ ");
cputs(" ╚═══════════════════╝");
window(32,13,49,13);
textbackground(LIGHTRED);
textcolor(BLACK);
cputs(str);
getch();
}
void add()
{
ITEM *current=head->last;
int key, i, y, n=head->number;
window(2,3,78,23);
textbackground(BLUE);
textcolor(WHITE);
clrscr();
window(2,3,78,3);
textbackground(MAGENTA);
textcolor(WHITE);
clrscr();
AddList();
for(;;)
{
key=getch();
if(key==27)//ESC
{
break;
}
else
{
if(key==13)//ENTER
{
i=head->number-n;
while(i-1!=0)
{
current=current->prev;
i--;
}
y=1;
while(current->next!=NULL)
{
window(2,(y+2),78,(y+2));
textbackground(BLUE);
textcolor(WHITE);
clrscr();
cprintf("%d. ", current->id);
puts(current->string);
y++;
current=current->next;
}
window(2,(y+2),78,(y+2));
textbackground(BLUE);
textcolor(WHITE);
clrscr();
cprintf("%d. ", current->id);
puts(current->string);
window(2,20,78,20);
textbackground(MAGENTA);
textcolor(WHITE);
clrscr();
AddList();
}
else
{
continue;
}
}
}
}
ITEM AddList()
{
ITEM *current=head->first, *new_node=NULL;
char *str;
if((new_node=(ITEM *)malloc(sizeof(ITEM)))==NULL)
{
ERRORS("Memory error");
}
else
{
gets(str);
strcpy(new_node->string, str);
new_node->id=++LastID;
new_node->next=NULL;
head->last=new_node;
head->number++;
if(head->number!=0)
{
while(current->next!=NULL)
{
current=current->next;
}
new_node->prev=current;
current->next=new_node;
}
else
{
new_node->prev=NULL;
head->first=new_node;
}
}
}