#include <iostream>
#include <windows.h>
#include <ctime>
#include <fstream>
void krypt(char z);
void decrypt(char kodis[]);
using namespace std;
ofstream outFile;
/*
för att inte vara allt för uppenbar representeras varje tecken av fyra siffror,
det första och den fjärde är själva teckenkoden,
dom andra är bara där för att vara fula,
en av dom räknar antalet gånger tecknet lästs in och den andra bara ett värde som du själv väljer
*/
struct bokstav
{
char stav;
char nr1;
char nr2;
int typ;
int count;
};
struct ibokstav
{
char stav;
int nr1;
int nr2;
int typ;
int count;
};
/* hade lite problem med char / int jämförelse (troligen för att det inte är möjligt?),
löste det lite provisoriskt med en utläsnings struktur och en mer för inläsning.*/
bokstav ialfabetet[35] =
{
{'a',0,1,0},
{'b',0,2,0},
{'c',0,3,0},
{'d',0,4,0},
{'e',0,5,0},
{'f',0,6,0},
{'g',0,7,0},
{'h',0,8,0},
{'i',0,9,0},
{'j',1,0,0},
{'k',1,1,0},
{'l',1,2,0},
{'m',1,3,0},
{'n',1,4,0},
{'o',1,5,0},
{'p',1,6,0},
{'q',1,7,0},
{'r',1,8,0},
{'s',1,9,0},
{'t',2,0,0},
{'u',2,1,0},
{'v',2,2,0},
{'x',2,3,0},
{'y',2,4,0},
{'z',2,5,0},
{'.',2,6,0},
{'!',2,7,0},//
{',',2,8,0},
{'1',2,9,0},//
{'"',3,0,0},//
{')',3,1,0},//
{'(',3,2,0}//
-
};
bokstav alfabetet[35] =
{
{'a','0','1',0},
{'b','0','2',0},
{'c','0','3',0},
{'d','0','4',0},
{'e','0','5',0},
{'f','0','6',0},
{'g','0','7',0},
{'h','0','8',0},
{'i','0','9',0},
{'j','1','0',0},
{'k','1','1',0},
{'l','1','2',0},
{'m','1','3',0},
{'n','1','4',0},
{'o','1','5',0},
{'p','1','6',0},
{'q','1','7',0},
{'r','1','8',0},
{'s','1','9',0},
{'t','2','0',0},
{'u','2','1',0},
{'v','2','2',0},
{'x','2','3',0},
{'y','2','4',0},
{'z','2','5',0},
{'.','2','6',0},
{'!','2','7',0},
{',','2','8',0},
{'1','2','9',0},
{'"','3','0',0},
{')','3','1',0},
{'(','3','2',0}
};
int main()
{
bool meny=true;
while(meny)
{
int val;
cout << "1.kryptera" << endl << "2.dekryptera" << endl << "3.avsluta"<< endl;
cin >> val;
cin.get();
if (val == 1)// krypterar
{
char filename[60];
ifstream inFile;
cout << "Kryptera fran: ";
cin.getline(filename, 60);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Kunde inte oppna " << filename << endl;
cout << "Program terminating.\n";
cin.get();
cin.get();
exit(EXIT_FAILURE);
}
char filename2[60];
//ofstream outFile;
cout << "och till: ";
cin.getline(filename2, 60);
outFile.open(filename2);
if (!outFile.is_open())
{
cout << "Kunde inte oppna " << filename2 << endl;
cout << "Program terminating.\n";
cin.get();
cin.get();
exit(EXIT_FAILURE);
}
-
char temp;
int count=0;
inFile >> temp;
while (inFile.good()) // while input good and not at EOF
{
++count; // one more item read
krypt(temp);
inFile >> temp; // get next value
}
outFile << "~";
cout << "------------" << endl << count << " tecken inlasta fran " << filename << " till " << filename2 << endl << "------------" << endl; }
if(val ==2)//dekrypterar
{
char filename[60];
ifstream inFile;
cout << "Dekryptera fran: ";
cin.getline(filename, 60);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Kunde inte oppna " << filename << endl;
cout << "Program terminating.\n";
cin.get();
cin.get();
exit(EXIT_FAILURE);
}
char filename2[60];
//ofstream outFile;
cout << "och till: ";
cin.getline(filename2, 60);
outFile.open(filename2);
if (!outFile.is_open())
{
cout << "Kunde inte oppna " << filename2 << endl;
cout << "Program terminating.\n";
cin.get();
cin.get();
exit(EXIT_FAILURE);
}
int count=0;
char kodis[3];
char temp;
bool forsta_gang=true;
inFile >> temp;
if(temp=='~')
break;
kodis[0]=temp;
while (inFile.good()) // while input good and not at EOF
{
-
if(!forsta_gang==true)
{
inFile >> temp;
if(temp=='~')
break;
kodis[0]=temp;
}
else
forsta_gang=false;
inFile >> temp;
if(temp=='~')
break;
kodis[1]=temp;
inFile >> temp;
if(temp=='~')
break;
kodis[2]=temp;
inFile >> temp;
if(temp=='~')
break;
kodis[3]=temp;
++count; // one more item read
decrypt(kodis);
}
cout << "------------" << endl << count << " tecken dekrypterade fran " << filename << " till " << filename2 << endl << "------------" << endl;
}
if(val == 3)//avsluta
meny=false;
outFile.close();
-
}// while loop slut
return 0;
}
void krypt(char z)
{
for(int t=0;t < 100;t++)
{
if (ialfabetet[t].stav == z)
{
int kod[3];
kod[0]=ialfabetet[t].nr1;
kod[1]=ialfabetet[t].typ;
kod[2]=ialfabetet[t].count;
kod[3]=ialfabetet[t].nr2;
ialfabetet[t].count =+ 1;
outFile << kod[0] << kod[1] << kod[2] << kod[3];
}
}
-
}
void decrypt(char kodis[])
{
-
for(int t=0;t < 100;t++)
{
if(alfabetet[t].nr1 == kodis[0] && alfabetet[t].nr2 == kodis[3])
{
//cout << alfabetet[t].stav;
outFile << alfabetet[t].stav;
}
}
}
-