Startpunkten

This commit is contained in:
2026-03-05 13:43:17 +01:00
commit f54dd53c73
13 changed files with 679 additions and 0 deletions

20
Java_Database/Book.java Normal file
View File

@@ -0,0 +1,20 @@
/**
* This is a class called Book wich is
* a part of Element. So, each Element
* is a Book containing all the info
* about every Book.
*
* @author Christian Ohlsson, Karlstad university 1999
* @version 1.0.0
*/
public class Book extends Element {
int MAXAUTHORS = 5;
String subject;
String title;
String allAuthors;
String []author = new String [MAXAUTHORS];
String publisher;
String year;
}

View File

@@ -0,0 +1,86 @@
/**
* This is a class containing functios
* for the Elements in the list.
*
*
* @author Christian Ohlsson, Karlstad university 1999
* @version 1.0.0
*/
public class Element {
Element suc;
/**
* This is just the constructor.
*/
public Element() { suc = null; }
/**
* Returns the current Element in the list
*/
public Element suc() { return suc; }
/**
* This function inserts a new Element
* at the end of the List.
* It first checks out if the List is empty,
* i.e the first Element is null, and then
* the first Element is this Element.
* Otherwise, it inserts the new Element
* at the end of the List.
*/
public void into(List theList) {
if (theList.first==null)
theList.first = theList.last = this;
else {
theList.last.suc = this;
theList.last = this;
}
suc = null;
}
/**
* This function inserts a new Element at
* the beginning of the list.
* It first checks out if the List is empty,
* i.e the first Element is null, and then
* the first Element is this Element.
* Otherwise, the Element that were in the
* beginning, now is in second place and
* the new Element is in front of the List.
*/
public void intoAsFirst(List theList) {
if (theList.first==null) {
theList.first = theList.last = this;
suc = null;
}
else {
suc = theList.first;
theList.first = this;
}
}
/**
* This function deletes an Element in
* the List by traversing through the
* List until it gets to the Element
* that should be deleted.
*/
public void out(List theList) {
Element prev = null;
Element e = theList.first;
while (e!=null && e!=this) {
prev = e;
e = e.suc;
}
if (e!=null) {
if (theList.last==this)
theList.last = prev;
if (theList.first==this)
theList.first = suc;
else
prev.suc = suc;
suc = null;
}
}
}

184
Java_Database/Linked.java Normal file
View File

@@ -0,0 +1,184 @@
import java.awt.*;
import java.applet.*;
import java.io.*;
import java.util.*;
/**
* This is the mainfile containing functions for
* this linked list with books. It also contains
* the constructor List that generates a new list
* called: myList.
*
*
* @author Christian Ohlsson, Karlstad university 1999
* @version 1.3.4
*/
public class Linked {
static List myList = new List();
/**
* This function opens up a file and reads it
* line by line into a Stringbuffer. This
* buffer gets later on splitted into different
* categories by the StringTokenizer. The
* token I have choosed is the ; character,
* which separates the different fields apart
* in the file containing the data. After a
* line is read and tokenized, different
* variables is set in the node called myBook.
* Every new node, book, is inserted into myList.
*/
public static int loadBooks(){
String s, token = ";";
int rows = 0;
try {
BufferedReader infil = new BufferedReader(new FileReader("fil.dat"));
do {
Book myBook = new Book();
s = infil.readLine();
StringTokenizer st = new StringTokenizer(s, token);
myBook.subject = st.nextToken(token);
myBook.title = st.nextToken(token);
setAuthors(myBook, st.nextToken(token));
myBook.publisher = st.nextToken(token);
myBook.year = st.nextToken(token);
myBook.into(myList);
rows++;
}while(true);
}
catch(NoSuchElementException e1){System.err.println("No Such Element.");}
catch(FileNotFoundException e2){System.err.println("File Not Found.");}
catch(IOException e3){System.err.println("I/O Error.");}
catch(NullPointerException e4){}
System.out.println("This database contains " + rows + " books.");
return rows;
}
/**
* This function reads a line from STDIN by using
* DataInputStream, and if the beginning character
* is an 'x', the program quits, if no character
* is given, the function restarts.
* If everything went OK, the search-string is sent
* to a function called inCatalog(String s) for
* further processing.
*/
public static void search(int rows) {
DataInputStream in = new DataInputStream(new BufferedInputStream(System.in));
String s;
try {
for (int i = 0; i < rows ; i++) {
System.out.print("\nBook register\n" +
"Press 'x' to exit.\n" +
"############################################\n" +
"Search for: ");
s = in.readLine();
if(s.charAt(0) == 'x')
break;
else
inCatalog(s, rows);
}
}
catch(IOException e1) { e1.printStackTrace(); }
catch(StringIndexOutOfBoundsException e2) {
System.out.println("Enter a value, or press 'x' to exit");
search(rows);
}
}
/**
* This function calls the function myList.getElement
* and gets every node, book, in the database.
* It then compares every value of the node to the
* given String, and if it finds a match: the whole
* node is printed out along with the number of entries
* the search value found matches with. Otherwise, a
* message is printed out on the screen.
*/
public static void inCatalog(String soktVarde, int rows) {
int found = 0;
try {
for (int i=0 ; i<rows ; i++) {
Book myBook = new Book();
myBook = (Book)myList.getElement(i);
if((myBook.subject.compareTo(soktVarde) == 0) ||
(myBook.title.compareTo(soktVarde) == 0) ||
(isAuthor(myBook.author, soktVarde) ) ||
(myBook.publisher.compareTo(soktVarde) == 0) ||
(myBook.year.compareTo(soktVarde) == 0)) {
found++;
System.out.println("\nSubject \t" + myBook.subject);
System.out.println("Title \t\t" + myBook.title);
System.out.print("Author \t\t" + myBook.allAuthors);
System.out.println("\nPublisher \t" + myBook.publisher);
System.out.println("Year \t\t" + myBook.year);
}
}
}
catch(NullPointerException e) {}
if (found > 0)
System.out.println("There were " + found +
" entries in the database about " +
soktVarde + ".");
else
System.out.println("There's no information about " +
soktVarde + " in this database");
}
/**
* This function recives an array of authors and a
* search string. If then loops through the array
* of authors and tries to locate the search string
* in the array. Because I use indexOf for the search,
* the author Kernighan A. B will be found if the search
* string is Kerni, rnighhan, or n A. B.
* It returns true if the author was found, otherwise false.
*/
public static boolean isAuthor(String author[], String soktVarde) {
try {
int i=0;
while(author[i] != null) {
if(author[i].indexOf(soktVarde) >= 0)
return true;
i++;
}
}
catch(NullPointerException e4){System.out.println("Null ptr in isAthor");}
return false;
}
/**
* This function recieves a Book, and a String
* containing all of the authors of a Book. It
* the splits up the String, using the StringTokenizer
* and puts the different authors in different
* places in the array of authors of each Book.
*/
public static void setAuthors(Book myBook, String inString) {
String token = ",";
int i=0;
myBook.allAuthors = inString;
try {
StringTokenizer st = new StringTokenizer(inString, token);
while(st.hasMoreTokens()) {
myBook.author[i] = st.nextToken(token);
i++;
}
}
catch(NoSuchElementException e1){System.err.println("No Such Element.");}
catch(NullPointerException e4){System.err.println("Null Pointer In setAuthors.");}
}
/**
* The main function really does'nt do much.
* It just calls the functions loadBooks()
* and gets the number of Books in the database
* from there and then calls the search-function
* with the number of Books as a parameter.
*/
public static void main(String args[]) {
int rows = loadBooks();
search(rows);
}
}

70
Java_Database/List.java Normal file
View File

@@ -0,0 +1,70 @@
/**
* This class contains the linked list.
* It also contains the constructor, wich
* generates a new list called myList
*
* @author Christian Ohlsson, Karlstad university 1999
* @version 1.0.0
*/
public class List {
Element first, last;
/**
* This is the constructor of the list.
* What it basically do is to set
* the two Elements constructed before
* to null.
*/
public List() { first = last = null; }
/**
* This function returns the first Element
* in the list.
*/
public Element first() { return first; }
/**
* This function returns the last Element
* in the list.
*/
public Element last() { return last; }
/**
* If the first element in the List is null,
* it returns true, that is, there's no Elements
* in the List.
*/
public boolean empty() { return first==null; }
/**
* This function recives a number and it traverses
* though the List until that number is reached
* and then returns the Element on that location
*/
public Element getElement(int number) {
Element e = first;
int antal = 0;
while (antal<number) {
antal++;
e = e.suc;
}
return e;
}
/**
* This function initialize a number to zero
* and the traverses though the List until
* it reaches the end and than returns
* the number of elements.
*/
public int cardinal() {
int antal = 0;
Element e = first;
while (e!=null) {
antal++;
e = e.suc;
}
return antal;
}
}

10
Java_Database/fil.dat Normal file
View File

@@ -0,0 +1,10 @@
C Lang;The C Programming Language;Kernighan B.W.,Ritchie D.M.;Prentice Hall;1988
C Lang;From C to C: An Intro to ANSI Standard C;Gardner J.;Harcourt, Brace Jovanovich;1989
Compilers;Compilers: Principles Techniques and Tools;Aho A.V.,Sethi R.,Ullman J.D.;Addison Wesley;1986
Compilers;Compiler Design in C;Holub A.I.;Prentice Hall;1990
Databases;An Introduction to Database Systems;Date C.J.;Addison Wesley;1995
Databases;The Theory of Relational Databases;Maier D.;Computer Science Press;1983
HCI;Human-Computer Interaction;Dix A.,Finlay J.,Abowd G.,Beale R.;Prentice Hall;1993
Prog Langs;Programming Languages, Concepts & Constructs;Sethi R.;Addison Wesley;1996
Prog Langs;Concepts of Programming Languages;Sebesta R.W.;Addison Wesley;1996
Prog Langs;Programming Languages;Louden K.C.;PWS Publishing;1993

11
Java_Database/read_me Normal file
View File

@@ -0,0 +1,11 @@
Readme fil för Lab2 i Programspråk
-------------------------------------------
Sökbar länkad lista i Java.
Kompilera med: javac Linked.java
Kör därefter programmet med: java Linked
Labben är provkörd och klar.
-------------------------------------------
Christian Ohlsson, di7chro@cse.kau.se

BIN
Java_Intro/HelloWorld.class Normal file

Binary file not shown.

104
Java_Intro/HelloWorld.java Normal file
View File

@@ -0,0 +1,104 @@
/**************************************************/
/* Java Lab 1 i Programspråk */
/**************************************************/
/* Textsträng som fladdrar runt i appleten */
/**************************************************/
/* Christian Ohlsson di7chro@cse.kau.se */
/**************************************************/
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
public class HelloWorld extends Applet
implements Runnable {
Thread animationThread = null;
int delay=100, xspeed, yspeed;
int xsize, ysize, xOffset, yOffset, helloTextWidth,
helloTextHeight;
Image backBuffer;
Graphics backGC; // Graphics Context
String helloText;
// Initialize the Applet
public void init() {
xsize=getSize().width;
ysize=getSize().height;
backBuffer = createImage(xsize, ysize);
backGC = backBuffer.getGraphics();
// Get the text from the html page that the user wants to scroll
helloText=getParameter("text");
String s=getParameter("speed");
xspeed=yspeed= Integer.parseInt(s);
// Calculate the real size of the string using the font
FontMetrics fontInfo=backGC.getFontMetrics();
helloTextWidth=fontInfo.stringWidth(helloText);
helloTextHeight=fontInfo.getHeight();
xOffset=5;
yOffset=5;
yOffset=helloTextHeight;
}
// This method is called when the applet becomes visible
public void start() {
// Create a thread and start it
if(animationThread == null) {
animationThread = new Thread(this);
animationThread.start();
}
}
// This method is called when the applet becomes invisible
public void stop() {
// Stop animation thread
animationThread = null;
}
public void run() {
long time = System.currentTimeMillis();
while(animationThread != null) {
// Use a constant frame rate based on the system clock
try {
time += delay;
Thread.sleep(Math.max(0, time -
System.currentTimeMillis()));
} catch (InterruptedException e) {}
// initiate a repaint of the screen
repaint();
}
}
public void paint(Graphics g) {
// Clear the screen
backGC.setColor(Color.yellow);
backGC.fillRect(0,0,xsize,ysize);
// Draw text
backGC.setColor(Color.black);
backGC.drawString(helloText, xOffset, yOffset);
xOffset-=xspeed;
if(xOffset <= 1)
xspeed = -xspeed;
if(xOffset+helloTextWidth >= xsize)
xspeed = -xspeed;
yOffset-=yspeed;
if(yOffset <= 5)
yspeed = -yspeed;
if(yOffset+helloTextHeight >= ysize)
yspeed = -yspeed;
// Copy the backbuffer to the screen
g.drawImage(backBuffer, 0, 0, this);
}
public void update(Graphics g) {
paint(g);
}
}

15
Java_Intro/view_it.html Normal file
View File

@@ -0,0 +1,15 @@
<HTML>
<TITLE> Java Lab1 </TITLE>
<BODY BGCOLOR="#FFFFFF">
<FONT FACE="ARIAL, TIMES" SIZE="3">
<FONT SIZE=+3>Java Lab 1
<BR>
<APPLET CODE="HelloWorld.class" HEIGHT=200 WIDTH=200>
<param name="text" value="GabbaGabbaHey!">

102
Lisp_Database/db.lisp Normal file
View File

@@ -0,0 +1,102 @@
; ==========================================
; # Laboration 2 i Programspråk #
; # LISP #
; # ======================================== #
; # Christian Ohlsson #
; # Karlstads universitet #
; # 1999-05-19 #
; # =========================================
;Main function which starts the program
;Prints welcome-msg and ask for yor choice
(defun run ()
(format t "~% Welcome to the database ")
(format t "~% ======================= ")
(format t "~% Press s to search, ")
(format t "~% or 'x' to exit. ")
(format t "~% ======================= ")
(format t "~% ")
(setq val (read))
(cond ((equal val 's) (dosearch (getdb)) (run))
((equal val 'x) (format t "~%Exiting") )
(t (format t "~% That's not a valid option") (run))
)
)
;Prints out each found book on the screen
(defun printbook(book)
(when book
(format t "~% Subject : ~a" (first book))
(format t "~% Title : ~a" (second book))
(format t "~% Publisher : ~a" (third book))
(format t "~% Year : ~a" (fourth book))
(printauthors (fifth book))
(format t "~% ")
(format t "~% ------------------------------------------------")
)
)
;Asks for the search value and then calls
;the search-procedure
(defun dosearch (library)
(format t "~% Enter search: ")
(setq value (read-line t))
(getit library value)
)
;Prints out the authors of each book
(defun printauthors(authors)
(when authors
(format t "~% Author : ~a"(first authors))
(printauthors (rest authors))
)
)
;Gets the subject, title, publisher, year and author
;for each book found
(defun getbook(book value)
(cond
((integerp(search value (first book))) (printbook book))
((integerp(search value (second book))) (printbook book))
((integerp(search value (third book))) (printbook book))
((integerp(search value (fourth book))) (printbook book))
((getauthor (fifth book) value) (printbook book))
)
)
;Searches recursivly throuh the library and
;lets getbook see if it's a match,
;and the do the same with the next book.
(defun getit(library value)
(when library
(getbook (first library) value)
(getit(rest library) value)
)
)
;Checks if it is a author
(defun getauthor (authors value)
(when authors
(cond
((integerp(search value (first authors))) t)
(t (getauthor (rest authors) value))
)
)
)
;Defines our database.
(defun getdb ()
(setf library '
(
( "C Lang" "From C to C An Intro to ANSI Standard C" "Harcourt Brace Jovanovic" "1989" ("Gardner J.") )
( "Compilers" "Compilers Principles Techniques and Tools" "Addison Wesley" "1986" ( "Ullman J.D." "Aho A.V." "Sethi R.") )
( "Compilers" "Compiler Design in C" "Prentice Hall" "1990" ( "Holub A.I.") )
( "Databases" "An Introduction to Database Systems" "Addison Wesley" "1995" ( "Date C.J.") )
( "Databases" "The Theory of Relational Databases" "Computer Science Press" "1983" ( "Maier D.") )
( "HCI" "Human-Computer Interaction" "Prentice Hall" "1993" ( "Dix A." "Finlay J." "Abowd G." "Beale R.") )
( "Prog Langs" "Programming Languages Concepts & Constructs" "Addison Wesley" "1996" ( "Sethi R.") )
( "Prog Langs" "Concepts of Programming Languages" "Addison Wesley" "1996" ( "Sebesta R.W.") )
( "C Lang" "The C Programming Language" "Prentice Hall" "1988" ( "Kernighan B.W." "Ritchie D.M.") )
( "Prog Langs" "Programming Languages Principles and Practice" "PWS Publishing" "1993" ( "Louden K.C." ))
)
)
)

14
Lisp_Database/read_me Normal file
View File

@@ -0,0 +1,14 @@
Readme fil för Lisp Lab i Programspråk
---------------------------------------------
Sökbar databas med böcker.
Denna lab är provkörd och klar.
Starta Lisp med: gcl
skriv:
(load "db.lisp")
(run)
för att köra programmet.
---------------------------------------------
Christian Ohlsson. di7chro@cse.kau.se

49
Prolog_Database/db.pl Normal file
View File

@@ -0,0 +1,49 @@
/********************************/
/* Laboration 4 i Programspråk */
/* Prolog */
/********************************/
/* Christian Ohlsson */
/* Karlstads universitet 990525 */
/********************************/
book('C Lang', 'The C Programming Language', ['Kernighan B.W.', 'Ritchie D.M.'], 'Prentice Hall', '1988').
book('C Lang', 'From C to C: An Intro to ANSI Standard C', ['Gardner J.'], 'Harcourt:Brace Jovanovic', '1989').
book('Compilers', 'Compilers: Principles Techniques and Tools', ['Ullman J.D.', 'Aho A.V.', 'Sethi R.'], 'Addison Wesley', '1986').
book('Compilers', 'Compiler Design in C', ['Holub A.I.'], 'Prentice Hall', '1990').
book('Databases', 'An Introduction to Database Systems', ['Date C.J.'], 'Addison Wesley', '1995').
book('Databases', 'The Theory of Relational Databases', ['Maier D.'], 'Computer Science Press', '1983').
book('HCI', 'Human-Computer Interaction', ['Dix A.', 'Finlay J.', 'Abowd G.', 'Beale R.'], 'Prentice Hall', '1993').
book('Prog Langs', 'Programming Languages Concepts & Constructs', ['Sethi R.'], 'Addison Wesley', '1996').
book('Prog Langs', 'Concepts of Programming Languages', ['Sebesta R.W.'], 'Addison Wesley', '1996').
book('Prog Langs', 'Programming Languages Principles and Practice', ['Louden K.C.'], 'PWS Publishing', '1993').
run :- write_ln(' '),
write_ln(' ========================================== '),
write_ln(' Welcome to the database. '),
write_ln(' Type search(searchstring). to search '),
write_ln(' ========================================== '),
write_ln(' ').
subject(X) :- book(X,T,A,P,Y), printbook(X,T,A,P,Y), fail.
title(X) :- book(S,X,A,P,Y), printbook(S,X,A,P,Y), fail.
author(X) :- book(S,T,A,P,Y), member(X,A), printbook(S,T,X,P,Y), fail.
publisher(X) :- book(S,T,A,X,Y), printbook(S,T,A,X,Y), fail.
year(X) :- book(S,T,A,P,X), printbook(S,T,A,P,X), fail.
search(X) :- subject(X).
search(X) :- title(X).
search(X) :- author(X).
search(X) :- publisher(X).
search(X) :- year(X).
member(X,[X|_]).
member(X,[_|Y]) :- member(X,Y).
printbook(S,T,A,P,Y) :- write('Subject: '), write_ln(S),
write('Title: '), write_ln(T),
write('Author: '), write_ln(A),
write('Publisher: '), write_ln(P),
write('Year: '), write_ln(Y),
write_ln(' ').

14
Prolog_Database/read_me Normal file
View File

@@ -0,0 +1,14 @@
Readme fil för Prolog Lab i Programspråk
---------------------------------------------
Sökbar databas med böcker.
Denna Lab är provkörd och klar.
Starta SWI Prolog med: pl
Skriv:
['db.pl'].
run.
för att köra programmet.
---------------------------------------------
Christian Ohlsson. di7chro@cse.kau.se