Вы находитесь на странице: 1из 4

// A little thing by xir1111011

// I'll be getting rid of the label/goto soon


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
void author(string);
void encrypt(string);
void decrypt(string);
string readLines(int, int, string);
int _tmain(int argc, _TCHAR* argv[])
{
int select;
int linect = 1;
string name;
a:
cout << "Name of log: ";
cin >> name;
cout << "Encrypting (1), Decrypting (2), or Authoring (3)?" << endl;
cin >> select;
while (select != 1 && select != 2 && select != 3)
{
cout << "Please choose 1, 2, or 3: ";
cin >> select;
}
if (select == 1)
{
encrypt(name);
cout << "Encryption Successful" << endl;
}
if (select == 2)
{
decrypt(name);
cout << "Decryption Successful" << endl;
}
if (select == 3)
author(name);
cout << "Run again?" << endl;
cin >> name;
if (name == "yes" || name == "Yes" || name == "y" || name == "Y")
goto a;
system("PAUSE");
cout << "Closing...";
return 0;
}
void author(string name)
{
string text;
int linect = 1;
ofstream stream;
stream.open((name + ".log").c_str());
cout << "Begin log..." << endl;
while (text != "*-*")
{
cin >> text;
if (text != "*-*")
stream << text << " ";
if (text == "`")
{
linect++;
stream << endl;
}
}
stream.close();
cout << "The number of lines in this document is: " << linect << endl;
return;
}
void encrypt(string name)
{
ofstream output;
string message;
int num;
char letter;
char letter2;
char elett;
char key;
output.open((name + "_enc.log").c_str());
cout << "How many lines of text exist in this log?" << endl;
cin >> num;
cout << "Please specify an encryption key-character: ";
cin >> key;
message = readLines(1, num, name);
int i;
int j;
i = 0;
j = message.length();
while (i < j)
{
letter = message.at(i);
if (i < (j - 1))
letter2 = message.at(i + 1);
if (i >= (j - 1))
letter2 = key;
elett = letter + letter2;
cout << elett;
output << elett;
i = i + 1;
}
cout << endl;
return;
}
void decrypt(string name)
{
ofstream output;
string message = "Error";
char letter;
char letter2;
char key;
char dlett;
cout << "Please specify the encryption key character: ";
cin >> key;
output.open((name + "_dec.log").c_str());
message = readLines(1, 1, name);
int i;
int j = message.length();
vector<char> decode(j);
i = j - 1;
while (i >= 0)
{
letter = message.at(i);
if (i == (j - 1))
letter2 = key;
if (i < (j - 1))
letter2 = decode[(i + 1)];
dlett = letter - letter2;
decode[i] = dlett;
i = i - 1;
}
for (int count = 0; count < j; count++)
{
if (decode[count] == '`')
{
cout << endl;
output << endl;
}
else
{
cout << decode[count];
output << decode[count];
}
}
output.close();
cout << endl;
return;
}
string readLines(int num, int count, string name)
{
ifstream infile;
string text;
string output;
output = "Error";
infile.open((name + ".log").c_str());
num = num - 1;
while (num > 0)
{
getline(infile, text);
if (infile.eof())
{
cout << "End of file reached! No Read" << endl;
output = "End of File, No Read";
return output;
}
num = num - 1;
}
output = "";
while (count > 0)
{
getline(infile, text);
output = output + text;
count = count - 1;
if (infile.eof() && count > 0)
{
cout << "End of file reached! Partial Read" << endl;
return output;
}
}
return output;
}

Вам также может понравиться