Startpunkten

This commit is contained in:
2026-03-05 13:44:23 +01:00
commit adf9f82c8b
26 changed files with 3313 additions and 0 deletions

6
Template_List/boolean.h Normal file
View 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
View 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
View 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
View 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
View 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
View 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