生活随笔
收集整理的這篇文章主要介紹了
编程能力强化(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
?
下面是參考答案:
[java]?view plaincopy
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?? {?? ?????? ????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){?? ?????????????? ????????????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);??? ????}????? ?????? ?????? ????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;?? ????????}?? ????????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;?? ????????????????}?? ????????????????System.out.println("");?? ????????????}?? ????????}?? ?????????? ????????System.out.println("................................");?? ????????return?true;?? ????}?? ????catch(Exception?e){?? ?????????? ????????return?false;?? ????}?? ????}?? ?????? ?????? ?????? ????public?boolean?checkWhere(MyRow?row,?List?where)?throws?Exception?? ????{?? ????????boolean?op=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!");???? ????????}?? ?????????? ????????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);???? ?????????? ????????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("無效的命令");?? ????????????}?? ????????}?? ?????????? ????}?? }??
總結
以上是生活随笔為你收集整理的编程能力强化(4)——模拟SQL语句解析的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。