Created
November 19, 2012 13:05
-
-
Save ernado/4110539 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <fstream> | |
| #include <stdlib.h> // не обязательно подключать | |
| #include <iomanip> | |
| using namespace std; | |
| void ZAPMASKEY(ifstream&, int*&, int&); | |
| void NEWDMASS(int *&, int&); | |
| int main () | |
| { | |
| //Для ключей | |
| ifstream FDK("fdk.txt", ios::in|ios::binary); | |
| if (!FDK) | |
| { | |
| cout << "Can not open." << endl; | |
| return -1; | |
| } | |
| int s1 = 50; // s1 изменяется в функции ZAPMASKEY | |
| int *maskey = new int [s1]; | |
| ZAPMASKEY (FDK, maskey, s1); | |
| cout << "Keys: " << s1 << endl; | |
| //Основная часть | |
| char w1, file1 [100], file2 [100]; | |
| int i=0; | |
| cout<<"Your input file: "; cin>>file1; | |
| cout<<"Your output file: "; cin>>file2; | |
| ofstream KF2(file2, ios::binary); | |
| ifstream KF1(file1, ios::binary); | |
| while (KF1) | |
| { | |
| KF1.get(w1); | |
| KF2.put(maskey[i%s1]^w1); | |
| i++; | |
| // убрал ненужное | |
| } | |
| char c; | |
| while (1) | |
| { | |
| cout<<"Do you want to see statistics? (y or n) "; cin>>c; | |
| if (c=='y') | |
| { | |
| KF1.clear(); KF1.seekg(0, ios::beg); | |
| int code, d, MASSS[256]; | |
| i = 0; | |
| for (d=0; d<256; d++) | |
| MASSS[d]=0; | |
| // он требует вводить по коду | |
| cout<<"symbol code: "; cin>>code; | |
| cout<<"symbol: " << char(code) << endl; | |
| while (KF1) | |
| { | |
| KF1.get(w1); | |
| if (w1==code) | |
| MASSS[maskey[i++%s1]^w1]++; | |
| } | |
| for (d=0; d<256; d++) | |
| { | |
| if (d%16==0) | |
| cout<<endl; | |
| cout<<setw(4)<<MASSS[d]; | |
| } | |
| cout << endl; | |
| } | |
| else break; | |
| } | |
| //system("pause"); | |
| return 0; | |
| } | |
| // т.к. мы изменяем указатель (увеличени массива), то тут тоже *& | |
| void ZAPMASKEY (ifstream &FDKF, int *&maskey, int& size) | |
| { | |
| int b; | |
| char w; | |
| int i=0; | |
| for (int j = 0; j<size; j++) | |
| maskey[j] = 0; | |
| while(FDKF) | |
| { | |
| FDKF.get(w); | |
| if (w!=' ' && w!='\n') | |
| maskey[i]+=w; | |
| else | |
| { | |
| maskey[i]%=256; | |
| if (w==' ') | |
| i++; | |
| } | |
| if ((i+1)>size) | |
| NEWDMASS(maskey, size); | |
| } | |
| // обработка последнего символа | |
| maskey[i]%=256; | |
| // размер больше индекса на 1, т.к. индексация с нуля | |
| size = i + 1; | |
| } | |
| void NEWDMASS (int *& maskey, int& s) | |
| { | |
| int *maskey2 = new int [s*2]; | |
| for (int i=0; i<s*2; i++) | |
| maskey2[i] = 0; | |
| for (int j=0; j<s; j++) | |
| maskey2[j]=maskey[j]; | |
| delete [] maskey; | |
| maskey=maskey2; | |
| s*=2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment