Created
October 16, 2012 18:31
-
-
Save LifeMoroz/3901099 to your computer and use it in GitHub Desktop.
Lab8
This file contains 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 <stdlib.h> // Для функции exit() | |
#include <fstream> // Для файловых потоков | |
#include <iostream> | |
#include "ConsolCyr.h" | |
#include <string> | |
using namespace std; | |
const int lenName = 13; // max длина имени файла | |
const int lenString = 60; // Длина вспомогательного массива | |
int sumS(char *chr); //подсчет суммы кодов | |
void main() | |
{ | |
char source[lenName]; // Массив для имени файла | |
cout << "\nВведите имя кодового файла: "; | |
cin >> source; | |
ifstream inFile; // Входной файловый поток | |
// Открыть файл source и связать его с потоком inFile: | |
inFile.open(source); | |
if (!inFile) // Проверить правильность открытия файла | |
{ cerr << "\nОшибка при открытии файла " << source; | |
exit(1); // Завершение программы | |
} | |
// Вспомогательный массив для чтения: | |
char string[lenString]; | |
char next; | |
cin.get(); // Убирает код из потока cin | |
int *ptr, j=0, max=1000; | |
ptr=new int[max]; | |
while(1) // Неограниченный цикл | |
{ | |
if (j==max) //расширяем массив ключей, если не хватает места | |
{ | |
int *ptr1; | |
ptr1=new int[max]; | |
for(int i=0;i<max;i++) | |
{ptr1[i]=ptr[i];} | |
delete[] ptr; | |
ptr=new int[max+1000]; | |
for(int i=0;i<max;i++) | |
{ptr[i]=ptr1[i];} | |
delete[] ptr1; | |
max+=1000; | |
} | |
for(int i=0; i<lenString; i++) // на всякий случай запонляем string пробелами, | |
//что бы было проще распознaвать конец слова. | |
{string[i]=' ';} | |
inFile >> string; //получаем слово | |
ptr[j]=sumS(string); //сумма кодов символов | |
next = inFile.peek(); | |
j++; | |
if (next == EOF) break; | |
} | |
inFile.close(); | |
ofstream onFile1("End_file.txt", ios::trunc | ios::binary); //открываем и усекаем фаил для конечного результата. | |
cout << "\nВведите имя кодируемого файла: "; | |
cin >> source; | |
ifstream inFile2(source, ios::binary); //поток на запись | |
int k=0; | |
char chr[1], ch; //вдруг пригодится | |
cout << "Идет шифрование..." << endl; | |
cout << "Какой символ исследуем?" << endl; | |
cin.setf (ios::binary); | |
cin >> ch; | |
cin.unsetf(ios::binary); | |
cout << ch << endl; | |
int charray[256]; | |
for (int i=0;i<256;i++) | |
{ | |
charray[i]=-200; | |
} | |
while(!inFile2.eof()) //шифрование | |
{ | |
if (k==j) k=0; // если ключей не хватает - начать с начала | |
inFile2.read(chr, 1); | |
cout << chr[0]<< endl; | |
if (ch==chr[0]) //сравнение не работает, т.к. один бинарный а другой нет. Хз че делать пока | |
charray[ch]=1; | |
if (inFile2.eof()) break; | |
//cout << "преобразую символ " << chr[0]; | |
chr[0]=(ptr[k]^chr[0]); | |
//cout << " Код равен " << ptr[k] << " получился символ " << chr[0] << endl; | |
onFile1.write(chr, 1); | |
k++; | |
} | |
j=0; | |
for (int i=0; i<256; i++) | |
{ | |
if (charray[i]==1) | |
{ | |
cout << char(i); | |
j++; | |
} | |
} | |
cout << "Всего разных кодов символа " <<j<< endl; | |
system("pause"); | |
} | |
int sumS(char *chr) //подсчет суммы кодов | |
{ | |
char result=0; | |
int i=0; | |
while (chr[i]!=' ') | |
{ | |
result+=chr[i]; | |
i++; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment