Startpunkten
This commit is contained in:
272
Binary_Search_Tree/huvud.cpp
Normal file
272
Binary_Search_Tree/huvud.cpp
Normal file
@@ -0,0 +1,272 @@
|
||||
//########################################################
|
||||
// LAB 4 I PUMA
|
||||
// CRILLE & ROBIN 980120
|
||||
// BINÄRT SÖK TRÄD
|
||||
//========================================================
|
||||
|
||||
#ifndef _HEADER_
|
||||
#define _HEADER_ //definiera _HEADER_
|
||||
#include <iostream.h> //för in och utmatning
|
||||
#include <conio.h> //för getch()
|
||||
#include <assert.h> //för assert()
|
||||
#include <stddef.h> //för definition av NULL
|
||||
#include <string.h> //för säker inmatning
|
||||
#include <stdlib.h> //för atoi()
|
||||
#include <fstream.h> //för save & load
|
||||
|
||||
const int FALSE = 0; //boolsk variabel
|
||||
const int TRUE = !FALSE; //boolsk variabel
|
||||
const int BUFFERSIZE = 100; //för säker inmatning
|
||||
const int NAMESIZE = 8; //för MS-DOS 6.0
|
||||
|
||||
typedef int bool; //definierar boolsk variabel
|
||||
typedef int dataType; //sätter datatypen
|
||||
struct treeNode; //förvarnar datorn om en struct
|
||||
typedef treeNode* ptrType; //ger en pekartype till en struct
|
||||
struct treeNode //en nod i trädet
|
||||
{
|
||||
dataType data;
|
||||
ptrType left;
|
||||
ptrType right;
|
||||
treeNode(dataType item, ptrType left, ptrType right);
|
||||
};
|
||||
#include "tree.cpp" //inkluderar trädets funktioner
|
||||
#endif //avslutar _HEADER_ definition
|
||||
|
||||
//#####################################################
|
||||
// SÄKER INMATNING
|
||||
// ser till att bara heltal matas in
|
||||
// returnerar värdet om ok, annars nytt försök
|
||||
//=====================================================
|
||||
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, försok igen: ";
|
||||
}
|
||||
}while (!f);
|
||||
}
|
||||
//########################################################
|
||||
// SÄTTER IN EN NOD I TRÄD
|
||||
// calls: treeClass::insert
|
||||
// calls: mataIn
|
||||
//========================================================
|
||||
void _insert(treeClass &myTree)
|
||||
{
|
||||
dataType item;
|
||||
cout <<" Vad vill du sätta in i trädet: ";
|
||||
item=mataIn();
|
||||
myTree.insert(myTree.root,item);
|
||||
}
|
||||
//########################################################
|
||||
// TAR BORT EN NOD UR TRÄD
|
||||
// calls: treeClass::del
|
||||
// calls: treeClass::isEmpty
|
||||
// calls: mataIn
|
||||
//========================================================
|
||||
void _del(treeClass &myTree)
|
||||
{
|
||||
if(!myTree.isEmpty())
|
||||
{
|
||||
dataType item;
|
||||
cout << " Vad vill du ta bort : ";
|
||||
item=mataIn();
|
||||
if(myTree.seek(myTree.root, item) != NULL)
|
||||
{
|
||||
myTree.del(myTree.root, item);
|
||||
return;
|
||||
}
|
||||
else
|
||||
cout << " Noden " << item << " finns inte. ";
|
||||
}
|
||||
else
|
||||
cout << " Du har inte gjort något träd ännu... ";
|
||||
getch();
|
||||
}
|
||||
//########################################################
|
||||
// SKRIVER UT I PREORDER
|
||||
// calls: treeClass::preOrder
|
||||
// calls: treeClass::isEmpty
|
||||
// calls: mataIn
|
||||
//========================================================
|
||||
void _preOrder(treeClass myTree)
|
||||
{
|
||||
if(!myTree.isEmpty())
|
||||
myTree.preOrder(myTree.root);
|
||||
else
|
||||
cout << " Du har inte gjort något träd ännu... ";
|
||||
getch();
|
||||
}
|
||||
//########################################################
|
||||
// SKRIVER UT I INORDER
|
||||
// calls: treeClass::inOrder
|
||||
// calls: treeClass::isEmpty
|
||||
// calls: mataIn
|
||||
//========================================================
|
||||
void _inOrder(treeClass myTree)
|
||||
{
|
||||
if(!myTree.isEmpty())
|
||||
myTree.inOrder(myTree.root);
|
||||
else
|
||||
cout << " Du har inte gjort något träd ännu... ";
|
||||
getch();
|
||||
}
|
||||
//########################################################
|
||||
// SKRIVER UT I POSTORDER
|
||||
// calls: treeClass::postOrder
|
||||
// calls: treeClass::isEmpty
|
||||
// calls: mataIn
|
||||
//========================================================
|
||||
void _postOrder(treeClass myTree)
|
||||
{
|
||||
if(!myTree.isEmpty())
|
||||
myTree.postOrder(myTree.root);
|
||||
else
|
||||
cout << " Du har inte gjort något träd ännu... ";
|
||||
getch();
|
||||
}
|
||||
//########################################################
|
||||
// SÖKER I TRÄDET
|
||||
// calls: treeClass::seek
|
||||
// calls: treeClass::isEmpty
|
||||
// calls: mataIn
|
||||
//========================================================
|
||||
void _seek(treeClass myTree)
|
||||
{
|
||||
if(!myTree.isEmpty())
|
||||
{
|
||||
dataType item;
|
||||
cout << " Mata in sökta värdet: ";
|
||||
item=mataIn();
|
||||
if(myTree.seek(myTree.root, item) != NULL)
|
||||
cout << " Ja, noden "<< item << " fanns med...";
|
||||
else
|
||||
cout << " Nej, noden "<< item << " fanns inte med...";
|
||||
}
|
||||
else
|
||||
cout << " Du har inte gjort något träd ännu... ";
|
||||
getch();
|
||||
}
|
||||
//########################################################
|
||||
// SPARAR TRÄDET PÅ FIL
|
||||
// calls: treeClass::save
|
||||
// calls: treeClass::isEmpty
|
||||
//========================================================
|
||||
void _save(treeClass myTree)
|
||||
{
|
||||
if(!myTree.isEmpty())
|
||||
{
|
||||
fstream fil;
|
||||
char filnamn[NAMESIZE];
|
||||
cout << " Ange filnamn att spara: ";
|
||||
cin >> filnamn;
|
||||
fil.open(filnamn,ios::out|ios::binary);
|
||||
myTree.save(myTree.root, fil);
|
||||
fil.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << " Du har inte gjort något träd ännu...";
|
||||
getch();
|
||||
}
|
||||
}
|
||||
//########################################################
|
||||
// HÄMTAR ETT TRÄD FRÅN EN FIL
|
||||
// läser in värdet i varje nod i preorder
|
||||
// och sätter in det i trädet i preoder
|
||||
// calls treeClass::insert
|
||||
// calls: treeClass::isEmpty
|
||||
//========================================================
|
||||
void _load(treeClass &myTree)
|
||||
{
|
||||
if(myTree.isEmpty())
|
||||
{
|
||||
fstream fil;
|
||||
dataType item;
|
||||
char filnamn[NAMESIZE];
|
||||
cout << " Ange filnamn att hämta: ";
|
||||
cin >> filnamn;
|
||||
fil.open(filnamn,ios::in|ios::binary);
|
||||
if(fil)
|
||||
while(fil.peek() != EOF)
|
||||
{
|
||||
fil.read((char*)&item,sizeof(item));
|
||||
myTree.insert(myTree.root, item);
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << " Filen finns inte...";
|
||||
getch();
|
||||
}
|
||||
fil.close();
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << " Det finns redan värden i trädet. \n"
|
||||
<< " Du måste ta bort samtliga noder \n"
|
||||
<< " eller starta om programmet före \n"
|
||||
<< " du kan hämta ett träd från fil \n";
|
||||
getch();
|
||||
}
|
||||
}
|
||||
//########################################################
|
||||
// MENY ITEMS
|
||||
// skriver ut menyn på skärmen
|
||||
//========================================================
|
||||
void meny()
|
||||
{
|
||||
clrscr();
|
||||
cout << " \n BINÄRT SÖK TRÄD "
|
||||
<< " \n --------------- "
|
||||
<< " \n 1. Lägg till nod "
|
||||
<< " \n 2. Ta bort nod "
|
||||
<< " \n 3. Sök efter nod "
|
||||
<< " \n 4. Skriv ut i preorder "
|
||||
<< " \n 5. Skriv ut i inorder "
|
||||
<< " \n 6. Skriv ut i postorder "
|
||||
<< " \n 7. Spara trädet på fil "
|
||||
<< " \n 8. Hämta träd från fil "
|
||||
<< " \n 0. Avsluta ";
|
||||
gotoxy(2,14); //ställer markören under menyn
|
||||
}
|
||||
//########################################################
|
||||
// MAIN FUNCTION
|
||||
// skapar trädet myTree av typen treeClass
|
||||
// calls: alla drivrutiner
|
||||
//========================================================
|
||||
void main()
|
||||
{
|
||||
treeClass myTree;
|
||||
char val = TRUE;
|
||||
do
|
||||
{
|
||||
meny();
|
||||
cin >> val;
|
||||
switch (val)
|
||||
{
|
||||
case '1' : _insert(myTree);break;
|
||||
case '2' : _del(myTree);break;
|
||||
case '3' : _seek(myTree);break;
|
||||
case '4' : _preOrder(myTree);break;
|
||||
case '5' : _inOrder(myTree);break;
|
||||
case '6' : _postOrder(myTree);break;
|
||||
case '7' : _save(myTree);break;
|
||||
case '8' : _load(myTree);break;
|
||||
case '0' : cout << " Programmet avslutat";break;
|
||||
default : cout << " Felaktigt val";
|
||||
getch();
|
||||
}
|
||||
}while(val != '0');
|
||||
}
|
||||
|
||||
226
Binary_Search_Tree/tree.cpp
Normal file
226
Binary_Search_Tree/tree.cpp
Normal file
@@ -0,0 +1,226 @@
|
||||
//########################################################
|
||||
// TREE.CPP
|
||||
// innehåller funktioner för trädet
|
||||
//========================================================
|
||||
|
||||
class treeClass
|
||||
{
|
||||
public:
|
||||
treeClass();
|
||||
~treeClass(){};
|
||||
ptrType root;
|
||||
ptrType seek(ptrType root, dataType item);
|
||||
bool isEmpty();
|
||||
void insert(ptrType &root,dataType item);
|
||||
void del(ptrType &root,dataType item);
|
||||
void delRoot(ptrType &root);
|
||||
void delLeft(ptrType &root, dataType &item);
|
||||
void preOrder(ptrType treeNode);
|
||||
void inOrder(ptrType treeNode);
|
||||
void postOrder(ptrType treeNode);
|
||||
void save(ptrType root, fstream &fil);
|
||||
};
|
||||
//########################################################
|
||||
// SKAPAR ETT TRÄD & SÄTTER ROOT TILL NULL
|
||||
//========================================================
|
||||
treeClass::treeClass():root(NULL)
|
||||
{
|
||||
}
|
||||
//########################################################
|
||||
// SKAPAR EN NOD
|
||||
//========================================================
|
||||
treeNode::treeNode(dataType item, ptrType L, ptrType R):data(item),left(L),right(R)
|
||||
{
|
||||
}
|
||||
//########################################################
|
||||
// KOLLAR OM DET FINNS NODER I TRÄDET
|
||||
// pre: TRUE
|
||||
// post: TRUE om trädet är tomt
|
||||
// post: FALSE om det finns noder i trädet
|
||||
//========================================================
|
||||
bool treeClass::isEmpty()
|
||||
{
|
||||
if (root==NULL)
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
//########################################################
|
||||
// SKRIVER UT TRÄDET I PREORDER
|
||||
// pre: att trädet inte är tomt
|
||||
// post: trädet har skrivits ut i preOrder
|
||||
// calls: preOrder (rekursivt)
|
||||
//========================================================
|
||||
void treeClass::preOrder(ptrType treeNode)
|
||||
{
|
||||
if(treeNode)
|
||||
{
|
||||
cout << " ";
|
||||
cout << treeNode->data;
|
||||
preOrder(treeNode->left);
|
||||
preOrder(treeNode->right);
|
||||
}
|
||||
}
|
||||
//########################################################
|
||||
// SKRIVER UT TRÄDET I INORDER
|
||||
// pre: att trädet inte är tomt
|
||||
// post: trädet har skrivits ut i inOrder
|
||||
// calls: inOrder (rekursivt)
|
||||
//========================================================
|
||||
void treeClass::inOrder(ptrType treeNode)
|
||||
{
|
||||
if(treeNode)
|
||||
{
|
||||
inOrder(treeNode->left);
|
||||
cout << treeNode->data << " ";
|
||||
inOrder(treeNode->right);
|
||||
}
|
||||
}
|
||||
//########################################################
|
||||
// SKRIVER UT TRÄDET I POSTORDER
|
||||
// pre: att trädet inte är tomt
|
||||
// post: trädet har skrivits ut i postOrder
|
||||
// calls: postOrder (rekursivt)
|
||||
//========================================================
|
||||
void treeClass::postOrder(ptrType treeNode)
|
||||
{
|
||||
if(treeNode)
|
||||
{
|
||||
postOrder(treeNode->left);
|
||||
postOrder(treeNode->right);
|
||||
cout << treeNode->data << " ";
|
||||
}
|
||||
}
|
||||
//########################################################
|
||||
// SÖKER EFTER ETT ELEMENT I TRÄDET
|
||||
// pre: att trädet inte är tomt
|
||||
// post: NULL om noden inte fanns
|
||||
// post: sökta värdet om värdet fanns
|
||||
// calls: seek (rekursivt)
|
||||
//========================================================
|
||||
ptrType treeClass::seek(ptrType root, dataType item)
|
||||
{
|
||||
ptrType treeNodeFound;
|
||||
if(root == NULL)
|
||||
treeNodeFound = NULL;
|
||||
else if (root->data == item)
|
||||
treeNodeFound = root;
|
||||
else if (root->data > item)
|
||||
treeNodeFound = seek(root->left, item);
|
||||
else
|
||||
treeNodeFound = seek(root->right, item);
|
||||
return treeNodeFound;
|
||||
}
|
||||
//########################################################
|
||||
// SÄTTER IN EN NOD I TRÄDET
|
||||
// pre: att inmatningen är ett heltal
|
||||
// post: trädet har fått en ny nod insatt
|
||||
// på rätt plats i trädet.
|
||||
// calls: insert (rekursivt)
|
||||
//========================================================
|
||||
void treeClass::insert(ptrType &root,dataType item)
|
||||
{
|
||||
if(root==NULL)
|
||||
root=new treeNode(item,NULL,NULL);
|
||||
else if(item < root->data)
|
||||
insert(root->left,item);
|
||||
else
|
||||
insert(root->right,item);
|
||||
}
|
||||
//########################################################
|
||||
// TAR BORT EN NODEN TILL VÄNSTER
|
||||
// pre: att det finns ett träd
|
||||
// pre: att noden finns
|
||||
// post: noden har tagits ur trädet och
|
||||
// de andra noderna sitter nu rätt placerade
|
||||
// calls: delLeft (rekursivt)
|
||||
//========================================================
|
||||
void treeClass::delLeft(ptrType &root, dataType &item)
|
||||
{
|
||||
if((root != NULL) && (root->left == NULL))
|
||||
{
|
||||
item = root->data;
|
||||
ptrType delPtr = root;
|
||||
root = root->right;
|
||||
delPtr->right = NULL;
|
||||
delete delPtr;
|
||||
}
|
||||
else
|
||||
delLeft(root->left, item);
|
||||
}
|
||||
//########################################################
|
||||
// TAR BORT NODEN OM DEN ÄR EN ROT
|
||||
// pre: att det finns ett träd
|
||||
// pre: att noden finns
|
||||
// post: noden har tagits ur trädet och
|
||||
// de andra noderna sitter nu rätt placerade
|
||||
//========================================================
|
||||
void treeClass::delRoot(ptrType &root)
|
||||
{
|
||||
ptrType delPtr;
|
||||
dataType nyItem;
|
||||
|
||||
if(root != NULL)
|
||||
{
|
||||
if((root->left == NULL) && (root->right == NULL))
|
||||
{
|
||||
delete root;
|
||||
root = NULL;
|
||||
}
|
||||
else if(root->left == NULL)
|
||||
{
|
||||
delPtr = root;
|
||||
root = root->right;
|
||||
delPtr->right = NULL;
|
||||
delete delPtr;
|
||||
}
|
||||
else if(root->right == NULL)
|
||||
{
|
||||
delPtr = root;
|
||||
root = root->left;
|
||||
delPtr->left = NULL;
|
||||
delete delPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
delLeft(root->right, nyItem);
|
||||
root->data = nyItem;
|
||||
}
|
||||
}
|
||||
}
|
||||
//########################################################
|
||||
// TAR BORT EN NOD UR TRÄDET
|
||||
// pre: att det finns ett träd
|
||||
// pre: att noden finns
|
||||
// post: noden har tagits ur trädet och
|
||||
// de andra noderna sitter nu rätt placerade
|
||||
// calls: delRoot om noden är en rot
|
||||
// calls: del (rekursivt)
|
||||
//========================================================
|
||||
void treeClass::del(ptrType &root, dataType item)
|
||||
{
|
||||
if(item==root->data)
|
||||
delRoot(root);
|
||||
else if(item < root->data)
|
||||
del(root->left, item);
|
||||
else
|
||||
del(root->right,item);
|
||||
}
|
||||
//########################################################
|
||||
// SPARAR TRÄDET PÅ FIL
|
||||
// läser av nodens värde i alla noder i
|
||||
// preorder och skriver de på filen.
|
||||
// pre: att det finns ett träd
|
||||
// post: trädet har sparats på fil
|
||||
// calls: save (rekursivt)
|
||||
//========================================================
|
||||
void treeClass::save(ptrType root, fstream &fil)
|
||||
{
|
||||
if(root)
|
||||
{
|
||||
fil.write((char*)&root->data,sizeof(root));
|
||||
save(root->left,fil);
|
||||
save(root->right,fil);
|
||||
}
|
||||
}
|
||||
|
||||
242
Linked_List/clasfile.cpp
Normal file
242
Linked_List/clasfile.cpp
Normal file
@@ -0,0 +1,242 @@
|
||||
/***************************************************/
|
||||
/* IMPLEMENTERINGSFIL FVR LAB 0 I PUMA */
|
||||
/***************************************************/
|
||||
|
||||
#include "lab_0.h"
|
||||
|
||||
struct listNode
|
||||
{
|
||||
listItemType listContent;
|
||||
ptrType next;
|
||||
};
|
||||
|
||||
/***************************************************/
|
||||
/*SDTTER LISTSTORLEKEN TILL NOLL */
|
||||
/***************************************************/
|
||||
listClass::listClass():size(0), head(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
listClass::listClass(const listClass &L):
|
||||
size(L.size)
|
||||
{
|
||||
if (L.head == NULL)
|
||||
head = NULL;
|
||||
else
|
||||
{
|
||||
head = new listNode;
|
||||
assert(head != NULL);
|
||||
head ->listContent = L.head -> listContent;
|
||||
ptrType newPrev = head;
|
||||
for (ptrType origCur = L.head -> next ; origCur != NULL ; origCur = origCur -> next)
|
||||
{
|
||||
newPrev -> next = new listNode;
|
||||
assert(newPrev -> next != NULL);
|
||||
newPrev = newPrev -> next;
|
||||
newPrev -> listContent = origCur -> listContent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*GVR INGET, MEN MEN ANROPAS NDR LISTAN DVR */
|
||||
/***************************************************/
|
||||
listClass::~listClass()
|
||||
{
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KOLLAR OM LISTAN DR TOM */
|
||||
/***************************************************/
|
||||
int listClass::listIsEmpty()
|
||||
{
|
||||
return int (size == 0);
|
||||
}
|
||||
|
||||
int listClass::listLength()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
|
||||
ptrType listClass::ptrTo(int position)
|
||||
{
|
||||
if ((position < 1) || (position > listLength()))
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
ptrType trav = head;
|
||||
for (int skip = 1 ; skip < position ; skip++)
|
||||
trav = trav -> next;
|
||||
return trav;
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*LDGGER TILL ETT NYTT ELEMENT I LISTAN */
|
||||
/*precondition: att listelementet dr ett heltal*/
|
||||
/* precondition: att listelementet inte finns */
|
||||
/* postcondition: listan har ett nytt element */
|
||||
/***************************************************/
|
||||
void listClass::listInsert(listItemType newItem)
|
||||
{
|
||||
int newPosition=1;
|
||||
int newLength = listLength() + 1;
|
||||
{
|
||||
size = newLength;
|
||||
ptrType newPtr = new listNode;
|
||||
if (int(newPtr != NULL))
|
||||
{
|
||||
newPtr -> listContent = newItem;
|
||||
if(newPosition == 1)
|
||||
{
|
||||
newPtr ->next = head;
|
||||
head = newPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptrType prev = ptrTo(newPosition-1);
|
||||
newPtr -> next = prev -> next;
|
||||
prev -> next = newPtr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*TAR BORT ELEMENT I LISTAN */
|
||||
/*precondition: att positionen finns */
|
||||
/* precondition: att positionen dr ett heltal */
|
||||
/* precondition*: att positionen >0 */
|
||||
/*postcondition: elementet har tagits bort */
|
||||
/***************************************************/
|
||||
void listClass::listDel(listItemType position)
|
||||
{
|
||||
ptrType cur;
|
||||
|
||||
if ((position >=1) && (position <= listLength()))
|
||||
{
|
||||
size--;
|
||||
if (position == 1)
|
||||
{
|
||||
cur = head;
|
||||
head = head -> next;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptrType prev = ptrTo(position-1);
|
||||
cur = prev -> next;
|
||||
prev -> next = cur ->next;
|
||||
}
|
||||
cur -> next = NULL;
|
||||
delete cur;
|
||||
cur = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*SKRIVER UT LISTAN PE SKDRMEN */
|
||||
/*precondition: TRUE */
|
||||
/*postcondition: listan har skrivits ut */
|
||||
/***************************************************/
|
||||
void listClass::listDisplay()
|
||||
{
|
||||
|
||||
cout << "Elementen dr: \n";
|
||||
for(ptrType cur = head ; cur != NULL ; cur = cur->next)
|
||||
cout << cur->listContent << endl;
|
||||
getch();
|
||||
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*SORTERAR LISTAN */
|
||||
/*precondition: TRUE */
|
||||
/*postcondition: elementen ligger i ordning */
|
||||
/***************************************************/
|
||||
void listClass::listSort()
|
||||
{
|
||||
listItemType temp;
|
||||
int bytt;
|
||||
|
||||
do
|
||||
{
|
||||
bytt=FALSE;
|
||||
for(ptrType cur=head ; cur->next != NULL ; cur = cur->next)
|
||||
if(cur->listContent > cur->next->listContent)
|
||||
{
|
||||
temp = cur->listContent;
|
||||
cur->listContent = cur->next->listContent;
|
||||
cur->next->listContent = temp;
|
||||
bytt=TRUE;
|
||||
}
|
||||
}while(bytt);
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*SVKER EFTER ETT ELEMENT I LISTAN */
|
||||
/* precondition: att svkta vdrdet dr ett heltal */
|
||||
/***************************************************/
|
||||
void listClass::listSeek(listItemType soktVarde)
|
||||
{
|
||||
ptrType cur;
|
||||
|
||||
for(cur=head ; cur != NULL ; cur = cur->next)
|
||||
if(cur->listContent == soktVarde)
|
||||
{
|
||||
cout <<"Elementet fanns med!";
|
||||
getch();
|
||||
return;
|
||||
}
|
||||
cout <<"Sorry, det fanns inte...";
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KOLLAR OM ETT ELEMENT FINNS */
|
||||
/*precondition: att elementet dr ett heltal */
|
||||
/*postcondition: returnerar FALSE om ej finns */
|
||||
/***************************************************/
|
||||
int listClass::newDuplicate(newItem)
|
||||
{
|
||||
ptrType cur;
|
||||
|
||||
for(cur=head ; cur != NULL ; cur = cur->next)
|
||||
if(cur->listContent == newItem)
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KONTROLLERAR OM DET FINNS ELEMENT I LISTAN */
|
||||
/*precondition: TRUE */
|
||||
/***************************************************/
|
||||
void listClass::listIsItEmpty()
|
||||
{
|
||||
if(head == NULL)
|
||||
cout <<"Listan dr tom..." << endl;
|
||||
else
|
||||
cout <<"Det finns element i listan...";
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*VISAR ANTALET ELEMENT I LISTAN */
|
||||
/*precondition: TRUE */
|
||||
/***************************************************/
|
||||
void listClass::listAntal()
|
||||
{
|
||||
cout <<"Det finns " << size << " element i listan.";
|
||||
getch();
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KONTROLLERAR OM EN POSITION FINNS */
|
||||
/*precondition: att positionen dr ett heltal */
|
||||
/***************************************************/
|
||||
int listClass::posIsOk(int position)
|
||||
{
|
||||
if((position <= size) && (position > 0))
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
47
Linked_List/lab_0.h
Normal file
47
Linked_List/lab_0.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*************************************************/
|
||||
/*HEADERFIL LABO.H */
|
||||
/*************************************************/
|
||||
|
||||
#ifndef _lab0_
|
||||
#define _lab0_
|
||||
#include <stddef.h> //för NULL
|
||||
#include <assert.h> //för assert()
|
||||
#include <iostream.h> //för in-ut matning
|
||||
#include <conio.h> //för getch();
|
||||
#include <string.h> //för säker inmatning
|
||||
#include <stdlib.h> //för atoi()
|
||||
|
||||
const int MAXLISTA = 20;
|
||||
const int BUFFERSIZE = 100;
|
||||
const int FALSE=0;
|
||||
const int TRUE=!FALSE;
|
||||
|
||||
typedef int listItemType;
|
||||
struct listNode;
|
||||
typedef listNode *ptrType;
|
||||
|
||||
class listClass
|
||||
{
|
||||
public:
|
||||
listClass();
|
||||
~listClass();
|
||||
listClass(const listClass &L);
|
||||
int listIsEmpty();
|
||||
int listLength();
|
||||
int posIsOk(position);
|
||||
int newDuplicate(newItem);
|
||||
void listInsert(newItem);
|
||||
void listDel(position);
|
||||
void listSort();
|
||||
void listDisplay();
|
||||
void listSeek(soktVarde);
|
||||
void listIsItEmpty();
|
||||
void listAntal();
|
||||
private:
|
||||
ptrType ptrTo(int position);
|
||||
int size;
|
||||
ptrType head;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
102
Linked_List/mainfile.cpp
Normal file
102
Linked_List/mainfile.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
/***************************************************/
|
||||
/* LAB 0 I PUMA */
|
||||
/* CRILLE & ROBIN 971114 */
|
||||
/***************************************************/
|
||||
|
||||
#include "lab_0.h"
|
||||
|
||||
/***************************************************/
|
||||
/*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, försök igen: ";
|
||||
}
|
||||
}while (!f);
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*MENYVAL */
|
||||
/***************************************************/
|
||||
void menuItems()
|
||||
{
|
||||
clrscr();
|
||||
cout <<" 1. Lägg till ett element \n"
|
||||
<<" 2. Ta bort element \n"
|
||||
<<" 3. Sortera \n"
|
||||
<<" 4. Skriv ut alla element \n"
|
||||
<<" 5. Sök efter ett element \n"
|
||||
<<" 6. Kolla om listan är tom \n"
|
||||
<<" 7. Returnera antal element i listan \n"
|
||||
<<" 0. Avsluta \n\n\n"
|
||||
<< endl;
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*MENU TILL PROGRAMMET */
|
||||
/***************************************************/
|
||||
void main()
|
||||
{
|
||||
listClass minLista;
|
||||
listItemType newItem;
|
||||
|
||||
char val = TRUE;
|
||||
do {
|
||||
menuItems();
|
||||
cin >> val;
|
||||
switch (val)
|
||||
{
|
||||
case '1' :
|
||||
cout << "skriv in element!\n";
|
||||
newItem = mataIn();
|
||||
if(!minLista.newDuplicate(newItem))
|
||||
minLista.listInsert(newItem);
|
||||
else {
|
||||
cout <<"Elementet finns redan";
|
||||
getch();
|
||||
}
|
||||
break;
|
||||
case '2' :
|
||||
int position;
|
||||
cout << "Vilken position vill du ta bort? ";
|
||||
position = mataIn();
|
||||
if(minLista.posIsOk(position))
|
||||
minLista.listDel(position);
|
||||
else {
|
||||
cout <<"Posten finns inte...";
|
||||
getch();
|
||||
}
|
||||
break;
|
||||
case '3' :
|
||||
minLista.listSort();
|
||||
cout <<"Listan är sorterad!...";
|
||||
getch();break;
|
||||
case '4' :
|
||||
minLista.listDisplay();break;
|
||||
case '5' :
|
||||
listItemType soktVarde;
|
||||
cout << "Ange vad du vill söka efter: ";
|
||||
soktVarde = mataIn();
|
||||
minLista.listSeek(soktVarde);break;
|
||||
case '6' :
|
||||
minLista.listIsItEmpty();break;
|
||||
case '7' :
|
||||
minLista.listAntal();break;
|
||||
case '0' :
|
||||
cout <<"Programmet avslutat"<< endl;break;
|
||||
default: cout <<"Fel val"<< endl;
|
||||
}
|
||||
}while (val != '0');
|
||||
}
|
||||
6
Stack_And_Queue/boolean.h
Normal file
6
Stack_And_Queue/boolean.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef __BOOLEAN_H_
|
||||
|
||||
#define __BOOLEAN_H_
|
||||
|
||||
typedef int boolean;
|
||||
|
||||
345
Stack_And_Queue/huvud.cpp
Normal file
345
Stack_And_Queue/huvud.cpp
Normal file
@@ -0,0 +1,345 @@
|
||||
//#####################################################
|
||||
// LAB 2 I PUMA
|
||||
// CRILLE & ROBIN 971208
|
||||
// LÄNKAD TEMPLATE LISTA
|
||||
//=====================================================
|
||||
|
||||
#include "lista.cpp"
|
||||
#include "twindow.hpp"
|
||||
|
||||
const int BUFFERSIZE = 100; //för säkerinmatning
|
||||
const int MAXSIZE = 5; //maximalt antal element
|
||||
const int SIST = 0; //inmatning i listan
|
||||
const int FORST=!SIST; //inmatning i listan
|
||||
const int FALSE=0; //boolsk varabel
|
||||
const int TRUE=!FALSE; //boolsk varabel
|
||||
|
||||
const int BREDD = 9; //fönstrets bredd
|
||||
const int HOJD = 3; //fönstrets höjd
|
||||
const int LEFT = 1; //fönstrets Y-position
|
||||
const int TOP = 18; //fönstrets X-position
|
||||
|
||||
const int moveX = 0; //fönsterflyttning X-led
|
||||
const int moveY = 5; //fönsterflyttning Y-led
|
||||
|
||||
//#####################################################
|
||||
// SÄKER INMATNING
|
||||
// SER TILL ATT BARA HELTAL MATAS IN
|
||||
//=====================================================
|
||||
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);
|
||||
}
|
||||
//#####################################################
|
||||
// SÄTTER IN ELEMENT I STACKEN
|
||||
//=====================================================
|
||||
void _push(stackClass <TWindow> &minStack)
|
||||
{
|
||||
int position = FORST;
|
||||
TWindow *newItem;
|
||||
newItem = new TWindow(LEFT, TOP,(BREDD+LEFT),(HOJD+TOP),BLUE,WHITE);
|
||||
if(minStack.length() < MAXSIZE)
|
||||
if(newItem)
|
||||
{
|
||||
cout << "Skriv in element!";
|
||||
cout << *newItem;
|
||||
newItem->editText();
|
||||
if(minStack.duplicate(*newItem))
|
||||
cout <<"\nElementet finns redan.";
|
||||
else
|
||||
{
|
||||
minStack.push(position, *newItem);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
cout <<"\nMinnet tog slut";
|
||||
else
|
||||
cout <<"Maximalt antal element i stacken";
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// SÄTTER IN ELEMENT I KÖN
|
||||
//=====================================================
|
||||
void _ins(queueClass <TWindow> &minQueue)
|
||||
{
|
||||
TWindow *newItem;
|
||||
int position = SIST;
|
||||
newItem = new TWindow(LEFT, TOP,(BREDD+LEFT),(HOJD+TOP),BLUE,WHITE);
|
||||
if(minQueue.length() < MAXSIZE)
|
||||
if(newItem)
|
||||
{
|
||||
cout << "Skriv in element!";
|
||||
cout << *newItem;
|
||||
newItem->editText();
|
||||
if(minQueue.duplicate(*newItem))
|
||||
cout <<"\nElementet finns redan.";
|
||||
else
|
||||
{
|
||||
minQueue.ins(position, *newItem);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
cout <<"\nMinnet tog slut";
|
||||
else
|
||||
cout <<"Maximalt antal element i kon";
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// TAR BORT ELEMENT I STACKEN
|
||||
//=====================================================
|
||||
void _pop(stackClass <TWindow> &minStack)
|
||||
{
|
||||
if(!minStack.isEmpty())
|
||||
minStack.pop();
|
||||
else
|
||||
{
|
||||
cout <<"Stacken ar tom.";
|
||||
getch();
|
||||
}
|
||||
}
|
||||
//#####################################################
|
||||
// TAR BORT ELEMENT I KÖN
|
||||
//=====================================================
|
||||
void _del(queueClass <TWindow> &minQueue)
|
||||
{
|
||||
if(!minQueue.isEmpty())
|
||||
minQueue.del();
|
||||
else
|
||||
{
|
||||
cout <<"Kon ar tom.";
|
||||
getch();
|
||||
}
|
||||
}
|
||||
//#####################################################
|
||||
// VISAR ÖVERSTA ELEMENTET I STACKEN
|
||||
//=====================================================
|
||||
void _top(stackClass <TWindow> &minStack)
|
||||
{
|
||||
if(!minStack.isEmpty())
|
||||
minStack.top();
|
||||
else
|
||||
cout <<"Stacken ar tom.";
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// VISAR ÖVERSTA ELEMENTET I KÖN
|
||||
//=====================================================
|
||||
void _getFront(queueClass <TWindow> &minQueue)
|
||||
{
|
||||
if(!minQueue.isEmpty())
|
||||
minQueue.getFront();
|
||||
else
|
||||
cout <<"Kon ar tom.";
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// VISAR ALLA ELEMENT I STACKEN
|
||||
//=====================================================
|
||||
void _stackDisplay(stackClass <TWindow> minStack)
|
||||
{
|
||||
if(!minStack.isEmpty())
|
||||
{
|
||||
cout << "Elementen ar: \n";
|
||||
minStack.display();
|
||||
}
|
||||
else
|
||||
cout <<"Stacken ar tom";
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// VISAR ALLA ELEMENT I KÖN
|
||||
//=====================================================
|
||||
void _queueDisplay(queueClass <TWindow> minQueue)
|
||||
{
|
||||
if(!minQueue.isEmpty())
|
||||
{
|
||||
cout << "Elementen ar: \n";
|
||||
minQueue.display();
|
||||
}
|
||||
else
|
||||
cout <<"Kon ar tom";
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// VISAR ANTALET ELEMENT I STACKEN
|
||||
//=====================================================
|
||||
void _stackAntal(stackClass <TWindow> minStack)
|
||||
{
|
||||
int antalElement;
|
||||
if(minStack.isEmpty())
|
||||
cout <<"Stacken ar tom.";
|
||||
else
|
||||
{
|
||||
antalElement=minStack.length();
|
||||
cout<<"Det finns "<< antalElement<< " element i stacken";
|
||||
}
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// VISAR ANTALET ELEMENT I KÖN
|
||||
//=====================================================
|
||||
void _queueAntal(queueClass <TWindow> minQueue)
|
||||
{
|
||||
int antalElement;
|
||||
if(minQueue.isEmpty())
|
||||
cout <<"Kon ar tom.";
|
||||
else
|
||||
{
|
||||
antalElement=minQueue.length();
|
||||
cout<<"Det finns "<< antalElement<< " element i kon";
|
||||
}
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM DET FINNS ELEMENT I STACEKN
|
||||
//=====================================================
|
||||
void _stackIsEmpty(stackClass <TWindow> minStack)
|
||||
{
|
||||
if(minStack.isEmpty())
|
||||
cout <<"Ja, stacken ar tom.";
|
||||
else
|
||||
cout <<"Nej, det finns element i stacken";
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM DET FINNS ELEMENT I KÖN
|
||||
//=====================================================
|
||||
void _queueIsEmpty(queueClass <TWindow> minQueue)
|
||||
{
|
||||
if(minQueue.isEmpty())
|
||||
cout <<"Ja, kon ar tom.";
|
||||
else
|
||||
cout <<"Nej, det finns element i kon";
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// STACK-MENY
|
||||
//=====================================================
|
||||
void stackMenuItems()
|
||||
{
|
||||
clrscr();
|
||||
cout <<" Meny for stacken \n"
|
||||
<<" ---------------- \n\n"
|
||||
<<" 1. Lagg till ett element overst \n"
|
||||
<<" 2. Ta bort oversta elementet \n"
|
||||
<<" 3. Skriv ut oversta elementet \n"
|
||||
<<" 4. Skriv ut alla element \n"
|
||||
<<" 5. Hur många element finns det \n"
|
||||
<<" 6. Ar stacken tom? \n"
|
||||
<<" 0. Huvudmeny \n\n";
|
||||
}
|
||||
void stackMenu()
|
||||
{
|
||||
stackClass<TWindow> minStack;
|
||||
char val = TRUE;
|
||||
do
|
||||
{
|
||||
stackMenuItems();
|
||||
cin >> val;
|
||||
switch (val)
|
||||
{
|
||||
case '1' : _push(minStack);break;
|
||||
case '2' : _pop(minStack);break;
|
||||
case '3' : _top(minStack);break;
|
||||
case '4' : _stackDisplay(minStack);break;
|
||||
case '5' : _stackAntal(minStack);break;
|
||||
case '6' : _stackIsEmpty(minStack);break;
|
||||
case '0' : ;break;
|
||||
default : cout <<" Fel val.";
|
||||
}
|
||||
}while(val!='0');
|
||||
}
|
||||
//#####################################################
|
||||
// QUEUE-MENYN
|
||||
//=====================================================
|
||||
void queueMenuItems()
|
||||
{
|
||||
clrscr();
|
||||
cout <<" Meny for kon \n"
|
||||
<<" ---------------- \n\n"
|
||||
<<" 1. Lagg till ett element sist i kon \n"
|
||||
<<" 2. Ta bort forsta elementet \n"
|
||||
<<" 3. Skriv ut forsta elementet \n"
|
||||
<<" 4. Skriv ut alla element \n"
|
||||
<<" 5. Hur många element finns det? \n"
|
||||
<<" 6. Ar kon tom? \n"
|
||||
<<" 0. Huvudmeny \n\n";
|
||||
}
|
||||
void queueMenu()
|
||||
{
|
||||
queueClass<TWindow> minQueue;
|
||||
char val = TRUE;
|
||||
do
|
||||
{
|
||||
queueMenuItems();
|
||||
cin >> val;
|
||||
switch (val)
|
||||
{
|
||||
case '1' : _ins(minQueue);break;
|
||||
case '2' : _del(minQueue);break;
|
||||
case '3' : _getFront(minQueue);break;
|
||||
case '4' : _queueDisplay(minQueue);break;
|
||||
case '5' : _queueAntal(minQueue);break;
|
||||
case '6' : _queueIsEmpty(minQueue);break;
|
||||
case '0' : ;break;
|
||||
default : cout <<" Fel val.";
|
||||
}
|
||||
}while(val!='0');
|
||||
}
|
||||
//#####################################################
|
||||
// MAIN-MENU-ITEMS
|
||||
//=====================================================
|
||||
void mainMenuItems()
|
||||
{
|
||||
clrscr();
|
||||
TWindow justText;
|
||||
justText.setText("Crille &Robin!");
|
||||
cout << justText;
|
||||
gotoxy(LEFT,TOP);
|
||||
cout << " Continue...";
|
||||
getch();
|
||||
clrscr();
|
||||
cout <<" Huvudmeny for lab 2 i PUMA. \n"
|
||||
<<" --------------------------- \n\n"
|
||||
<<" 1. Jobba med en stack. \n"
|
||||
<<" 2. Jobba med en ko. \n"
|
||||
<<" 0. Avsluta programmet. \n\n";
|
||||
|
||||
}
|
||||
//#####################################################
|
||||
// MAINFUNCTION
|
||||
//=====================================================
|
||||
void main()
|
||||
{
|
||||
char val = TRUE;
|
||||
do
|
||||
{
|
||||
mainMenuItems();
|
||||
cin >> val;
|
||||
switch (val)
|
||||
{
|
||||
case '1' : stackMenu();break;
|
||||
case '2' : queueMenu();break;
|
||||
case '0' : cout <<" Programmet avslutat"<< endl;break;
|
||||
default: cout <<" Fel val"<< endl;
|
||||
}
|
||||
}while (val != '0');
|
||||
}
|
||||
|
||||
12
Stack_And_Queue/keycode.h
Normal file
12
Stack_And_Queue/keycode.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef __KEYCODE_H
|
||||
|
||||
#define __KEYCODE_H
|
||||
|
||||
const int LeftTop = 201;
|
||||
|
||||
const int RightTop = 187;
|
||||
|
||||
const int RightBottom = 188;
|
||||
|
||||
const int LeftBottom = 200;
|
||||
|
||||
177
Stack_And_Queue/lista.cpp
Normal file
177
Stack_And_Queue/lista.cpp
Normal file
@@ -0,0 +1,177 @@
|
||||
//#####################################################
|
||||
// LISTA.CPP
|
||||
//=====================================================
|
||||
|
||||
#include <stddef.h> //för NULL
|
||||
#include <assert.h> //för assert()
|
||||
#include <iostream.h> //för in-ut matning
|
||||
#include <conio.h> //för getch();
|
||||
#include <string.h> //för säker inmatning
|
||||
#include <stdlib.h> //för atoi()
|
||||
#include "stack.cpp"
|
||||
#include "queue.cpp"
|
||||
|
||||
template <class T>
|
||||
class listClass
|
||||
{
|
||||
public:
|
||||
listClass();
|
||||
~listClass();
|
||||
int listIsEmpty();
|
||||
int listLength();
|
||||
int newDuplicate(T newItem);
|
||||
T retrive(int position);
|
||||
void listInsert(int position, T newItem);
|
||||
void listDel();
|
||||
void listDisplay();
|
||||
void listAntal();
|
||||
private:
|
||||
struct listNode
|
||||
{
|
||||
T listContent;
|
||||
listNode* next;
|
||||
};
|
||||
typedef struct listNode listNode;
|
||||
typedef struct listNode* ptrType;
|
||||
ptrType head;
|
||||
int size;
|
||||
};
|
||||
//#####################################################
|
||||
// CONSTRUCTOR
|
||||
// SÄTTER LISTSTORLEKEN TILL NOLL & HEAD TILL NULL
|
||||
// pre: TRUE
|
||||
// post: en lista har skapats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
listClass<T>::listClass():size(0), head(NULL)
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// DESTRUCTOR
|
||||
// FÖRSTÖR LISTAN, ANROPAS AUTOMATISKT VID AVSLUT
|
||||
// pre: TRUE
|
||||
// post: listan har raderats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
listClass<T>::~listClass()
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM LISTAN ÄR TOM
|
||||
// pre: TRUE
|
||||
// post: TRUE om listan är tom
|
||||
// post: FALSE om listan inte är tom
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int listClass<T>::listIsEmpty()
|
||||
{
|
||||
return int (size == 0);
|
||||
}
|
||||
//#####################################################
|
||||
// RETURNERAR ANTALET ELEMENT I LISTAN
|
||||
// pre: TRUE
|
||||
// post: antalet element i listan är returnerade
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int listClass<T>::listLength()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
//#####################################################
|
||||
// LÄGGER TILL ETT NYTT ELEMENT I LISTAN
|
||||
// pre: minne fanns för att skapa fönstret
|
||||
// pre: att maximalt antal element inte finns
|
||||
// pre: att listelementet inte finns
|
||||
// post: listan har ett nytt element
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void listClass<T>::listInsert(int position, T newItem)
|
||||
{
|
||||
size++;
|
||||
ptrType newPtr = new listNode,cur;
|
||||
if(position == FORST)
|
||||
{
|
||||
newPtr -> listContent = newItem;
|
||||
newPtr ->next = head;
|
||||
head = newPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(size == 1)
|
||||
{
|
||||
newPtr -> listContent = newItem;
|
||||
newPtr ->next = head;
|
||||
head = newPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(cur=head ; cur!=NULL ; cur=cur->next)
|
||||
if(cur->next == NULL)
|
||||
{
|
||||
newPtr -> listContent = newItem;
|
||||
cur->next = newPtr;
|
||||
newPtr->next = NULL;
|
||||
}
|
||||
delete cur;
|
||||
cur = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
//#####################################################
|
||||
// TAR BORT ELEMENT I LISTAN
|
||||
// pre: att det finns en lista
|
||||
// post: ett elementet har tagits bort
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void listClass<T>::listDel()
|
||||
{
|
||||
ptrType cur=head;
|
||||
size--;
|
||||
head = head -> next;
|
||||
cur -> next = NULL;
|
||||
delete cur;
|
||||
cur = NULL;
|
||||
}
|
||||
//#####################################################
|
||||
// SKRIVER UT LISTAN PÅ SKÄRMEN
|
||||
// pre: att det finns en lista
|
||||
// post: listan har skrivits ut
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void listClass<T>::listDisplay()
|
||||
{
|
||||
TWindow ruta;
|
||||
for(ptrType cur = head ; cur != NULL ; cur = cur->next)
|
||||
{
|
||||
ruta=cur->listContent;
|
||||
cout << ruta;
|
||||
ruta.move(moveX,moveY);
|
||||
}
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM ETT ELEMENT FINNS
|
||||
// pre: att det finns en lista
|
||||
// post: TRUE om elementet redan finns
|
||||
// post: FALSE om elementet inte redan finns
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int listClass<T>::newDuplicate(T newItem)
|
||||
{
|
||||
ptrType cur;
|
||||
for(cur=head ; cur != NULL ; cur = cur->next)
|
||||
if(cur->listContent == newItem)
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
//#####################################################
|
||||
// HÄMTAR ÖVERSTA ELEMENTET
|
||||
// pre: att det finns en lista
|
||||
// post: det översta elementet har returnerats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
T listClass<T>::retrive(int position)
|
||||
{
|
||||
ptrType cur=head;
|
||||
for(int i=0 ; i<position-1 ; i++)
|
||||
cur=cur->next;
|
||||
return (cur->listContent);
|
||||
119
Stack_And_Queue/queue.cpp
Normal file
119
Stack_And_Queue/queue.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
//#####################################################
|
||||
// QUEUE.CPP
|
||||
//=====================================================
|
||||
|
||||
template <class T>
|
||||
class queueClass
|
||||
{
|
||||
public:
|
||||
queueClass();
|
||||
~queueClass();
|
||||
int length();
|
||||
int duplicate(T newItem);
|
||||
void ins(int position, T newItem);
|
||||
void del();
|
||||
int isEmpty();
|
||||
void getFront();
|
||||
void display();
|
||||
private:
|
||||
listClass<TWindow>minLista;
|
||||
typedef struct queueNode* ptrType;
|
||||
ptrType head;
|
||||
int size;
|
||||
};
|
||||
|
||||
//#####################################################
|
||||
// CONSTRUCTOR
|
||||
// SÄTTER KÖSTORLEKEN TILL NOLL & HEAD TILL NULL
|
||||
// pre: TRUE
|
||||
// post: en kö har skapats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
queueClass<T>::queueClass():size(0), head(NULL)
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// DESTRUCTOR
|
||||
// FÖRSTÖR KÖN, ANROPAS AUTOMATISKT VID AVSLUT
|
||||
// pre: att det finns en kö
|
||||
// post: kön har raderats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
queueClass<T>::~queueClass()
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// RETURNERAR ANTALET ELEMENT I KÖN
|
||||
// pre: TRUE
|
||||
// post: antalet element i kön är returnerade
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int queueClass<T>::length()
|
||||
{
|
||||
return (minLista.listLength());
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM ETT ELEMENT FINNS
|
||||
// pre: att det finns en kö
|
||||
// post: TRUE om elementet redan finns
|
||||
// post: FALSE om elementet inte redan finns
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int queueClass<T>::duplicate(T newItem)
|
||||
{
|
||||
return (minLista.newDuplicate(newItem));
|
||||
}
|
||||
//#####################################################
|
||||
// LÄGGER TILL ETT NYTT ELEMENT I KÖN
|
||||
// pre: minne fanns för att skapa fönstret
|
||||
// pre: att maximalt antal element inte finns
|
||||
// pre: att köelementet inte finns
|
||||
// post: kön har ett nytt element
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void queueClass<T>::ins(int position, T newItem)
|
||||
{
|
||||
minLista.listInsert(position, newItem);
|
||||
}
|
||||
//#####################################################
|
||||
// TAR BORT ELEMENT I LISTAN
|
||||
// pre: att det finns en kö
|
||||
// post: översta elementet har tagits bort
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void queueClass<T>::del()
|
||||
{
|
||||
minLista.listDel();
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM KÖN ÄR TOM
|
||||
// pre: TRUE
|
||||
// post: TRUE om kön är tom
|
||||
// post: FALSE om kön inte är tom
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int queueClass<T>::isEmpty()
|
||||
{
|
||||
return (minLista.listIsEmpty());
|
||||
}
|
||||
//#####################################################
|
||||
// SKRIVER UT ÖVERSTA ELEMENTET
|
||||
// pre: att det finns en kö
|
||||
// post: översta elementet har skrivits ut
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void queueClass<T>::getFront()
|
||||
{
|
||||
TWindow ruta;
|
||||
ruta=minLista.retrive(FORST);
|
||||
cout << ruta;
|
||||
ruta.move(moveX,moveY);
|
||||
}
|
||||
//#####################################################
|
||||
// SKRIVER UT KÖN PÅ SKÄRMEN
|
||||
// pre: att det finns en kö
|
||||
// post: kön har skrivits ut
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void queueClass<T>::display()
|
||||
{
|
||||
118
Stack_And_Queue/stack.cpp
Normal file
118
Stack_And_Queue/stack.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
//#####################################################
|
||||
// STACK.CPP
|
||||
//=====================================================
|
||||
|
||||
template <class T>
|
||||
class stackClass
|
||||
{
|
||||
public:
|
||||
stackClass();
|
||||
~stackClass();
|
||||
int length();
|
||||
int duplicate(T newItem);
|
||||
void push(int position, T newItem);
|
||||
void pop();
|
||||
void top();
|
||||
void display();
|
||||
int isEmpty();
|
||||
private:
|
||||
listClass<TWindow>minLista;
|
||||
typedef struct stackNode* ptrType;
|
||||
ptrType head;
|
||||
int size;
|
||||
};
|
||||
//#####################################################
|
||||
// CONSTRUCTOR
|
||||
// SÄTTER STACKSTORLEKEN TILL NOLL OCH HEAD TILL NULL
|
||||
// pre: TRUE
|
||||
// post: en stack har skapats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
stackClass<T>::stackClass():size(0), head(NULL)
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// DESTRUCTOR
|
||||
// FÖRSTÖR STACKEN, ANROPAS AUTOMATISKT VID AVSLUT
|
||||
// pre: TRUE
|
||||
// post: stacken har raderats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
stackClass<T>::~stackClass()
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// RETURNERAR ANTALET ELEMENT I STACKEN
|
||||
// pre: TRUE
|
||||
// post: antalet element i stacken är returnerade
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int stackClass<T>::length()
|
||||
{
|
||||
return (minLista.listLength());
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM ETT ELEMENT FINNS
|
||||
// pre: att det finns en stack
|
||||
// post: TRUE om elementet redan finns
|
||||
// post: FALSE om elementet inte redan finns
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int stackClass<T>::duplicate(T newItem)
|
||||
{
|
||||
return(minLista.newDuplicate(newItem));
|
||||
}
|
||||
//#####################################################
|
||||
// LÄGGER TILL ETT NYTT ELEMENT I STACKEN
|
||||
// pre: minne fanns för att skapa fönstret
|
||||
// pre: att maximalt antal element inte finns
|
||||
// pre: att elementet inte redan finns
|
||||
// post: stacken har ett nytt element
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void stackClass<T>::push(int position, T newItem)
|
||||
{
|
||||
minLista.listInsert(position, newItem);
|
||||
}
|
||||
//#####################################################
|
||||
// TAR BORT ELEMENT I STACKEN
|
||||
// pre: att det finns en stack
|
||||
// post: elementet har tagits bort
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void stackClass<T>::pop()
|
||||
{
|
||||
minLista.listDel();
|
||||
}
|
||||
//#####################################################
|
||||
// SKRIVER UT ÖVERSTA ELEMENTET
|
||||
// pre: att det finns en stack
|
||||
// post: elementet har skrivits ut
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void stackClass<T>::top()
|
||||
{
|
||||
TWindow ruta;
|
||||
ruta=minLista.retrive(FORST);
|
||||
cout << ruta;
|
||||
ruta.move(moveX,moveY);
|
||||
}
|
||||
//#####################################################
|
||||
// SKRIVER UT STACKEN PÅ SKÄRMEN
|
||||
// pre: att det finns en stack
|
||||
// post: stacken har skrivits ut
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void stackClass<T>::display()
|
||||
{
|
||||
minLista.listDisplay();
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM STACKEN ÄR TOM
|
||||
// pre: TRUE
|
||||
// post: TRUE om stacken är tom
|
||||
// post: FALSE om stacken inte är tom
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int stackClass<T>::isEmpty()
|
||||
{
|
||||
203
Stack_And_Queue/twindow.cpp
Normal file
203
Stack_And_Queue/twindow.cpp
Normal file
@@ -0,0 +1,203 @@
|
||||
#include <dos.h>
|
||||
#include <conio.h>
|
||||
#include <bios.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "twindow.hpp"
|
||||
|
||||
const int MAXX=80;
|
||||
const int MAXY=25;
|
||||
const int MINX=0;
|
||||
const int MINY=0;
|
||||
class Display
|
||||
{
|
||||
public:
|
||||
typedef unsigned far* ScreenPtr;
|
||||
static void drawChar(unsigned x,unsigned y, char c, unsigned fg, unsigned bg);
|
||||
};
|
||||
void Display::drawChar(unsigned x,unsigned y, char c, unsigned fg, unsigned bg)
|
||||
{
|
||||
ScreenPtr display = (ScreenPtr)MK_FP(0xB800,0x0);
|
||||
display[y*80+x] = (((bg<<4)|fg)<<8)|(c & 255);
|
||||
}
|
||||
//=============================================================================
|
||||
// default constructor: skapar fönstret med vissa koordinater och färger
|
||||
//-----------------------------------------------------------------------------
|
||||
TWindow::TWindow()
|
||||
{
|
||||
Left=69;Right=78;Bottom=4;Top=1;background=BLUE;foreground=WHITE;
|
||||
text=NULL;
|
||||
}
|
||||
//=============================================================================
|
||||
// constructor: skapar fönstret med vissa koordinater och färger
|
||||
//-----------------------------------------------------------------------------
|
||||
TWindow::TWindow(unsigned left,unsigned top,unsigned right ,unsigned bottom, DColor bg, DColor fg):
|
||||
Left(left),Right(right),Bottom(bottom),Top(top),background(bg), foreground(fg)
|
||||
{
|
||||
theState = 0;//FALSE;
|
||||
text=NULL;
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// destructor: förstör fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
TWindow::~TWindow()
|
||||
{
|
||||
if(isVisible())
|
||||
hide();
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// hide: gömmer fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::hide()
|
||||
{
|
||||
erase();
|
||||
}
|
||||
//=============================================================================
|
||||
// show: visar fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::display()
|
||||
{
|
||||
draw();
|
||||
if(text)
|
||||
showText();
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// draw: ritar upp fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::draw()
|
||||
{
|
||||
theState = 1;//;TRUE;
|
||||
for(int j= Top; j< Bottom; j++ )
|
||||
for(int i=Left; i< Right ; i++ )
|
||||
{
|
||||
Display::drawChar(i,j,' ',foreground,background);
|
||||
}
|
||||
drawBorder();
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// erase: ritar över fönstret med svart
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::erase()
|
||||
{
|
||||
for(int j= Top; j<= Bottom; j++ )
|
||||
for(int i=Left; i<= Right ; i++ )
|
||||
Display::drawChar(i,j,' ',BLACK,BLACK);
|
||||
theState = 0;//FALSE;
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// drewBorder: ritar ramen runt fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::drawBorder()
|
||||
{
|
||||
for(int i=Left+1; i< Right; i++)Display::drawChar(i ,Top , HLine , foreground, background);
|
||||
for( i=Left+1; i< Right; i++)Display::drawChar(i ,Bottom , HLine , foreground, background);
|
||||
for( i=Top+1; i< Bottom; i++)Display::drawChar(Left ,i , VLine , foreground, background);
|
||||
for( i=Top+1; i< Bottom; i++)Display::drawChar(Right ,i , VLine , foreground, background);
|
||||
Display::drawChar(Left ,Top , LeftTop , foreground, background);
|
||||
Display::drawChar(Right ,Top ,RightTop , foreground, background);
|
||||
Display::drawChar(Right ,Bottom ,RightBottom, foreground, background);
|
||||
Display::drawChar(Left ,Bottom , LeftBottom, foreground, background);
|
||||
}
|
||||
void TWindow::move(unsigned relX, unsigned relY)
|
||||
{
|
||||
Left=Left+relX;
|
||||
Right=Right+relX;
|
||||
Top=Top+relY;
|
||||
Bottom=Bottom+relY;
|
||||
}
|
||||
void TWindow::moveAbs(unsigned x, unsigned y)
|
||||
{
|
||||
unsigned width=Right-Left;
|
||||
unsigned height=Bottom-Top;
|
||||
Left=x;
|
||||
Right=x+width;
|
||||
Top=y;
|
||||
Bottom=y+height;
|
||||
}
|
||||
|
||||
void TWindow::setText(char* txt)
|
||||
{
|
||||
text=new char[strlen(txt)];
|
||||
strcpy(text, txt);
|
||||
}
|
||||
void TWindow::showText()
|
||||
{
|
||||
int x=Left+1,y=Top+1;
|
||||
for(int i=0;i<strlen(text);i++)
|
||||
{
|
||||
Display::drawChar(x, y, text[i], foreground, background);
|
||||
if(++x>=Right)
|
||||
{
|
||||
x=Left+1;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
void TWindow::editText()
|
||||
{
|
||||
int i=0;
|
||||
delete text;
|
||||
int size=(Right-Left-1)*(Bottom-Top-1);
|
||||
text=new char[size];
|
||||
text[0]='\0';
|
||||
char* buffer=new char[size];
|
||||
while(((buffer[i]=bioskey(0))!=ENTER)&&(i<size))
|
||||
{
|
||||
if(buffer[i]==BACK)
|
||||
{
|
||||
text[i-1]='\0';
|
||||
if(--i<0)i=0;
|
||||
draw();
|
||||
showText();
|
||||
}
|
||||
else if(buffer[i]==ESC)
|
||||
{
|
||||
delete text;
|
||||
text=NULL;
|
||||
draw();
|
||||
return;
|
||||
}
|
||||
else if(isalnum(buffer[i])||buffer[i]==' ')
|
||||
{
|
||||
text[i]=buffer[i];
|
||||
text[i+1]='\0';
|
||||
showText();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
TWindow& TWindow::operator=(TWindow& t)
|
||||
{
|
||||
delete text;
|
||||
text=new char[strlen(t.text)];
|
||||
strcpy(text, t.text);
|
||||
return *this;
|
||||
}
|
||||
boolean TWindow::operator==(TWindow& t)
|
||||
{
|
||||
return strcmp(t.text, text)==0;
|
||||
}
|
||||
boolean TWindow::operator<(TWindow& t)
|
||||
{
|
||||
return strcmp(text, t.text)<0;
|
||||
}
|
||||
boolean TWindow::operator>(TWindow& t)
|
||||
{
|
||||
return strcmp(text, t.text)>0;
|
||||
}
|
||||
ostream& operator<<(ostream& o, TWindow& t)
|
||||
{
|
||||
t.display();
|
||||
return o;
|
||||
}
|
||||
istream& operator>>(istream&i, TWindow& t)
|
||||
{
|
||||
t.editText();
|
||||
return i;
|
||||
}
|
||||
|
||||
39
Stack_And_Queue/twindow.hpp
Normal file
39
Stack_And_Queue/twindow.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef __TWINDOW__H
|
||||
#define __TWINDOW__H
|
||||
#include "keycode.h"
|
||||
#include "boolean.h"
|
||||
#include <iostream.h>
|
||||
typedef unsigned char DColor;
|
||||
class TWindow
|
||||
{
|
||||
public:
|
||||
TWindow();
|
||||
TWindow(unsigned left,unsigned top,unsigned right ,unsigned bottom, DColor bg, DColor fg);
|
||||
~TWindow();
|
||||
boolean isVisible() {return theState;}
|
||||
void hide();
|
||||
void display();
|
||||
void move(unsigned nX, unsigned nY);
|
||||
void TWindow::moveAbs(unsigned x, unsigned y);
|
||||
void setText(char* txt);
|
||||
void editText();
|
||||
char* getText(){return text;};
|
||||
void setColor(unsigned char col){background=col;};
|
||||
TWindow& operator=(TWindow& t);
|
||||
boolean operator==(TWindow& t);
|
||||
boolean operator<(TWindow& t);
|
||||
boolean operator>(TWindow& t);
|
||||
private:
|
||||
char* text;
|
||||
DColor background, foreground;
|
||||
boolean theState;
|
||||
unsigned Left,Top, Right,Bottom;
|
||||
void draw();
|
||||
void erase();
|
||||
void drawBorder();
|
||||
void showText();
|
||||
};
|
||||
ostream& operator<<(ostream& o, TWindow& t);
|
||||
istream& operator>>(istream&i, TWindow& t);
|
||||
#endif
|
||||
|
||||
6
Template_List/boolean.h
Normal file
6
Template_List/boolean.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef __BOOLEAN_H_
|
||||
#define __BOOLEAN_H_
|
||||
typedef int boolean;
|
||||
//const int FALSE=0;
|
||||
//const int TRUE=!FALSE;
|
||||
#endif
|
||||
196
Template_List/huvud.cpp
Normal file
196
Template_List/huvud.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
/***************************************************/
|
||||
/* 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');
|
||||
}
|
||||
|
||||
12
Template_List/keycode.h
Normal file
12
Template_List/keycode.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef __KEYCODE_H
|
||||
#define __KEYCODE_H
|
||||
const int LeftTop = 201;
|
||||
const int RightTop = 187;
|
||||
const int RightBottom = 188;
|
||||
const int LeftBottom = 200;
|
||||
const int HLine = 205;
|
||||
const int VLine = 186;
|
||||
const int ESC = 27;
|
||||
const int ENTER = 13;
|
||||
const int BACK = 8;
|
||||
#endif
|
||||
205
Template_List/lista.cpp
Normal file
205
Template_List/lista.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
//LISTA.CPP
|
||||
|
||||
#include <stddef.h> //för NULL
|
||||
#include <assert.h> //för assert()
|
||||
#include <iostream.h> //för in-ut matning
|
||||
#include <conio.h> //för getch();
|
||||
#include <string.h> //för säker inmatning
|
||||
#include <stdlib.h> //för atoi()
|
||||
|
||||
const int MAXLISTA = 20;
|
||||
const int BUFFERSIZE = 100;
|
||||
const int FALSE=0;
|
||||
const int TRUE=!FALSE;
|
||||
|
||||
template <class T>
|
||||
class listClass
|
||||
{
|
||||
public:
|
||||
listClass();
|
||||
~listClass();
|
||||
int listIsEmpty();
|
||||
int listLength();
|
||||
int posIsOk(int position);
|
||||
int newDuplicate(T newItem);
|
||||
void listInsert(T newItem);
|
||||
void listDel(int position);
|
||||
void listSort();
|
||||
void listDisplay();
|
||||
int listSeek(T soktVarde);
|
||||
void listAntal();
|
||||
private:
|
||||
struct listNode {
|
||||
T listContent;
|
||||
listNode* next;
|
||||
};
|
||||
typedef struct listNode listNode;
|
||||
typedef struct listNode* ptrType;
|
||||
ptrType head;
|
||||
int size;
|
||||
};
|
||||
|
||||
/***************************************************/
|
||||
/*CONSTRUCTOR */
|
||||
/*SÄTTER LISTSTORLEKEN TILL NOLL */
|
||||
/* precondition: TRUE */
|
||||
/* postcondition: TRUE */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
listClass<T>::listClass():size(0), head(NULL){}
|
||||
|
||||
/***************************************************/
|
||||
/*DESTRUCTOR */
|
||||
/*GÖR INGET, MEN MEN ANROPAS NÄR LISTAN DÖR */
|
||||
/* precondition: TRUE */
|
||||
/* postcondition: TRUE */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
listClass<T>::~listClass(){}
|
||||
|
||||
/***************************************************/
|
||||
/*KOLLAR OM LISTAN ÄR TOM */
|
||||
/* precondition: TRUE */
|
||||
/* postcondition: TRUE */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
int listClass<T>::listIsEmpty(){ return int (size == 0); }
|
||||
|
||||
/***************************************************/
|
||||
/*RETURNERAR ANTALET ELEMENT I LISTAN */
|
||||
/* precondition: TRUE */
|
||||
/* postcondition: TRUE */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
int listClass<T>::listLength() { return size;}
|
||||
|
||||
/***************************************************/
|
||||
/*LÄGGER TILL ETT NYTT ELEMENT I LISTAN */
|
||||
/* precondition: minne fanns för att skapa fönst*/
|
||||
/* precondition: att listelementet inte finns */
|
||||
/* postcondition: listan har ett nytt element */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
void listClass<T>::listInsert(T newItem)
|
||||
{
|
||||
int newPosition=1;
|
||||
int newLength = listLength() + 1;
|
||||
size = newLength;
|
||||
ptrType newPtr = new listNode;
|
||||
if (newPtr != NULL) {
|
||||
newPtr -> listContent = newItem;
|
||||
newPtr ->next = head;
|
||||
head = newPtr;
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*TAR BORT ELEMENT I LISTAN */
|
||||
/* precondition: att det finns en lista */
|
||||
/* precondition: att positionen är OK */
|
||||
/* postcondition: elementet har tagits bort */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
void listClass<T>::listDel(int position)
|
||||
{
|
||||
ptrType trav=head, cur;
|
||||
size--;
|
||||
if (position == 1) {
|
||||
cur = head;
|
||||
head = head -> next;
|
||||
|
||||
}
|
||||
else {
|
||||
for (int skip = 1 ; skip < position -1; skip++)
|
||||
trav = trav -> next;
|
||||
cur = trav -> next;
|
||||
trav -> next = cur ->next;
|
||||
}
|
||||
cur -> next = NULL;
|
||||
delete cur;
|
||||
cur = NULL;
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*SORTERAR LISTAN */
|
||||
/* precondition: att det finns en lista */
|
||||
/* postcondition: elementen ligger i ordning */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
void listClass<T>::listSort()
|
||||
{
|
||||
T temp;
|
||||
int bytt;
|
||||
|
||||
do {
|
||||
bytt=FALSE;
|
||||
for(ptrType cur=head ; cur->next != NULL ; cur = cur->next)
|
||||
if(cur->listContent > cur->next->listContent) {
|
||||
temp = cur->listContent;
|
||||
cur->listContent = cur->next->listContent;
|
||||
cur->next->listContent = temp;
|
||||
bytt=TRUE;
|
||||
}
|
||||
}while(bytt);
|
||||
}
|
||||
/***************************************************/
|
||||
/*SKRIVER UT LISTAN PÅ SKÄRMEN */
|
||||
/* precondition: att det finns en lista */
|
||||
/* postcondition: listan har skrivits ut */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
void listClass<T>::listDisplay()
|
||||
{
|
||||
TWindow ruta;
|
||||
for(ptrType cur = head ; cur != NULL ; cur = cur->next) {
|
||||
ruta=cur->listContent;
|
||||
cout << ruta;
|
||||
ruta.move(11,0);
|
||||
}
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*SÖKER EFTER ETT ELEMENT I LISTAN */
|
||||
/* precondition: att det finns en lista */
|
||||
/* postcondition: TRUE */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
int listClass<T>::listSeek(T soktVarde)
|
||||
{
|
||||
ptrType cur;
|
||||
for(cur=head ; cur != NULL ; cur = cur->next)
|
||||
if(cur->listContent == soktVarde)
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KOLLAR OM ETT ELEMENT FINNS */
|
||||
/* precondition: TRUE */
|
||||
/* postcondition: TRUE/FALSE */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
int listClass<T>::newDuplicate(T newItem)
|
||||
{
|
||||
ptrType cur;
|
||||
|
||||
for(cur=head ; cur != NULL ; cur = cur->next)
|
||||
if(cur->listContent == newItem)
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/***************************************************/
|
||||
/*KONTROLLERAR OM EN POSITION FINNS */
|
||||
/* precondition: att positionen är tillåten */
|
||||
/* postcondition: TRUE/FALSE */
|
||||
/***************************************************/
|
||||
template <class T>
|
||||
int listClass<T>::posIsOk(int position)
|
||||
{
|
||||
if((position <= size) && (position > 0))
|
||||
return TRUE;
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
196
Template_List/twindow.cpp
Normal file
196
Template_List/twindow.cpp
Normal file
@@ -0,0 +1,196 @@
|
||||
#include <dos.h>
|
||||
#include <conio.h>
|
||||
#include <bios.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "twindow.hpp"
|
||||
|
||||
const int MAXX=80;
|
||||
const int MAXY=25;
|
||||
const int MINX=0;
|
||||
const int MINY=0;
|
||||
class Display
|
||||
{
|
||||
public:
|
||||
typedef unsigned far* ScreenPtr;
|
||||
static void drawChar(unsigned x,unsigned y, char c, unsigned fg, unsigned bg);
|
||||
};
|
||||
void Display::drawChar(unsigned x,unsigned y, char c, unsigned fg, unsigned bg)
|
||||
{
|
||||
ScreenPtr display = (ScreenPtr)MK_FP(0xB800,0x0);
|
||||
display[y*80+x] = (((bg<<4)|fg)<<8)|(c & 255);
|
||||
}
|
||||
//=============================================================================
|
||||
// default constructor: skapar fönstret med vissa koordinater och färger
|
||||
//-----------------------------------------------------------------------------
|
||||
TWindow::TWindow()
|
||||
{
|
||||
Left=10;Right=18;Bottom=18;Top=15;background=BLUE;foreground=WHITE;
|
||||
text=NULL;
|
||||
}
|
||||
//=============================================================================
|
||||
// constructor: skapar fönstret med vissa koordinater och färger
|
||||
//-----------------------------------------------------------------------------
|
||||
TWindow::TWindow(unsigned left,unsigned top,unsigned right ,unsigned bottom, DColor bg, DColor fg):
|
||||
Left(left),Right(right),Bottom(bottom),Top(top),background(bg), foreground(fg)
|
||||
{
|
||||
theState = 0;//FALSE;
|
||||
text=NULL;
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// destructor: förstör fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
TWindow::~TWindow()
|
||||
{
|
||||
if(isVisible())
|
||||
hide();
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// hide: gömmer fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::hide()
|
||||
{
|
||||
erase();
|
||||
}
|
||||
//=============================================================================
|
||||
// show: visar fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::display()
|
||||
{
|
||||
draw();
|
||||
if(text)
|
||||
showText();
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// draw: ritar upp fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::draw()
|
||||
{
|
||||
theState = 1;//;TRUE;
|
||||
for(int j= Top; j< Bottom; j++ )
|
||||
for(int i=Left; i< Right ; i++ ) {
|
||||
Display::drawChar(i,j,' ',foreground,background);
|
||||
}
|
||||
drawBorder();
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// erase: ritar över fönstret med svart
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::erase()
|
||||
{
|
||||
for(int j= Top; j<= Bottom; j++ )
|
||||
for(int i=Left; i<= Right ; i++ )
|
||||
Display::drawChar(i,j,' ',BLACK,BLACK);
|
||||
theState = 0;//FALSE;
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// drewBorder: ritar ramen runt fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::drawBorder()
|
||||
{
|
||||
for(int i=Left+1; i< Right; i++)Display::drawChar(i ,Top , HLine , foreground, background);
|
||||
for( i=Left+1; i< Right; i++)Display::drawChar(i ,Bottom , HLine , foreground, background);
|
||||
for( i=Top+1; i< Bottom; i++)Display::drawChar(Left ,i , VLine , foreground, background);
|
||||
for( i=Top+1; i< Bottom; i++)Display::drawChar(Right ,i , VLine , foreground, background);
|
||||
Display::drawChar(Left ,Top , LeftTop , foreground, background);
|
||||
Display::drawChar(Right ,Top ,RightTop , foreground, background);
|
||||
Display::drawChar(Right ,Bottom ,RightBottom, foreground, background);
|
||||
Display::drawChar(Left ,Bottom , LeftBottom, foreground, background);
|
||||
}
|
||||
void TWindow::move(unsigned relX, unsigned relY)
|
||||
{
|
||||
Left=Left+relX;
|
||||
Right=Right+relX;
|
||||
Top=Top+relY;
|
||||
Bottom=Bottom+relY;
|
||||
}
|
||||
void TWindow::moveAbs(unsigned x, unsigned y)
|
||||
{
|
||||
unsigned width=Right-Left;
|
||||
unsigned height=Bottom-Top;
|
||||
Left=x;
|
||||
Right=x+width;
|
||||
Top=y;
|
||||
Bottom=y+height;
|
||||
}
|
||||
|
||||
void TWindow::setText(char* txt)
|
||||
{
|
||||
text=new char[strlen(txt)];
|
||||
strcpy(text, txt);
|
||||
}
|
||||
void TWindow::showText()
|
||||
{
|
||||
int x=Left+1,y=Top+1;
|
||||
for(int i=0;i<strlen(text);i++) {
|
||||
Display::drawChar(x, y, text[i], foreground, background);
|
||||
if(++x>=Right) {
|
||||
x=Left+1;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
void TWindow::editText()
|
||||
{
|
||||
int i=0;
|
||||
delete text;
|
||||
int size=(Right-Left-1)*(Bottom-Top-1);
|
||||
text=new char[size];
|
||||
text[0]='\0';
|
||||
char* buffer=new char[size];
|
||||
while(((buffer[i]=bioskey(0))!=ENTER)&&(i<size)) {
|
||||
if(buffer[i]==BACK) {
|
||||
text[i-1]='\0';
|
||||
if(--i<0)i=0;
|
||||
draw();
|
||||
showText();
|
||||
}
|
||||
else if(buffer[i]==ESC) {
|
||||
delete text;
|
||||
text=NULL;
|
||||
draw();
|
||||
return;
|
||||
}
|
||||
else if(isalnum(buffer[i])||buffer[i]==' ') {
|
||||
text[i]=buffer[i];
|
||||
text[i+1]='\0';
|
||||
showText();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
TWindow& TWindow::operator=(TWindow& t)
|
||||
{
|
||||
delete text;
|
||||
text=new char[strlen(t.text)];
|
||||
strcpy(text, t.text);
|
||||
return *this;
|
||||
}
|
||||
boolean TWindow::operator==(TWindow& t)
|
||||
{
|
||||
return strcmp(t.text, text)==0;
|
||||
}
|
||||
boolean TWindow::operator<(TWindow& t)
|
||||
{
|
||||
return strcmp(text, t.text)<0;
|
||||
}
|
||||
boolean TWindow::operator>(TWindow& t)
|
||||
{
|
||||
return strcmp(text, t.text)>0;
|
||||
}
|
||||
ostream& operator<<(ostream& o, TWindow& t)
|
||||
{
|
||||
t.display();
|
||||
return o;
|
||||
}
|
||||
istream& operator>>(istream&i, TWindow& t)
|
||||
{
|
||||
t.editText();
|
||||
return i;
|
||||
}
|
||||
|
||||
39
Template_List/twindow.hpp
Normal file
39
Template_List/twindow.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef __TWINDOW__H
|
||||
#define __TWINDOW__H
|
||||
#include "keycode.h"
|
||||
#include "boolean.h"
|
||||
#include <iostream.h>
|
||||
typedef unsigned char DColor;
|
||||
class TWindow
|
||||
{
|
||||
public:
|
||||
TWindow();
|
||||
TWindow(unsigned left,unsigned top,unsigned right ,unsigned bottom, DColor bg, DColor fg);
|
||||
~TWindow();
|
||||
boolean isVisible() {return theState;}
|
||||
void hide();
|
||||
void display();
|
||||
void move(unsigned nX, unsigned nY);
|
||||
void TWindow::moveAbs(unsigned x, unsigned y);
|
||||
void setText(char* txt);
|
||||
void editText();
|
||||
char* getText(){return text;};
|
||||
void setColor(unsigned char col){background=col;};
|
||||
TWindow& operator=(TWindow& t);
|
||||
boolean operator==(TWindow& t);
|
||||
boolean operator<(TWindow& t);
|
||||
boolean operator>(TWindow& t);
|
||||
private:
|
||||
char* text;
|
||||
DColor background, foreground;
|
||||
boolean theState;
|
||||
unsigned Left,Top, Right,Bottom;
|
||||
void draw();
|
||||
void erase();
|
||||
void drawBorder();
|
||||
void showText();
|
||||
};
|
||||
ostream& operator<<(ostream& o, TWindow& t);
|
||||
istream& operator>>(istream&i, TWindow& t);
|
||||
#endif
|
||||
|
||||
6
The_Store/boolean.h
Normal file
6
The_Store/boolean.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#ifndef __BOOLEAN_H_
|
||||
|
||||
#define __BOOLEAN_H_
|
||||
|
||||
typedef int boolean;
|
||||
|
||||
202
The_Store/huvud.cpp
Normal file
202
The_Store/huvud.cpp
Normal file
@@ -0,0 +1,202 @@
|
||||
//#####################################################
|
||||
// LAB 3 I PUMA
|
||||
// CRILLE & ROBIN 980107
|
||||
// AFFÄREN
|
||||
//=====================================================
|
||||
|
||||
#include "lista.cpp"
|
||||
#include "twindow.hpp"
|
||||
|
||||
const int BUFFERSIZE = 100; //för säkerinmatning
|
||||
const int MAXSIZE = 5; //maximalt antal element
|
||||
const int SIST = 0; //inmatning i listan
|
||||
const int FORST=!SIST; //inmatning i listan
|
||||
const int FALSE=0; //boolsk varabel
|
||||
const int TRUE=!FALSE; //boolsk varabel
|
||||
const int antalKassor = 2;
|
||||
|
||||
const int BREDD = 7; //fönstrets bredd
|
||||
const int HOJD = 2; //fönstrets höjd
|
||||
|
||||
//#####################################################
|
||||
// SÄKER INMATNING
|
||||
// SER TILL ATT BARA HELTAL MATAS IN
|
||||
//=====================================================
|
||||
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);
|
||||
}
|
||||
//#####################################################
|
||||
// SÄTTER IN ELEMENT I LISTAN
|
||||
//=====================================================
|
||||
void _listInsert(listClass <TWindow> &minLista)
|
||||
{
|
||||
if(minLista.listLength() < MAXSIZE)
|
||||
{
|
||||
TWindow *newItem;
|
||||
newItem = new TWindow(Lleft, Ltop,(BREDD+Lleft),(HOJD+Ltop),BLUE,WHITE);
|
||||
cout << " Vem kommer in i affaren?";
|
||||
cout << *newItem;
|
||||
newItem->editText();
|
||||
if(minLista.newDuplicate(*newItem))
|
||||
{
|
||||
cout <<"\n Personen ar redan har.";
|
||||
getch();
|
||||
}
|
||||
else
|
||||
minLista.listInsert(minLista.listLength()+1, *newItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
cout <<" Affaren ar full !!!";
|
||||
getch();
|
||||
}
|
||||
}
|
||||
//#####################################################
|
||||
// VISAR ALLA ELEMENT I LISTAN
|
||||
//=====================================================
|
||||
void _listDisplay(listClass <TWindow> minLista)
|
||||
{
|
||||
TWindow *a;
|
||||
a = new TWindow(1, 20,(BREDD+1),(HOJD+20),GREEN,WHITE);
|
||||
if(!minLista.listIsEmpty())
|
||||
for(int i=0 ; i<minLista.listLength() ; i++)
|
||||
{
|
||||
*a=minLista.retrive(i);
|
||||
a->move(LmoveX,LmoveY);
|
||||
cout << *a;
|
||||
}
|
||||
}
|
||||
//#####################################################
|
||||
// VISAR ALLA ELEMENT I KASSORNA
|
||||
//=====================================================
|
||||
void _queueDisplay(queueClass <TWindow> minQueue[])
|
||||
{
|
||||
TWindow *b,*c;
|
||||
b = new TWindow(Qleft[0],Qtop,(BREDD+Qleft[0]),(HOJD+Qtop),RED,WHITE);
|
||||
c = new TWindow(Qleft[1],Qtop,(BREDD+Qleft[1]),(HOJD+Qtop),RED,WHITE);
|
||||
if(!minQueue[0].isEmpty())
|
||||
{
|
||||
for(int i=0 ; i<minQueue[0].length() ; i++)
|
||||
{
|
||||
*b=minQueue[0].retrive(i);
|
||||
cout << *b;
|
||||
b->move(QmoveX,QmoveY);
|
||||
}
|
||||
}
|
||||
if(!minQueue[1].isEmpty())
|
||||
{
|
||||
for(int i=0 ; i<minQueue[1].length() ; i++)
|
||||
{
|
||||
*c=minQueue[1].retrive(i);
|
||||
cout << *c;
|
||||
c->move(QmoveX,QmoveY);
|
||||
}
|
||||
}
|
||||
}
|
||||
//#####################################################
|
||||
// FLYTTAR EN PERSON TILL EN KASSA
|
||||
//=====================================================
|
||||
void _toQueue(queueClass<TWindow> minQueue[],listClass<TWindow> &minLista)
|
||||
{
|
||||
int kassa,position;
|
||||
TWindow *flyttGubbe;
|
||||
flyttGubbe = new TWindow(Lleft, Ltop,(BREDD+Lleft),(HOJD+Ltop),BLUE,WHITE);
|
||||
cout <<" Vem vill du flytta till kon?";
|
||||
cout << *flyttGubbe;
|
||||
flyttGubbe->editText();
|
||||
if(minLista.newDuplicate(*flyttGubbe))
|
||||
{
|
||||
cout <<"\n Till vilken kassa [1/2] ?";
|
||||
cin >> kassa;
|
||||
position=minLista.getPos(*flyttGubbe);
|
||||
minQueue[kassa-1].ins(minLista.retrive(position));
|
||||
minLista.listDel(position+1);
|
||||
}
|
||||
else
|
||||
{
|
||||
cout <<"\n Personen fanns inte i affaren!";
|
||||
getch();
|
||||
}
|
||||
}
|
||||
//#####################################################
|
||||
// BETJÄNAR EN PERSON UR EN KASSA (TAR BORT)
|
||||
//=====================================================
|
||||
void _getOut(queueClass <TWindow> minQueue[])
|
||||
{
|
||||
int kassa;
|
||||
cout <<"\n Fran vilken kassa [1/2] ?";
|
||||
kassa=mataIn();
|
||||
if(kassa>antalKassor)
|
||||
cout <<" Vi har inte sa manga kassor!";
|
||||
else if(minQueue[kassa-1].length()==0)
|
||||
cout <<" Det finns ingen att betjana.";
|
||||
else
|
||||
{
|
||||
minQueue[kassa-1].del(FORST);
|
||||
return;
|
||||
}
|
||||
getch();
|
||||
}
|
||||
//#####################################################
|
||||
// MAIN-MENU-ITEMS
|
||||
//=====================================================
|
||||
void menuItems()
|
||||
{
|
||||
clrscr();
|
||||
TWindow *separera;
|
||||
separera = new TWindow(63,0,64,15,YELLOW,YELLOW);
|
||||
cout << *separera;
|
||||
separera->move(LmoveX,LmoveY);
|
||||
cout <<" Bengans begagnade barnklader. \n"
|
||||
<<" ----------------------------- \n\n"
|
||||
<<" 1. Lagg till person i affaeren. \n"
|
||||
<<" 2. Flytta person till en ko. \n"
|
||||
<<" 3. Betjana person ur en ko.\n"
|
||||
<<" 0. Avsluta programmet. \n\n";
|
||||
gotoxy(52,1);
|
||||
cout <<"Kassa 1";
|
||||
gotoxy(72,wherey());
|
||||
cout <<"Kassa 2";
|
||||
gotoxy(2,20);
|
||||
cout <<"Dessa personer ar inne i affaren...";
|
||||
gotoxy(2,9);
|
||||
}
|
||||
//#####################################################
|
||||
// MAINFUNCTION
|
||||
//=====================================================
|
||||
void main()
|
||||
{
|
||||
queueClass<TWindow> minQueue[antalKassor];
|
||||
listClass<TWindow> minLista;
|
||||
char val = TRUE;
|
||||
do
|
||||
{
|
||||
menuItems();
|
||||
_listDisplay(minLista);
|
||||
_queueDisplay(minQueue);
|
||||
cin >> val;
|
||||
switch (val)
|
||||
{
|
||||
case '1' : _listInsert(minLista);break;
|
||||
case '2' : _toQueue(minQueue,minLista);break;
|
||||
case '3' : _getOut(minQueue);break;
|
||||
case '0' : cout <<" Programmet avslutat";break;
|
||||
default: cout <<" Fel val"<< endl;
|
||||
}
|
||||
}while (val != '0');
|
||||
12
The_Store/keycode.h
Normal file
12
The_Store/keycode.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#ifndef __KEYCODE_H
|
||||
|
||||
#define __KEYCODE_H
|
||||
|
||||
const int LeftTop = 201;
|
||||
|
||||
const int RightTop = 187;
|
||||
|
||||
const int RightBottom = 188;
|
||||
|
||||
const int LeftBottom = 200;
|
||||
|
||||
179
The_Store/lista.cpp
Normal file
179
The_Store/lista.cpp
Normal file
@@ -0,0 +1,179 @@
|
||||
//#####################################################
|
||||
// LISTA.CPP
|
||||
//=====================================================
|
||||
|
||||
#include <stddef.h> //för NULL
|
||||
#include <assert.h> //för assert()
|
||||
#include <iostream.h> //för in-ut matning
|
||||
#include <conio.h> //för getch();
|
||||
#include <string.h> //för säker inmatning
|
||||
#include <stdlib.h> //för atoi()
|
||||
#include "queue.cpp"
|
||||
|
||||
const int Lleft = 1; //fönstrets X-position
|
||||
const int Ltop = 13; //fönstrets Y-position
|
||||
const int LmoveX = 9; //fönsterflyttning X-led
|
||||
const int LmoveY = 0; //fönsterflyttning Y-led
|
||||
|
||||
template <class T>
|
||||
class listClass
|
||||
{
|
||||
public:
|
||||
listClass();
|
||||
~listClass();
|
||||
T retrive(int position);
|
||||
int listIsEmpty();
|
||||
int listLength();
|
||||
int newDuplicate(T newItem);
|
||||
int getPos(T newItem);
|
||||
void listInsert(int position, T newItem);
|
||||
void listDel(int position);
|
||||
private:
|
||||
struct listNode
|
||||
{
|
||||
T listContent;
|
||||
listNode* next;
|
||||
};
|
||||
typedef struct listNode listNode;
|
||||
typedef struct listNode* ptrType;
|
||||
ptrType head;
|
||||
int size;
|
||||
};
|
||||
//#####################################################
|
||||
// CONSTRUCTOR
|
||||
// SÄTTER LISTSTORLEKEN TILL NOLL & HEAD TILL NULL
|
||||
// pre: TRUE
|
||||
// post: en lista har skapats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
listClass<T>::listClass():size(0), head(NULL)
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// DESTRUCTOR
|
||||
// FÖRSTÖR LISTAN, ANROPAS AUTOMATISKT VID AVSLUT
|
||||
// pre: TRUE
|
||||
// post: listan har raderats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
listClass<T>::~listClass()
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM LISTAN ÄR TOM
|
||||
// pre: TRUE
|
||||
// post: TRUE om listan är tom
|
||||
// post: FALSE om listan inte är tom
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int listClass<T>::listIsEmpty()
|
||||
{
|
||||
return int (size == 0);
|
||||
}
|
||||
//#####################################################
|
||||
// RETURNERAR ANTALET ELEMENT I LISTAN
|
||||
// pre: TRUE
|
||||
// post: antalet element i listan är returnerade
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int listClass<T>::listLength()
|
||||
{
|
||||
return size;
|
||||
}
|
||||
//#####################################################
|
||||
// LÄGGER TILL ETT NYTT ELEMENT I LISTAN
|
||||
// pre: att maximalt antal element inte finns
|
||||
// pre: att listelementet inte finns
|
||||
// post: listan har ett nytt element
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void listClass<T>::listInsert(int position, T newItem)
|
||||
{
|
||||
size++;
|
||||
ptrType newPtr= new listNode;
|
||||
newPtr->listContent=newItem;
|
||||
if (position==1)
|
||||
{
|
||||
newPtr->next=head;
|
||||
head=newPtr;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptrType prev=head;
|
||||
for (int i=1;i<position-1;i++)
|
||||
prev=prev->next;
|
||||
newPtr->next=prev->next;
|
||||
prev->next=newPtr;
|
||||
}
|
||||
}
|
||||
//#####################################################
|
||||
// TAR BORT ELEMENT I LISTAN
|
||||
// pre: att det finns en lista
|
||||
// post: ett elementet har tagits bort
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void listClass<T>::listDel(int position)
|
||||
{
|
||||
ptrType prev,cur;
|
||||
size--;
|
||||
if(position == 1)
|
||||
{
|
||||
cur=head;
|
||||
head=head->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
prev=head;
|
||||
for (int i=1;i<position-1;i++)
|
||||
prev=prev->next;
|
||||
cur=prev->next;
|
||||
prev->next=cur->next;
|
||||
}
|
||||
delete cur;
|
||||
cur=NULL;
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM ETT ELEMENT FINNS
|
||||
// pre: att det finns en lista
|
||||
// post: TRUE om elementet redan finns
|
||||
// post: FALSE om elementet inte redan finns
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int listClass<T>::newDuplicate(T newItem)
|
||||
{
|
||||
ptrType cur;
|
||||
for(cur=head ; cur!=NULL ; cur=cur->next)
|
||||
if(cur->listContent == newItem)
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
//#####################################################
|
||||
// HÄMTAR ÖVERSTA ELEMENTET
|
||||
// pre: att det finns en lista
|
||||
// post: det översta elementet har returnerats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
T listClass<T>::retrive(int position)
|
||||
{
|
||||
ptrType cur=head;
|
||||
for(int i=0 ; i<position ; i++)
|
||||
cur=cur->next;
|
||||
return (cur->listContent);
|
||||
}
|
||||
//#####################################################
|
||||
// HÄMTAR POSITIONEN ETT SÖKT ELEMENT FINNS PÅ
|
||||
// pre: att det finns en lista
|
||||
// post: om TRUE returneras positionen
|
||||
// post: FALSE om elementet inte fanns
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int listClass<T>::getPos(T newItem)
|
||||
{
|
||||
ptrType cur=head;
|
||||
for(int position=1 ; position<size ; position++)
|
||||
{
|
||||
cur=cur->next;
|
||||
if(cur->listContent == newItem)
|
||||
return position;
|
||||
}
|
||||
return FALSE;
|
||||
110
The_Store/queue.cpp
Normal file
110
The_Store/queue.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
//#####################################################
|
||||
// QUEUE.CPP
|
||||
//=====================================================
|
||||
|
||||
const int Qleft[2] = {50,70};
|
||||
const int Qtop = 1;
|
||||
const int QmoveX = 0;
|
||||
const int QmoveY = 4;
|
||||
|
||||
template <class T>
|
||||
class queueClass
|
||||
{
|
||||
public:
|
||||
queueClass();
|
||||
~queueClass();
|
||||
T retrive(int position);
|
||||
int isEmpty();
|
||||
int length();
|
||||
int duplicate(T newItem);
|
||||
void ins(T newItem);
|
||||
void del(int position);
|
||||
private:
|
||||
listClass<TWindow>minLista;
|
||||
typedef struct queueNode* ptrType;
|
||||
ptrType head;
|
||||
int size;
|
||||
};
|
||||
//#####################################################
|
||||
// CONSTRUCTOR
|
||||
// SÄTTER KÖSTORLEKEN TILL NOLL & HEAD TILL NULL
|
||||
// pre: TRUE
|
||||
// post: en kö har skapats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
queueClass<T>::queueClass():size(0), head(NULL)
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// DESTRUCTOR
|
||||
// FÖRSTÖR KÖN, ANROPAS AUTOMATISKT VID AVSLUT
|
||||
// pre: att det finns en kö
|
||||
// post: kön har raderats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
queueClass<T>::~queueClass()
|
||||
{
|
||||
}
|
||||
//#####################################################
|
||||
// RETURNERAR ANTALET ELEMENT I KÖN
|
||||
// pre: TRUE
|
||||
// post: antalet element i kön är returnerade
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int queueClass<T>::length()
|
||||
{
|
||||
return (minLista.listLength());
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM ETT ELEMENT FINNS
|
||||
// pre: att det finns en kö
|
||||
// post: TRUE om elementet redan finns
|
||||
// post: FALSE om elementet inte redan finns
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int queueClass<T>::duplicate(T newItem)
|
||||
{
|
||||
return (minLista.newDuplicate(newItem));
|
||||
}
|
||||
//#####################################################
|
||||
// LÄGGER TILL ETT NYTT ELEMENT I KÖN
|
||||
// pre: att maximalt antal element inte finns
|
||||
// pre: att elementet inte finns
|
||||
// post:ett nytt element
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void queueClass<T>::ins(T newItem)
|
||||
{
|
||||
size++;
|
||||
minLista.listInsert(size, newItem);
|
||||
}
|
||||
//#####################################################
|
||||
// TAR BORT ELEMENT I LISTAN
|
||||
// pre: att det finns en kö
|
||||
// post: översta elementet har tagits bort
|
||||
//=====================================================
|
||||
template <class T>
|
||||
void queueClass<T>::del(int position)
|
||||
{
|
||||
size--;
|
||||
minLista.listDel(position);
|
||||
}
|
||||
//#####################################################
|
||||
// KOLLAR OM KÖN ÄR TOM
|
||||
// pre: TRUE
|
||||
// post: TRUE om kön är tom
|
||||
// post: FALSE om kön inte är tom
|
||||
//=====================================================
|
||||
template <class T>
|
||||
int queueClass<T>::isEmpty()
|
||||
{
|
||||
return (minLista.listIsEmpty());
|
||||
}
|
||||
//#####################################################
|
||||
// HÄMTAR ELEMENTET
|
||||
// pre: att det finns en lista
|
||||
// post: det översta elementet har returnerats
|
||||
//=====================================================
|
||||
template <class T>
|
||||
T queueClass<T>::retrive(int position)
|
||||
{
|
||||
203
The_Store/twindow.cpp
Normal file
203
The_Store/twindow.cpp
Normal file
@@ -0,0 +1,203 @@
|
||||
#include <dos.h>
|
||||
#include <conio.h>
|
||||
#include <bios.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "twindow.hpp"
|
||||
|
||||
const int MAXX=80;
|
||||
const int MAXY=25;
|
||||
const int MINX=0;
|
||||
const int MINY=0;
|
||||
class Display
|
||||
{
|
||||
public:
|
||||
typedef unsigned far* ScreenPtr;
|
||||
static void drawChar(unsigned x,unsigned y, char c, unsigned fg, unsigned bg);
|
||||
};
|
||||
void Display::drawChar(unsigned x,unsigned y, char c, unsigned fg, unsigned bg)
|
||||
{
|
||||
ScreenPtr display = (ScreenPtr)MK_FP(0xB800,0x0);
|
||||
display[y*80+x] = (((bg<<4)|fg)<<8)|(c & 255);
|
||||
}
|
||||
//=============================================================================
|
||||
// default constructor: skapar fönstret med vissa koordinater och färger
|
||||
//-----------------------------------------------------------------------------
|
||||
TWindow::TWindow()
|
||||
{
|
||||
Left=1;Right=8;Bottom=24;Top=20;background=BLUE;foreground=WHITE;
|
||||
text=NULL;
|
||||
}
|
||||
//=============================================================================
|
||||
// constructor: skapar fönstret med vissa koordinater och färger
|
||||
//-----------------------------------------------------------------------------
|
||||
TWindow::TWindow(unsigned left,unsigned top,unsigned right ,unsigned bottom, DColor bg, DColor fg):
|
||||
Left(left),Right(right),Bottom(bottom),Top(top),background(bg), foreground(fg)
|
||||
{
|
||||
theState = 0;//FALSE;
|
||||
text=NULL;
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// destructor: förstör fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
TWindow::~TWindow()
|
||||
{
|
||||
if(isVisible())
|
||||
hide();
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// hide: gömmer fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::hide()
|
||||
{
|
||||
erase();
|
||||
}
|
||||
//=============================================================================
|
||||
// show: visar fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::display()
|
||||
{
|
||||
draw();
|
||||
if(text)
|
||||
showText();
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// draw: ritar upp fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::draw()
|
||||
{
|
||||
theState = 1;//;TRUE;
|
||||
for(int j= Top; j< Bottom; j++ )
|
||||
for(int i=Left; i< Right ; i++ )
|
||||
{
|
||||
Display::drawChar(i,j,' ',foreground,background);
|
||||
}
|
||||
drawBorder();
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// erase: ritar över fönstret med svart
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::erase()
|
||||
{
|
||||
for(int j= Top; j<= Bottom; j++ )
|
||||
for(int i=Left; i<= Right ; i++ )
|
||||
Display::drawChar(i,j,' ',BLACK,BLACK);
|
||||
theState = 0;//FALSE;
|
||||
}
|
||||
|
||||
//=============================================================================
|
||||
// drewBorder: ritar ramen runt fönstret
|
||||
//-----------------------------------------------------------------------------
|
||||
void TWindow::drawBorder()
|
||||
{
|
||||
for(int i=Left+1; i< Right; i++)Display::drawChar(i ,Top , HLine , foreground, background);
|
||||
for( i=Left+1; i< Right; i++)Display::drawChar(i ,Bottom , HLine , foreground, background);
|
||||
for( i=Top+1; i< Bottom; i++)Display::drawChar(Left ,i , VLine , foreground, background);
|
||||
for( i=Top+1; i< Bottom; i++)Display::drawChar(Right ,i , VLine , foreground, background);
|
||||
Display::drawChar(Left ,Top , LeftTop , foreground, background);
|
||||
Display::drawChar(Right ,Top ,RightTop , foreground, background);
|
||||
Display::drawChar(Right ,Bottom ,RightBottom, foreground, background);
|
||||
Display::drawChar(Left ,Bottom , LeftBottom, foreground, background);
|
||||
}
|
||||
void TWindow::move(unsigned relX, unsigned relY)
|
||||
{
|
||||
Left=Left+relX;
|
||||
Right=Right+relX;
|
||||
Top=Top+relY;
|
||||
Bottom=Bottom+relY;
|
||||
}
|
||||
void TWindow::moveAbs(unsigned x, unsigned y)
|
||||
{
|
||||
unsigned width=Right-Left;
|
||||
unsigned height=Bottom-Top;
|
||||
Left=x;
|
||||
Right=x+width;
|
||||
Top=y;
|
||||
Bottom=y+height;
|
||||
}
|
||||
|
||||
void TWindow::setText(char* txt)
|
||||
{
|
||||
text=new char[strlen(txt)];
|
||||
strcpy(text, txt);
|
||||
}
|
||||
void TWindow::showText()
|
||||
{
|
||||
int x=Left+1,y=Top+1;
|
||||
for(int i=0;i<strlen(text);i++)
|
||||
{
|
||||
Display::drawChar(x, y, text[i], foreground, background);
|
||||
if(++x>=Right)
|
||||
{
|
||||
x=Left+1;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
}
|
||||
void TWindow::editText()
|
||||
{
|
||||
int i=0;
|
||||
delete text;
|
||||
int size=(Right-Left-1)*(Bottom-Top-1);
|
||||
text=new char[size];
|
||||
text[0]='\0';
|
||||
char* buffer=new char[size];
|
||||
while(((buffer[i]=bioskey(0))!=ENTER)&&(i<size))
|
||||
{
|
||||
if(buffer[i]==BACK)
|
||||
{
|
||||
text[i-1]='\0';
|
||||
if(--i<0)i=0;
|
||||
draw();
|
||||
showText();
|
||||
}
|
||||
else if(buffer[i]==ESC)
|
||||
{
|
||||
delete text;
|
||||
text=NULL;
|
||||
draw();
|
||||
return;
|
||||
}
|
||||
else if(isalnum(buffer[i])||buffer[i]==' ')
|
||||
{
|
||||
text[i]=buffer[i];
|
||||
text[i+1]='\0';
|
||||
showText();
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
TWindow& TWindow::operator=(TWindow& t)
|
||||
{
|
||||
delete text;
|
||||
text=new char[strlen(t.text)];
|
||||
strcpy(text, t.text);
|
||||
return *this;
|
||||
}
|
||||
boolean TWindow::operator==(TWindow& t)
|
||||
{
|
||||
return strcmp(t.text, text)==0;
|
||||
}
|
||||
boolean TWindow::operator<(TWindow& t)
|
||||
{
|
||||
return strcmp(text, t.text)<0;
|
||||
}
|
||||
boolean TWindow::operator>(TWindow& t)
|
||||
{
|
||||
return strcmp(text, t.text)>0;
|
||||
}
|
||||
ostream& operator<<(ostream& o, TWindow& t)
|
||||
{
|
||||
t.display();
|
||||
return o;
|
||||
}
|
||||
istream& operator>>(istream&i, TWindow& t)
|
||||
{
|
||||
t.editText();
|
||||
return i;
|
||||
}
|
||||
|
||||
39
The_Store/twindow.hpp
Normal file
39
The_Store/twindow.hpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef __TWINDOW__H
|
||||
#define __TWINDOW__H
|
||||
#include "keycode.h"
|
||||
#include "boolean.h"
|
||||
#include <iostream.h>
|
||||
typedef unsigned char DColor;
|
||||
class TWindow
|
||||
{
|
||||
public:
|
||||
TWindow();
|
||||
TWindow(unsigned left,unsigned top,unsigned right ,unsigned bottom, DColor bg, DColor fg);
|
||||
~TWindow();
|
||||
boolean isVisible() {return theState;}
|
||||
void hide();
|
||||
void display();
|
||||
void move(unsigned nX, unsigned nY);
|
||||
void TWindow::moveAbs(unsigned x, unsigned y);
|
||||
void setText(char* txt);
|
||||
void editText();
|
||||
char* getText(){return text;};
|
||||
void setColor(unsigned char col){background=col;};
|
||||
TWindow& operator=(TWindow& t);
|
||||
boolean operator==(TWindow& t);
|
||||
boolean operator<(TWindow& t);
|
||||
boolean operator>(TWindow& t);
|
||||
private:
|
||||
char* text;
|
||||
DColor background, foreground;
|
||||
boolean theState;
|
||||
unsigned Left,Top, Right,Bottom;
|
||||
void draw();
|
||||
void erase();
|
||||
void drawBorder();
|
||||
void showText();
|
||||
};
|
||||
ostream& operator<<(ostream& o, TWindow& t);
|
||||
istream& operator>>(istream&i, TWindow& t);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user