Skip to content

Instantly share code, notes, and snippets.

@RicardoLara
Last active February 22, 2018 05:44
Show Gist options
  • Save RicardoLara/e9e37c3e2a82100d0c65547e3fc676c8 to your computer and use it in GitHub Desktop.
Save RicardoLara/e9e37c3e2a82100d0c65547e3fc676c8 to your computer and use it in GitHub Desktop.
Sds_P1_02 v0.2
#include <bits/stdc++.h>
using namespace std;
map <char,char> Mi,Ci;
map <char,char>::iterator it;
int main(){
string palabra,ended;
int u = 0, keyi = 65;
cout << "Hey bruh! Need a word: ";
cin >> palabra;
for(int i=0; i<palabra.length(); i++){
cout << "Palabra(" << i << ") => " << palabra[i] << endl;
it = Ci.find(toupper(palabra[i]));
if(it == Ci.end())
Ci[toupper(palabra[i])] = keyi++;
else u++;
}
keyi = 65 + palabra.length() - u;
for(int i=0; i<26; i++){
it = Ci.find(i+65);
if(it == Ci.end())
Ci[i+65] = keyi++;
}
for(it = Ci.begin(); it!=Ci.end(); it++)
Mi[it->second] = it->first;
for(it = Mi.begin(); it!=Mi.end(); it++)
cout << "First: " << it->first << " | Second: " << it->second << endl;
fstream ficheroEntrada;
string nombre;
string frase;
int fl = 0;
char current_char;
cout << "Dime el nombre del fichero: " << endl;
cin >> nombre;
ficheroEntrada.open ( nombre.c_str(), ios::in);
if (ficheroEntrada.is_open()) {
while(ficheroEntrada.get(current_char)){
if(current_char == EOF) break;
printf("%c",current_char);
fl++;
}
}
else{
cout << "Fichero inexistente o faltan permisos para abrirlo" << endl;
return 0;
}
ficheroEntrada.clear();
ficheroEntrada.seekg(0, ios::beg);
printf("\n");
char cif[fl]; int i = 0;
if (ficheroEntrada.is_open()) {
while(ficheroEntrada.get(current_char)){
if(current_char == EOF) break;
it = Mi.find(toupper(current_char));
if(it != Mi.end()){
cout << it->second;
cif[i++] = it->second;
}
else {
cif[i++] = current_char;
cout << current_char;
}
}
}
cif[i] = '\0';
fstream myfile;
myfile.open (nombre.c_str());
myfile.seekg (0,myfile.end);
long size = myfile.tellg();
myfile << endl; myfile << endl;
myfile << "------------------------" << endl;
myfile << "TEXTO CIFRADO: " << endl;
myfile << "------------------------" << endl;
myfile << cif;
cout << endl;
myfile.seekg (0,myfile.end);
size = myfile.tellg();
myfile << endl;
myfile << "------------------------" << endl;
myfile << "TEXTO DESCIFRADO: " << endl;
myfile << "------------------------" << endl;
for(int i=0; i<fl; i++){
it = Ci.find(toupper(cif[i]));
if( it != Ci.end() ){
cout << it->second;
myfile << it->second;
}
else {
cout << cif[i];
myfile << cif[i];
}
}
myfile.close();
printf("\n");
return 0;
}
Notas:
- Palabra debe tener UNICAMENTE letras del alfabeto ingles
- No importa repeticion de letras en la palabra
- El archivo debe ser previamente creado y poner texto en el
- Para usos practicas use como ejemplo este archivo jiji [sus.txt]
We will meet at midnight
Perro :v What the funk?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment