initial setup

This commit is contained in:
Jaanus Poeial 2016-10-17 16:38:41 +03:00
commit 144168daa3
11 changed files with 1336 additions and 0 deletions

112
GraafiYlesandedNew.html Normal file
View File

@ -0,0 +1,112 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Graafi ülesanded</title>
</head>
<body>
<h3>Graafid</h3>
<p>Kõigi ülesannete puhul tuleb kasutada Moodle'st võetud
programmitoorikut (klassid Graph, Vertex, Arc). Vajadusel tohib
nendes klassides lisada isendimuutujaid ja meetodeid. <br>
Aruandes peab olema vähemalt 5 lahendusnäidet erinevate olukordade
jaoks.<br>
</p>
<ol>
</ol>
<b>Kui leiate ise huvitava ülesande, mida on võimalik lahendada
graafide abil (kasutades programmitoorikus etteantud viisi graafi
kujutamiseks külgnevusstruktuuri kujul)</b><b>, siis leppige see
õppejõuga kokku - see on väga hea võimalus individuaaltöö endale
huvitavaks teha.</b> Allpool mõned õppejõu poolt pakutavad
ülesanded.<br>
<ol>
<li>Koostada graafi sügava kloonimise meetod (tulemuses peavad
olema kõik Vertex ja Arc tüüpi objektid samuti kloonitud)
kooskõlas Java konventsioonidega.<br>
</li>
<li>Koostada meetod, mis teeb kindlaks, kas etteantud sidusas
lihtgraafis leidub Euleri tsükkel, ning selle leidumisel
nummerdab kõik servad vastavalt nende järjekorrale Euleri tsükli
läbimisel (vt. ka Fleury' algoritm).</li>
<li>Koostada meetod etteantud graafist etteantud tipu
eemaldamiseks (eemaldada tuleb ka kõik sellesse tippu suubuvad
ning sellest tipust väljuvad kaared).</li>
<li>Koostada meetod, mis arvutab etteantud sidusa lihtgraafi G=(V,
E) iga tipu v jaoks välja selle ekstsentrilisuse:</li>
</ol>
<div style="margin-left: 80px;">&nbsp;e(v) = max {d(u, v)| u kuulub
hulka V } , kus d(u, v) on tipu u kaugus tipust v (lühima tee
pikkus tipust u tippu v)<br>
</div>
<ol start="5">
<li>Koostada meetod n-tipulise täisgraafi moodustamiseks (Moodle
programmitoorikus kokkulepitud sisekujul), kus n &gt;= 0 on ette
antud meetodi parameetrina.<br>
</li>
<li>Koostada meetod, mis leiab sidusa lihtgraafi kõik niisugused
servad, millest ühe eemaldamine muudaks graafi mittesidusaks
(nn. "sillad").</li>
<li>Koostada meetod etteantud sidusa lihtgraafi minimaalse kaaluga
aluspuu (minimaalse toese) leidmiseks.</li>
<li>Koostada meetod etteantud lihtgraafi täiendgraafi leidmiseks.</li>
<li>Koostada meetod etteantud graafi transitiivse sulundi
leidmiseks.</li>
<li>Koostada meetod etteantud graafi n astme leidmiseks (n&gt;=0)
kasutades korrutamist.</li>
<li>*On antud sidus lihtgraaf, mille iga serva jaoks on teada
selle läbilaskevõime ("toru jämedus", mittenegatiivne). Koostada
meetod, mis leiab kahe etteantud tipu&nbsp; vahelise maksimaalse
läbilaskevõime Ford-Fulkersoni meetodil.</li>
<li>Koostada meetod, mis leiab etteantud sidusas lihtgraafis
etteantud tipust v kõige kaugemal asuva tipu.<br>
</li>
<li>Koostada meetod, mis leiab etteantud sidusas lihtgraafis
lühima tee kahe teineteisest maksimaalselt kaugel paikneva tipu
vahel.</li>
<li>Koostada meetod, mis leiab etteantud sidusas lihtgraafis kahe
etteantud tipu vahelise lühima tee.</li>
<li>*Koostada meetod, mis leiab etteantud lihtgraafis pikima
võimaliku tsükli (maksimaalse pikkusega kinnise ahela, milles
servad ei kordu).</li>
<li>Buldas, Laud, Villemson. Graafid (käsikiri, 2008, vt.
Moodle)&nbsp; lk.20, ül.12 - koostada meetod etteantud graafi
servgraafi leidmiseks.</li>
<li>- " -. lk. 20, ül. 15 - koostada meetod etteantud sidusas
lihtgraafis G etteantud tipu v ekstsentrilisuse leidmiseks.</li>
<li>- " -. lk. 20, ül. 15 - koostada meetod etteantud sidusa
lihtgraafi G raadiuse leidmiseks.</li>
<li>- " -. lk. 20, ül. 15 - koostada meetod etteantud sidusa
lihtgraafi G diameetri leidmiseks.</li>
<li>- " -. lk. 20, ül. 15 - koostada meetod etteantud sidusa
lihtgraafi G tsentri leidmiseks.<br>
</li>
<li>- " -. lk. 21, ül. 24 - koostada meetod etteantud lihtgraafi
kolmnurkade graafi moodustamiseks.</li>
<li>http://en.wikipedia.org/wiki/Pr%C3%BCfer_sequence - koostada
meetod etteantud puu Prüferi koodi leidmiseks.</li>
<li>J.Kiho. A&amp;A ülesannete kogu, 2005. lk.14, ül.6 - koostada
meetod ohutuima tee leidmiseks.</li>
<li>- " -. lk.14, ül.7a - koostada meetod, mis leiab tee läbi
kõrgeima punkti.</li>
<li>- " -. lk.15, ül.7c - koostada meetod, mis leiab tee, mille
kõrgeim tipp on võimalikult madalal.</li>
<li>Goodrich, Tamassia, 4th Ed., 2006. lk. 644, ül. C-13.12</li>
<li>- " -. lk. 643, ül. C-13.6<br>
</li>
<li>- " -. lk. 644, ül. C-13.20</li>
<li>- " -. lk. 643, ül. C-13.9</li>
<li>- " -. lk. 644, ül. C-13.20</li>
<li>- " -. lk. 645, ül. C-13.21</li>
<li>- " -. lk. 645, ül. C-13.23</li>
<li>- " -. lk. 646, ül. C-13.27</li>
<li>- " -. lk. 647, ül. P-13.13<br>
</li>
</ol>
<br>
<hr size="2" width="100%">Jaanus Pöial<br>
<br>
<br>
<br>
</body>
</html>

57
README.md Normal file
View File

@ -0,0 +1,57 @@
# README #
See on kuuenda kodutöö failide hoidla, mida saab kasutada ainult algseks lugemiseks.
Töötamiseks looge endale isiklik repositoorium, näiteks privaatne 'fork' bitbucket serverisse, millest saate luua klooni oma arvutisse.
## Näidete kasutamine käsurealt ##
#### Kompileerimine: ####
```
#!bash
javac -cp src src/GraphTask.java
```
#### Käivitamine: ####
```
#!bash
java -cp src GraphTask
```
### Testide kasutamine ###
#### Testi kompileerimine: ####
```
#!bash
javac -encoding utf8 -cp 'src:test:test/junit-4.12.jar:test/hamcrest-core-1.3.jar' test/GraphTaskTest.java
```
Sama Windows aknas (koolonite asemel peavad olema semikoolonid):
```
#!bash
javac -encoding utf8 -cp 'src;test;test/junit-4.12.jar;test/hamcrest-core-1.3.jar' test/GraphTaskTest.java
```
#### Testi käivitamine: ####
```
#!bash
java -cp 'src:test:test/junit-4.12.jar:test/hamcrest-core-1.3.jar' org.junit.runner.JUnitCore GraphTaskTest
```
Sama Windows aknas (koolonite asemel semikoolonid):
```
#!bash
java -cp 'src;test;test/junit-4.12.jar;test/hamcrest-core-1.3.jar' org.junit.runner.JUnitCore GraphTaskTest
```

32
description.html Normal file
View File

@ -0,0 +1,32 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>i231.graph.v8</title>
</head>
<body>
Graph problems are individual for each student and junit-testing is
therefore impossible. You must still submit your program text here, also
you find here the pointer structure description (classes Graph, Vertex and Arc)
to present graphs. To receive a task you have to visit the lab (problems are
in different books and you must choose one, it is not possible to make all
problems available electronically).
Do not forget that you also have to write a report on your solution and
upload it as a pdf-file in Moodle.
<br><br>
Graafi ülesanne on igal inimesel individuaalne ning selle õigsust automaattest
kontrollida ei saa. Küll aga kasutame me seda kanalit programmi teksti
esitamiseks ning soovitatava viidastruktuuri kirjelduse andmiseks, samuti
tagasiside andmiseks programmi kohta.
&Uuml;lesannete valik on laiali erinevates raamatutes ning kahjuks neti kaudu
endale ülesannet valida ei saa - tuleb vahetult kontakteeruda õppejõuga.
&Auml;rge unustage, et aruande pdf-fail tuleb samuti esitada Moodle kaudu.
Nõuded aruande vormistamiseks leiate
http://www.itcollege.ee/~jpoial/algoritmid/vormistus.html
<br>
.
</body>
</html>

17
git_eclipse.txt Normal file
View File

@ -0,0 +1,17 @@
Ava Eclipse ja veendu, et egit-moodul on olemas, vajadusel lisa see Eclipse Marketplace kaudu.
Lisa projekti vaatesse git repositooriumite aken:
Window -> Show View -> Other... -> Git -> Git Repositories
Git Repositories aknas klooni vajalik repositoorium bitbucket-ist.
Loo Java projekt.
Kustuta src kataloog.
vali "Build Path -> Link Source" ja seo projekti source kloonimisest tekkinud
lokaalse repositooriumiga, pane Eclipse projektis selle kataloogi nimeks src.
Majanda versioonihaldusega ainult git repositooriumite aknas.

54
kodutqq6.html Normal file
View File

@ -0,0 +1,54 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>Kodutöö 6</title>
</head>
<body>
<h2 style="margin-left: 40px;">Graaf</h2>
<div style="margin-left: 40px;"> </div>
<div style="margin-left: 40px;">Graafi ülesanne on igal inimesel
individuaalne, koos programmiga tuleb vormistada ka aruanne.
Kasutada tuleb graafi esitust külgnevusstruktuurina (klassid
Vertex, Arc ja Graph), mille jaoks põhja leiate Moodle
keskkonnast.<br>
<br>
Ülesannete valik on laiali erinevates raamatutes <b><span
style="color: rgb(153, 0, 0);"></span></b><b> </b>- ülesande
registreerimiseks tuleb vahetult kontakteeruda õppejõuga.
Varasemal alustajal on suurem hulk ülesandeid, mille seast valida.
Tutvumiseks vt.<a
href="http://enos.itcollege.ee/%7Ejpoial/algoritmid/GraafiYlesandedNew.html">
http://enos.itcollege.ee/~jpoial/algoritmid/GraafiYlesandedNew.html<br>
</a> <br>
Aruande pdf-fail ja programmi tekst (java-fail) tuleb esitada
Moodle kaudu.<br>
<br>
Programmis tuleb koostatud meetodid javadoc abil dokumenteerida.
Nõuded aruande vormistamiseks leiate <a
href="http://enos.itcollege.ee/%7Ejpoial/algoritmid/vormistus.html">http://www.itcollege.ee/~jpoial/algoritmid/vormistus.html</a><br>
<br>
</div>
<div style="margin-left: 40px;"><span style="font-weight: bold;">Kodutöö
tähtaeg</span> on teie rühma materjalide juures Moodle's.
Õigeaegse ja korrektse lahenduse eest saab kuni 16 punkti (kuni 8
punkti programmi eest ja kuni 8 punkti aruande eest).<br>
</div>
<hr style="width: 100%; height: 2px; margin-left: 40px;">
<div style="margin-left: 40px;">
<h2>Graph</h2>
Individual task (visit the lab to get it). 8p for the program and
8p for the report. Report must be self explaining and contain full
description of the problem, description of the solution
(algorithms), instructions for the end user (how to run the
program), testing plan (at least 5 test cases), used literature
sources and appendices: program text (incl javadoc-comments),
screenshots of test cases, etc. Report must be a single pdf-file.
Program inception and deadlines reside is in <a
href="https://moodle.hitsa.ee/">Moodle</a>.<br>
<div style="margin-left: 40px;"> <br>
</div>
<hr style="width: 100%; height: 2px;">Jaanus Pöial<br>
</div>
</body>
</html>

234
src/GraphTask.java Normal file
View File

@ -0,0 +1,234 @@
import java.util.*;
/** Container class to different classes, that makes the whole
* set of classes one class formally.
*/
public class GraphTask {
/** Main method. */
public static void main (String[] args) {
GraphTask a = new GraphTask();
a.run();
throw new RuntimeException ("Nothing implemented yet!"); // delete this
}
/** Actual main method to run examples and everything. */
public void run() {
Graph g = new Graph ("G");
g.createRandomSimpleGraph (6, 9);
System.out.println (g);
// TODO!!! Your experiments here
}
// TODO!!! add javadoc relevant to your problem
class Vertex {
private String id;
private Vertex next;
private Arc first;
private int info = 0;
// You can add more fields, if needed
Vertex (String s, Vertex v, Arc e) {
id = s;
next = v;
first = e;
}
Vertex (String s) {
this (s, null, null);
}
@Override
public String toString() {
return id;
}
// TODO!!! Your Vertex methods here!
}
/** Arc represents one arrow in the graph. Two-directional edges are
* represented by two Arc objects (for both directions).
*/
class Arc {
private String id;
private Vertex target;
private Arc next;
private int info = 0;
// You can add more fields, if needed
Arc (String s, Vertex v, Arc a) {
id = s;
target = v;
next = a;
}
Arc (String s) {
this (s, null, null);
}
@Override
public String toString() {
return id;
}
// TODO!!! Your Arc methods here!
}
class Graph {
private String id;
private Vertex first;
private int info = 0;
// You can add more fields, if needed
Graph (String s, Vertex v) {
id = s;
first = v;
}
Graph (String s) {
this (s, null);
}
@Override
public String toString() {
String nl = System.getProperty ("line.separator");
StringBuffer sb = new StringBuffer (nl);
sb.append (id);
sb.append (nl);
Vertex v = first;
while (v != null) {
sb.append (v.toString());
sb.append (" -->");
Arc a = v.first;
while (a != null) {
sb.append (" ");
sb.append (a.toString());
sb.append (" (");
sb.append (v.toString());
sb.append ("->");
sb.append (a.target.toString());
sb.append (")");
a = a.next;
}
sb.append (nl);
v = v.next;
}
return sb.toString();
}
public Vertex createVertex (String vid) {
Vertex res = new Vertex (vid);
res.next = first;
first = res;
return res;
}
public Arc createArc (String aid, Vertex from, Vertex to) {
Arc res = new Arc (aid);
res.next = from.first;
from.first = res;
res.target = to;
return res;
}
/**
* Create a connected undirected random tree with n vertices.
* Each new vertex is connected to some random existing vertex.
* @param n number of vertices added to this graph
*/
public void createRandomTree (int n) {
if (n <= 0)
return;
Vertex[] varray = new Vertex [n];
for (int i = 0; i < n; i++) {
varray [i] = createVertex ("v" + String.valueOf(n-i));
if (i > 0) {
int vnr = (int)(Math.random()*i);
createArc ("a" + varray [vnr].toString() + "_"
+ varray [i].toString(), varray [vnr], varray [i]);
createArc ("a" + varray [i].toString() + "_"
+ varray [vnr].toString(), varray [i], varray [vnr]);
} else {}
}
}
/**
* Create an adjacency matrix of this graph.
* Side effect: corrupts info fields in the graph
* @return adjacency matrix
*/
public int[][] createAdjMatrix() {
info = 0;
Vertex v = first;
while (v != null) {
v.info = info++;
v = v.next;
}
int[][] res = new int [info][info];
v = first;
while (v != null) {
int i = v.info;
Arc a = v.first;
while (a != null) {
int j = a.target.info;
res [i][j]++;
a = a.next;
}
v = v.next;
}
return res;
}
/**
* Create a connected simple (undirected, no loops, no multiple
* arcs) random graph with n vertices and m edges.
* @param n number of vertices
* @param m number of edges
*/
public void createRandomSimpleGraph (int n, int m) {
if (n <= 0)
return;
if (n > 2500)
throw new IllegalArgumentException ("Too many vertices: " + n);
if (m < n-1 || m > n*(n-1)/2)
throw new IllegalArgumentException
("Impossible number of edges: " + m);
first = null;
createRandomTree (n); // n-1 edges created here
Vertex[] vert = new Vertex [n];
Vertex v = first;
int c = 0;
while (v != null) {
vert[c++] = v;
v = v.next;
}
int[][] connected = createAdjMatrix();
int edgeCount = m - n + 1; // remaining edges
while (edgeCount > 0) {
int i = (int)(Math.random()*n); // random source
int j = (int)(Math.random()*n); // random target
if (i==j)
continue; // no loops
if (connected [i][j] != 0 || connected [j][i] != 0)
continue; // no multiple edges
Vertex vi = vert [i];
Vertex vj = vert [j];
createArc ("a" + vi.toString() + "_" + vj.toString(), vi, vj);
connected [i][j] = 1;
createArc ("a" + vj.toString() + "_" + vi.toString(), vj, vi);
connected [j][i] = 1;
edgeCount--; // a new edge happily created
}
}
// TODO!!! Your Graph methods here! Probably your solution belongs here.
}
}

552
src/TextIO.java Normal file
View File

@ -0,0 +1,552 @@
/*
The file defines a class TextIO, which provides a simple interface
to Java's standard console input and output. This class defines
several static methods for reading and writing
values of various type.
This class will only work with standard, interactive applications.
When it is used in such an application, System.out and System.in
should not be used directly, since the TextIO class thinks it has
exclusive control of System.out and System.in. (Actually, using
System.out will probably not cause any problems, but don't use
System.in.)
To use this class in your program, simply include the compiled class
file TextIO.class in the same directory with the class file for your
main program. (If you are using a development environment such as
CodeWarrior or Visual J++, you can include the source file,
TextIO.java in your project.) You can then use all the public static methods
from the TextIO class in your program. (In your programs, the names
of the methods must be prefaced with "TextIO." For example, you should
use the name TextIO.getln() rather than simply getln().)
(This class is for use with my on-line introductory java textbook,
which is available at http://math.hws.edu/eck/cs124/notes/index.html.)
Written by: David Eck
Department of Mathematics and Computer Science
Hobart and William Smith Colleges
Geneva, NY 14456
Email: eck@hws.edu
WWW: http://math.hws.edu/eck/
July 16, 1998
Modified February, 2000; getChar() now skips blanks and CR's, and getAnyChar()
can be used to read the next char even if it's a blank or CR.
*/
import java.io.*;
public class TextIO {
// *************************** I/O Methods *********************************
// Methods for writing the primitive types, plus type String,
// to the console, with no extra spaces.
//
// Note that the real-number data types, float
// and double, a rounded version is output that will
// use at most 10 or 11 characters. If you want to
// output a real number with full accuracy, use
// "TextIO.put(String.valueOf(x))", for example.
public static void put(int x) { put(x,0); } // Note: also handles byte and short!
public static void put(long x) { put(x,0); }
public static void put(double x) { put(x,0); } // Also handles float.
public static void put(char x) { put(x,0); }
public static void put(boolean x) { put(x,0); }
public static void put(String x) { put(x,0); }
// Methods for writing the primitive types, plus type String,
// to the console,followed by a carriage return, with
// no extra spaces.
public static void putln(int x) { put(x,0); newLine(); } // Note: also handles byte and short!
public static void putln(long x) { put(x,0); newLine(); }
public static void putln(double x) { put(x,0); newLine(); } // Also handles float.
public static void putln(char x) { put(x,0); newLine(); }
public static void putln(boolean x) { put(x,0); newLine(); }
public static void putln(String x) { put(x,0); newLine(); }
// Methods for writing the primitive types, plus type String,
// to the console, with a minimum field width of w,
// and followed by a carriage return.
// If output value is less than w characters, it is padded
// with extra spaces in front of the value.
public static void putln(int x, int w) { put(x,w); newLine(); } // Note: also handles byte and short!
public static void putln(long x, int w) { put(x,w); newLine(); }
public static void putln(double x, int w) { put(x,w); newLine(); } // Also handles float.
public static void putln(char x, int w) { put(x,w); newLine(); }
public static void putln(boolean x, int w) { put(x,w); newLine(); }
public static void putln(String x, int w) { put(x,w); newLine(); }
// Method for outputting a carriage return
public static void putln() { newLine(); }
// Methods for writing the primitive types, plus type String,
// to the console, with minimum field width w.
public static void put(int x, int w) { dumpString(String.valueOf(x), w); } // Note: also handles byte and short!
public static void put(long x, int w) { dumpString(String.valueOf(x), w); }
public static void put(double x, int w) { dumpString(realToString(x), w); } // Also handles float.
public static void put(char x, int w) { dumpString(String.valueOf(x), w); }
public static void put(boolean x, int w) { dumpString(String.valueOf(x), w); }
public static void put(String x, int w) { dumpString(x, w); }
// Methods for reading in the primitive types, plus "words" and "lines".
// The "getln..." methods discard any extra input, up to and including
// the next carriage return.
// A "word" read by getlnWord() is any sequence of non-blank characters.
// A "line" read by getlnString() or getln() is everything up to next CR;
// the carriage return is not part of the returned value, but it is
// read and discarded.
// Note that all input methods except getAnyChar(), peek(), the ones for lines
// skip past any blanks and carriage returns to find a non-blank value.
// getln() can return an empty string; getChar() and getlnChar() can
// return a space or a linefeed ('\n') character.
// peek() allows you to look at the next character in input, without
// removing it from the input stream. (Note that using this
// routine might force the user to enter a line, in order to
// check what the next character is.)
// Acceptable boolean values are the "words": true, false, t, f, yes,
// no, y, n, 0, or 1; uppercase letters are OK.
// None of these can produce an error; if an error is found in input,
// the user is forced to re-enter.
// Available input routines are:
//
// getByte() getlnByte() getShort() getlnShort()
// getInt() getlnInt() getLong() getlnLong()
// getFloat() getlnFloat() getDouble() getlnDouble()
// getChar() getlnChar() peek() getAnyChar()
// getWord() getlnWord() getln() getString() getlnString()
//
// (getlnString is the same as getln and is only provided for consistency.)
public static byte getlnByte() { byte x=getByte(); emptyBuffer(); return x; }
public static short getlnShort() { short x=getShort(); emptyBuffer(); return x; }
public static int getlnInt() { int x=getInt(); emptyBuffer(); return x; }
public static long getlnLong() { long x=getLong(); emptyBuffer(); return x; }
public static float getlnFloat() { float x=getFloat(); emptyBuffer(); return x; }
public static double getlnDouble() { double x=getDouble(); emptyBuffer(); return x; }
public static char getlnChar() { char x=getChar(); emptyBuffer(); return x; }
public static boolean getlnBoolean() { boolean x=getBoolean(); emptyBuffer(); return x; }
public static String getlnWord() { String x=getWord(); emptyBuffer(); return x; }
public static String getlnString() { return getln(); } // same as getln()
public static String getln() {
StringBuffer s = new StringBuffer(100);
char ch = readChar();
while (ch != '\n') {
s.append(ch);
ch = readChar();
}
return s.toString();
}
public static byte getByte() { return (byte)readInteger(-128L,127L); }
public static short getShort() { return (short)readInteger(-32768L,32767L); }
public static int getInt() { return (int)readInteger((long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE); }
public static long getLong() { return readInteger(Long.MIN_VALUE, Long.MAX_VALUE); }
public static char getAnyChar(){ return readChar(); }
public static char peek() { return lookChar(); }
public static char getChar() { // skip spaces & cr's, then return next char
char ch = lookChar();
while (ch == ' ' || ch == '\n') {
readChar();
if (ch == '\n')
dumpString("? ",0);
ch = lookChar();
}
return readChar();
}
public static float getFloat() {
float x = 0.0F;
while (true) {
String str = readRealString();
if (str.equals("")) {
errorMessage("Illegal floating point input.",
"Real number in the range " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
}
else {
Float f = null;
try { f = Float.valueOf(str); }
catch (NumberFormatException e) {
errorMessage("Illegal floating point input.",
"Real number in the range " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
continue;
}
if (f.isInfinite()) {
errorMessage("Floating point input outside of legal range.",
"Real number in the range " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
continue;
}
x = f.floatValue();
break;
}
}
return x;
}
public static double getDouble() {
double x = 0.0;
while (true) {
String str = readRealString();
if (str.equals("")) {
errorMessage("Illegal floating point input",
"Real number in the range " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
}
else {
Double f = null;
try { f = Double.valueOf(str); }
catch (NumberFormatException e) {
errorMessage("Illegal floating point input",
"Real number in the range " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
continue;
}
if (f.isInfinite()) {
errorMessage("Floating point input outside of legal range.",
"Real number in the range " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
continue;
}
x = f.doubleValue();
break;
}
}
return x;
}
public static String getWord() {
char ch = lookChar();
while (ch == ' ' || ch == '\n') {
readChar();
if (ch == '\n')
dumpString("? ",0);
ch = lookChar();
}
StringBuffer str = new StringBuffer(50);
while (ch != ' ' && ch != '\n') {
str.append(readChar());
ch = lookChar();
}
return str.toString();
}
public static boolean getBoolean() {
boolean ans = false;
while (true) {
String s = getWord();
if ( s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t") ||
s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") ||
s.equals("1") ) {
ans = true;
break;
}
else if ( s.equalsIgnoreCase("false") || s.equalsIgnoreCase("f") ||
s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") ||
s.equals("0") ) {
ans = false;
break;
}
else
errorMessage("Illegal boolean input value.",
"one of: true, false, t, f, yes, no, y, n, 0, or 1");
}
return ans;
}
// ***************** Everything beyond this point is private *******************
// ********************** Utility routines for input/output ********************
private static InputStream in = System.in; // rename standard input stream
private static PrintStream out = System.out; // rename standard output stream
private static String buffer = null; // one line read from input
private static int pos = 0; // position of next char in input line that has
// not yet been processed
private static String readRealString() { // read chars from input following syntax of real numbers
StringBuffer s=new StringBuffer(50);
char ch=lookChar();
while (ch == ' ' || ch == '\n') {
readChar();
if (ch == '\n')
dumpString("? ",0);
ch = lookChar();
}
if (ch == '-' || ch == '+') {
s.append(readChar());
ch = lookChar();
while (ch == ' ') {
readChar();
ch = lookChar();
}
}
while (ch >= '0' && ch <= '9') {
s.append(readChar());
ch = lookChar();
}
if (ch == '.') {
s.append(readChar());
ch = lookChar();
while (ch >= '0' && ch <= '9') {
s.append(readChar());
ch = lookChar();
}
}
if (ch == 'E' || ch == 'e') {
s.append(readChar());
ch = lookChar();
if (ch == '-' || ch == '+') {
s.append(readChar());
ch = lookChar();
}
while (ch >= '0' && ch <= '9') {
s.append(readChar());
ch = lookChar();
}
}
return s.toString();
}
private static long readInteger(long min, long max) { // read long integer, limited to specified range
long x=0;
while (true) {
StringBuffer s=new StringBuffer(34);
char ch=lookChar();
while (ch == ' ' || ch == '\n') {
readChar();
if (ch == '\n')
dumpString("? ",0);
ch = lookChar();
}
if (ch == '-' || ch == '+') {
s.append(readChar());
ch = lookChar();
while (ch == ' ') {
readChar();
ch = lookChar();
}
}
while (ch >= '0' && ch <= '9') {
s.append(readChar());
ch = lookChar();
}
if (s.equals("")){
errorMessage("Illegal integer input.",
"Integer in the range " + min + " to " + max);
}
else {
String str = s.toString();
try {
x = Long.parseLong(str);
}
catch (NumberFormatException e) {
errorMessage("Illegal integer input.",
"Integer in the range " + min + " to " + max);
continue;
}
if (x < min || x > max) {
errorMessage("Integer input outside of legal range.",
"Integer in the range " + min + " to " + max);
continue;
}
break;
}
}
return x;
}
private static String realToString(double x) {
// Goal is to get a reasonable representation of x in at most
// 10 characters, or 11 characters if x is negative.
if (Double.isNaN(x))
return "undefined";
if (Double.isInfinite(x))
if (x < 0)
return "-INF";
else
return "INF";
if (Math.abs(x) <= 5000000000.0 && Math.rint(x) == x)
return String.valueOf( (long)x );
String s = String.valueOf(x);
if (s.length() <= 10)
return s;
boolean neg = false;
if (x < 0) {
neg = true;
x = -x;
s = String.valueOf(x);
}
if (x >= 0.00005 && x <= 50000000 && (s.indexOf('E') == -1 && s.indexOf('e') == -1)) { // trim x to 10 chars max
s = round(s,10);
s = trimZeros(s);
}
else if (x > 1) { // construct exponential form with positive exponent
long power = (long)Math.floor(Math.log(x)/Math.log(10));
String exp = "E" + power;
int numlength = 10 - exp.length();
x = x / Math.pow(10,power);
s = String.valueOf(x);
s = round(s,numlength);
s = trimZeros(s);
s += exp;
}
else { // constuct exponential form
long power = (long)Math.ceil(-Math.log(x)/Math.log(10));
String exp = "E-" + power;
int numlength = 10 - exp.length();
x = x * Math.pow(10,power);
s = String.valueOf(x);
s = round(s,numlength);
s = trimZeros(s);
s += exp;
}
if (neg)
return "-" + s;
else
return s;
}
private static String trimZeros(String num) { // used by realToString
if (num.indexOf('.') >= 0 && num.charAt(num.length() - 1) == '0') {
int i = num.length() - 1;
while (num.charAt(i) == '0')
i--;
if (num.charAt(i) == '.')
num = num.substring(0,i);
else
num = num.substring(0,i+1);
}
return num;
}
private static String round(String num, int length) { // used by realToString
if (num.indexOf('.') < 0)
return num;
if (num.length() <= length)
return num;
if (num.charAt(length) >= '5' && num.charAt(length) != '.') {
char[] temp = new char[length+1];
int ct = length;
boolean rounding = true;
for (int i = length-1; i >= 0; i--) {
temp[ct] = num.charAt(i);
if (rounding && temp[ct] != '.') {
if (temp[ct] < '9') {
temp[ct]++;
rounding = false;
}
else
temp[ct] = '0';
}
ct--;
}
if (rounding) {
temp[ct] = '1';
ct--;
}
// ct is -1 or 0
return new String(temp,ct+1,length-ct);
}
else
return num.substring(0,length);
}
private static void dumpString(String str, int w) { // output string to console
for (int i=str.length(); i<w; i++)
out.print(' ');
for (int i=0; i<str.length(); i++)
if ((int)str.charAt(i) >= 0x20 && (int)str.charAt(i) != 0x7F) // no control chars or delete
out.print(str.charAt(i));
else if (str.charAt(i) == '\n' || str.charAt(i) == '\r')
newLine();
}
private static void errorMessage(String message, String expecting) {
// inform user of error and force user to re-enter.
newLine();
dumpString(" *** Error in input: " + message + "\n", 0);
dumpString(" *** Expecting: " + expecting + "\n", 0);
dumpString(" *** Discarding Input: ", 0);
if (lookChar() == '\n')
dumpString("(end-of-line)\n\n",0);
else {
while (lookChar() != '\n')
out.print(readChar());
dumpString("\n\n",0);
}
dumpString("Please re-enter: ", 0);
readChar(); // discard the end-of-line character
}
private static char lookChar() { // return next character from input
if (buffer == null || pos > buffer.length())
fillBuffer();
if (pos == buffer.length())
return '\n';
return buffer.charAt(pos);
}
private static char readChar() { // return and discard next character from input
char ch = lookChar();
pos++;
return ch;
}
private static void newLine() { // output a CR to console
out.println();
out.flush();
}
private static boolean possibleLinefeedPending = false;
private static void fillBuffer() { // Wait for user to type a line and press return,
// and put the typed line into the buffer.
StringBuffer b = new StringBuffer();
out.flush();
try {
int ch = in.read();
if (ch == '\n' && possibleLinefeedPending)
ch = in.read();
possibleLinefeedPending = false;
while (ch != -1 && ch != '\n' && ch != '\r') {
b.append((char)ch);
ch = in.read();
}
possibleLinefeedPending = (ch == '\r');
if (ch == -1) {
System.out.println("\n*** Found an end-of-file while trying to read from standard input!");
System.out.println("*** Maybe your Java system doesn't implement standard input?");
System.out.println("*** Program will be terminated.\n");
throw new RuntimeException("End-of-file on standard input.");
}
}
catch (IOException e) {
System.out.println("Unexpected system error on input.");
System.out.println("Terminating program.");
System.exit(1);
}
buffer = b.toString();
pos = 0;
}
private static void emptyBuffer() { // discard the rest of the current line of input
buffer = null;
}
} // end of class TextIO

261
test/Aout.java Normal file
View File

@ -0,0 +1,261 @@
/** Helper methods.
* Arrays converted to array expressions (toString(...))
* and prettyprinting (toPrettyString(...)).
* @author Jaanus
*/
public class Aout {
/** Just testing. */
public static void main (String[] args) {
System.out.println (toString (new String[]
{null, "", "0", "a\nb", " ", " ", "\t", "möäüÕga", "\"", "\\", "\'"}));
} // main
/** Conversion for int[][] . */
public static String toString (int[][] m) {
if (m == null) return "(int[][])null";
StringBuffer sb = new StringBuffer();
sb.append ("new int[][]{");
for (int i=0; i < m.length; i++) {
if (i > 0) sb.append (", ");
if (m[i] == null)
sb.append ("null");
else {
sb.append ("{");
for (int j=0; j < m[i].length; j++) {
if (j > 0) sb.append (", ");
sb.append (String.valueOf (m[i][j]));
} // for j
sb.append ("}");
}
} // for i
sb.append ("}");
return sb.toString();
} // toString int[][]
/** Conversion for double[][] . */
public static String toString (double[][] m) {
if (m == null) return "(double[][])null";
StringBuffer sb = new StringBuffer();
sb.append ("new double[][]{");
for (int i=0; i < m.length; i++) {
if (i > 0) sb.append (", ");
if (m[i] == null)
sb.append ("null");
else {
sb.append ("{");
for (int j=0; j < m[i].length; j++) {
if (j > 0) sb.append (", ");
sb.append (String.valueOf (m[i][j]));
} // for j
sb.append ("}");
}
} // for i
sb.append ("}");
return sb.toString();
} // toString double[][]
/** Conversion for int[] . */
public static String toString (int[] m) {
if (m == null) return "(int[])null";
StringBuffer sb = new StringBuffer();
sb.append ("new int[]{");
for (int i=0; i < m.length; i++) {
if (i > 0) sb.append (", ");
sb.append (String.valueOf (m[i]));
} // for i
sb.append ("}");
return sb.toString();
} // toString int[]
/** Conversion for double[] . */
public static String toString (double[] m) {
if (m == null) return "(double[])null";
StringBuffer sb = new StringBuffer();
sb.append ("new double[]{");
for (int i=0; i < m.length; i++) {
if (i > 0) sb.append (", ");
sb.append (String.valueOf (m[i]));
} // for i
sb.append ("}");
return sb.toString();
} // toString double[]
/** Conversion for int . */
public static String toString (int n) {
return String.valueOf (n);
} // toString int
/** Conversion for double . */
public static String toString (double d) {
return String.valueOf (d);
} // toString double
/** Conversion for String . */
public static String toString (String s) {
if (s == null)
return "null";
StringBuffer tmp = new StringBuffer();
for (int k=0; k < s.length(); k++) {
char c = s.charAt (k);
switch (c) {
case '\n': { tmp.append ("\\n"); break; }
case '\t': { tmp.append ("\\t"); break; }
case '\b': { tmp.append ("\\b"); break; }
case '\f': { tmp.append ("\\f"); break; }
case '\r': { tmp.append ("\\r"); break; }
case '\\': { tmp.append ("\\\\"); break; }
case '\'': { tmp.append ("\\\'"); break; }
case '\"': { tmp.append ("\\\""); break; }
// TODO!!! add more escapes if needed
default: tmp.append (c);
} // switch
} // for k
return "\"" + tmp.toString() + "\"";
} // toString String
/** Conversion for String[] . */
public static String toString (String[] m) {
if (m == null)
return "(String[])null";
StringBuffer sb = new StringBuffer();
sb.append ("new String[]{");
for (int i=0; i < m.length; i++) {
if (i > 0)
sb.append (", ");
sb.append (toString (m[i]));
} // for i
sb.append ("}");
return sb.toString();
} // toString String[]
/** Double number as string with the given length.
* @param d argument
* @param len length
* @return d as string
*/
public static String fString (double d, int len) {
if (len<1)
return "";
// pad on ruum punkti ja v6imaliku miinusm2rgi jaoks
int pad = 1 + ((d<0)?1:0);
// loga on t2isosa numbrikohtade arv
int loga = (int)Math.max (0., Math.log10 (Math.abs (d))) + 1;
// kk on punkti j2rel olevate kohtade arv
int kk = (int)Math.max (len-pad-loga, 0);
String fs = "%" + String.valueOf (len) + "." +
String.valueOf (kk) + "f";
String res = "";
try {
res = String.format ((java.util.Locale)null, fs, d);
} catch (IllegalArgumentException e) {
res = String.valueOf (d);
} // try
return res;
} // fString
/** Prettyprint for double[][] .
* @param m array to print
* @param fs format string for element
* @return m array as multiline string
*/
public static String toPrettyString (double[][] m, String fs) {
String nl = System.getProperty ("line.separator");
if (m == null)
return "nullpointer instead of this matrix" + nl;
// throw new NullPointerException ("(double[][])null"); // alternative
if (m.length == 0)
return "this matrix is empty" + nl;
StringBuffer sb = new StringBuffer(nl);
for (int i=0; i < m.length; i++) {
if (m[i] == null)
sb.append ("nullpointer instead of this row" + nl);
else {
if (m[i].length == 0)
sb.append ("this row is empty");
else {
for (int j=0; j < m[i].length; j++) {
String elem = "";
if (fs == null || fs.length() < 1) {
// TODO!!! keera siit, kui tahad pilti muuta
elem = fString (m[i][j], 6) + "\t";
} else {
try {
elem = String.format ((java.util.Locale)null,
fs, m[i][j]) + " "; // remove space if needed
} catch (IllegalArgumentException e) {
elem = fString (m[i][j], 6) + "\t";
} // try
}
sb.append (elem);
} // for j
} // nonempty row
sb.append (nl);
} // non-null row
} // for i
return sb.toString();
} // toPrettyString double[][]
/** Version of double[][] prettyprint without format string. */
public static String toPrettyString (double[][] m) {
return toPrettyString (m, null);
} // toPrettyString double[][]
/** Prettyprint for int[][] .
* @param m array to print
* @param fs format string for element
* @return m array as a multiline string
*/
public static String toPrettyString (int[][] m, String fs) {
String nl = System.getProperty ("line.separator");
if (m == null)
return "nullpointer instead of this matrix" + nl;
// throw new NullPointerException ("(double[][])null"); // alternative
if (m.length == 0)
return "this matrix is empty" + nl;
StringBuffer sb = new StringBuffer(nl);
for (int i=0; i < m.length; i++) {
if (m[i] == null)
sb.append ("nullpointer instead of this row" + nl);
else {
if (m[i].length == 0)
sb.append ("this row is empty");
else {
for (int j=0; j < m[i].length; j++) {
String elem = "";
if (fs == null || fs.length() < 1)
fs = "%5d"; // TODO!!! keera siit, kui vaja
try {
elem = String.format ((java.util.Locale)null,
fs, m[i][j]) + " "; // remove space if needed
} catch (IllegalArgumentException e) {
elem = String.valueOf (m[i][j]) + "\t";
} // try
sb.append (elem);
} // for j
} // nonempty row
sb.append (nl);
} // non-null row
} // for i
return sb.toString();
} // toPrettyString int[][]
/** Version of int[][] prettyprint without format string. */
public static String toPrettyString (int[][] m) {
return toPrettyString (m, null);
} // toPrettyString int[][]
/** Prettyprint for String[] . */
public static String toPrettyString (String[] m) {
String nl = System.getProperty ("line.separator");
if (m == null) return "(String[])null";
StringBuffer sb = new StringBuffer();
for (int i=0; i < m.length; i++) {
sb.append (toString (m[i]) + nl);
} // for i
return sb.toString();
} // toPrettyString String[]
} // Aout

17
test/GraphTaskTest.java Normal file
View File

@ -0,0 +1,17 @@
import static org.junit.Assert.*;
import org.junit.Test;
import java.util.*;
/** Testklass.
* @author jaanus
*/
public class GraphTaskTest {
@Test (timeout=20000)
public void test1() {
GraphTask.main (null);
assertTrue ("There are no tests", true);
}
}

BIN
test/hamcrest-core-1.3.jar Normal file

Binary file not shown.

BIN
test/junit-4.12.jar Normal file

Binary file not shown.