跪求JAVA高手帮忙编一个程序!急急急,非常感谢呀~

跪求JAVA高手帮忙编一个程序!急急急,非常感谢呀~,第1张

public class Complex {

private int entry//实部

private int visual//虚念喊歼部

public Complex() {

super()

}

public Complex(int en, int vi) {

super()

this.setEntry(en)

this.setVisual(vi)

}

public int getEntry() {

return entry

}

public void setEntry(int entry) {

this.entry = entry

}

public int getVisual() {

return visual

}

public void setVisual(int visual) {

this.visual = visual

}

//显示:

public void showComp(){

System.out.println(this.getEntry()+"+"+this.getVisual()+"i")

}

//加法

public Complex addComp(Complex c1,Complex c2){

int e = c1.entry + c2.entry

int v = c1.visual + c2.visual

Complex c = new Complex(e,v)

return c

}

//减法

public Complex subComp(Complex c1,Complex c2){

int e = c1.entry - c2.entry

int v = c1.visual - c2.visual

Complex c = new Complex(e,v)

return c

}

//乘法

public Complex multiComp(Complex c1,Complex c2){

int e = c1.entry*c2.entry - c1.visual*c2.visual

int v = c1.visual*c2.entry + c1.entry*c2.visual

Complex c = new Complex(e,v)

return c

}

//相同返回true,不同返回false

public boolean equalComp(Complex c1,Complex c2){

return c1.entry==c2.entry &&c1.visual == c2.visual

}

}

//测试类

public class Test {

public static void main(String [] args){

//验证默认构造初始化:实部是0,虚部也是0

Complex c = new Complex()

System.out.print("默认渗缺构造结果是:")

System.out.println(c.getEntry())

System.out.println(c.getVisual())

//加法:

Complex c1 = new Complex(3,7)

Complex c2 = new Complex(5,6)

Complex cj = new Complex()

cj = c.addComp(c1,c2)

System.out.print("加法结果是:")

cj.showComp()

//减法:

Complex c3 = new Complex()

Complex c4 = new Complex()

Complex cm = new Complex()

cm = c.subComp(c1,c2)

System.out.print("减法结果是:")

cm.showComp()

//乘法:

Complex c5 = new Complex()

Complex c6 = new Complex()

Complex cc = new Complex()

cc = c.multiComp(c1,c2)

System.out.print("乘法结果是仔冲:")

cc.showComp()

//相等比较:

Complex c7 = new Complex(3,-3)

Complex c8 = new Complex(3,4)

Complex c9 = new Complex(3,4)

boolean resuls1 = c.equalComp(c7,c8)

boolean resuls2 = c.equalComp(c8,c9)

System.out.print("比较结果是:")

System.out.print(resuls1?true:false)

System.out.print(resuls2?true:false)

}

}

首先下载JAVACC 编写 jj文件 定义要空开的分隔符SKIP :{  | \t | \n | \r | \f } 定义关键字 from 为HQL的关键字 Teacher是用户输入的类名 应当是一个喊梁宏任意由字母和数字组成的单词 我们可以用正则表达式:[ A Z a z ]来表示 TOKEN: /*RESERVED TOKENS FOR UQL */{<FROM: from >  | <FROM_OBJECT:([ A Z a z ])+ >} 接下来定义一下输入的顺序与规范void expression() :{ Token tTable}{ ( <FROM> tTable = <FROM_OBJECT>) { sqlSB append( SELECT * ) sqlSB append( FROM ) append(tTable image)}}最后就是写解析代码 以便生成java代码PARSER_BEGIN(HQLParser)import java lang StringBufferimport java io StringReaderimport java io Readerpublic class HQLParser {private static StringBuffer sqlSB/**A String based constructor for ease of use  **/public HQLParser(String s) 郑册{this((Reader)(new StringReader(s)))sqlSB = new StringBuffer()}public String getSQL(){return sqlSB toString()}  public static void main(String args[]){try{ String query = args[ ] HQLParser parser = new HQLParser(query)  parser parse() System out println( SQL: +parser getSQL())}catch(Exception e){渣雹 e printStackTrace()}}public void parse(){ try { expression()} catch(Exception e){e printStackTrace()}}}PARSER_END(HQLParser)接下来到dos下输入:javacc debug_parser test jj debug_parser:用来输出语法树这时候会生成 个java文件 每个文件的作用以后会详细说明这时候只需要javac * java即可编译全部的java文件然后执行java HQLParser from Teacher 这时候屏幕上就会显示出 select * from Teacher lishixinzhi/Article/program/Java/JSP/201311/19735


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12384662.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存