Created
March 7, 2012 04:53
-
-
Save MarcoPolo/1991001 to your computer and use it in GitHub Desktop.
caesar sallad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <limits> | |
int INPUT_MESSAGE = 1; | |
int ENCRYPT = 2; | |
int DECRYPT = 3; | |
int ANALYZE = 4; | |
int QUIT = 0; | |
using namespace std; | |
int& displayMenu(){ | |
int* option = new int(); | |
cout << "MAIN MENU" << endl << endl; | |
cout << "1. To input a message" << endl; | |
cout << "2. To encrypt a message " << endl; | |
cout << "3. To decrypt a message" << endl; | |
cout << "4. to perform a statisical analysis of the message" << endl; | |
cout << "Press 0 to quit" << endl <<endl; | |
cout << "Please choose:"; | |
cin >> *option; | |
return *option; | |
} | |
void analyze(char *message){ | |
int letterFrequency[26] ={0}; | |
for( int i=0; i < ((string)message).length(); i++){ | |
letterFrequency[tolower(message[i]) - 97]++; | |
} | |
for( int i=0; i < sizeof(letterFrequency)/4; i++){ | |
cout << (char)(i + 97) << ": " << letterFrequency[i] << endl; | |
} | |
float vowelCount = 0; | |
float constCount = 0; | |
for(int i = 0; i < sizeof(letterFrequency)/4; i++){ | |
if( i == (int) 'a' - 97 || i == (int) 'e' - 97 || i == (int) 'i' - 97 || i == (int) 'o' - 97 || i == (int) 'u' - 97){ | |
vowelCount += letterFrequency[i]; | |
} | |
else{ | |
constCount += letterFrequency[i]; | |
} | |
} | |
float vc = vowelCount/constCount; | |
if( constCount == 0){ | |
cout << numeric_limits<float>::has_infinity << endl; | |
} | |
else{ | |
cout << "The V/C count is: " << vc << endl; | |
} | |
} | |
char* caesarShift(char *message, int shift, char *encryptedMessage){ | |
for( int i=0; i < ((string)message).length(); i++){ | |
if( islower(message[i])){ | |
encryptedMessage[i] = (((int)message[i] - 97) + shift)%26 + 97; | |
if(encryptedMessage[i] < 97){ | |
encryptedMessage[i] += 26; | |
} | |
} | |
else if(isupper(message[i])){ | |
encryptedMessage[i] = (((int)message[i] - 65) + shift)%26 + 65; | |
if(encryptedMessage[i] < 65){ | |
encryptedMessage[i] += 26; | |
} | |
} | |
} | |
encryptedMessage[((string)message).length()] = NULL; | |
return message; | |
} | |
int main(){ | |
char message[141]; | |
char encryptedMessage[141]; | |
int option=1; | |
int shift; | |
bool messageGiven = false; | |
char Answer; | |
char newMessage; | |
cout << "COP 2271 Encrypter/Decrypter" << endl; | |
cout << "This program is suppose to encrypt and decrypt messages using the Caesar Cipher algorithm." << endl << endl; | |
while(option != QUIT){ | |
option = displayMenu(); | |
if(option == INPUT_MESSAGE){ | |
if(messageGiven == true){ | |
cout << "Are you sure you want to modify the input? (y/n)"; | |
cin >> Answer; | |
if(Answer == 'n'){ | |
cout << "Returning to the main menu..\n\n"; } | |
else if(Answer == 'y'){ | |
cout << "Please give new text: " << endl; | |
cin.get(); | |
cin.getline(message,141); | |
} | |
} | |
else{ | |
cout << "Please give message:"; | |
cin.get(); | |
cin.getline(message,sizeof(message)); | |
messageGiven = true; | |
} | |
} | |
else if(option == ENCRYPT){ | |
cin.get(); | |
cout << "Please give the number of shifts to the left:"; | |
cin >> shift; | |
caesarShift(message, -shift, encryptedMessage); | |
cout << "The encrypted message will be:"<< encryptedMessage << endl; | |
} | |
else if(option == DECRYPT){ | |
cin.get(); | |
cout << "Please give the number of shifts to the right:"; | |
cin >> shift; | |
caesarShift(message, shift, encryptedMessage); | |
cout << "The decrypted message is:"<< encryptedMessage << endl << endl; | |
} | |
else if(option == ANALYZE){ | |
cin.get(); | |
analyze(message); | |
} | |
else if(option == QUIT){ | |
cout << "Now quitting..."<<endl; | |
break; | |
} | |
else | |
cout << "Wrong choice, please choose again."<< endl <<endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment