版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、软件体系结构 上机实验报告书 中国石油大学(北京)信息学院 计算机科学与技术系 制 订 人:周新 学 号: 2008082207 指导教师:朱雪峰 博士 2011 年 10 月 27 日 1、课程实验目的 通过 KWIC(Key Word in Context )检索系统,对软件体系结构有更加深入 的了解和认识。通过运用几种体系结构, 熟悉常见的软件体系结构, 并对这几种 结构有进一步了解。 2、任务概述 用管道过滤器,主程序、子程序,抽象数据类型,隐式调用这四种结构来分 别实现KWIC佥索系统。 3、实现方法 用C+主要实现了两种结构:主程序、子程序结构,抽象数据类型。 (1)KWIC1X程
2、的入口函数 int _tmain(int argc, _TCHAR* argv) / 界面,结构选择 coutfilename; coutChoose KWICfunctionendl1 is Main Program/Subroutine with Shared Dataendl2 is Abstract Data Typeschoose; if(1=choose)/ 主程序和子程序 MainSubroutine mainSub; vectorvector lines=mainSub.readLines(filename); vector lineIndex=mainSub.shiftStr
3、ing(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
4、); 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.getlin
5、e(temp,100) int k=0,j=0; vector line; line.swap(vector(); char s20=;/ 存储从行中取字符串的临时变量 while(tempk!=0) / 每个单词的截取条件为下一个字符为空格或者为行的末尾 if(tempk+1= |tempk+1=0) sj+1=0; string ss=s; line.push_back(ss); j=0; else if(tempk= ) j=0; else sj=tempk; j+; k+; dst 中 dst.push_back(line);/ 将从文件中中读出的行加入到 infile.close()
6、; 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();/ 获取每一行的字符串个数 colnum /对第row行循环colnum
7、 (字符串个数)次,生成循环移位后的 行 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 MainSub
8、routine: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);
9、/ 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 colPos=col; min=firstCharrowcol; firstCharr
10、owPoscolPos= ; int linePos=0;/ 在原行矩阵中的位置 for(int line=0;linerowPos;line+) linePos+=srcLinesline.size(); linePos=linePos+colPos; dstIndex.push_back(lineIndexlinePos); return dstIndex; / 按照 lineIndex 中的字符串的行标和列标输出所有字符串 lineIndex, void MainSubroutine:outPut(vector vectorvector srcLines) for(int row=0;r
11、owlineIndex.size();row+) for(int col=0;collineIndexrow.colIndex.size();col+) coutsrcLineslineIndexrow.rowIndexlineIndexrow.colIndexcol II J coutendl; coutendl; ( 3)抽象数据类型结构实现 行存储类 / 增加行 / 参数: line 字符串向量列表 void Lines:addLine(vector line) lines.push_back(line); / 从存储结构中获取某一行 / 参数: lineIndex 为获取的行的序号,从
12、 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 Line
13、s:getString(int lineIndex,int stringIndex) return lineslineIndexstringIndex; / 增加字符 / 参数: inchar 为增加的字符, stringIndex 为增加的字符所在的字符串的序号, lineIndex 为增加的字符所在的行的序号 void Lines:addChar(char inchar, int stringIndex, int lineIndex) lineslineIndexstringIndex.push_back(inchar); / 获取字符 / 参数: lineIndex 为行序号, stri
14、ngIndex 为字符串的序号, 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(); voi
15、d Input:readLines(char* filename, Lines 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=;/ 存储从行中取字符串
16、的临时变量 int stringIndex=0;/ 字符串序号 readLines.addString(s,lineIndex); while(tempk!=0) if(tempk!= ) readLines.addChar(tempk,stringIndex,lineIndex); / 每个单词的截取条件为下一个字符为空格或者为行的末尾 if(tempk= / 字符串加入到指定行的指定字符串中 / readLines.addChar(0,stringIndex,lineIndex); s0=0;/ 清空字符串 readLines.addString(s,lineIndex); stringI
17、ndex+;/ 字符串序号加 1 k+; lineIndex+; dst 中 / dst.push_back(line);/ 将从文件中中读出的行加入到 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 lineI
18、ndex=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
19、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,
20、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
21、 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 rowIndex=row; firs
22、tCharrowIndex= ;/ 将找到的最小的字母置为 ,以便在下 charSort.push_back(rowIndex); 一次查找时不再保留 / 首字母排序 vector FirstAlphaSort:getCharSort() return charSort; / 获取行数 int FirstAlphaSort:getLineNum(void) return shiftLines.getLineNum(); / 按行的序号,将各字符串合并成一个字符串,然后获取一行 /lineIndex 为行序号 string FirstAlphaSort:getLineAsString(int l
23、ineIndex) 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)主程序、子程序运行结果 1n
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年 重庆两江新区人才发展集团有限公司招聘考试笔试试卷附答案
- 跨境代理风险管控-洞察与解读
- 售后服务质量与反馈改进方案模板
- 初级社会工作者考试《社会工作综合能力》考试试卷含答案
- 专题《汉字》公开课(简案)
- 品牌推广策略执行计划表线上线下一体化型
- 水工环地质工程技术人员岗位资质试卷与答案
- 雨中的回忆描写一场雨天的经历13篇范文
- 高压试验工技术考核试卷及答案
- 2025年加油站培训考试题及答案
- 土地管理法-课件
- 出库登记表格
- 托管中心消防应急预案
- 建筑变形分析st1165使用手册
- GB/T 25067-2020信息技术安全技术信息安全管理体系审核和认证机构要求
- GB/T 17473.3-2008微电子技术用贵金属浆料测试方法方阻测定
- 常用钢材热处理工艺参数
- 市场监督管理局结构化面试真题及答案
- 二次函数平行四边行存在性问题课件
- 经典16PF分量表内容
- 律师职业访谈
评论
0/150
提交评论