Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.7 kB
0
Indexable
Never
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int PEREVOROT(int a) {
	int h = a;
	int j = 1;
	int f = 0;
	int result = 0;
	while (h >= 10) {
		h /= 10;
		j += 1;
	}
	h = a;
	for (int i = 0; i < j; i++) {
		f = h % 10;
		h = h / 10;
		result = result * 10;
		result = result + f;
	}
	return result;
}
string Cesar(string str,int sdvig){
	string result = str;
	for (int i = 0; i < str.size(); i++){
		if (result[i] != ' ') {
			if (int(result[i]) >= 65 && int(result[i]) <= 90) {
				result[i] = char(int(result[i]) + sdvig);
				while (int(result[i]) > 90) result[i] = char(int(result[i]) - 26);
				while (int(result[i]) < 65) result[i] = char(int(result[i]) + 26);
			}
			else {
				result[i] = char(int(result[i]) + sdvig);
				while (int(result[i]) > 122) result[i] = char(int(result[i]) - 26);
				while (int(result[i]) < 97) result[i] = char(int(result[i]) + 26);
			}
		}
	}
	return result;
}
string SHKILA(string str) {
	string result = str;
	for (int i = 0; i < str.size(); i++) {
		if (result[i] != ' ') {
			if (int(result[i]) >= 65 && int(result[i]) <= 90) {
				result[i] = char(PEREVOROT(result[i]));
				while (int(result[i]) > 90) result[i] = char(int(result[i]) - 26);
				while (int(result[i]) < 65) result[i] = char(int(result[i]) + 26);
			}
			else {
				result[i] = char(PEREVOROT(result[i]));
				while (int(result[i]) > 122) result[i] = char(int(result[i]) - 26);
				while (int(result[i]) < 97) result[i] = char(int(result[i]) + 26);
			}
		}
	}
	return result;
}
string TRANS(string str, int sdvig) {
	string result = str;
	char tmp;
	for (int i = 0; i < str.size()-sdvig+1; i+=sdvig){
		tmp = result[i];
		result[i] = result[i + sdvig - 1];
		result[i + sdvig - 1] = tmp;
	}
	return result;
}
string UnCesar(string str, int offset) {
	string result = str;
	for (int i = 0; i < str.size(); i++){
		if (result[i] != ' ') {
			if (int(result[i]) >= 65 && int(result[i]) <= 90) {
				result[i] -= offset;
				while (int(result[i]) > 90) result[i] = char(int(result[i]) - 26);
				while (int(result[i]) < 65) result[i] = char(int(result[i]) + 26);
			}
			else {
				result[i] -= offset;
				while (int(result[i]) > 122) result[i] = char(int(result[i]) - 26);
				while (int(result[i]) < 97) result[i] = char(int(result[i]) + 26);
			}
		}
	}
	return result;
}
int main() {
	ifstream in("input.txt");
	ofstream out("result.txt");
	string a;
	cout << "You want encryption or unencryption text? (1 or 2)" << '\n';
	int g;
	cin >> g;
	int sdvig;
	if (g == 1) {
		cout << "Choose encryption method:" << '\n' << "Cesar method [1]" << '\n' << "Transportation method [2]" << '\n' << "SHKILA method [3]" << '\n';
		int choose;
		cin >> choose;
		if (choose == 1) {
			cout << "Input offset" << '\n';
			cin >> sdvig;
			while (getline(in, a)) {
				out << Cesar(a, sdvig) << '\n';
			}
		}
		if (choose == 2) {
			cout << "Input offset" << '\n';
			cin >> sdvig;
			while (getline(in, a)) {
				out << TRANS(a, sdvig) << '\n';
			}
		}
		if (choose == 3) {
			while (getline(in, a)) {
				out << SHKILA(a) << '\n';
			}
		}
	}
	else if (g == 2) {
		cout << "Choose unencryption method:" << '\n' << "Cesar method [1]" << '\n' << "Transportation method [2]" << '\n';
		int choose;
		cin >> choose;
		if (choose == 1) {
			cout << "Input offset" << '\n';
			cin >> sdvig;
			while (getline(in, a)) {
				out << UnCesar(a,sdvig) << '\n';
			}
		}
		if (choose == 2) {
			cout << "Input offset" << '\n';
			cin >> sdvig;
			while (getline(in, a)) {
				out << TRANS(a, sdvig) << '\n';
			}
		}
	}
}