实验目
1 熟悉正规式构造方法
2 熟悉字符串中识特定字符串方法
3 复文件操作
二 实验容求
已知段C语言程序:
#include stdioh
int gcd(int k){
int m
mk+2
return m
}
main(){ *main function*
int i mainy1
i mainy +gcd(j)
printf(i d \n i)
}
求:
1 面C程序容文件保存
2 请构造出识面C程序中关键字mainintreturn正规式
3 正规式编写段代码作子程序
4 遍写程序调子程序完成关键字字符串mainintreturn识
5 指出关键字首字符处行号列号出出现时第次出现该结果输出屏幕保存文件中格式:
main 2 1
三 实验原理(包括实验算法软件结构框图流程框图)
四 源程序(交盘)
package byyl
import javaioFileNotFoundException
import javaioFileReader
import javaioFileWriter
import javaioIOException
import javautilArrayList
import javautilList
class StringPosition{
private int row
private int column
private String s
private int count出现次数
public StringPosition(int rowint columnString s) {
TODO Autogenerated constructor stub
thisrow row
thiscolumn column
thiss s
count 1
}
public int getRow() {
return row
}
public void setRow(int row) {
thisrow row
}
public int getColumn() {
return column
}
public void setColumn(int column) {
thiscolumn column
}
public String getS() {
return s
}
public void setS(String s) {
thiss s
}
public int getCount() {
return count
}
设置出现次数
public void setCount(int count){
thiscount count
}
显示信息
public void showInfor(){
String content 关键词:+s+\n行:+row+ 列:+column+ 次数:+count
Systemoutprintln(content)
byyllsyprintStringInfor(content)
}
}
public class byyllsy {
public static String getContent(){
String str null
try {
FileReader fr new FileReader(cdeftxt)
char[] data new char[1024]
int len 0
try {
while ((lenfrread(data 0datalength))>0) {
str new String(data0len)
}
} catch (IOException e) {
TODO Autogenerated catch block
eprintStackTrace()
Systemoutprintln(文件读写错误)
}
frclose()
} catch (FileNotFoundException e) {
TODO Autogenerated catch block
eprintStackTrace()
Systemoutprintln(文件存)
} catch (IOException e) {
TODO Autogenerated catch block
eprintStackTrace()
}
return str
}
分析文件存储子串
public static List
List
int row1column0
char [] chararray new char[1024]
chararray[0]''判断否值
int count 0
try {
FileReader fr new FileReader(fileName)
int temp1
int i 0
while (true) {
temp frread()
char c (char) temp
碰回车行数加1列数变0
column++
if (c'\n') {
row++
column0
}
if (isSpace(c)&&temp1) {
chararray[i]c
i++
}
判断否注释
else if(c''){
temp frread()
c (char) temp
if (c'*') {
while (true) {
temp frread()
c (char) temp
if (c'*') {
if (c'') {
break
}
else if (temp1) {
break
}
}
}
}
}
else if((isSpace(c)&&chararray[0]'')){
String s new String(chararray0i)
strListadd(new StringPosition(row columni s))
chararray[0]''
i0
}
else if ((temp1&&chararray[0]'')) {
String s new String(chararray0i)
strListadd(new StringPosition(row columni s))
break
}
else if((temp1&&chararray[0]'')) {
break
}
}
frclose()关闭流释放资源
} catch (FileNotFoundException e) {
TODO Autogenerated catch block
eprintStackTrace()
Systemoutprintln(文件存)
} catch (IOException e) {
TODO Autogenerated catch block
eprintStackTrace()
}
return strList
}
判断否空格换行tab键
public static boolean isSpace(char c){
if (c' '||c'('||c9|c'\n') {
return true
}
else
return false
}
通字符串查找出应位置
public static List
ArrayList
int count 1记录次数
for (StringPosition stringPosition strlist) {
if(stringPositiongetS()equals(name)){
subspadd(stringPosition)
stringPositionsetCount(count)
count++
}
}
return subsp
}
写入文件保存
public static void printStringInfor(String content){
FileWriter fw null
try {
fw new FileWriter(showtxt true)
} catch (IOException e) {
TODO Autogenerated catch block
eprintStackTrace()
}
try {
fwappend(content+\n)
fwclose()
} catch (IOException e) {
TODO Autogenerated catch block
eprintStackTrace()
}
}
关键字显示
public static void showKey(String key){
List
List
for (StringPosition stringPosition sublist) {
stringPositionshowInfor()
}
}
public static void main(String[] args) {
TODO Autogenerated method stub
showKey(main)
showKey(return)
showKey(int)
}
}
五 程序运行结果
六 实验总结(包括实验心实验中问题解决办法)
文档香网(httpswwwxiangdangnet)户传
《香当网》用户分享的内容,不代表《香当网》观点或立场,请自行判断内容的真实性和可靠性!
该内容是文档的文本内容,更好的格式请下载文档