这是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
  1. import java.io.*;
  2. import java.util.*;
  3. // 代表每行数据
  4. class MyRow
  5. {
  6. private String name;    // 名字
  7. private double length;  // 长度
  8. private double weight;  // 重量
  9. private double power;   // 威力
  10. private double price;   // 价格
  11. public MyRow(String x)
  12. {
  13. String[] ss = x.split("/t");
  14. name = ss[0].trim();
  15. length = Double.parseDouble(ss[1]);
  16. weight = Double.parseDouble(ss[2]);
  17. power = Double.parseDouble(ss[3]);
  18. price = Double.parseDouble(ss[4]);
  19. }
  20. public String toString()
  21. {
  22. return name + "/t" + length + "/t" + weight + "/t" + power + "/t" + price;
  23. }
  24. public String getName() { return name; }
  25. public double getLength() { return length; }
  26. public double getWeight() { return weight; }
  27. public double getPower() { return power; }
  28. public double getPrice() { return price; }
  29. }
  30. // 代表所有数据
  31. class MyData
  32. {
  33. // 内部类,“裁判”类,用于裁决Vector中的对象的比较大小问题
  34. class CaiPan implements Comparator
  35. {
  36. private int type;
  37. public CaiPan(int type)
  38. {
  39. this.type = type;
  40. }
  41. public int compare(Object o1, Object o2)
  42. {
  43. if(!(o1 instanceof MyRow)) return 0;
  44. if(!(o2 instanceof MyRow)) return 0;
  45. MyRow r1 = (MyRow)o1;
  46. MyRow r2 = (MyRow)o2;
  47. switch(type){
  48. case 1:
  49. return Double.compare(r1.getLength(),r2.getLength());
  50. case 2:
  51. return Double.compare(r1.getWeight(),r2.getWeight());
  52. case 3:
  53. return Double.compare(r1.getPower(),r2.getPower());
  54. case 4:
  55. return Double.compare(r1.getPrice(),r2.getPrice());
  56. default:
  57. return 0;
  58. }
  59. }
  60. }
  61. private Vector _v = new Vector();
  62. public void show()
  63. {
  64. System.out.println("................................");
  65. System.out.println("名称/t长度/t重量/t威力/t价格");
  66. for(int i=0; i<_v.size(); i++){
  67. System.out.println(_v.get(i));
  68. }
  69. System.out.println("................................");
  70. }
  71. public boolean load(String x)
  72. {
  73. try{
  74. BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(x)));
  75. _v.clear();
  76. br.readLine();  // 第一行不要
  77. for(;;){
  78. String s = br.readLine();
  79. if(s==null) break;
  80. MyRow row = new MyRow(s);
  81. _v.add(row);
  82. }
  83. show();
  84. return true;
  85. }
  86. catch(Exception e){
  87. //e.printStackTrace();
  88. return false;
  89. }
  90. }
  91. public boolean sort(String x)
  92. {
  93. if(x.equals("length")){
  94. Collections.sort(_v, new CaiPan(1));
  95. show();
  96. return true;
  97. }
  98. if(x.equals("weight")){
  99. Collections.sort(_v, new CaiPan(2));
  100. show();
  101. return true;
  102. }
  103. if(x.equals("power")){
  104. Collections.sort(_v, new CaiPan(3));
  105. show();
  106. return true;
  107. }
  108. if(x.equals("price")){
  109. Collections.sort(_v, new CaiPan(4));
  110. show();
  111. return true;
  112. }
  113. return false;
  114. }
  115. // 初步解析命令
  116. public boolean select(String x)
  117. {
  118. Vector sort = new Vector();  // 显示的字段
  119. Vector where = new Vector();  // 过滤的条件语句
  120. String[] ss = x.split(" ");
  121. Vector t = sort;
  122. for(int i=0; i<ss.length; i++){
  123. if(ss[i].length()==0) continue;  // 防止多个空格
  124. if(ss[i].equals("where")){
  125. t = where;
  126. continue;
  127. }
  128. t.add(ss[i]);
  129. }
  130. if(sort.size()==0) return false;  // 字段必须指定
  131. if(sort.size()>5) return false;  // 字段太多
  132. return select(sort, where);
  133. }
  134. // 执行select任务
  135. public boolean select(List sort, List where)
  136. {
  137. try{
  138. System.out.println("................................");
  139. //输出标题
  140. for(int k=0; k<sort.size(); k++){
  141. if(sort.get(k).equals("name"))
  142. System.out.print("姓名/t");
  143. else if(sort.get(k).equals("length"))
  144. System.out.print("长度/t");
  145. else if(sort.get(k).equals("weight"))
  146. System.out.print("重量/t");
  147. else if(sort.get(k).equals("power"))
  148. System.out.print("威力/t");
  149. else if(sort.get(k).equals("price"))
  150. System.out.print("价格/t");
  151. else if(sort.get(k).equals("*"))
  152. System.out.print("名称/t长度/t重量/t威力/t价格/t");
  153. else
  154. return false;
  155. }// 枚举sort
  156. System.out.println("");
  157. //输出内容
  158. for(int i=0; i<_v.size(); i++){
  159. MyRow row = (MyRow)_v.get(i);
  160. if(checkWhere(row, where)){
  161. for(int k=0; k<sort.size(); k++){
  162. if(sort.get(k).equals("name"))
  163. System.out.print(row.getName() + "/t");
  164. else if(sort.get(k).equals("length"))
  165. System.out.print(row.getLength() + "/t");
  166. else if(sort.get(k).equals("weight"))
  167. System.out.print(row.getLength() + "/t");
  168. else if(sort.get(k).equals("power"))
  169. System.out.print(row.getLength() + "/t");
  170. else if(sort.get(k).equals("price"))
  171. System.out.print(row.getLength() + "/t");
  172. else if(sort.get(k).equals("*"))
  173. System.out.print(row + "/t");
  174. else
  175. return false;
  176. }// 枚举sort
  177. System.out.println("");
  178. }//检查过滤条件
  179. }//对每个行处理
  180. System.out.println("................................");
  181. return true;
  182. }
  183. catch(Exception e){
  184. //e.printStackTrace();
  185. return false;
  186. }
  187. }
  188. // 返回true 则该行记录显示,返回false,则不显示
  189. public boolean checkWhere(MyRow row, List where) throws Exception
  190. {
  191. boolean op=true;  // true: 表示比较符号为 > , 否则为 <
  192. String field = "";  // 过滤条件的字段
  193. String value = "";  // 过滤值
  194. // 对每一个条件处理
  195. for(int i=0; i<where.size(); i++){
  196. String s = (String)where.get(i);
  197. String[] ss = s.split(">");
  198. if(ss.length==2){
  199. field = ss[0];
  200. op = true;
  201. value = ss[1];
  202. }
  203. else{
  204. ss = s.split("<");
  205. if(ss.length==2){
  206. field = ss[0];
  207. op = false;
  208. value = ss[1];
  209. }
  210. else
  211. return false;  // 既没有"<"也没有">"的情况
  212. }
  213. double d_value = Double.parseDouble(value);
  214. if(field.equals("length")){
  215. if(op){
  216. if(row.getLength() <= d_value)   return false;
  217. }
  218. else{
  219. if(row.getLength() >= d_value) return false;
  220. }
  221. }
  222. else if(field.equals("weight")){
  223. if(op){
  224. if(row.getWeight() <= d_value) return false;
  225. }
  226. else{
  227. if(row.getWeight() >= d_value) return false;
  228. }
  229. }
  230. else if(field.equals("power")){
  231. if(op){
  232. if(row.getPower() <= d_value) return false;
  233. }
  234. else{
  235. if(row.getPower() >= d_value) return false;
  236. }
  237. }
  238. else if(field.equals("price")){
  239. if(op){
  240. if(row.getPrice() <= d_value) return false;
  241. }
  242. else{
  243. if(row.getPrice() >= d_value) return false;
  244. }
  245. }
  246. else
  247. throw new Exception("valid field name!");  // 无法识别的 field,则算错表达式错
  248. }
  249. return true;
  250. }
  251. }
  252. // 负责解释用户输入的命令
  253. class MyCommand
  254. {
  255. private MyData data;
  256. public MyCommand(MyData x)
  257. {
  258. data = x;
  259. }
  260. public boolean execute(String x)
  261. {
  262. int d = x.indexOf(" ");  // 找第一个空格的位置
  263. if(d<0) return false;
  264. String x1 = x.substring(0,d);
  265. String x2 = x.substring(d+1);
  266. if(x1.equals("load")){
  267. if(!data.load(x2.trim()))
  268. System.out.println("装入文件出错!");
  269. return true;
  270. }
  271. if(x1.equals("sort"))
  272. return data.sort(x2.trim());
  273. if(x1.equals("select"))
  274. return data.select(x2);
  275. return false;
  276. }
  277. }
  278. public class My
  279. {
  280. private static BufferedReader br_keyboard;
  281. static
  282. {
  283. br_keyboard = new BufferedReader(new InputStreamReader(System.in));  // 将它用于从键盘读入
  284. }
  285. public static void main(String[] args) throws Exception
  286. {
  287. MyData data = new MyData();
  288. MyCommand cmd = new MyCommand(data);  // cmd 服务于 data
  289. for(;;){
  290. System.out.print("请输入命令(输入help显示帮助信息):");
  291. String s = br_keyboard.readLine();
  292. if(s.equals("exit")) break;
  293. if(s.equals("help")){
  294. System.out.println("----------------------------");
  295. System.out.println("load data.txt");
  296. System.out.println("从当前目录装入文件data.txt,并显示");
  297. System.out.println("sort weight");
  298. System.out.println("按“重量”排序,并显示");
  299. System.out.println("类似地,还可以是 sort length, sort price,sort power等");
  300. System.out.println("select weight length");
  301. System.out.println("只显示 重量,长度两列");
  302. System.out.println("select weight length where price > 50");
  303. System.out.println("只显示 重量,长度两列, 只包含价格 > 50 的行");
  304. System.out.println("select * where price>50 length<30");
  305. System.out.println("显示所有列, 只包含价格>50 且 长度<30 的行");
  306. System.out.println("其它的组合,从上边类推");
  307. System.out.println("exit");
  308. System.out.println("退出程序");
  309. System.out.println("----------------------------");
  310. continue;
  311. }
  312. if(!cmd.execute(s)){
  313. System.out.println("无效的命令");
  314. }
  315. }
  316. }
  317. }

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

  1. Python编程 模拟SQL语句 实现对员工信息的增删改查

    一.问题描述 用 Python 模拟 sql 语句,实现对员工信息的增删改查. 封装函数,传入参数:文件路径和 sql 命令. 模拟 sql 语句实现对员工信息的现增删改查,并打印结果. 二.Pyth ...

  2. python对excel增删改查语句_利用python模拟sql语句对员工表格进行增删改查

    本文主要给大家介绍了关于python模拟sql语句对员工表格进行增删改查的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 具体需求: 员工信息表程序,实现增删改查操作: 可进行模糊查询, ...

  3. python对excel增删改查_利用python模拟sql语句对员工表格进行增删改查

    本文主要给大家介绍了关于python模拟sql语句对员工表格进行增删改查的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 具体需求: 员工信息表程序,实现增删改查操作: 可进行模糊查询, ...

  4. sql java 解析_Java 实现对Sql语句解析

    最近要实现一个简易的数据库系统,除了要考虑如何高效的存储和访问数据,建立表关系外,对基本的sql查询语句要做一个解析,这样我们才能知道用户的查询要求:因为时间关系,参考了已有的一篇文章,并对其实现中出 ...

  5. java狼追着羊跑的程序_Java编程能力强化——狼羊过河问题

    题目:有3只狼和3只羊要过河,只有一条船,一次最多只能坐两只动物并且每次必须有动物开船,如果某一边的狼的个数大于羊的个数,羊将被吃掉,编程给出解. 关于编程思路,参考:Java编程能力强化(2)--搜 ...

  6. java sql语句逗号_Java 实现对Sql语句解析

    最近要实现一个简易的数据库系统,除了要考虑如何高效的存储和访问数据,建立表关系外,对基本的sql查询语句要做一个解析,这样我们才能知道用户的查询要求:因为时间关系,参考了已有的一篇文章,并对其实现中出 ...

  7. oracle12测试骤,Oracle中SQL语句解析的步骤

    我们都知道在Oracle中每条SQL语句在执行之前都需要经过解析,这里面又分为软解析和硬解析.那么这两种解析有何不同之处呢?它们又分别是如何进行解析呢?Oracle内部解析的步骤又是如何进行的呢?下面 ...

  8. python写sql语句_Python操作文件模拟SQL语句功能

    一.需求 当然此表你在文件存储时可以这样表示 1,Alex Li,22,13651054608,IT,2013-04-01 现需要对这个员工信息文件,实现增删改查操作 1. 可进行模糊查询,语法至少支 ...

  9. Oracle进阶学习3:SQL语句解析类型——硬解析和软解析

    文章目录 硬解析和软解析 shared pool功能 shared pool组成 SQL语句 硬解析和软解析 Oracle数据信息全部存储在数据字典中, Oracle的解析有两种,软解析以及硬解析 硬 ...

