头文件:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <time.h>
#include <windows.h>
using namespace std;
class dataClass {
public:
string former;
string latter[50] = { "\0" };
int latterCount = 0;
};
//文件读入
string streamIn(int &countArray, int former_len, int latter_len, dataClass*);
//对读入的文件进行前后缀分析
void streamAnalyse(string fullText, int &countArray, int former_len, int latter_len, dataClass*);
//输出文章
void streamOut(string fullText, int &countArray, int former_len, int latter_len, dataClass*);
C++_Big_1.cpp
//main函数
#include "Head.h"
//void test(dataClass*);
const int NUM_DATA = 700;
int main() {
#pragma comment(linker, "/STACK:873741824")
string fullText;
dataClass dataArray[NUM_DATA];
int var = 0;
int countArray;
countArray = 0;
int former_len, latter_len; //前缀词长度和后缀词长度
cout << "请输入前缀长度:" << endl;
cin >> former_len;
cout << "请输入后缀长度:" << endl;
cin >> latter_len;
fullText = streamIn(countArray, former_len, latter_len, dataArray);
streamOut(fullText, countArray, former_len, latter_len, dataArray);
cout << endl;
//test(dataArray); //用于测试内存极限
//cout << dataArray[0].former;
return 0;
}
stream.cpp
#include "Head.h"
/*
重写输入和分析函数
1.读入文件,存储在一个string中
2.对该string对象进行分析
3.按用户给定的前缀长度和后缀长度进行读入
3.1循环开始后前缀按上一个后缀赋值
3.2遇到"\n"时后缀停止并记录后缀指针长度用于前缀指针前进
*/
void streamAnalyse(string fullText, int& countArray, int former_len, int latter_len, dataClass* dataArray) {
int textPoint = 0, loopCount, pointLength = 2 * former_len, loopCountCopy;
const char* char_fullText;
char temp[1000] = "\0";
char_fullText = fullText.c_str(); //文件存在一个string中
string tempFormer, tempLatter;
do {
//前缀检查
for (loopCount = 0; loopCount < pointLength; loopCount++)
temp[loopCount] = char_fullText[textPoint + loopCount];
temp[loopCount] = '\0';
tempFormer = temp;
//后缀检查
loopCountCopy = loopCount;
for (loopCount = 0; loopCount < 2 * latter_len && char_fullText[textPoint + loopCountCopy + loopCount] != ' '; loopCount++) {
temp[loopCount] = char_fullText[textPoint + loopCountCopy + loopCount];
}
loopCountCopy = loopCount;
for (loopCount = 0; loopCount < loopCountCopy; loopCount++)
temp[loopCount] = char_fullText[textPoint + loopCountCopy + loopCount];
temp[loopCount] = '\0';
tempLatter = temp;
//指针检查
for (loopCount = 0; loopCount < 2 * former_len && char_fullText[textPoint + loopCount] != ' '; loopCount++) {}
if (loopCount == 2 * former_len)
pointLength = 2 * former_len;
else if (loopCount == 0) {
textPoint += 1;
pointLength = 2 * former_len;
}
else
pointLength = loopCount;
//前缀查重
for (loopCount = 0; loopCount < countArray && dataArray[loopCount].former != tempFormer; loopCount++) {}
if (loopCount == countArray) {
dataArray[countArray].former = tempFormer;
dataArray[countArray].latter[0] = tempLatter;
dataArray[countArray].latterCount++;
countArray++;
}
else {
if (dataArray[loopCount].latterCount > 49) {
textPoint += pointLength;
continue;
}
dataArray[loopCount].latter[dataArray[loopCount].latterCount] = tempLatter;
dataArray[countArray].latterCount++;
}
textPoint += pointLength;
} while ((textPoint + former_len) < fullText.length());
}
/*
重写输出函数_2
0.初始化随机数
1.进入循环
1.1取随机数
1.2按顺序遍历所有前缀,随机输出其后缀
1.3到达指定字数后停止
*/
void streamOut(string fullText, int& countArray, int former_len, int latter_len, dataClass* dataArray) {
srand(time(0));
int randNum, loopCount, textPoint = 0, pointLength, count = 0;
const char* char_fullText;
char temp[1000] = "\0";
char_fullText = fullText.c_str();
string start, temp_string;
cout << "请输入启动词(如:春天等):" << endl;
cin >> start;
cout << " " << start;
for (loopCount = 0; dataArray[loopCount].former != start && loopCount < countArray; loopCount++) {}
if (loopCount == countArray) {
cout << "语料库中不存在该启动词!";
return;
}
else {
randNum = rand() % dataArray[loopCount].latterCount;
cout << dataArray[loopCount].latter[randNum];
}
do {
//指针检查
for (loopCount = 0; loopCount < 2 * former_len && char_fullText[textPoint + loopCount] != ' '; loopCount++) {}
if (loopCount == 2 * former_len)
pointLength = 2 * former_len;
else if (loopCount == 0) {
textPoint += 1;
pointLength = 2 * former_len;
}
else
pointLength = loopCount + 1;
for (loopCount = 0; loopCount < pointLength; loopCount++)
temp[loopCount] = char_fullText[textPoint + loopCount];
temp[loopCount] = '\0';
for (loopCount = 0; dataArray[loopCount].former != temp && loopCount < countArray; loopCount++) {}
if (loopCount == countArray) {
loopCount = rand() % countArray;
randNum = rand() % dataArray[loopCount].latterCount;
cout << dataArray[loopCount].latter[randNum];
}
else {
randNum = rand() % dataArray[loopCount].latterCount;
cout << dataArray[loopCount].latter[randNum];
}
textPoint += pointLength;
count++;
Sleep(12);
}while ((textPoint + former_len) <= fullText.length()-2);
}
string streamIn(int& countArray, int former_len, int latter_len, dataClass* dataArray) {
string fullText;
const char* char_fullText;
ifstream infile;
//读入并分析
infile.open("D:\\spring_data_in\\1.txt", ios::in);
infile >> fullText;
char_fullText = fullText.c_str();
streamAnalyse(fullText, countArray, former_len, latter_len, dataArray);
infile.close();
return fullText;
}
?
|