assalamualaikum wr.wb
c++ :
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <string.h>
using namespace std;
class Sandi {
friend ostream& operator<<(ostream&, Sandi&);
public:
Sandi() { kalimat = "Algoritma dan Pemrograman";}
void enkripsi(char *);
void dekripsi(int *);
private:
char *kalimat;
int *kalimatTersandi; };
void Sandi::enkripsi(char *kalimat) {
for (int i=0; kalimat[i] != '\0'; i++)
if (kalimat[i] != ' ')
if ( kalimat[i]%2==0 ) kalimatTersandi[i] = kalimat[i] + 3;
else kalimatTersandi[i] = kalimat[i] + 5;
else kalimatTersandi[i] = 0;
}
void Sandi::dekripsi(int *kalimatTersandi) {
for (int i=0; kalimat[i] != '\0'; i++)
if (kalimatTersandi[i] != 0)
if ( kalimat[i]%2==1 ) kalimat[i] = kalimatTersandi[i] - 5;
else kalimat[i] = kalimatTersandi[i] - 3;
else kalimat[i] = ' ';
}
ostream& operator<<(ostream& out, Sandi& snd) {
cout << "Kata Asli : " << snd.kalimat;
snd.enkripsi(snd.kalimat);
cout << "\nSetelah disandikan : ";
for (int i=0; i<=strlen(snd.kalimat); i++)
out << snd.kalimatTersandi[i] << " ";
snd.dekripsi(snd.kalimatTersandi);
cout << "\nSetelah dikembalikan : " << snd.kalimat;
return out;
}
int main() {
Sandi X;
cout << X;
}
0 komentar:
Posting Komentar