[First commit]
Basic caesar encryption.
This commit is contained in:
commit
597d48ba0f
5 changed files with 100 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
.vscode/
|
34
caesar.cpp
Normal file
34
caesar.cpp
Normal file
|
@ -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;
|
||||
}
|
19
caesar.hpp
Normal file
19
caesar.hpp
Normal file
|
@ -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
|
25
main.cpp
Normal file
25
main.cpp
Normal file
|
@ -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;
|
||||
}
|
21
makefile
Normal file
21
makefile
Normal file
|
@ -0,0 +1,21 @@
|
|||
|
||||
CXX = g++
|
||||
|
||||
CXXFLAGS = -std=c++11 -Wall
|
||||
|
||||
TARGET = caesar
|
||||
|
||||
SRCS = main.cpp caesar.cpp
|
||||
|
||||
OBJS = $(SRCS:.cpp=.o)
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CXX) $(CXXFLAGS) $(OBJS) -o $(TARGET)
|
||||
|
||||
%.o: %.cpp
|
||||
$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
|
||||
.PHONY: clean
|
Loading…
Reference in a new issue