Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * caesar.cpp
- *
- * <#Names#>
- * <#Uniqnames#>
- *
- * EECS 183: Project 3
- *
- * <#description#>
- */
- #include "caesar.h"
- #include <iostream>
- char shiftAlphaCharacter(char c, int n) {
- int shift;
- //store dummy character
- char result = '#';
- //find shift in positive integer between 0 and 26
- if (n < 0) {
- shift = n % 26 + 26;
- }
- else {
- shift = n % 26;
- }
- //separate case for lowercase
- if (c >= 'a') {
- //loop back if go past 'z'
- if (c + shift > 'z') {
- result = c + shift - 26;
- }
- else {
- result = c + shift;
- }
- }
- //separate case for uppercase
- else {
- // loop back if go past 'Z'
- if (c + shift > 'Z') {
- result = c + shift - 26;
- }
- else {
- result = c + shift;
- }
- }
- return result;
- }
- string caesarCipher(string original, int key, bool encrypt) {
- //Multiplies key by -1 if already encrypted to decrypt
- int multiplier = (encrypt * 2 - 1) * key;
- for (int i = 0; i < original.size(); i++) {
- if (isalpha(original.at(i))) {
- original.at(i) = shiftAlphaCharacter(original.at(i), multiplier);
- }
- }
- // returning a string to avoid compile error
- return original;
- }
- /**
- * utility.cpp
- *
- * <#Names#>
- * <#Uniqnames#>
- *
- * EECS 183: Project 3
- *
- * <#description#>
- */
- #include "utility.h"
- #include <iostream>
- string toUpperCase(string original) {
- //Calls and replaces each char with the uppercase version
- for (int i = 0; i < original.size(); i++) {
- original.at(i) = toupper(original.at(i));
- }
- return original;
- }
- string removeNonAlphas(string original) {
- string result = "";
- for (int i = 0; i < original.size(); i++) {
- //Adds only alphabetical characters to result
- if (isalpha(original.at(i))) {
- result += original.at(i);
- }
- }
- return result;
- }
- string removeDuplicate(string original) {
- string result = "";
- for (int i = 0; i < original.size(); i++) {
- bool hasAppeared = false;
- char current = original.at(i);
- //Check to see if character has already appeared
- for (int j = 0; j < result.size(); j++) {
- if (current == result.at(j)) {
- hasAppeared = true;
- }
- }
- if (!hasAppeared) {
- result += current;
- }
- }
- return result;
- }
- int charToInt(char original) {
- int result = -1;
- switch (original) {
- case '0':
- result = 0;
- break;
- case '1':
- result = 1;
- break;
- case '2':
- result = 2;
- break;
- case '3':
- result = 3;
- break;
- case '4':
- result = 4;
- break;
- case '5':
- result = 5;
- break;
- case '6':
- result = 6;
- break;
- case '7':
- result = 7;
- break;
- case '8':
- result = 8;
- break;
- case '9':
- result = 9;
- break;
- }
- return result;
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Do not touch code below. ////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- void printGrid(const char grid[SIZE][SIZE]) {
- for (int col = 0; col < SIZE; col++) {
- cout << " ---";
- }
- cout << endl;
- for (int row = 0; row < SIZE; row++) {
- cout << "| ";
- for (int col = 0; col < SIZE; col++) {
- cout << string(1, grid[row][col]) + " | ";
- }
- cout << endl;
- for (int col = 0; col < SIZE; col++) {
- cout << " ---";
- }
- cout << endl;
- }
- }
- /**
- * vigenere.cpp
- *
- * <#Names#>
- * <#Uniqnames#>
- *
- * EECS 183: Project 3
- *
- * <#description#>
- */
- #include "utility.h"
- #include "caesar.h"
- #include "vigenere.h"
- #include <iostream>
- string vigenereCipher(string original, string keyword, bool encrypt) {
- int multiplier = encrypt * 2 - 1;
- string key = removeNonAlphas(toUpperCase(keyword));
- int keyCounter = 0;
- int keyCap = key.size() - 1;
- for (int i = 0; i < original.size(); i++) {
- if (isalpha(original.at(i))) {
- int shift = (key.at(keyCounter) - 'A') * multiplier;
- original.at(i) = shiftAlphaCharacter(original.at(i), shift);
- keyCounter++;
- }
- //loop keyCounter to repeat the key.
- if (keyCounter > keyCap) {
- keyCounter = 0;
- }
- }
- // returning a string to avoid compile error
- return original;
- }
- /*
- * test.cpp
- * EECS 183 Project 3: Ciphers
- *
- * Justin Lee & Lucy Liu
- * JJCLEE &
- *
- * Testing helper functions for Project 3
- */
- #include "utility.h"
- #include "caesar.h"
- #include "vigenere.h"
- #include "polybius.h"
- #include <iostream>
- #include <string>
- void testToUpperCase();
- void testRemoveNonAlphas();
- void testRemoveDuplicate();
- void testCharToInt();
- void testShiftAlphaCharacter();
- void testCaesarCipher();
- void testVigenereCipher();
- void testFillGrid();
- void testMixKey();
- void testFindInGrid();
- void testPolybiusSquare();
- int main(){
- testToUpperCase();
- testRemoveNonAlphas();
- testRemoveDuplicate();
- testCharToInt();
- testShiftAlphaCharacter();
- testCaesarCipher();
- testVigenereCipher();
- testFillGrid();
- testMixKey();
- testFindInGrid();
- testPolybiusSquare();
- return 0;
- }
- void testToUpperCase() {
- //testing toUpperCase()
- cout << "TESTING toUpperCase()" << endl;
- cout << toUpperCase("hi") << endl;
- cout << toUpperCase("HI") << endl;
- cout << toUpperCase("Hi") << endl;
- cout << toUpperCase("!@#$%^&*()") << endl;
- cout << toUpperCase("ThIs Is A vErY fUn PrOjEcT!") << endl;
- cout << "FINISHED TESTING toUpperCase()" << endl << endl;
- }
- void testRemoveNonAlphas() {
- //testing removeNonAlphas()
- cout << "TESTING removeNonAlphas()" << endl;
- cout << removeNonAlphas("Hi") << endl;
- cout << removeNonAlphas("Hi!") << endl;
- cout << removeNonAlphas("!Hi") << endl;
- cout << removeNonAlphas("#&^$*&^*") << endl;
- cout << removeNonAlphas("A*SF&S^AD*&AS^D") << endl;
- cout << removeNonAlphas("&*#(HFA(*SDJA") << endl;
- cout << removeNonAlphas("a0a0a0a0a0a0a0a0a") << endl;
- cout << "FINISHED TESTING removeNonAlphas()" << endl << endl;
- }
- void testRemoveDuplicate() {
- //testing removeDuplicate()
- cout << "TESTING removeDuplicate()" << endl;
- cout << removeDuplicate("AAAAAAAAAAAA") << endl;
- cout << removeDuplicate("A1B1C1D1E1F1") << endl;
- cout << removeDuplicate("AABBCCDDEEFF") << endl;
- cout << removeDuplicate("001122334455") << endl;
- cout << removeDuplicate("AAAAAFFFFFFF") << endl;
- cout << removeDuplicate("A1A1B2B2C3C3") << endl;
- cout << "FINISHED TESTING removeDuplicate()" << endl << endl;
- }
- void testCharToInt() {
- //testing charToInt()
- cout << "TESTING charToInt()" << endl;
- cout << charToInt('0') << endl;
- cout << charToInt('1') << endl;
- cout << charToInt('2') << endl;
- cout << charToInt('3') << endl;
- cout << charToInt('4') << endl;
- cout << charToInt('5') << endl;
- cout << charToInt('6') << endl;
- cout << charToInt('7') << endl;
- cout << charToInt('8') << endl;
- cout << charToInt('9') << endl;
- cout << "FINISHED TESTING charToInt()" << endl << endl;
- }
- void testShiftAlphaCharacter() {
- //testing shiftAlphaCharacters()
- cout << "TESTING shiftAlphaCharacters()" << endl;
- cout << shiftAlphaCharacter('A', 52) << endl;
- cout << shiftAlphaCharacter('a', 0) << endl;
- cout << shiftAlphaCharacter('b', 2) << endl;
- cout << shiftAlphaCharacter('X', 5) << endl;
- cout << shiftAlphaCharacter('X', 50) << endl;
- cout << shiftAlphaCharacter('A', -3) << endl;
- cout << "FINISHED TESTING shiftAlphaCharacters()" << endl << endl;
- }
- void testCaesarCipher() {
- //testing caesarCipher()
- cout << "TESTING caesarCipher()" << endl;
- cout << caesarCipher("EECS 183 is a fun class", 12, true) << endl;
- cout << caesarCipher("QQOE 183 ue m rgz oxmee", 12, false) << endl;
- cout << caesarCipher("QQOE 183 ue m rgz oxmee", -12, true) << endl;
- cout << caesarCipher("EECS 183 is a fun class", -12, false) << endl;
- cout << "FINISHED TESTING caesarCipher()" << endl << endl;
- }
- void testVigenereCipher() {
- //testing vigenereCipher()
- cout << "TESTING vigenereCipher()" << endl;
- cout << vigenereCipher("Meet me at the Diag at 11 p.m.", "Squirrel!", true)
- << endl;
- cout << vigenereCipher("Euyb dv ee lxy Lzrk ll 11 f.g.", "Squirrel!", false)
- << endl;
- cout << vigenereCipher("EECS 183 is a fun class", "m1m2m3", true) << endl;
- cout << "FINISHED TESTING vigenereCipher()" << endl << endl;
- }
- void testFillGrid() {
- //testing fillGrid()
- }
- void testMixKey() {
- //testing mixKey()
- }
- void testFindInGrid() {
- //testing findInGrid()
- }
- void testPolybiusSquare() {
- //testing polybiusSquare()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement