KWIC实验报告(C++).doc

上传人:w****2 文档编号:6522775 上传时间:2020-02-28 格式:DOC 页数:21 大小:124KB
返回 下载 相关 举报
KWIC实验报告(C++).doc_第1页
第1页 / 共21页
KWIC实验报告(C++).doc_第2页
第2页 / 共21页
KWIC实验报告(C++).doc_第3页
第3页 / 共21页
点击查看更多>>
资源描述
软件体系结构上机实验报告书中国石油大学(北京)信息学院计算机科学与技术系制 订 人:周新学 号:2008082207指导教师:朱雪峰 博士2011年10月27日 1、课程实验目的通过KWIC(Key Word in Context)检索系统,对软件体系结构有更加深入的了解和认识。通过运用几种体系结构,熟悉常见的软件体系结构,并对这几种结构有进一步了解。2、任务概述用管道过滤器,主程序、子程序,抽象数据类型,隐式调用这四种结构来分别实现KWIC检索系统。3、实现方法用C+主要实现了两种结构:主程序、子程序结构,抽象数据类型。(1)KWIC1工程的入口函数int _tmain(int argc, _TCHAR* argv)/界面,结构选择coutfilename;coutChoose KWIC functionendl1 is Main Program/Subroutine with Shared Dataendl2 is Abstract Data Typeschoose;if(1=choose)/主程序和子程序MainSubroutine mainSub;vectorvector lines=mainSub.readLines(filename);vector lineIndex=mainSub.shiftString(lines);lineIndex=mainSub.firstAlphaSort(lineIndex,lines);mainSub.outPut(lineIndex,lines);else/抽象收据类型Lines *lines=new Lines;Input input;Shift *shift=new Shift;FirstAlphaSort alphaSort;Output output;input.readLines(filename,*lines);shift-circularShift(*lines);alphaSort.alphaSort(*shift);output.print(alphaSort);delete shift;delete lines;return 0;(2)主程序、子程序结构实现类/从文件中按行读取vectorvector MainSubroutine:readLines(char* filename)vectorvector dst;ifstream infile(filename,ios:in);if(!infile)coutopen error!endl;exit(0);char temp100=;/存储从文件中读取的行的临时变量/按行读取while(infile.getline(temp,100)int k=0,j=0;vector line;line.swap(vector();char s20=;/存储从行中取字符串的临时变量while(tempk!=0)/每个单词的截取条件为下一个字符为空格或者为行的末尾if(tempk+1= |tempk+1=0)&tempk!= )sj=tempk;sj+1=0;string ss=s;line.push_back(ss);j=0;else if(tempk= )j=0;elsesj=tempk;j+; k+;dst.push_back(line);/将从文件中中读出的行加入到dst中infile.close();for(int i=0;idst.size();i+)for(int j=0;jdst.at(i).size();j+)coutdstij ;coutendl;coutendlendl;return dst;/循环左移vector MainSubroutine:shiftString(vectorvector srcLines) vector shiftLines; for(int row=0;rowsrcLines.size();row+) int colnum=srcLinesrow.size();/获取每一行的字符串个数 /对第row行循环colnum(字符串个数)次,生成循环移位后的colnum行 for(int col=0;colcolnum;col+) LINE linePos;/声明存放一行的行标以及各列的列表的结构体 linePos.rowIndex=row;/给行标赋值 /循环生成一行的列标 for(int colshift=0;colshiftcolnum;colshift+) linePos.colIndex.push_back(col+colshift)%colnum);/取模运算 shiftLines.push_back(linePos); return shiftLines;/字母排序vector MainSubroutine:firstAlphaSort(vector lineIndex,vectorvector srcLines)vectorvector firstChar;vector dstIndex;for(int row=0;rowsrcLines.size();row+)vector firstCharLine;/逐行提取各个字符串的首字母for(int col=0;colsrcLinesrow.size();col+)firstCharLine.push_back(srcLinesrowcol0);firstChar.push_back(firstCharLine);/int rowPos=0;int colPos=0;/外面的两层for循环是控制循环次数的/内部的两层for循环是遍历所有的字符串首字母,寻找最小的字母for(int row=0;rowfirstChar.size();row+)for(int col=0;colfirstCharrow.size();col+)char min=z;for(int row=0;rowfirstChar.size();row+)for(int col=0;col=firstCharrowcol& !=firstCharrowcol)rowPos=row;colPos=col;min=firstCharrowcol;firstCharrowPoscolPos= ;int linePos=0;/在原行矩阵中的位置for(int line=0;linerowPos;line+)linePos+=srcLinesline.size();linePos=linePos+colPos;dstIndex.push_back(lineIndexlinePos); return dstIndex;/按照lineIndex中的字符串的行标和列标输出所有字符串void MainSubroutine:outPut(vector lineIndex, vectorvector srcLines)for(int row=0;rowlineIndex.size();row+)for(int col=0;collineIndexrow.colIndex.size();col+)coutsrcLineslineIndexrow.rowIndexlineIndexrow.colIndexcol ;coutendl;coutendl;(3)抽象数据类型结构实现行存储类/增加行/参数:line字符串向量列表void Lines:addLine(vector line)lines.push_back(line);/从存储结构中获取某一行/参数:lineIndex为获取的行的序号,从0开始/返回获取的行vector Lines:getLine(int lineIndex)return lineslineIndex;/ 增加字符串/参数:instring为所添加字符串,lineIndex为字符串所在行的序号(从0开始)void Lines:addString(string instring, int lineIndex)lineslineIndex.push_back(instring);/获取字符串/参数:lineIndex为行序号,stringIndex为字符串在行中的序号/返回获取的字符串string Lines:getString(int lineIndex,int stringIndex)return lineslineIndexstringIndex;/增加字符/参数:inchar为增加的字符,stringIndex为增加的字符所在的字符串的序号,lineIndex为增加的字符所在的行的序号void Lines:addChar(char inchar, int stringIndex, int lineIndex)lineslineIndexstringIndex.push_back(inchar);/获取字符/参数:lineIndex为行序号,stringIndex为字符串的序号,charIndex为字符的序号/返回获取的字符char Lines:getChar(int lineIndex, int stringIndex, int charIndex)return lineslineIndexstringIndexcharIndex;/获取总的行数int Lines:getLineNum(void)return lines.size();/获取特定行的字符串个数int Lines:getStringNum(int lineIndex)return lineslineIndex.size();输入类void Input:readLines(char* filename, Lines &readLines)/vectorvector dst;ifstream infile(filename,ios:in);if(!infile)coutopen error!endl;exit(0);char temp100=;/存储从文件中读取的行的临时变量int lineIndex=0;/行序号/按行读取while(infile.getline(temp,100)int k=0,j=0;vector line;line.swap(vector();readLines.addLine(line);char s20=;/存储从行中取字符串的临时变量int stringIndex=0;/字符串序号readLines.addString(s,lineIndex);while(tempk!=0)if(tempk!= )readLines.addChar(tempk,stringIndex,lineIndex);/每个单词的截取条件为下一个字符为空格或者为行的末尾if(tempk= &tempk+1!= )/readLines.addChar(tempk,stringIndex,lineIndex);/将该字符串加入到指定行的指定字符串中/readLines.addChar(0,stringIndex,lineIndex);s0=0;/清空字符串readLines.addString(s,lineIndex);stringIndex+;/字符串序号加1 k+;lineIndex+;/dst.push_back(line);/将从文件中中读出的行加入到dst中infile.close();for(int i=0;ireadLines.getLineNum();i+)for(int j=0;jreadLines.getStringNum(i);j+)coutreadLines.getString(i,j) ;coutendl;coutendlendl;循环左移类/实现所有行的循环移位void Shift:circularShift(Lines srcLines)int lineIndex=0;for(int row=0;rowsrcLines.getLineNum();row+)int cols=srcLines.getStringNum(row);for(int col=0;colcols;col+)vector newLine;lineShift.addLine(newLine);for(int newcol=0;newcolcols;newcol+)lineShift.addString(srcLines.getString(row,(col+newcol)%cols),lineIndex);lineIndex+;/获取所有行Lines Shift:getLines(void)return lineShift;/获取某一行vector Shift:getLine(int lineIndex)return lineShift.getLine(lineIndex);/获取某一行中某一位置的字符串string Shift:getString(int lineIndex,int stringIndex)return lineShift.getString(lineIndex,stringIndex);/获取某一行中某一个字符串中的某一位置的字符char Shift:getChar(int lineIndex, int stringIndex, int charIndex)return lineShift.getChar(lineIndex,stringIndex,charIndex);/获取行数int Shift:getLineNum(void)return lineShift.getLineNum();/获取某一行的字符串个数int Shift:getStringNum(int lineIndex)return lineShift.getStringNum(lineIndex);排序类/实现按首字母排序void FirstAlphaSort:alphaSort(Shift srcShiftLines)shiftLines=srcShiftLines;/将传进得Shift对象赋值给成员变量vector firstChar;for(int row=0;rowshiftLines.getLineNum();row+)firstChar.push_back(shiftLines.getChar(row,0,0);/获取首字母/首字母排序for(int loop=0;loopfirstChar.size();loop+)char min=z;int rowIndex=0;for(int row=0;row=firstCharrow& !=firstCharrow)min=firstCharrow;rowIndex=row;charSort.push_back(rowIndex);firstCharrowIndex= ;/将找到的最小的字母置为 ,以便在下一次查找时不再保留/首字母排序vector FirstAlphaSort:getCharSort()return charSort;/获取行数int FirstAlphaSort:getLineNum(void)return shiftLines.getLineNum();/按行的序号,将各字符串合并成一个字符串,然后获取一行/lineIndex为行序号string FirstAlphaSort:getLineAsString(int lineIndex)string lineString;for(int strCount=0;strCountshiftLines.getStringNum(lineIndex);strCount+)lineString+=shiftLines.getString(lineIndex,strCount)+ ;lineString+=0;return lineString;输出类/按字母表顺序输出void Output:print(FirstAlphaSort sortLines) for(int row=0;rowsortLines.getLineNum();row+) coutsortLines.getLineAsString(sortLines.getCharSort()row)endl; coutendl;4、实验的例程(1)主程序、子程序运行结果1选择的实现结构为主程序、子程序(2)抽象收据类型2选择的实现结构为抽象数据类型5、总结通过本次实验,首先对软件体系结构有了更真切的了解,尤其是对管道过滤器结构,主程序、子程序结构,抽象数据类型结构,隐式调用这四种结构的理解更加透彻了。同时还对KWIC有个一个认知的过程。在实现的上述两种结构的过程中,对增强了数据结构的掌握,以及对面向对象概念的进一步理解。编程时,遇到了各种大大小小的问题,尤其是数据结构定义方面的,深刻体会到了数据结构的重要性,同时发现了自己编写的代码移植性以及编码风格都不是很好,希望老师给予指点。
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 管理文书 > 工作总结


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!