64,654
社区成员
发帖
与我相关
我的任务
分享
#ifndef CIPHER_H
#define CIPHER_H
class Cipher {
public:
Cipher();
virtual ~Cipher();
char encode(char );
void print();
private:
char alphabet[27];
char cipherkey[27];
void shuffle( char[] , int );
};
#endif
#include <iostream>
#include <time.h>
#include <string.h>
#include "Cipher.hpp"
using namespace std;
Cipher::Cipher() {
strcpy( alphabet, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
strcpy( cipherkey, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
shuffle( cipherkey, 26 );
}
Cipher::~Cipher() {
}
void Cipher::shuffle( char charArray[] , const int size ) {
static_cast<unsigned int>(time(NULL));
for(int k=size;k>1;k--)
{
char temp,randpos;
randpos=rand()%k;
temp=charArray[k-1];
charArray[k-1]=charArray[randpos];
charArray[randpos]=temp;
}
return;
}
char Cipher::encode( char ch ) {
int n=ch;
if (n>=97&&n<=122)
n=n-32-65;
else if(n>=65&&n<=90)
n=n-65;
else
return ch;
return ch=cipherkey[n];
}
void Cipher::print() {
cout<<"alphabet:"<<alphabet<<endl;
cout<<"cipherkey:"<<cipherkey<<endl;
}
#include "tools.hpp"
#include "Cipher.hpp"
#include <iostream>
#include <fstream>
using namespace std;
int main( void ) {
banner();
Cipher c;
c.print();
ifstream infile("F:\\Fall 2013\\CS 620\\P1.txt");
ofstream outfile("F:\\Fall 2013\\CS 620\\P1.enc");
if(!infile){
fatal("%s\n", "Unable to open infile");
}
if(!outfile){
fatal("%s\n", "Unable to open outfile");
}
char e;
infile.open("F:\\Fall 2013\\CS 620\\P1.txt",ios::in);
while (infile.peek()!=EOF){
infile>>e;
e=c.encode(e);
outfile<<e;
}
infile.close();
outfile.close();
bye();
return 0;
}