#includeusing namespace std; string s,word_text, words; vector words_single; //存放要查找的单词 方便比较 vector v; vector v3; vector v4; int n,row,col,temp; string j = "%"; //解决最后一次出现的位置为0的问题 int find_word(int col) { words_single.insert(words_single.end(),j); for(int i=col;i 11) { row = col/11+1; temp_col = col%11; } printf("它在文件output中第%d次出现的位置位于第%d行第%d列n",n,row,temp_col); words_single.clear(); find_word(col); } else { return 0; } } int main() { int flag=0; bool sign = 0; printf("请输入用户ID号:"); cin >> s; vector v2; vector word_num; int num; //去重后单词的个数 int sub_num; //检查ID号格式是否正确 两个大写字母和四个数字 regex pattern("[A-Z][A-Z]\d{4}"); //正则 sign = std::regex_match(s,pattern); while(sign) { printf("请选择操作:n"); cout << "1 建立文件" << endl << "2 单词统计" < ,n 1234567890"; //遇到这些字符时不作处理 char *p; p = strtok(c,d); if(flag==2) //词频统计 { while(p) { v.push_back(p);//在容器尾部加入一个数据 p=strtok(NULL,d); } sort(v.begin(),v.end()); //先排序 后统计 for(int i=0;i > words; int cnt1 = count(v2.begin(),v2.end(), words); //判断目标单词是否存在 if(!cnt1) { cout << "查无此词"< 代码分析 万能头文件 全局变量声明 #includeusing namespace std; string s,word_text, words; vector words_single; //存放要查找的单词 方便比较 vector v; //用于存放初始输入经过规格化的文本 vector v3; //工具vector vector v4; //工具vector int n,row,col,temp; //n 第几次出现 row 行数 col 列数 temp工具人 string j = "%"; //解决最后一次出现的位置为0的问题 好的点是全局变量声明后,在整个程序段中都可以使用
然后,声明了四个vector数组 缺点就是占的空间比较大,优点的话是不会发生数据的错乱或者混淆
功能三的实现 实现文件中单词的定位查询int find_word(int col) //col 用于记录行数 { words_single.insert(words_single.end(),j); //j就是字符串"%" for(int i=col;i11) { row = col/11+1; temp_col = col%11; } printf("它在文件output中第%d次出现的位置位于第%d行第%d列n",n,row,temp_col); words_single.clear(); find_word(col); } else { return 0; } } 算法:切片,重新输入 然后定位查询
在words_single开始插入字符 “%”的原因是,占起址为0的空间,然后减的时候减去0刚好得到对应的位置,也就是单词对应的位置
col用作标志位,每次从col的下一个开始再次存入数组中,然后采用find对新的数组进行查找相关词,找到后,再次重找到位置的下一个切片存入数组中,再次遍历查询,直到没有找到目标词,然后退出
v3是在主函数中经过处理的 char 类型的数组,以一个单词为单位进行遍历
主函数中依次实现四个功能int main() { int flag=0; //标志用于指定进入哪个功能 bool sign = 0; // ID号是否符合格式的代码 printf("请输入用户ID号:"); cin >> s; vectorv2; //字符型数组v2 vector word_num; // int num; //去重后单词的个数 int sub_num; //功能2中查找对应单词出现次数的标志 //检查ID号格式是否正确 两个大写字母和四个数字 regex pattern("[A-Z][A-Z]\d{4}"); //正则 判断是否符合格式要求 sign = std::regex_match(s,pattern); while(sign) //当符合要求后进入 { printf("请选择操作:n"); cout << "1 建立文件" << endl << "2 单词统计" < ,n 1234567890"; //遇到这些字符时,单词统计不作处理 char *p; p = strtok(c,d); if(flag==2) //词频统计 { while(p) { //压入容器中便于遍历处理 v.push_back(p); //在容器尾部加入一个数据 p=strtok(NULL,d); } sort(v.begin(),v.end()); //先排序 后统计 按A-Z和a-z 的方式进行统计 for(int i=0;i > words; int cnt1 = count(v2.begin(),v2.end(), words); //判断目标单词是否存在 if(!cnt1) { cout << "查无此词"<



