Skip to content
Snippets Groups Projects
Commit ec8e6d80 authored by Egon Nathan Bittencourt Araujo's avatar Egon Nathan Bittencourt Araujo
Browse files

Error with reading

parent 53cc3969
No related branches found
No related tags found
No related merge requests found
*.o
*.swp
*.swo
playunfair
*.out
......@@ -2,12 +2,13 @@
#define _charbit(X) (1 << (X - 'a'))
#define _pos(I,J) ((I)*5 + (J)%5)
//Base idea is to use an 32-bit int as mask to detect which char
// is already on the matrix
std::vector< char > createKM ( const std::string key){
std::vector< char > mKey(25);
std::string abc(key + "abcdefghijklmnopqrstuvxwyz");
std::string abc(key + "abcdefghijklmnopqrstuvwxyz");
int mask =0;
//mask with 32 bits for the 25 letter alfabet
int si=0;
......@@ -30,22 +31,61 @@ std::vector< char > createKM ( const std::string key){
std::string playfair(std::string text, const std::string key){
std::ostringstream ret;
std::vector< char > mKey(createKM(key));
for (int i=0; i<5; ++i){
/*for (int i=0; i<5; ++i){
for (int j=0; j<5; ++j){
std::cout << mKey[i*5 + j] << ' ';
}
std::cout << std::endl;
}
int indexT = -1;
char fst = text[++indexT];
char snd = text[++indexT];
if (fst == snd && snd != 'x'){
snd = 'x';
--indexT;
}
}*/
unsigned int indexT = 0;
int repetitions=0;
while(indexT <text.size() ){
char fst = text[indexT];
char snd = (++indexT< text.size())?text[indexT]:'x';
std::cout << fst << " " << snd << std::endl;
++indexT;
if (fst == snd){
if(snd!='x'){
snd = 'x';
}else{
snd='q';
}
--indexT;
++repetitions;
}
int iFst, iSnd;
int iFst, iSnd;
for (int i=0; i<25; ++i){
if(mKey[i] == fst ||
((fst == 'i' || fst=='j') &&
(mKey[i] =='i' || mKey[i]=='j'))){
iFst=i;
}
if(mKey[i] == snd ||
((snd == 'i' || snd =='j') &&
(mKey[i] =='i' || mKey[i]=='j'))){
iSnd=i;
}
}
char retfst,retsnd;
if(iFst/5 == iSnd/5){
retfst= mKey[_pos( iFst/5, (iFst+1))];
retsnd= mKey[_pos( iSnd/5 ,(iSnd+1))];
}else if(iFst%5 == iSnd%5){
std::cout << iFst << " " << iFst/5 << " " << ((iFst/5)+1)%5 << std::endl;
retfst= mKey[_pos( ((iFst/5)+1)%5 , (iFst))];
std::cout << retfst << " " << _pos( ((iFst/5)+1)%5 , (iFst)) << std::endl;
retsnd= mKey[_pos( ((iSnd/5)+1)%5 , (iSnd))];
}else{
retfst = mKey[ _pos( iFst/5, iSnd)];
retsnd = mKey[_pos ( iSnd/5, iFst)];
}
ret << retfst << retsnd;
}
std::cout << ret.str() << std::endl;
//TODO:change
return text;
return ret.str();
}
......@@ -13,11 +13,13 @@ int main(int argc, char *argv[]){
}
std::fstream input, output;
char *inputName, *outputName;
std::string aux, key = "cachorro", crip; //a key vai vir do dicionario futuramente
std::string aux="", key = "cachorro", crip; //a key vai vir do dicionario futuramente
std::ostringstream text;
inputName = argv[1];
outputName = argv[2];
//TODO: ELE TA LENDO CAGADO JA, olha o fim desse comment
input.open(inputName, std::ifstream::in);
if(!input.good()){
std::cout << "Nao foi possivel abrir o arquivo de entrada" << std::endl;
......@@ -27,9 +29,13 @@ int main(int argc, char *argv[]){
while(std::getline(input, aux))
text << aux;
std::cout<<text.str()[0] << std::endl;
//TODO: ERRO ACABA AQUI, veja o resultado disso aqui^ com o teste3.in
return 0;
crip = text.str();
parser(crip);
std::cout << crip << std::endl;
crip = playfair(crip, key);
std::cout << crip << std::endl;
return 0;
}
youshallnot pas
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment