197 lines
5.3 KiB
C++
197 lines
5.3 KiB
C++
/***************************************************/
|
|
/* LAB 1 I PUMA */
|
|
/* CRILLE & ROBIN 971114 */
|
|
/***************************************************/
|
|
|
|
#include "lista.cpp"
|
|
#include "twindow.hpp"
|
|
|
|
/***************************************************/
|
|
/*SÄKER INMATNING */
|
|
/***************************************************/
|
|
int mataIn()
|
|
{
|
|
char buffer[BUFFERSIZE];
|
|
int f;
|
|
|
|
do {
|
|
cin >> buffer;
|
|
if (strcmp(buffer,"0") == 0)
|
|
return 0;
|
|
else {
|
|
f = atoi(buffer);
|
|
if (f!=0)
|
|
return f;
|
|
else
|
|
cout <<"Inget tal, forsok igen: ";
|
|
}
|
|
}while (!f);
|
|
}
|
|
|
|
/***************************************************/
|
|
/* _listInsert */
|
|
/***************************************************/
|
|
void _listInsert(listClass <TWindow> &minLista)
|
|
{
|
|
TWindow *newItem;
|
|
newItem = new TWindow(minLista.listLength(), 18,minLista.listLength()+8, 21,BLUE,WHITE);
|
|
if(newItem) {
|
|
cout << "Skriv in element!";
|
|
cout << *newItem;
|
|
newItem->editText();
|
|
if(minLista.newDuplicate(*newItem)) {
|
|
cout <<"\nElementet finns redan.";
|
|
getch();
|
|
}
|
|
else
|
|
minLista.listInsert(*newItem);
|
|
}
|
|
else
|
|
cout <<"\nMinnet tog slut";
|
|
}
|
|
|
|
/***************************************************/
|
|
/* _listDel */
|
|
/***************************************************/
|
|
void _listDel(listClass <TWindow> &minLista)
|
|
{
|
|
if(!minLista.listIsEmpty()) {
|
|
cout << "Vilken position vill du ta bort? ";
|
|
int position = mataIn();
|
|
if(minLista.posIsOk(position))
|
|
minLista.listDel(position);
|
|
else {
|
|
cout <<"Positionen är inte OK.";
|
|
getch();
|
|
}
|
|
}
|
|
else {
|
|
cout <<"Listan ar tom.";
|
|
getch();
|
|
}
|
|
}
|
|
|
|
/***************************************************/
|
|
/* _listSort */
|
|
/***************************************************/
|
|
void _listSort(listClass <TWindow> minLista)
|
|
{
|
|
if(!minLista.listIsEmpty()) {
|
|
minLista.listSort();
|
|
cout <<"Listan ar sorterad!...";
|
|
}
|
|
else
|
|
cout <<"Listan ar tom";
|
|
getch();
|
|
}
|
|
|
|
/***************************************************/
|
|
/* _listDisplay */
|
|
/***************************************************/
|
|
void _listDisplay(listClass <TWindow> minLista)
|
|
{
|
|
if(!minLista.listIsEmpty()) {
|
|
cout << "Elementen ar: \n";
|
|
minLista.listDisplay();
|
|
}
|
|
else
|
|
cout <<"Listan ar tom";
|
|
getch();
|
|
}
|
|
|
|
/***************************************************/
|
|
/* _listSeek */
|
|
/***************************************************/
|
|
void _listSeek(listClass <TWindow> minLista)
|
|
{
|
|
TWindow *soktVarde;
|
|
soktVarde = new TWindow(minLista.listLength(), 18,minLista.listLength()+8, 21,BLUE,WHITE);
|
|
if(!minLista.listIsEmpty()) {
|
|
cout << "Ange vad du vill soka efter: \n";
|
|
cout << *soktVarde;
|
|
soktVarde->editText();
|
|
if(minLista.listSeek(*soktVarde))
|
|
cout <<"\nElementet fanns med!";
|
|
else
|
|
cout <<"\nElementet kunde inte hittas.";
|
|
}
|
|
else
|
|
cout <<"Listan ar tom";
|
|
getch();
|
|
}
|
|
|
|
/***************************************************/
|
|
/* _listAntal */
|
|
/***************************************************/
|
|
void _listAntal(listClass <TWindow> minLista)
|
|
{
|
|
int antalElement;
|
|
if(minLista.listIsEmpty())
|
|
cout <<"Listan ar tom.";
|
|
else {
|
|
antalElement=minLista.listLength();
|
|
cout<<"Det finns "<< antalElement<< " element i listan";
|
|
}
|
|
getch();
|
|
}
|
|
|
|
/***************************************************/
|
|
/* _listIsEmpty */
|
|
/***************************************************/
|
|
void _listIsEmpty(listClass <TWindow> minLista)
|
|
{
|
|
if(minLista.listIsEmpty())
|
|
cout <<"Listan ar tom.";
|
|
else
|
|
cout <<"Det finns element i listan";
|
|
getch();
|
|
}
|
|
|
|
/***************************************************/
|
|
/*MENUITEMS */
|
|
/* precondition: TRUE */
|
|
/* postcondition: TRUE */
|
|
/***************************************************/
|
|
void menuItems()
|
|
{
|
|
clrscr();
|
|
cout <<" Lankad lista \n"
|
|
<<" ------------ \n\n"
|
|
<<" 1. Lagg till ett element \n"
|
|
<<" 2. Ta bort element \n"
|
|
<<" 3. Sortera \n"
|
|
<<" 4. Skriv ut alla element \n"
|
|
<<" 5. Sok efter ett element \n"
|
|
<<" 6. Kolla om listan ar tom \n"
|
|
<<" 7. Returnera antal element i listan \n"
|
|
<<" 0. Avsluta \n\n";
|
|
}
|
|
|
|
/***************************************************/
|
|
/*MAINFUNCTION */
|
|
/* precondition: TRUE */
|
|
/* postcondition: TRUE */
|
|
/***************************************************/
|
|
void main()
|
|
{
|
|
listClass<TWindow> minLista;
|
|
char val = TRUE;
|
|
do {
|
|
menuItems();
|
|
cin >> val;
|
|
switch (val)
|
|
{
|
|
case '1' : _listInsert(minLista);break;
|
|
case '2' : _listDel(minLista);break;
|
|
case '3' : _listSort(minLista);break;
|
|
case '4' : _listDisplay(minLista);break;
|
|
case '5' : _listSeek(minLista);break;
|
|
case '6' : _listIsEmpty(minLista);break;
|
|
case '7' : _listAntal(minLista);break;
|
|
case '0' : cout <<"Programmet avslutat"<< endl;break;
|
|
default: cout <<"Fel val"<< endl;
|
|
}
|
|
}while (val != '0');
|
|
}
|
|
|