ผลต่างระหว่างรุ่นของ "01204212/spreadsheet"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
 
(ไม่แสดง 2 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 24: แถว 24:
 
public class Main {
 
public class Main {
  
Map<Integer,Integer> table;
+
    Map<Integer,Integer> table;
+
   
public static void main(String[] args) throws IOException {
+
    public static void main(String[] args) throws IOException {
Main m = new Main();
+
        Main m = new Main();
+
       
m.process();
+
        m.process();
}
+
    }
  
Main() {
+
    Main() {
table = new HashMap<Integer, Integer>();
+
        table = new HashMap<Integer, Integer>();
}
+
    }
+
   
private void process() throws IOException {
+
    private void process() throws IOException {
    BufferedReader reader = new BufferedReader(
+
        BufferedReader reader = new BufferedReader(
              new InputStreamReader(System.in) );
+
                  new InputStreamReader(System.in) );
  
    System.out.println("Hello!");
+
        System.out.println("Hello!");
    while(true) {
+
        while(true) {
    System.out.print("> ");
+
            System.out.print("> ");
    String cmd = reader.readLine();
+
            String cmd = reader.readLine();
   
+
           
    if(cmd.equals("quit")) {
+
            if(cmd.equals("quit")) {
    return;
+
                return;
    }
+
            }
   
+
           
    if(cmd.contains("=")) {
+
            if(cmd.contains("=")) {
    String[] items = cmd.split("=");
+
                String[] items = cmd.split("=");
    int cellNum = getCellNumber(items[0]);
+
                int cellNum = getCellNumber(items[0]);
    int val = Integer.parseInt(items[1]);
+
                int val = Integer.parseInt(items[1]);
   
+
               
    table.put(cellNum, val);
+
                table.put(cellNum, val);
    } else {
+
            } else {
    int cellNum = getCellNumber(cmd);
+
                int cellNum = getCellNumber(cmd);
   
+
               
    Integer val = table.get(cellNum);
+
                Integer val = table.get(cellNum);
   
+
               
    if(val == null) {
+
                if(val == null) {
    val = 0;
+
                    val = 0;
    }
+
                }
    System.out.println(val);
+
                System.out.println(val);
    }
+
            }
    }
+
        }
}
+
    }
  
private int getCellNumber(String location) {
+
    private int getCellNumber(String location) {
location = location.toUpperCase();
+
        location = location.toUpperCase();
try {
+
        try {
int colNum = 0;
+
            int colNum = 0;
int i = 0;
+
            int i = 0;
while((location.charAt(i) >= 'A') &&
+
            while((location.charAt(i) >= 'A') &&
(location.charAt(i) <= 'Z')) {
+
                    (location.charAt(i) <= 'Z')) {
colNum = colNum * 26 + (location.charAt(i) - 'A');
+
                colNum = colNum * 26 + (location.charAt(i) - 'A');
i++;
+
                i++;
}
+
            }
String rowStr = location.substring(i);
+
            String rowStr = location.substring(i);
int rowNum = Integer.parseInt(rowStr);
+
            int rowNum = Integer.parseInt(rowStr);
return colNum * 1000000 + rowNum;
+
            return colNum * 1000000 + rowNum;
} catch(Exception e) {
+
        } catch(Exception e) {
return -1;
+
            return -1;
}
+
        }
}
+
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
=== Hash table (linear probing) ===
 
=== Hash table (linear probing) ===
<syntaxhighlight>
+
<syntaxhighlight lang="java">
 
public class HashTable {
 
public class HashTable {
static final int M = 1000;
+
    static final int M = 1000;
static final int P = 997;
+
    static final int P = 997;
static final int EMPTY_KEY = -1;
+
    static final int EMPTY_KEY = -1;
+
   
int [] values;
+
    int [] values;
int [] keys;
+
    int [] keys;
+
   
public HashTable() {
+
    public HashTable() {
values = new int[M];
+
        values = new int[M];
keys = new int[M];
+
        keys = new int[M];
for(int i=0; i<M; i++) {
+
        for(int i=0; i<M; i++) {
keys[i] = EMPTY_KEY;
+
            keys[i] = EMPTY_KEY;
}
+
        }
}
+
    }
+
   
int hash(int key) {
+
    int hash(int key) {
return key % P;
+
        return key % P;   // VERY BAD HASH FUNCTION. YOU HAVE TO CHANGE THIS
}
+
    }
+
   
void put(int k, int v) {
+
    void put(int k, int v) {
int idx = hash(k);  
+
        int idx = hash(k);  
//System.out.println("H = " + idx);
+
        //System.out.println("H = " + idx);
while(keys[idx] != EMPTY_KEY) {
+
        while((keys[idx] != EMPTY_KEY) && (keys[idx] != k)) {
idx++;
+
            idx++;
if(idx == M) {
+
            if(idx == M) {
idx = 0;
+
                idx = 0;
}
+
            }
//System.out.println("Not empty move to " + idx);
+
            //System.out.println("Not empty move to " + idx);
}
+
        }
keys[idx] = k;
+
        keys[idx] = k;
values[idx] = v;
+
        values[idx] = v;
}
+
    }
+
   
int get(int k) {
+
    int get(int k) {
int idx = hash(k);
+
        int idx = hash(k);
//System.out.println("H = " + idx);
+
        //System.out.println("H = " + idx);
while((keys[idx] != k) && (keys[idx] != EMPTY_KEY)) {
+
        while((keys[idx] != k) && (keys[idx] != EMPTY_KEY)) {
idx++;
+
            idx++;
if(idx == M) {
+
            if(idx == M) {
idx = 0;
+
                idx = 0;
}
+
            }
//System.out.println("Not the key, move to " + idx);
+
            //System.out.println("Not the key, move to " + idx);
}
+
        }
if(keys[idx] == k) {
+
        if(keys[idx] == k) {
return values[idx];
+
            return values[idx];
} else {
+
        } else {
throw new RuntimeException("Sorry");
+
            throw new RuntimeException("Sorry");
}
+
        }
}
+
    }
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

รุ่นแก้ไขปัจจุบันเมื่อ 23:46, 23 พฤศจิกายน 2559

This is part of 01204212

Examples

> A10=5
> B10=20
> A12=30
> A10
5
> B12345=12345
> B12345
12345
> quit

Code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Main {

    Map<Integer,Integer> table;
    
    public static void main(String[] args) throws IOException {
        Main m = new Main();
        
        m.process();
    }

    Main() {
        table = new HashMap<Integer, Integer>();
    }
    
    private void process() throws IOException {
        BufferedReader reader = new BufferedReader(
                   new InputStreamReader(System.in) );

        System.out.println("Hello!");
        while(true) {
            System.out.print("> ");
            String cmd = reader.readLine();
            
            if(cmd.equals("quit")) {
                return;
            }
            
            if(cmd.contains("=")) {
                String[] items = cmd.split("=");
                int cellNum = getCellNumber(items[0]);
                int val = Integer.parseInt(items[1]);
                
                table.put(cellNum, val);
            } else {
                int cellNum = getCellNumber(cmd);
                
                Integer val = table.get(cellNum);
                
                if(val == null) {
                    val = 0;
                }
                System.out.println(val);
            }
        }
    }

    private int getCellNumber(String location) {
        location = location.toUpperCase();
        try {
            int colNum = 0;
            int i = 0;
            while((location.charAt(i) >= 'A') &&
                    (location.charAt(i) <= 'Z')) {
                colNum = colNum * 26 + (location.charAt(i) - 'A');
                i++;
            }
            String rowStr = location.substring(i);
            int rowNum = Integer.parseInt(rowStr);
            return colNum * 1000000 + rowNum;
        } catch(Exception e) {
            return -1;
        }
    }
}

Hash table (linear probing)

public class HashTable {
    static final int M = 1000;
    static final int P = 997;
    static final int EMPTY_KEY = -1;
    
    int [] values;
    int [] keys;
    
    public HashTable() {
        values = new int[M];
        keys = new int[M];
        for(int i=0; i<M; i++) {
            keys[i] = EMPTY_KEY;
        }
    }
    
    int hash(int key) {
        return key % P;   // VERY BAD HASH FUNCTION. YOU HAVE TO CHANGE THIS
    }
    
    void put(int k, int v) {
        int idx = hash(k); 
        //System.out.println("H = " + idx);
        while((keys[idx] != EMPTY_KEY) && (keys[idx] != k)) {
            idx++;
            if(idx == M) {
                idx = 0;
            }
            //System.out.println("Not empty move to " + idx);
        }
        keys[idx] = k;
        values[idx] = v;
    }
    
    int get(int k) {
        int idx = hash(k);
        //System.out.println("H = " + idx);
        while((keys[idx] != k) && (keys[idx] != EMPTY_KEY)) {
            idx++;
            if(idx == M) {
                idx = 0;
            }
            //System.out.println("Not the key, move to " + idx);
        }
        if(keys[idx] == k) {
            return values[idx];
        } else {
            throw new RuntimeException("Sorry");
        }
    }
}