1、2 is Abstract Data Typesendl; int choose=0;choose; if(1=choose)/主程序和子程序 MainSubroutine mainSub; vectorvector lines=mainSub.readLines(filename);LINE lineIndex=mainSub.shiftString(lines); lineIndex=mainSub.firstAlphaSort(lineIndex,lines); mainSub.outPut(lineIndex,lines); else/抽象收据类型 Lines *lines=new L
2、ines; 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)主程序、子程序结构实现类/从文件中按行读取 MainSubroutine:readLines(char* filena
3、me) vector dst; ifstream infile(filename,ios:in); if(!infile) coutopen error! exit(0); char temp100=/存储从文件中读取的行的临时变量 /按行读取 while(infile.getline(temp,100) int k=0,j=0; line; line.swap(vector(); char s20=/存储从行中取字符串的临时变量 while(tempk!=0) /每个单词的截取条件为下一个字符为空格或者为行的末尾 if(tempk+1= |tempk+1=0)&tempk!=) sj=tem
4、pk; sj+1=0 string ss=s; line.push_back(ss); j=0; else if(tempk= else 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 return dst;/循环左移shiftString(vector srcLines) shiftLines; for(int row=0;rowsrcLines.size();row+)
5、int colnum=srcLinesrow.size();/获取每一行的字符串个数 /对第row行循环colnum(字符串个数)次,生成循环移位后的colnum行 for(int col=0;colcolnum;col+) LINE linePos;/声明存放一行的行标以及各列的列表的结构体 linePos.rowIndex=row;/给行标赋值 /循环生成一行的列标 for(int colshift=0;colshiftcolshift+) linePos.colIndex.push_back(col+colshift)%colnum);/取模运算 shiftLines.push_back
6、(linePos); return shiftLines;/字母排序firstAlphaSort(vector lineIndex,vector firstChar; dstIndex; firstCharLine; /逐行提取各个字符串的首字母srcLinesrow.size(); firstCharLine.push_back(srcLinesrowcol0); firstChar.push_back(firstCharLine);/ int rowPos=0; int colPos=0; /外面的两层for循环是控制循环次数的 /内部的两层for循环是遍历所有的字符串首字母,寻找最小的字
7、母firstChar.size();firstCharrow.size(); char min=z for(int row=0; for(int col=0; if(min=firstCharrowcol&!=firstCharrowcol) rowPos=row; colPos=col; min=firstCharrowcol; firstCharrowPoscolPos= int linePos=0;/在原行矩阵中的位置 for(int line=0;linerowPos;line+) linePos+=srcLinesline.size(); linePos=linePos+colPos
8、; dstIndex.push_back(lineIndexlinePos); return dstIndex;/按照lineIndex中的字符串的行标和列标输出所有字符串void MainSubroutine:outPut(vector lineIndex, vectorlineIndex.size();lineIndexrow.colIndex.size();srcLineslineIndexrow.rowIndexlineIndexrow.colIndexcol(3)抽象数据类型结构实现行存储类/增加行/参数:line字符串向量列表void Lines:addLine(vector li
9、ne) lines.push_back(line);/从存储结构中获取某一行lineIndex为获取的行的序号,从0开始/返回获取的行 Lines:getLine(int lineIndex) return lineslineIndex;/ 增加字符串instring为所添加字符串,lineIndex为字符串所在行的序号(从0开始)addString(string instring, int lineIndex) lineslineIndex.push_back(instring);/获取字符串lineIndex为行序号,stringIndex为字符串在行中的序号/返回获取的字符串string
10、 Lines:getString(int lineIndex,int stringIndex) return lineslineIndexstringIndex;/增加字符inchar为增加的字符,stringIndex为增加的字符所在的字符串的序号,lineIndex为增加的字符所在的行的序号addChar(char inchar, int stringIndex, int lineIndex) lineslineIndexstringIndex.push_back(inchar);/获取字符lineIndex为行序号,stringIndex为字符串的序号,charIndex为字符的序号/返
11、回获取的字符char Lines:getChar(int lineIndex, int stringIndex, int charIndex) return lineslineIndexstringIndexcharIndex;/获取总的行数int Lines:getLineNum(void) return lines.size();/获取特定行的字符串个数getStringNum(int lineIndex) return lineslineIndex.size();输入类void Input:readLines(char* filename, Lines &readLines)/ vect
12、or int lineIndex=0;/行序号 readLines.addLine(line); int stringIndex=0;/字符串序号 readLines.addString(s,lineIndex); if(tempk! readLines.addChar(tempk,stringIndex,lineIndex); if(tempk=tempk+1! / readLines.addChar(tempk,stringIndex,lineIndex);/将该字符串加入到指定行的指定字符串中 / readLines.addChar(,stringIndex,lineIndex); s0
13、=/清空字符串 readLines.addString(s,lineIndex); stringIndex+;/字符串序号加1 lineIndex+; / dst.push_back(line);readLines.getLineNum();readLines.getStringNum(i);readLines.getString(i,j)循环左移类/实现所有行的循环移位void Shift:circularShift(Lines srcLines)srcLines.getLineNum(); int cols=srcLines.getStringNum(row);cols; vector n
14、ewLine; lineShift.addLine(newLine); for(int newcol=0;newcolnewcol+) lineShift.addString(srcLines.getString(row,(col+newcol)%cols),lineIndex); lineIndex+;/获取所有行Lines Shift:getLines(void) return lineShift;/获取某一行 Shift: return lineShift.getLine(lineIndex);/获取某一行中某一位置的字符串string Shift: return lineShift.g
15、etString(lineIndex,stringIndex);/获取某一行中某一个字符串中的某一位置的字符char Shift: return lineShift.getChar(lineIndex,stringIndex,charIndex);/获取行数int Shift: return lineShift.getLineNum();/获取某一行的字符串个数 return lineShift.getStringNum(lineIndex);排序类/实现按首字母排序void FirstAlphaSort:alphaSort(Shift srcShiftLines) shiftLines=sr
16、cShiftLines;/将传进得Shift对象赋值给成员变量shiftLines.getLineNum(); firstChar.push_back(shiftLines.getChar(row,0,0);/获取首字母 /首字母排序 for(int loop=0;loop=firstCharrow&=firstCharrow) min=firstCharrow; rowIndex=row; charSort.push_back(rowIndex); firstCharrowIndex=/将找到的最小的字母置为 ,以便在下一次查找时不再保留/首字母排序int FirstAlphaSort:ge
17、tCharSort() return charSort;int FirstAlphaSort: return shiftLines.getLineNum();/按行的序号,将各字符串合并成一个字符串,然后获取一行/lineIndex为行序号string FirstAlphaSort:getLineAsString(int lineIndex) string lineString; for(int strCount=0;strCountshiftLines.getStringNum(lineIndex);strCount+) lineString+=shiftLines.getString(li
18、neIndex,strCount)+ lineString+=0 return lineString;输出类/按字母表顺序输出void Output:print(FirstAlphaSort sortLines)sortLines.getLineNum();sortLines.getLineAsString(sortLines.getCharSort()row)4、实验的例程(1)主程序、子程序运行结果(2)抽象收据类型5、总结通过本次实验,首先对软件体系结构有了更真切的了解,尤其是对管道过滤器结构,主程序、子程序结构,抽象数据类型结构,隐式调用这四种结构的理解更加透彻了。同时还对KWIC有个一个认知的过程。在实现的上述两种结构的过程中,对增强了数据结构的掌握,以及对面向对象概念的进一步理解。编程时,遇到了各种大大小小的问题,尤其是数据结构定义方面的,深刻体会到了数据结构的重要性,同时发现了自己编写的代码移植性以及编码风格都不是很好,希望老师给予指点。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1