Lausnum skal skilað til viðkomandi dæmakennara (sjá lista yfir dæmatíma).
Reynið að skrifa stutt, skýr og hnitmiðuð forrit. Notið réttan inndrátt og gætið þess að forritin ykkar séu læsileg.
Skiladagur: þriðjudaginn 7. febrúar fyrir kl 16:00
Takið upphaflega RPN forritið sem var sýnt í fyrirlestri og breytið því á eftirfarandi hátt.
Aðgerðir eiga að vera strengir ("+", "*", "p") en ekki bókstafir ('+', '*', 'p'). Það á að vera hægt að bæta inn aðgerð sem inniheldur fleiri en einn bókstaf, t.d. "drop". Gerið tilheyrandi breytingar á struct operation, operations og eval_op.
Bætið við nýrri skipun "drop" sem fleygir burt efsta stakinu af hlaðanum.
Bætið við nýrri skipun "dup" sem tekur efsta stakið á hlaðanum og setur það tvisvar á hlaðann (duplicate top value).
Bætið við nýrri skipun "swap" sem víxlar tveimur efstu stökunum á hlaðanum.
Leysið dæmi 1.3.4 í bókinni í Java. Notið Stack klasann úr fyrirlestri.
Write a stack client Parentheses in Java that reads in a text stream from standard input and uses a stack to determine whether its parentheses are properly balanced. For example, your program should print true for [()]{}{[()()]()} and false for [(]).
[()]{}{[()()]()}
[(])
true false
Takið upphaflega RPN forritið (rpn.zip) sem var sýnt í fyrirlestri og forritið hliðstæða útgáfu í Java. Forritið á að hegða sér nákvæmlega eins og C forritið. Notandi forritsins ætti ekki að geta sagt til um hvort forritið hann er að keyra.
2 token: 2 3 token: 3 4 token: 4 5 token: 5 p token: p Stack (from top to bottom): [5] [4] [3] [2] + token: + * token: * + token: + p token: p Stack (from top to bottom): [29]
public class Stack<T>
{
class Node
{
T value;
Node next;
};
private Node top;
private int n;
Stack()
{
top = null;
n = 0;
}
public void push(T value)
{
Node x = new Node();
x.value = value;
x.next = top;
top = x;
n++;
}
public T pop()
{
T x = top.value;
top = top.next;
n--;
return x;
}
public T peek()
{
return top.value;
}
public boolean empty()
{
return top == null;
}
public int count()
{
return n;
}
public static void main(String[] args)
{
Stack<String> s = new Stack<String>();
s.push("A");
s.push("B");
s.push("C");
while (!s.empty())
System.out.println(s.pop());
}
}
Klárið útfærslu af walktree fallinu í beinagrindinni. Fallið tekur inn skrá/möppu (File object) og FileVisitor hlut. Fallið gengur yfir allar skrár og möppur sem liggja undir slóðinni og fyrir hverja skrá/möppu þá er kallað á visit í FileVisitor.
Forritið á að nota hlaða, sem fylgir með beinagrindinni. Forritið tekur næstu skrá af hlaðanum, kallar á FileVisitor og síðan kallar það á listFiles á viðkomandi skrá, sem sækir allar undirskrár/undirmöppur, og setur allar undirskrár/undirmöppur á hlaðann.
import java.io.File;
interface FileVisitor
{
void visit(File file);
}
public class Walker
{
static class PrintFile implements FileVisitor
{
public void visit(File file)
{
if (file.isDirectory())
System.out.printf("D %s\n", file.getAbsolutePath());
else
System.out.printf("F %s (%d bytes)\n", file.getAbsolutePath(), file.length());
}
}
public static void walktree(File root, FileVisitor visitor)
{
Stack<File> s = new Stack<File>();
s.push(root);
while (!s.empty())
{
// ...
}
}
public static void main(String[] args)
{
if (args.length < 1)
{
System.err.println("usage: java Walker <path>");
return;
}
File root = new File(args[0]);
walktree(root, new PrintFile());
}
}
public class Stack<T>
{
class Node
{
T value;
Node next;
};
private Node top;
private int n;
Stack()
{
top = null;
n = 0;
}
public void push(T value)
{
Node x = new Node();
x.value = value;
x.next = top;
top = x;
n++;
}
public T pop()
{
T x = top.value;
top = top.next;
n--;
return x;
}
public T peek()
{
return top.value;
}
public boolean empty()
{
return top == null;
}
public int count()
{
return n;
}
public static void main(String[] args)
{
Stack<String> s = new Stack<String>();
s.push("A");
s.push("B");
s.push("C");
while (!s.empty())
System.out.println(s.pop());
}
}