mirror of
https://bitbucket.org/Mattrixwv/cipherstream.git
synced 2025-12-06 18:33:58 -05:00
Added the Caesar Cipher to the program
This commit is contained in:
127
SourceFiles/Caesar.cpp
Normal file
127
SourceFiles/Caesar.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
//Ciphers/SourceFiles/Caesar.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 4-25-18
|
||||
//Modified: 4-25-18
|
||||
//This file contains the implementation of the Caesar class
|
||||
//This class implements the Caesar Cipher and is inteded to be turned into a library
|
||||
|
||||
|
||||
#include "../Headers/Caesar.hpp"
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
|
||||
|
||||
//std::string input;
|
||||
//std::string output;
|
||||
//int shift;
|
||||
|
||||
Caesar::Caesar(){
|
||||
shift = 0;
|
||||
}
|
||||
|
||||
Caesar::~Caesar(){
|
||||
}
|
||||
|
||||
void Caesar::setInput(std::string inputString){
|
||||
input = inputString;
|
||||
}
|
||||
|
||||
std::string Caesar::getInput() const{
|
||||
return input;
|
||||
}
|
||||
|
||||
void Caesar::setShift(int shiftAmount){
|
||||
//If you shift more than 26 you will just be wrapping back around again
|
||||
shift = shiftAmount % 26;
|
||||
}
|
||||
|
||||
int Caesar::getShift() const{
|
||||
return shift;
|
||||
}
|
||||
|
||||
std::string Caesar::getOutput() const{
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string Caesar::encode(){
|
||||
output = "";
|
||||
char temp; //A temperary holder for the current working character
|
||||
for(int cnt = 0;cnt < input.size();++cnt){
|
||||
temp = input.at(cnt);
|
||||
//If it is a upper case letter shift it and wrap if necessary
|
||||
if(isupper(temp)){
|
||||
temp += shift;
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(temp < 'A'){
|
||||
temp += 26;
|
||||
}
|
||||
else if(temp > 'Z'){
|
||||
temp -= 26;
|
||||
}
|
||||
}
|
||||
//If it is a lower case letter shift it and wrap if necessary
|
||||
else if(islower(temp)){
|
||||
temp += shift;
|
||||
//Wrap around if the letter is now out of bounds
|
||||
if(temp < 'a'){
|
||||
temp += 26;
|
||||
}
|
||||
else if(temp > 'z'){
|
||||
temp -= 26;
|
||||
}
|
||||
}
|
||||
//If it is whitespace, number, or punctuation just let it pass through
|
||||
//Add it to the output string
|
||||
output += temp;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string Caesar::encode(int shiftAmount, std::string inputString){
|
||||
setShift(shiftAmount);
|
||||
setInput(inputString);
|
||||
return encode();
|
||||
}
|
||||
|
||||
std::string Caesar::decode(){
|
||||
output = "";
|
||||
char temp;
|
||||
for(int cnt = 0;cnt < input.size();++cnt){
|
||||
temp = input.at(cnt);
|
||||
//If it is an upper case letter shift it and wrap if necessary
|
||||
if(isupper(temp)){
|
||||
temp -= shift;
|
||||
if(temp < 'A'){
|
||||
temp += 26;
|
||||
}
|
||||
else if(temp > 'Z'){
|
||||
temp -= 26;
|
||||
}
|
||||
}
|
||||
//If it is a lower case letter shift it and wrap if necessary
|
||||
else if(islower(temp)){
|
||||
temp -= shift;
|
||||
if(temp < 'a'){
|
||||
temp += 26;
|
||||
}
|
||||
else if(temp > 'z'){
|
||||
temp -= 26;
|
||||
}
|
||||
}
|
||||
//If it is whitespace, number, or punctuation just let it pass through
|
||||
//Add it to the output string
|
||||
output += temp;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string Caesar::decode(int shiftAmount, std::string inputString){
|
||||
setShift(shiftAmount);
|
||||
setInput(inputString);
|
||||
return decode();
|
||||
}
|
||||
|
||||
void Caesar::reset(){
|
||||
input = output = "";
|
||||
shift = 0;
|
||||
}
|
||||
Reference in New Issue
Block a user