commit
597d48ba0f
|
@ -0,0 +1 @@
|
|||
.vscode/
|
|
@ -0,0 +1,34 @@
|
|||
#include "caesar.hpp"
|
||||
#include <string>
|
||||
|
||||
Caesar::Caesar(int rotation) {
|
||||
setRotation(rotation);
|
||||
}
|
||||
|
||||
void Caesar::setRotation(int rotation) {
|
||||
this->rotation = rotation;
|
||||
}
|
||||
|
||||
int Caesar::getRotation() {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
char Caesar::encryptChar(char toEncrypt) {
|
||||
char newChar = toEncrypt + rotation;
|
||||
|
||||
if(newChar > MAX) newChar -= MAX;
|
||||
|
||||
if(newChar < MIN) newChar += MIN;
|
||||
|
||||
return newChar;
|
||||
}
|
||||
|
||||
std::string Caesar::encryptString(std::string toEncrypt) {
|
||||
std::string encrypted = "";
|
||||
|
||||
for(int i = 0; i < toEncrypt.length(); i++) {
|
||||
encrypted += encryptChar(toEncrypt[i]);
|
||||
}
|
||||
|
||||
return encrypted;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
#ifndef CAESAR_H
|
||||
#define CAESAR_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class Caesar {
|
||||
public:
|
||||
Caesar(int rotation);
|
||||
void setRotation(int rotation);
|
||||
int getRotation();
|
||||
char encryptChar(char toEncrypt);
|
||||
std::string encryptString(std::string toEncrypt);
|
||||
private:
|
||||
int rotation;
|
||||
const int MIN = 33;
|
||||
const int MAX = 126;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -0,0 +1,25 @@
|
|||
#include <string>
|
||||
#include <iostream>
|
||||
#include "caesar.hpp"
|
||||
|
||||
const std::string usage = "caesar <rotation> <text to encrypt>";
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if(argc < 3) {
|
||||
std::cout << usage << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int rotation = std::stoi(argv[1]);
|
||||
std::string toEncrypt = "";
|
||||
|
||||
for(int i = 2; i < argc; i++) {
|
||||
toEncrypt += argv[i];
|
||||
if(i < argc - 1) toEncrypt += " ";
|
||||
}
|
||||
|
||||
Caesar caesar{rotation};
|
||||
std::cout << caesar.encryptString(toEncrypt) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue