编译实验一单词识别

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

编译原理实验一设计一个单词识别的程序,从文件中读取字符串,将字符串中的字符按空格逐个识别,并比较zero,one,two,three,four,five,Six,seven,eight,nine转换成相应的阿拉伯数字。程序packagezuoye;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.util.*;publicclassbianyi{staticStringa=;staticinti=0;staticString[]b=newString[500];publicstaticvoidNumberChange(Stringa[]){//转换函数;intj=0;for(j=0;ja.length;j++){{if(zero.equals(a[j]))System.out.print(0);if(one.equals(a[j]))System.out.print(1);if(two.equals(a[j]))System.out.print(2);if(three.equals(a[j]))System.out.print(3);if(four.equals(a[j]))System.out.print(4);if(five.equals(a[j]))System.out.print(5);if(six.equals(a[j]))System.out.print(6);if(seven.equals(a[j]))System.out.print(7);if(eight.equals(a[j]))System.out.print(8);if(nine.equals(a[j]))System.out.print(9);}}}publicstaticvoidmain(String[]ags)throwsFileNotFoundException{try{//从文件中读取内容;System.out.println(System.in);FileReaderfileReader=newFileReader(D:\\a.txt);BufferedReaderbuf=newBufferedReader(fileReader);inti=0;StringreadLine=;while((readLine=buf.readLine())!=null){a=readLine;}}catch(Exceptione){e.printStackTrace();}b=a.split();//分离单词;System.out.println(文件中的内容:);//输出结果;System.out.println(a);System.out.printf(识别单词:);for(intj=0;jb.length;j++){System.out.print(b[+j+]=+b[j]+);}System.out.println();System.out.println(识别数字:);NumberChange(b);}}importjava.io.BufferedReader;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.util.*;importjava.util.regex.Pattern;publicclassbianyier{staticStringa=;staticinti=0;staticString[]b=newString[500];staticboolean[]c=newboolean[500];staticboolean[]d=newboolean[500];publicstaticvoidNumberChange(Stringa){//转换函数;intj=0;//for(j=0;ja.length;j++){//{if(while.equals(a))System.out.print(3);//if(one.equals(a[j]))System.out.print(1);//if(two.equals(a[j]))System.out.print(2);//if(three.equals(a[j]))System.out.print(3);//if(four.equals(a[j]))System.out.print(4);//if(five.equals(a[j]))System.out.print(5);//if(six.equals(a[j]))System.out.print(6);//if(seven.equals(a[j]))System.out.print(7);//if(eight.equals(a[j]))System.out.print(8);//if(nine.equals(a[j]))System.out.print(9);//}//}}publicstaticvoidfunctionA(Stringa[]){Patternp=Pattern.compile(^[a-zA-Z].*);for(inti=0;ia.length;i++){java.util.regex.Matcherm=p.matcher(a[i]);c[i]=m.matches();//booleanb=m.matches();System.out.println(c[i]);}/*for(intj=0;ja.length;j++){if(c[j])NumberChange(a[i]);else}*/}publicstaticvoidfunctionB(Stringa[]){Patternp=Pattern.compile(^[0-9]*$);for(inti=0;ia.length;i++){java.util.regex.Matcherm=p.matcher(a[i]);d[i]=m.matches();System.out.println(d[i]);}}/*publicstaticvoidfunctionC(Stringa[]){Patternp=Pattern.compile(^[A-Za-z]+$);for(inti=0;ia.length;i++){java.util.regex.Matcherm=p.matcher(a[i]);c[i]=m.matches();System.out.println(c[i]);}}*/publicstaticvoidmain(String[]ags)throwsFileNotFoundException{try{//从文件中读取内容;System.out.println(System.in);FileReaderfileReader=newFileReader(D:\\a.txt);BufferedReaderbuf=newBufferedReader(fileReader);inti=0;StringreadLine=;while((readLine=buf.readLine())!=null){a=readLine;}}catch(Exceptione){e.printStackTrace();}b=a.split();//分离单词;System.out.println(文件中的内容:);//输出结果;System.out.println(a);System.out.printf(识别单词:);for(intj=0;jb.length;j++){System.out.print(b[+j+]=+b[j]+);}System.out.println();functionA(b);functionB(b);//System.out.println(识别数字:);//NumberChange(b);}}

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功