From 29b119dc63a283aa7187cc0b0c6f565c7efde24b Mon Sep 17 00:00:00 2001 From: laoheihei Date: Wed, 5 Apr 2017 15:59:51 +0800 Subject: [PATCH 1/2] change readBinaryCode: add {} in if --- group22/2622819383/Task4/jvm/loader/ClassFileLoader.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/group22/2622819383/Task4/jvm/loader/ClassFileLoader.java b/group22/2622819383/Task4/jvm/loader/ClassFileLoader.java index 3c8f84ebfb..e366d710e1 100644 --- a/group22/2622819383/Task4/jvm/loader/ClassFileLoader.java +++ b/group22/2622819383/Task4/jvm/loader/ClassFileLoader.java @@ -16,9 +16,10 @@ public class ClassFileLoader { public byte[] readBinaryCode(String className) { for (String path : clzPaths) { File f = new File(path + className.replace('.', '/') + ".class"); - if (f.exists() && f.isFile()) + if (f.exists() && f.isFile()) { System.out.println("f.length(): " + f.length()); - return readBytes(f); + return readBytes(f); + } } return null; } From 7e0dd93bcc094d356f753908710df5aef94ca870 Mon Sep 17 00:00:00 2001 From: laoheihei Date: Sat, 6 May 2017 23:58:23 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A1=A5=E4=BA=A4jvm=E5=89=8D5=E6=AC=A1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84=E4=BD=9C=E4=B8=9A=EF=BC=9A?= =?UTF-8?q?Task4-Task8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- group22/2622819383/Task5/MyArrayList.java | 164 +++++++++++++++++ group22/2622819383/Task5/Stack.java | 28 +++ group22/2622819383/Task5/StackUtil.java | 95 ++++++++++ group22/2622819383/Task6/InfixExpr.java | 168 ++++++++++++++++++ group22/2622819383/Task7/InfixToPostfix.java | 152 ++++++++++++++++ group22/2622819383/Task7/MyArrayList.java | 164 +++++++++++++++++ group22/2622819383/Task7/PostfixExpr.java | 132 ++++++++++++++ group22/2622819383/Task7/PrefixExpr.java | 52 ++++++ group22/2622819383/Task7/Stack.java | 28 +++ group22/2622819383/Task8/CircleQueue.java | 41 +++++ group22/2622819383/Task8/Josephus.java | 34 ++++ .../2622819383/Task8/QueueWithTwoStacks.java | 51 ++++++ 12 files changed, 1109 insertions(+) create mode 100644 group22/2622819383/Task5/MyArrayList.java create mode 100644 group22/2622819383/Task5/Stack.java create mode 100644 group22/2622819383/Task5/StackUtil.java create mode 100644 group22/2622819383/Task6/InfixExpr.java create mode 100644 group22/2622819383/Task7/InfixToPostfix.java create mode 100644 group22/2622819383/Task7/MyArrayList.java create mode 100644 group22/2622819383/Task7/PostfixExpr.java create mode 100644 group22/2622819383/Task7/PrefixExpr.java create mode 100644 group22/2622819383/Task7/Stack.java create mode 100644 group22/2622819383/Task8/CircleQueue.java create mode 100644 group22/2622819383/Task8/Josephus.java create mode 100644 group22/2622819383/Task8/QueueWithTwoStacks.java diff --git a/group22/2622819383/Task5/MyArrayList.java b/group22/2622819383/Task5/MyArrayList.java new file mode 100644 index 0000000000..265fa629b6 --- /dev/null +++ b/group22/2622819383/Task5/MyArrayList.java @@ -0,0 +1,164 @@ +import java.util.Iterator; +public class MyArrayList implements Iterable { + private T[] elems; + private int size; + private int capacity; + private static final int DEFAULT_CAPACITY = 10; + //modCount��¼����MyArrayList���󴴽�����Ԫ�ؽṹ�ı����ÿ��add��remove��������modCount�� + //Ŀ�����ڰ�����������⼯���еı仯��ʹ�ڵ����������ⲿ�Ķ�Ԫ�ؽṹ�仯��ͬ���� + private int modCount; + + public MyArrayList() { + clear(); + } + + public void clear() { + size = 0; + elems = (T[])new Object[capacity = DEFAULT_CAPACITY]; + modCount++; + } + + public int size() { return size; } + + public void expand() { + if (size < capacity) return; + T[] old = elems; + elems = (T[])new Object[capacity *= 2]; + for (int i = 0; i < size; i++) + elems[i] = old[i]; + } + public boolean find(T e) { + + int times = size - 1; + while (times != -1) { + if (e.equals(elems[times])) + return true; + --times; + } + return false; + + + } + //capacity >= 4*size ��shrink()��capacity���� + public void shrink() { + if (capacity < DEFAULT_CAPACITY * 2) return; + if (capacity < size * 4) return; + T[] old = elems; + elems = (T[])new Object[capacity /= 2]; + for (int i = 0; i < size; i++) + elems[i] = old[i]; + } + + + public boolean add(T e) { + add(size, e); + return true; + } + + public void add(int index, T e) { + if (index < 0 || size < index) throw new ArrayIndexOutOfBoundsException(); + expand(); + for (int i = size; i > index; i--) + elems[i] = elems[i - 1]; + elems[index] = e; + size++; + modCount++; + } + + public T get(int index) { + if (index < 0 || size <= index) throw new ArrayIndexOutOfBoundsException(); + return elems[index]; + } + + public boolean isEmpty() { + return size == 0; + } + + public T remove(int index) { + if (index < 0 || size <= index) throw new ArrayIndexOutOfBoundsException(); + T removed = elems[index]; + for (int i = index; i < size - 1; i++) + elems[i] = elems[i + 1]; + + size--; + shrink(); + modCount++; + return removed; + } + + public T set(int index, T e) { + if (index < 0 || size <= index) throw new ArrayIndexOutOfBoundsException(); + T old = elems[index]; + elems[index] = e; + return old; + } + + //ArrayListIterator����ת��Ϊjava.util.Iterator + public java.util.Iterator iterator() { + return new ArrayListIterator(); + } + + //���⣺Ϊʲôjava.util.IteratorҪ����ArrayListIterator���ü�? + /*�ش���Ϊjava.util.Iterator���ⲿ�ӿڣ����Ѿ�����õĶ����������ڷ�����MyArrayList�����ӵġ� + ��ʹ���Ѿ�����õķ��ͽӿڻ���ʱ�� + ���߲�Ϊ���ͽӿڴ���ʵ�Σ���ֱ��ʹ��java.util.Iterator������java.util.Iterator�в�����Ԫ������ȫ��Object�� + ����һ��ҪΪ���ͽӿڴ���ʵ�Σ��ڱ�����ʵ��Ϊ������MyArrayList��T�� + ����java.util.Iterator�в�����Ԫ������ȫ��T����֤�˴���MyArrayList��ͨ��Iterator���в�����Ԫ�ص����͡� + + �����Ƕ����ArrayListIterator��������ArrayListIterator��ԭ����ArrayListIterator�����ڷ�����MyArrayList�У� + �������еķ�������Ա�������ڲ���(ArrayListIterator)��������ʹ�÷��Ͳ���T������������ + + */ + + private class ArrayListIterator implements java.util.Iterator { + private int current = 0; + private int expectedModCount = modCount; + private boolean okToRemove = false; + + public boolean hasNext() { return current < size; } + public T next() { + if (modCount != expectedModCount) + throw new java.util.ConcurrentModificationException(); + if (!hasNext()) throw new java.util.NoSuchElementException(); + + okToRemove = true; + return elems[current++]; + } + public void remove() { + if (modCount != expectedModCount) + throw new java.util.ConcurrentModificationException(); + if (!okToRemove) + throw new IllegalStateException(); + + MyArrayList.this.remove(--current); + expectedModCount++; + okToRemove = false; + } + } + + //boolean remove(Object o) +} + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group22/2622819383/Task5/Stack.java b/group22/2622819383/Task5/Stack.java new file mode 100644 index 0000000000..4d2db455c2 --- /dev/null +++ b/group22/2622819383/Task5/Stack.java @@ -0,0 +1,28 @@ +public class Stack { + private MyArrayList elementData = new MyArrayList<>(); + + public void push(T o){ + elementData.add(o); + } + + public T pop(){ + return elementData.remove(size() - 1); + } + + public T peek(){ + if (size() == 0) return null; + return elementData.get(size() - 1); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } + + public boolean find(T e) { + return elementData.find(e); + } +} diff --git a/group22/2622819383/Task5/StackUtil.java b/group22/2622819383/Task5/StackUtil.java new file mode 100644 index 0000000000..1a70ca0d7a --- /dev/null +++ b/group22/2622819383/Task5/StackUtil.java @@ -0,0 +1,95 @@ +public class StackUtil { + public static void reverse(Stack s) { + if (s == null || s.isEmpty()) return; + + Integer top = s.pop(); + reverse(s); + addToBottom(s, top); + } + + private static void addToBottom(Stack s, int num) { + if (s.size() == 0) + s.push(num); + + else { + int top = s.pop(); + addToBottom(s, num); + s.push(top); + } + } + + public static void remove(Stack s, Object o) { + if (s.isEmpty()) return; + + Stack temp = new Stack(); + + while (!s.isEmpty()) { + Object val = s.pop(); + if (!value.equals(o)) + temp.push(value); + } + + while (!temp.isEmpty()) + s.push(temp.pop()); + + } + + public static Object[] getTop(Stack s, int len) { + if (s == null || s.isEmpty() || s.size() < len || len <= 0) + return null; + + Stack temp = new Stack(); + int idx = 0; + Object ret = new Object[len]; + while (!s.isEmpty()) { + Object val = s.pop(); + temp.push(value); + result[i++] = val; + if (i == len) + break; + } + while (temp.isEmpty()) + s.push(temp.pop()); + + return ret; + } + + public static boolean isValidPairs(String s) { + char[] arrS = s.toCharArray(); + Stack stack = new Stack<>(); + int idx = 0; + while (idx++ < arrS.length) + switch (arrS[idx]) { + case '(': + case '[': + case '{': + stack.push(arrS[idx]); break; + case ')': + if (stack.isEmpty() || stack.pop() != '(') + return false; + break; + case ']': + if (stack.isEmpty() || stack.pop() != '[') + return false; + break; + case '}': + if (stack.isEmpty() || stack.pop() != '{') + return false; + break; + + default: + break; + } + + return stack.isEmpty(); + } + + public static void main(String[] args) { + Stack s = new Stack<>(); + for (int i = 0; i < 6; i++) + s.push(i); + reverse(s); + while (!s.isEmpty()) + System.out.println(s.pop()); + } +} diff --git a/group22/2622819383/Task6/InfixExpr.java b/group22/2622819383/Task6/InfixExpr.java new file mode 100644 index 0000000000..50c993d593 --- /dev/null +++ b/group22/2622819383/Task6/InfixExpr.java @@ -0,0 +1,168 @@ +//��׺����ʽ��ֵ +//����ο��ԡ����ݽṹ�� +public class InfixExpr { + + //֧�ֵİ�����ѧ�������'\0'��ʾ��ѧ����ʽ���� + static char[] operation = {'+', '-', '*', '/', '^', '!', '(', ')', '\0'}; + + /* + * pri[][]: + * ջ��������뵱ǰ����������ȼ��Ƚ�. ' '��ʾ�﷨����; + * '>'��ʾջ����������ȼ����ڵ�ǰ����ѧ����ʽ�ж�ȡ���������˵�����Խ���ջ�����������������. + * e.g. "1+2-3" + * ջ�������Ϊ'+'����ǰ�����Ϊ'-'�������֪�����ȼ��ȽϽ��Ϊ'>'�����Զ�'1+2'���м��� + */ + static char[][] pri = { + /* |------------��ǰ�����-----------------| */ + /* + - * / ^ ! ( ) \0 */ + /* -- + */ {'>', '>', '<', '<', '<', '<', '<', '>', '>'}, + /* | - */ {'>', '>', '<', '<', '<', '<', '<', '>', '>'}, + /* ջ * */ {'>', '>', '>', '>', '<', '<', '<', '>', '>'}, + /* �� / */ {'>', '>', '>', '>', '<', '<', '<', '>', '>'}, + /* �� ^ */ {'>', '>', '>', '>', '>', '<', '<', '>', '>'}, + /* �� ! */ {'>', '>', '>', '>', '>', '>', ' ', '>', '>'}, + /* �� ( */ {'<', '<', '<', '<', '<', '<', '<', '=', ' '}, + /* | ) */ {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, + /* -- \0 */ {'<', '<', '<', '<', '<', '<', '<', ' ', '='} + }; + + //�Ƚ�ջ��������뵱ǰ����������ȼ�. + public static char orderBetween(char stackTop, char current) { + //System.out.println("top, cur: " + stackTop + ", " + current); + int top = findOptr(stackTop); + int cur = findOptr(current); + return pri[top][cur]; + } + + //�ҵ�����������c��operation[]�е�����λ��. + private static int findOptr(char c) { + int i = 0; + while (i < operation.length && c != operation[i]) + ++i; + return i; + } + + //��s[]����s[idx]��ʼ�����ֲ�����ջopnd�У����idx. + //����idxʱs[idx]Ϊ���֣����idxʱs[idx]Ϊ������. + public static int readNum(char[] s, int idx, Stack opnd) { + float current = 0; + float value = 1; //С�����ָ�λȨֵ��10^(-n) + boolean isDecimal = false; + opnd.push((float)(s[idx++] - '0')); + + while (idx < s.length) { + if (s[idx] == '.') { + isDecimal = true; + ++idx; + } + else if (isDigit(s[idx])) { + current = s[idx] - '0'; + if (!isDecimal) { + opnd.push(opnd.pop() * 10 + current); + ++idx; + } + else { + value /= 10; + opnd.push(opnd.pop() + current * value); + ++idx; + } + } + else + break; + } + //System.out.println(opnd.peek()); + return idx; + } + + public static boolean isDigit(char s) { + return '0' <= s && s <= '9'; + } + + //�Ա���ʽstr��ֵ + public static float evaluate(String str) throws Exception { + str += '\0'; //��ǰ��������ʽ�����ı�־ + char[] s = str.toCharArray(); + Stack opnd = new Stack<>(); //opnd: �洢������ + Stack optr = new Stack<>(); //optr: �洢����� + optr.push('\0'); //�ڱ��ڵ� + int i = 0; //s������ + while (!optr.isEmpty()) { + if (isDigit(s[i])) { + //readNum(char[], int, Stack)����ֵΪchar[]��int���һ���������ַ�������. + i = readNum(s, i, opnd); + } + else + //�Ƚϵ�ǰ�������ջ�������. + switch (orderBetween(optr.peek(), s[i])) { + //��ջ����������ȼ�С�ڵ�ǰ���������ǰ��������ջ����ȡ��һ���ַ�. + case '<': + optr.push(s[i++]); + break; + //��ǰ�����Ϊ')'��β���ڱ�'\0'������ջ�����������ȡ��һ���ַ�����ʱջ���������ӦΪ'('��'\0'. + case '=': + optr.pop(); ++i; + break; + //'>': ջ����������Խ�������. + //NOTICE�� case '>'ʱû��++i����Ϊs[i]��һֱ��ջ������������������ȼ��Ƚ�. + //���ջ������� '>' s[i]��˵�����Խ������㣺����ջ�����������Ҫ�IJ������������㣬Ȼ���ٽ�s[i]��ջ��������Ƚ�... + //ֱ��ջ������� '<=' s[i]ʱ���Ż��ǵ�ǰ�����s[i]��ջoptr���ж���һ���ַ�(++i). + case '>': + char op = optr.pop(); + if ('!' == op) { + float pOpnd = opnd.pop(); + opnd.push(calcu(op, pOpnd)); + } else { + float pOpnd2 = opnd.pop(), pOpnd1 = opnd.pop(); + opnd.push(calcu(pOpnd1, op, pOpnd2)); + } + break; + default: + + } + } + return opnd.pop(); + } + + //��ֻ��Ҫһ���������������'!'�������㲢���ؽ�� + public static float calcu(char c, float opnd) throws Exception { + + float ret = opnd; + float temp = opnd; + + if (c != '!') { + System.out.println("�ڴ��������'!'����ǰ�����Ϊ��" + c); + throw new Exception("���������"); + } + + if (temp == 0) + ret = 1; + + while (--temp > 1) { + ret *= temp; + } + System.out.println("" + opnd + c + " = " + ret); + return ret; + } + + //����Ҫ������������������������㲢���ؽ�� + public static float calcu(float opnd1, char optr, float opnd2) throws Exception { + float ret; + switch(optr) { + case '+': ret = opnd1 + opnd2; break; + case '-': ret = opnd1 - opnd2; break; + case '*': ret = opnd1 * opnd2; break; + case '/': ret = opnd1 / opnd2; break; + case '^': ret = (float)Math.pow(opnd1, opnd2); break; + default: + System.out.println("����δ֪�������" + optr); + throw new Exception("��֧�ֵ������"); + } + System.out.println("" + opnd1 + optr + opnd2 + " = " + ret); + return ret; + } + + public static void main(String[] args) throws Exception { + String s = "(0!+1)*2^(3!+4)-(5!-67-(8+9))"; + System.out.println(evaluate(s)); + } +} diff --git a/group22/2622819383/Task7/InfixToPostfix.java b/group22/2622819383/Task7/InfixToPostfix.java new file mode 100644 index 0000000000..a9eac1f0e7 --- /dev/null +++ b/group22/2622819383/Task7/InfixToPostfix.java @@ -0,0 +1,152 @@ +public class InfixToPostfix { + public static String convert2Postfix(String infixStr) throws Exception { + infixStr += '\0'; + char[] s = infixStr.toCharArray(); + Stack opnd = new Stack<>(); + Stack optr = new Stack<>(); + StringBuilder ret = new StringBuilder(); + optr.push('\0'); + int idx = 0; + + while (!optr.isEmpty()) { + if (isDigit(s[idx])) { + idx = readNum(s, idx, opnd); + append(ret, opnd.peek()); + } + + else { + switch (orderBetween(optr.peek(), s[idx])) { + case '<': + optr.push(s[idx++]); + break; + case '=': + optr.pop(); idx++; + break; + case '>': + ret.append(optr.pop()).append(' '); + break; + default: + System.out.println("���ִ����������" + s[idx]); + System.out.println("���������������" + idx); + throw new Exception("�﷨����"); + } + } + } + + return new String(ret); + } + + private static void append(StringBuilder s, float opnd) { + if ((float)(int)opnd != opnd) + s.append(opnd).append(' '); + else + s.append((int)opnd).append(' '); + } + + //֧�ֵİ�����ѧ�������'\0'��ʾ��ѧ����ʽ���� + static char[] operation = {'+', '-', '*', '/', '^', '!', '(', ')', '\0'}; + + /* + * pri[][]: + * ջ��������뵱ǰ����������ȼ��Ƚ�. ' '��ʾ�﷨����; + * '>'��ʾջ����������ȼ����ڵ�ǰ����ѧ����ʽ�ж�ȡ���������˵�����Խ���ջ�����������������. + * e.g. "1+2-3" + * ջ�������Ϊ'+'����ǰ�����Ϊ'-'�������֪�����ȼ��ȽϽ��Ϊ'>'�����Զ�'1+2'���м��� + */ + static char[][] pri = { + /* |------------��ǰ�����-----------------| */ + /* + - * / ^ ! ( ) \0 */ + /* -- + */ {'>', '>', '<', '<', '<', '<', '<', '>', '>'}, + /* | - */ {'>', '>', '<', '<', '<', '<', '<', '>', '>'}, + /* ջ * */ {'>', '>', '>', '>', '<', '<', '<', '>', '>'}, + /* �� / */ {'>', '>', '>', '>', '<', '<', '<', '>', '>'}, + /* �� ^ */ {'>', '>', '>', '>', '>', '<', '<', '>', '>'}, + /* �� ! */ {'>', '>', '>', '>', '>', '>', ' ', '>', '>'}, + /* �� ( */ {'<', '<', '<', '<', '<', '<', '<', '=', ' '}, + /* | ) */ {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}, + /* -- \0 */ {'<', '<', '<', '<', '<', '<', '<', ' ', '='} + }; + + //�Ƚ�ջ��������뵱ǰ����������ȼ�. + public static char orderBetween(char stackTop, char current) { + //System.out.println("top, cur: " + stackTop + ", " + current); + int top = findOptr(stackTop); + int cur = findOptr(current); + return pri[top][cur]; + } + + //�ҵ�����������c��operation[]�е�����λ��. + private static int findOptr(char c) { + int i = 0; + while (i < operation.length && c != operation[i]) + ++i; + return i; + } + + //��s[]����s[idx]��ʼ�����ֲ�����ջopnd�У����idx. + //����idxʱs[idx]Ϊ���֣����idxʱs[idx]Ϊ������. + private static int readNum(char[] s, int idx, Stack opnd) { + float current = 0; + float value = 1; //С�����ָ�λȨֵ��10^(-n) + boolean isDecimal = false; + opnd.push((float)(s[idx++] - '0')); + + while (idx < s.length) { + if (s[idx] == '.') { + isDecimal = true; + ++idx; + } + else if (isDigit(s[idx])) { + current = s[idx] - '0'; + if (!isDecimal) { + opnd.push(opnd.pop() * 10 + current); + ++idx; + } + else { + value /= 10; + opnd.push(opnd.pop() + current * value); + ++idx; + } + } + else + break; + } + //System.out.println(opnd.peek()); + return idx; + } + + private static boolean isDigit(char s) { + return '0' <= s && s <= '9'; + } + + public static void main(String[] args) throws Exception { + String s = "(0!+1)*2^(3!+4)-(5!-67-(8+9))"; + System.out.println(convert2Postfix(s)); + } + +} + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group22/2622819383/Task7/MyArrayList.java b/group22/2622819383/Task7/MyArrayList.java new file mode 100644 index 0000000000..265fa629b6 --- /dev/null +++ b/group22/2622819383/Task7/MyArrayList.java @@ -0,0 +1,164 @@ +import java.util.Iterator; +public class MyArrayList implements Iterable { + private T[] elems; + private int size; + private int capacity; + private static final int DEFAULT_CAPACITY = 10; + //modCount��¼����MyArrayList���󴴽�����Ԫ�ؽṹ�ı����ÿ��add��remove��������modCount�� + //Ŀ�����ڰ�����������⼯���еı仯��ʹ�ڵ����������ⲿ�Ķ�Ԫ�ؽṹ�仯��ͬ���� + private int modCount; + + public MyArrayList() { + clear(); + } + + public void clear() { + size = 0; + elems = (T[])new Object[capacity = DEFAULT_CAPACITY]; + modCount++; + } + + public int size() { return size; } + + public void expand() { + if (size < capacity) return; + T[] old = elems; + elems = (T[])new Object[capacity *= 2]; + for (int i = 0; i < size; i++) + elems[i] = old[i]; + } + public boolean find(T e) { + + int times = size - 1; + while (times != -1) { + if (e.equals(elems[times])) + return true; + --times; + } + return false; + + + } + //capacity >= 4*size ��shrink()��capacity���� + public void shrink() { + if (capacity < DEFAULT_CAPACITY * 2) return; + if (capacity < size * 4) return; + T[] old = elems; + elems = (T[])new Object[capacity /= 2]; + for (int i = 0; i < size; i++) + elems[i] = old[i]; + } + + + public boolean add(T e) { + add(size, e); + return true; + } + + public void add(int index, T e) { + if (index < 0 || size < index) throw new ArrayIndexOutOfBoundsException(); + expand(); + for (int i = size; i > index; i--) + elems[i] = elems[i - 1]; + elems[index] = e; + size++; + modCount++; + } + + public T get(int index) { + if (index < 0 || size <= index) throw new ArrayIndexOutOfBoundsException(); + return elems[index]; + } + + public boolean isEmpty() { + return size == 0; + } + + public T remove(int index) { + if (index < 0 || size <= index) throw new ArrayIndexOutOfBoundsException(); + T removed = elems[index]; + for (int i = index; i < size - 1; i++) + elems[i] = elems[i + 1]; + + size--; + shrink(); + modCount++; + return removed; + } + + public T set(int index, T e) { + if (index < 0 || size <= index) throw new ArrayIndexOutOfBoundsException(); + T old = elems[index]; + elems[index] = e; + return old; + } + + //ArrayListIterator����ת��Ϊjava.util.Iterator + public java.util.Iterator iterator() { + return new ArrayListIterator(); + } + + //���⣺Ϊʲôjava.util.IteratorҪ����ArrayListIterator���ü�? + /*�ش���Ϊjava.util.Iterator���ⲿ�ӿڣ����Ѿ�����õĶ����������ڷ�����MyArrayList�����ӵġ� + ��ʹ���Ѿ�����õķ��ͽӿڻ���ʱ�� + ���߲�Ϊ���ͽӿڴ���ʵ�Σ���ֱ��ʹ��java.util.Iterator������java.util.Iterator�в�����Ԫ������ȫ��Object�� + ����һ��ҪΪ���ͽӿڴ���ʵ�Σ��ڱ�����ʵ��Ϊ������MyArrayList��T�� + ����java.util.Iterator�в�����Ԫ������ȫ��T����֤�˴���MyArrayList��ͨ��Iterator���в�����Ԫ�ص����͡� + + �����Ƕ����ArrayListIterator��������ArrayListIterator��ԭ����ArrayListIterator�����ڷ�����MyArrayList�У� + �������еķ�������Ա�������ڲ���(ArrayListIterator)��������ʹ�÷��Ͳ���T������������ + + */ + + private class ArrayListIterator implements java.util.Iterator { + private int current = 0; + private int expectedModCount = modCount; + private boolean okToRemove = false; + + public boolean hasNext() { return current < size; } + public T next() { + if (modCount != expectedModCount) + throw new java.util.ConcurrentModificationException(); + if (!hasNext()) throw new java.util.NoSuchElementException(); + + okToRemove = true; + return elems[current++]; + } + public void remove() { + if (modCount != expectedModCount) + throw new java.util.ConcurrentModificationException(); + if (!okToRemove) + throw new IllegalStateException(); + + MyArrayList.this.remove(--current); + expectedModCount++; + okToRemove = false; + } + } + + //boolean remove(Object o) +} + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group22/2622819383/Task7/PostfixExpr.java b/group22/2622819383/Task7/PostfixExpr.java new file mode 100644 index 0000000000..1545dd31ec --- /dev/null +++ b/group22/2622819383/Task7/PostfixExpr.java @@ -0,0 +1,132 @@ +public class PostfixExpr { + public static float calculate(String postfixExpr) throws Exception { + char[] s = postfixExpr.toCharArray(); + Stack opnd = new Stack<>(); + int idx = 0; + while (idx < s.length) { + if (isDigit(s[idx])) + //++: �����ո�. + idx = 1 + readNum(s, idx, opnd); + + else { + if (s[idx] == '!') + opnd.push(calcu('!', opnd.pop())); + else { + float pOpnd2 = opnd.pop(), pOpnd1 = opnd.pop(); + opnd.push(calcu(pOpnd1, s[idx], pOpnd2)); + } + idx += 2; + } + } + + return opnd.pop(); + } + + private static int readNum(char[] s, int idx, Stack opnd) { + float current = 0; + float value = 1; //С�����ָ�λȨֵ��10^(-n) + boolean isDecimal = false; + opnd.push((float)(s[idx++] - '0')); + + while (idx < s.length) { + if (s[idx] == '.') { + isDecimal = true; + ++idx; + } + else if (isDigit(s[idx])) { + current = s[idx] - '0'; + if (!isDecimal) { + opnd.push(opnd.pop() * 10 + current); + ++idx; + } + else { + value /= 10; + opnd.push(opnd.pop() + current * value); + ++idx; + } + } + else + break; + } + //System.out.println(opnd.peek()); + return idx; + } + + private static float calcu(char c, float opnd) throws Exception { + + float ret = opnd; + float temp = opnd; + + if (c != '!') { + System.out.println("�ڴ��������'!'����ǰ�����Ϊ��" + c); + throw new Exception("���������"); + } + + if (temp == 0) + ret = 1; + + while (--temp > 1) { + ret *= temp; + } + //System.out.println("" + opnd + c + " = " + ret); + return ret; + } + + //����Ҫ������������������������㲢���ؽ�� + private static float calcu(float opnd1, char optr, float opnd2) throws Exception { + float ret; + switch(optr) { + case '+': ret = opnd1 + opnd2; break; + case '-': ret = opnd1 - opnd2; break; + case '*': ret = opnd1 * opnd2; break; + case '/': ret = opnd1 / opnd2; break; + case '^': ret = (float)Math.pow(opnd1, opnd2); break; + default: + System.out.println("����δ֪�������" + optr); + throw new Exception("��֧�ֵ������"); + } + //System.out.println("" + opnd1 + optr + opnd2 + " = " + ret); + return ret; + } + + private static boolean isDigit(char s) { + return '0' <= s && s <= '9'; + } + + public static void main(String[] args) throws Exception { + String s = "0 ! 1 + 2 3 ! 4 + ^ * 5 ! 67 - 8 9 + - -"; + System.out.println(calculate(s)); + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/group22/2622819383/Task7/PrefixExpr.java b/group22/2622819383/Task7/PrefixExpr.java new file mode 100644 index 0000000000..1e3b737af1 --- /dev/null +++ b/group22/2622819383/Task7/PrefixExpr.java @@ -0,0 +1,52 @@ + +//��ʦ�ķ����ð�����һ�³�һ��^^ +public class PrefixExpr { + public static float calculate(String prefixExpr) { + String expr = null; + + public PrefixExpr(String expr) { + this.expr = expr; + } + + public float evaluate() { + TokenParser parser = new TokenParser(); + List tokens = parser.parse(this.expr); + + Stack exprStack = new Stack<>(); + Stack numStack = new Stack<>(); + for(Token token : tokens){ + exprStack.push(token); + } + + while(!exprStack.isEmpty()){ + Token t = exprStack.pop(); + if(t.isNumber()){ + numStack.push(new Float(t.getIntValue())); + }else{ + Float f1 = numStack.pop(); + Float f2 = numStack.pop(); + numStack.push(calculate(t.toString(),f1,f2)); + + } + } + return numStack.pop().floatValue(); + } + + private Float calculate(String op, Float f1, Float f2){ + if(op.equals("+")){ + return f1+f2; + } + if(op.equals("-")){ + return f1-f2; + } + if(op.equals("*")){ + return f1*f2; + } + if(op.equals("/")){ + return f1/f2; + } + throw new RuntimeException(op + " is not supported"); + } +} + + \ No newline at end of file diff --git a/group22/2622819383/Task7/Stack.java b/group22/2622819383/Task7/Stack.java new file mode 100644 index 0000000000..4d2db455c2 --- /dev/null +++ b/group22/2622819383/Task7/Stack.java @@ -0,0 +1,28 @@ +public class Stack { + private MyArrayList elementData = new MyArrayList<>(); + + public void push(T o){ + elementData.add(o); + } + + public T pop(){ + return elementData.remove(size() - 1); + } + + public T peek(){ + if (size() == 0) return null; + return elementData.get(size() - 1); + } + + public boolean isEmpty(){ + return size() == 0; + } + + public int size(){ + return elementData.size(); + } + + public boolean find(T e) { + return elementData.find(e); + } +} diff --git a/group22/2622819383/Task8/CircleQueue.java b/group22/2622819383/Task8/CircleQueue.java new file mode 100644 index 0000000000..48d2ce90b4 --- /dev/null +++ b/group22/2622819383/Task8/CircleQueue.java @@ -0,0 +1,41 @@ +class CircleQueue { + private int size; + private int capacity; + private int headPos; + private int tailPos; + private T[] theItems; + + public CircleQueue(int maxSize) { + theItems = (T[])new Object[capacity = maxSize]; + size = 0; + headPos = tailPos = 0; + } + + public CircleQueue() { + this(100); + } + + private boolean isFull() { return size == capacity; } + public boolean isEmpty() { return size == 0; } + + public void enqueue(T e) { + if (!isFull()) { + theItems[++tailPos % capacity] = e; + size++; + } + else + throw new IndexOutOfBoundsException(); + } + + public T dequeue() { + if (isEmpty()) + throw new NoSuchElementException(); + T ret = null; + + ret = theItems[headPos]; + headPos = ++headPos % capacity; + size--; + + return ret; + } +} \ No newline at end of file diff --git a/group22/2622819383/Task8/Josephus.java b/group22/2622819383/Task8/Josephus.java new file mode 100644 index 0000000000..cdfcf7ffaf --- /dev/null +++ b/group22/2622819383/Task8/Josephus.java @@ -0,0 +1,34 @@ +//N���˱�Ŵ�1��N��Χ��ԲȦ����1�ſ�ʼ������������ +//����M�δ��ݺ��������������˳��֣����ʣ�µ���ȡʤ�� +import java.util.LinkedList; +public class Josephus { + public static void pass(int M, int N) { + Queue q = new Queue<>(); + for (int i = 1; i <= N; i++) + q.enqueue(i); + + while (0 < q.size()) { + M = M % N; + //i�ij�ʼֵ��һ�� + for (int i = 0; i < M; i++) { + q.enqueue(q.dequeue()); + } + System.out.print(q.dequeue() + " "); + } + } + + public static void main(String[] args) { + pass(1, 5); + } +} + +class Queue { + LinkedList l = new LinkedList<>(); + public T dequeue() { + return l.removeFirst(); + } + public int size() { return l.size(); } + public void enqueue(T x) { + l.addLast(x); + } +} \ No newline at end of file diff --git a/group22/2622819383/Task8/QueueWithTwoStacks.java b/group22/2622819383/Task8/QueueWithTwoStacks.java new file mode 100644 index 0000000000..1c067df03e --- /dev/null +++ b/group22/2622819383/Task8/QueueWithTwoStacks.java @@ -0,0 +1,51 @@ +//������stackʵ��queue +class QueueWithTwoStacks { + //stack1��������Ԫ�� + private Stack stack1 = new Stack<>(); + //stack2����ɾ��Ԫ�� + private Stack stack2 = new Stack<>(); + + public boolean isEmpty() { + return stack1.isEmpty() && stack2.isEmpty(); + } + + public void addToTail(T e) { + stack1.push(e); + } + + public T getFromHead() { + if (isEmpty()) + throw new ArrayIndexOutOfBoundsException(); + //stack1�� & stack2�ǿ� + if (stack1.isEmpty()) + return stack2.pop(); + //stack1�ǿ� & stack2�� + else if (stack2.isEmpty()) { + while (!stack1.isEmpty()) + stack2.push(stack1.pop()); + return stack2.pop(); + } + //stack1�ǿ� & stack2�ǿ� + else + return stack2.pop(); + } + + public int size() { + return stack2.size() + stack1.size(); + } + + public static void main(String[] args) { + QueueWithTwoStacks q = new QueueWithTwoStacks<>(); + q.getFromHead(); + for (int i = 0; i < 4; i++) + q.addToTail(i); + System.out.println(q.getFromHead()); + System.out.println(q.getFromHead()); + for (int i = 4; i < 10; i++) + q.addToTail(i); + while (q.size() > 0) + System.out.println(q.getFromHead()); + + } + +} \ No newline at end of file