日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

编程能力强化(4)——模拟SQL语句解析

發布時間:2024/4/20 数据库 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编程能力强化(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??
  • {??
  • ????//?內部類,“裁判”類,用于裁決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("無效的命令");??
  • ????????????}??
  • ????????}??
  • ??????????
  • ????}??
  • }??
  • 總結

    以上是生活随笔為你收集整理的编程能力强化(4)——模拟SQL语句解析的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。