最新文章

  1. hive 表的常用操作
  2. python DbUtils 使用教程
  3. 【GDOI2016模拟3.11】历史
  4. Java I/O体系详细讲解
  5. 大型企业用什么orm_在大型仓储物流企业中使用什么类型的货架更为合适呢?
  6. sql server 内存_SQL Server内存性能指标–第5部分–了解惰性写入,空闲列表停顿/秒和待批内存授予
  7. hive sql 报错后继续执行_Hive迁移Presto在OPPO的实践
  8. CSS基础知识——常用基础命令
  9. 坐标转换-大地转高斯平面平面坐标转换
  10. 詹姆斯·高斯林接下来要做什么?
  11. iOS(swift): Core ML的使用
  12. 【网络安全】威胁情报信息
  13. iphone8验证服务器出错,某平台上买的“全新国行正品iphone8手机,支持官方验证”用了不到两个月坏了...
  14. 给大家推荐一下常用的镜像站
  15. win10怎么新建计算机用户,Win10添加用户教程(Microsoft微软帐户、本地帐户、儿童帐户)...
  16. ————博客永久废止————转到http://1su.net/nsB
  17. js大数值单位转换千、万、亿单位,复制即可用
  18. 2021美亚杯(个人赛)练习记录
  19. 【Procmon教程2】如何揪出篡改注册表的元凶?
  20. 对设计模式的总结之工厂方法模式和抽象工厂模式

热门文章

  1. 第10章 例题 7-4 汉诺(Hanoi)塔问题
  2. 图像坐标球面投影_坐标系统及投影概述
  3. Java黑皮书课后题第3章:**3.23(几何:点是否在矩形内)编写程序,提示用户输入点(x,y),然后检测该点是否在以原点为中心、宽为10、高为5的矩形中
  4. C语言学习之用*打印菱形
  5. Using libcurl in VC++
  6. MVC强类型和弱类型的区别
  7. 设计模式(七)适配器模式(Adapter Pattern)
  8. 在.net中读写config文件的各种方法(转载)
  9. C#一列数的规则如下: 1、1、2、3、5、8、13、21、34...... 求第100位数是多少, 用递归算法实现。...
  10. [置顶]信息发布系统 Jquery+MVC架构开发(4)Model 层