編程能力強化(4)——模擬SQL語句解析
這是2010年軟件大賽的樣題,用到的知識點比較多,也比較實用。
題目:
數據庫中有“表”的概念。“表”由若幹“行”組成,每“行”由許多“列”組成。一般的數據庫都提供了對SQL的支持。
我們可以模擬一個最簡單版的SQL,隻能實現簡單的排序,簡單的選擇條件,列的顯示順序等功能。
具體如下:
(1)輸入help,會輸出幫助信息
(2)輸入load data.txt,會輸出文件中的內容
(3)輸入sort weight,會根據重量排序,然後輸出,也可以根據其他幾個屬性排序,每次指定一個
(4)輸入select *,顯示所有的數據
(5)輸入select name,顯示某一列,如果要顯示多列,多列之間使用空格隔開
(6)輸入select * where price>50,條件隻能使用大於或者小於,單個條件不用空格,多個條件之間使用空格隔開
(7)輸入exit,程序退出
自己看看能不能做出來,如果做不出來,看看後麵的參考答案。
數據文件(data.txt)內容如下:
名稱 長度 重量 威力 價格
A 25.1 12.3 24.6 105.3
B 180.5 11.6 41.2 36.5
C 103.6 33.1 28.4 78
D 21.5 18.6 17.6 38.5
E 33.6 28.5 11.9 27.0
F 31.6 19.8 23.5 36.3
G 88.3 17.9 16.4 22.9
下麵是參考答案:
- import java.io.*;
- import java.util.*;
- // 代表每行數據
- class MyRow
- {
- private String name; // 名字
- private double length; // 長度
- private double weight; // 重量
- private double power; // 威力
- private double price; // 價格
- public MyRow(String x)
- {
- String[] ss = x.split("/t");
- name = ss[0].trim();
- length = Double.parseDouble(ss[1]);
- weight = Double.parseDouble(ss[2]);
- power = Double.parseDouble(ss[3]);
- price = Double.parseDouble(ss[4]);
- }
- public String toString()
- {
- return name + "/t" + length + "/t" + weight + "/t" + power + "/t" + price;
- }
- public String getName() { return name; }
- public double getLength() { return length; }
- public double getWeight() { return weight; }
- public double getPower() { return power; }
- public double getPrice() { return price; }
- }
- // 代表所有數據
- class MyData
- {
- // 內部類,“裁判”類,用於裁決Vector中的對象的比較大小問題
- class CaiPan implements Comparator
- {
- private int type;
- public CaiPan(int type)
- {
- this.type = type;
- }
- public int compare(Object o1, Object o2)
- {
- if(!(o1 instanceof MyRow)) return 0;
- if(!(o2 instanceof MyRow)) return 0;
- MyRow r1 = (MyRow)o1;
- MyRow r2 = (MyRow)o2;
- switch(type){
- case 1:
- return Double.compare(r1.getLength(),r2.getLength());
- case 2:
- return Double.compare(r1.getWeight(),r2.getWeight());
- case 3:
- return Double.compare(r1.getPower(),r2.getPower());
- case 4:
- return Double.compare(r1.getPrice(),r2.getPrice());
- default:
- return 0;
- }
- }
- }
- private Vector _v = new Vector();
- public void show()
- {
- System.out.println("................................");
- System.out.println("名稱/t長度/t重量/t威力/t價格");
- for(int i=0; i<_v.size(); i++){
- System.out.println(_v.get(i));
- }
- System.out.println("................................");
- }
- public boolean load(String x)
- {
- try{
- BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(x)));
- _v.clear();
- br.readLine(); // 第一行不要
- for(;;){
- String s = br.readLine();
- if(s==null) break;
- MyRow row = new MyRow(s);
- _v.add(row);
- }
- show();
- return true;
- }
- catch(Exception e){
- //e.printStackTrace();
- return false;
- }
- }
- public boolean sort(String x)
- {
- if(x.equals("length")){
- Collections.sort(_v, new CaiPan(1));
- show();
- return true;
- }
- if(x.equals("weight")){
- Collections.sort(_v, new CaiPan(2));
- show();
- return true;
- }
- if(x.equals("power")){
- Collections.sort(_v, new CaiPan(3));
- show();
- return true;
- }
- if(x.equals("price")){
- Collections.sort(_v, new CaiPan(4));
- show();
- return true;
- }
- return false;
- }
- // 初步解析命令
- public boolean select(String x)
- {
- Vector sort = new Vector(); // 顯示的字段
- Vector where = new Vector(); // 過濾的條件語句
- String[] ss = x.split(" ");
- Vector t = sort;
- for(int i=0; i<ss.length; i++){
- if(ss[i].length()==0) continue; // 防止多個空格
- if(ss[i].equals("where")){
- t = where;
- continue;
- }
- t.add(ss[i]);
- }
- if(sort.size()==0) return false; // 字段必須指定
- if(sort.size()>5) return false; // 字段太多
- return select(sort, where);
- }
- // 執行select任務
- public boolean select(List sort, List where)
- {
- try{
- System.out.println("................................");
- //輸出標題
- for(int k=0; k<sort.size(); k++){
- if(sort.get(k).equals("name"))
- System.out.print("姓名/t");
- else if(sort.get(k).equals("length"))
- System.out.print("長度/t");
- else if(sort.get(k).equals("weight"))
- System.out.print("重量/t");
- else if(sort.get(k).equals("power"))
- System.out.print("威力/t");
- else if(sort.get(k).equals("price"))
- System.out.print("價格/t");
- else if(sort.get(k).equals("*"))
- System.out.print("名稱/t長度/t重量/t威力/t價格/t");
- else
- return false;
- }// 枚舉sort
- System.out.println("");
- //輸出內容
- for(int i=0; i<_v.size(); i++){
- MyRow row = (MyRow)_v.get(i);
- if(checkWhere(row, where)){
- for(int k=0; k<sort.size(); k++){
- if(sort.get(k).equals("name"))
- System.out.print(row.getName() + "/t");
- else if(sort.get(k).equals("length"))
- System.out.print(row.getLength() + "/t");
- else if(sort.get(k).equals("weight"))
- System.out.print(row.getLength() + "/t");
- else if(sort.get(k).equals("power"))
- System.out.print(row.getLength() + "/t");
- else if(sort.get(k).equals("price"))
- System.out.print(row.getLength() + "/t");
- else if(sort.get(k).equals("*"))
- System.out.print(row + "/t");
- else
- return false;
- }// 枚舉sort
- System.out.println("");
- }//檢查過濾條件
- }//對每個行處理
- System.out.println("................................");
- return true;
- }
- catch(Exception e){
- //e.printStackTrace();
- return false;
- }
- }
- // 返回true 則該行記錄顯示,返回false,則不顯示
- public boolean checkWhere(MyRow row, List where) throws Exception
- {
- boolean op=true; // true: 表示比較符號為 > , 否則為 <
- String field = ""; // 過濾條件的字段
- String value = ""; // 過濾值
- // 對每一個條件處理
- for(int i=0; i<where.size(); i++){
- String s = (String)where.get(i);
- String[] ss = s.split(">");
- if(ss.length==2){
- field = ss[0];
- op = true;
- value = ss[1];
- }
- else{
- ss = s.split("<");
- if(ss.length==2){
- field = ss[0];
- op = false;
- value = ss[1];
- }
- else
- return false; // 既沒有"<"也沒有">"的情況
- }
- double d_value = Double.parseDouble(value);
- if(field.equals("length")){
- if(op){
- if(row.getLength() <= d_value) return false;
- }
- else{
- if(row.getLength() >= d_value) return false;
- }
- }
- else if(field.equals("weight")){
- if(op){
- if(row.getWeight() <= d_value) return false;
- }
- else{
- if(row.getWeight() >= d_value) return false;
- }
- }
- else if(field.equals("power")){
- if(op){
- if(row.getPower() <= d_value) return false;
- }
- else{
- if(row.getPower() >= d_value) return false;
- }
- }
- else if(field.equals("price")){
- if(op){
- if(row.getPrice() <= d_value) return false;
- }
- else{
- if(row.getPrice() >= d_value) return false;
- }
- }
- else
- throw new Exception("valid field name!"); // 無法識別的 field,則算錯表達式錯
- }
- return true;
- }
- }
- // 負責解釋用戶輸入的命令
- class MyCommand
- {
- private MyData data;
- public MyCommand(MyData x)
- {
- data = x;
- }
- public boolean execute(String x)
- {
- int d = x.indexOf(" "); // 找第一個空格的位置
- if(d<0) return false;
- String x1 = x.substring(0,d);
- String x2 = x.substring(d+1);
- if(x1.equals("load")){
- if(!data.load(x2.trim()))
- System.out.println("裝入文件出錯!");
- return true;
- }
- if(x1.equals("sort"))
- return data.sort(x2.trim());
- if(x1.equals("select"))
- return data.select(x2);
- return false;
- }
- }
- public class My
- {
- private static BufferedReader br_keyboard;
- static
- {
- br_keyboard = new BufferedReader(new InputStreamReader(System.in)); // 將它用於從鍵盤讀入
- }
- public static void main(String[] args) throws Exception
- {
- MyData data = new MyData();
- MyCommand cmd = new MyCommand(data); // cmd 服務於 data
- for(;;){
- System.out.print("請輸入命令(輸入help顯示幫助信息):");
- String s = br_keyboard.readLine();
- if(s.equals("exit")) break;
- if(s.equals("help")){
- System.out.println("----------------------------");
- System.out.println("load data.txt");
- System.out.println("從當前目錄裝入文件data.txt,並顯示");
- System.out.println("sort weight");
- System.out.println("按“重量”排序,並顯示");
- System.out.println("類似地,還可以是 sort length, sort price,sort power等");
- System.out.println("select weight length");
- System.out.println("隻顯示 重量,長度兩列");
- System.out.println("select weight length where price > 50");
- System.out.println("隻顯示 重量,長度兩列, 隻包含價格 > 50 的行");
- System.out.println("select * where price>50 length<30");
- System.out.println("顯示所有列, 隻包含價格>50 且 長度<30 的行");
- System.out.println("其它的組合,從上邊類推");
- System.out.println("exit");
- System.out.println("退出程序");
- System.out.println("----------------------------");
- continue;
- }
- if(!cmd.execute(s)){
- System.out.println("無效的命令");
- }
- }
- }
- }
最後更新:2017-04-03 22:15:32
上一篇:
Struts 2基礎
下一篇:
賤人就是矯情——當打假成為一門生意
圖片onerror事件,為圖片加載指定默認圖片
【雲計算的1024種玩法】打造自己的在線編程環境
SQL Server Parameter Sniffing及其改進方法
《軟件工藝師:專業、務實、自豪》一2.5 由傳統開發方式向敏捷轉型
《Linux From Scratch》第一部分:介紹 第一章:介紹-1.1 如何構建LFS係統
《數據結構與抽象:Java語言描述(原書第4版)》一2.1.4 讓實現安全
ubuntu11.04啟動 及虛擬文件係統
WCF技術剖析之二十四: ServiceDebugBehavior服務行為是如何實現異常的傳播的?
互聯網時代,我們還有隱私嗎?那些容易被忽視但又非常重要的安全知識
阿裏雲服務器怎麼更換係統盤,如何更換操作係統?