Лаба 1(лин односв список)
.pdfstrcpy(pFirst->key, strBuf); item* P = pFirst;
P->next = 0;
//считываем остальные строки for(;;)
{
if (*(fgets(strBuf, STR_LEN, F)) == '\0') return 0; strBuf[strlen(strBuf)-1] = '\0';
P->next = new item; P = P->next;
P->key = new char [strlen(strBuf)]; strcpy(P->key, strBuf);
P->next = 0;
}
return 0;
}
void Menu1()
{
const char* MENU_1 = "______\n\n \
1 - New list\n \
2 - Open list\n \ Esc - Exit\n";
char Key;
char FileName[FN_LEN]; item* pL1;
FILE* F;
for(;;)
{
system("cls"); puts(MENU_1); pL1 = 0;
Key = getch(); switch (Key)
{
case '1':
{
Menu2(pL1); break;
}
case '2':
{
system("cls");
printf("Enter the name of file: \n"); gets(FileName);
F = fopen(FileName,"r"); if (!F)
{
printf("Error open file"); getch();
break;
}
OpenList(pL1, F); fclose(F); Menu2(pL1); break;
}
case 27:
{
return;
}
default:
{
11
system("cls");
printf("Wrong instruction\n\nPress any key"); getch();
break;
}
}
}
}
void Menu2(item*& pL1)
{
const char* MENU_2 = "______\n\n \
1 - Save list\t\t 7 - Change print direction\n \ 2 - Add item\t\t 8 - Link with other list\n \
3 - Find item\t\t 9 - Cross with other list\n \ 4 - Count items\t 0 - Subtract other list\n \
5 - Sort list\t\t Esc - Close list\n \
6 - Delete double items\n";
item* pL2; item* pL3; item* pCur;
char Key, FileName[FN_LEN], strBuf[STR_LEN]; int Direction = DIR;
FILE* F;
for(;;)
{
system("cls"); PrintList(pL1, Direction); puts(MENU_2);
Key = getch(); switch (Key)
{
case '1':
{
system("cls");
printf("Enter the name of file: \n"); gets(FileName);
if (!(F = fopen(FileName, "w")))
{
printf("Error open file\n\nPress any key"); getch();
break;
}
SaveList(pL1, F); fclose(F);
break;
}
case '2':
{
system("cls");
printf("Enter the string: \n"); gets(strBuf);
AddItem(pL1, strBuf); break;
}
case '3':
{
system("cls");
printf("Enter the string: \n"); gets(strBuf);
switch (FindItem(pL1, pCur, strBuf))
{
case 1:
{
12
system("cls");
printf("Empty list\n\nPress any key"); getch();
break;
}
case 2:
{
system("cls");
printf("No such item\n\nPress any key"); getch();
break;
}
default:
Menu3(pL1, pCur);
}
break;
}
case '4':
{
system("cls");
printf("%i items\n\nPress any key", CountItems(pL1)); getch();
break;
}
case '5':
{
SortList(pL1); break;
}
case '6':
{
DeleteDblItems(pL1); break;
}
case '7':
{
Direction = -(Direction); break;
}
case '8':
{
system("cls");
printf("Enter the name of file: \n"); gets(FileName);
F = fopen(FileName, "r"); if (!F)
{
printf("Error open file\n\nPress any key"); getch();
break;
}
OpenList(pL2, F); fclose(F);
pL3 = LinkLists(pL1, pL2); DeleteList(pL2); Menu2(pL3);
break;
}
case '9':
{
system("cls");
printf("Enter the name of file: \n"); gets(FileName);
F = fopen(FileName, "r"); if (!F)
{
printf("Error open file\n\nPress any key");
13
getch(); break;
}
OpenList(pL2, F); fclose(F);
pL3 = CrossLists(pL1, pL2); DeleteList(pL2); Menu2(pL3);
break;
}
case '0':
{
system("cls");
printf("Enter the name of file: \n"); gets(FileName);
F = fopen(FileName, "r"); if (!F)
{
printf("Error open file\n\nPress any key"); getch();
break;
}
OpenList(pL2, F); fclose(F);
pL3 = SubtrLists(pL1, pL2); DeleteList(pL2); Menu2(pL3);
break;
}
case 27:
{
DeleteList(pL1); return;
}
default:
{
system("cls");
printf("Wrong instruction\n\nPress any key"); getch();
break;
}
}
}
}
void Menu3(item*& pL1, item*& pCur)
{
const char* MENU_3 = "______\n\n \
1 - Delete item\n \ Esc - Back\n";
char Key;
for(;;)
{
system("cls"); puts(pCur->key); puts(MENU_3); Key = getch(); switch (Key)
{
case '1':
{
DeleteItem(pL1, pCur); return;
}
14
case 27:
{
return;
}
default:
{
system("cls");
printf("Wrong instruction\n\nPress any key"); getch();
break;
}
}
}
}
15