1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include "File.h"
using namespace std;
int parse2string(char *filename, string *&outp) {
int wCnt=0, elem=0, strChar=0;
ifstream inp(filename);
inp.seekg(0, ios::end);
istream::pos_type size = inp.tellg();
char *c = new char[size];
inp.seekg(0, ios::beg);
inp.read(c, size);
inp.close();
for (int i=0; i<size; i++)
if (c[i]=='\n')
wCnt++;
wCnt++;
outp = new string[wCnt];
for (int i=0; i<size; i++) {
if (c[i]=='\r')
i++;
if (c[i]=='\n') {
elem++;
strChar=0;
} else {
outp[elem] += c[i];
strChar++;
}
}
return wCnt;
}
int strCompare(string a, string b) {
for (int i=0; i<a.length(); i++)
a[i]=toupper(a[i]);
for (int i=0; i<b.length(); i++)
b[i]=toupper(b[i]);
if (a==b)
return 0;
if (a>b)
return 1;
if (a<b)
return -1;
}
void merge2file(char *inpFile1, char *inpFile2, char *outpFile) {
cout << "Merging files..." << endl;
string *first, *second;
int words1 = parse2string(inpFile1, first);
int words2 = parse2string(inpFile2, second);
ofstream outp(outpFile, ios::trunc);
int pos1=0, pos2=0;
while (pos1<words1 && pos2<words2) {
switch (strCompare(first[pos1], second[pos2])) {
case 1:
outp << second[pos2] << endl;
pos2++;
break;
case -1:
outp << first[pos1] << endl;
pos1++;
break;
case 0:
outp << first[pos1] << endl;
pos1++;
pos2++;
break;
}
}
if (pos1==words1) {
for (int i=pos2; i<words2; i++)
outp << second[i] << endl;
} else if (pos2==words2) {
for (int i=pos1; i<words1; i++)
outp << first[i] << endl;
}
delete [] first;
delete [] second;
outp.close();
}
void genInp(char * outpFile) {
cout << "Generating inp file..." << endl;
string * dict;
int wCnt = parse2string("eng_dict.txt", dict);
ofstream outp(outpFile, ios::trunc);
for (int i=0; i<wCnt; i++)
if(rand()%2)
outp << dict[i] << endl;
outp.close();
}
| |