This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:mowens3:portfolio:cprogproject1 [2012/02/14 21:19] – [Objectives] mowens3 | user:mowens3:portfolio:cprogproject1 [2012/02/29 00:37] (current) – [Project: SECRET AGENT] mowens3 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ======Project: | ||
+ | A project for C++ by Michael during the Spring 2012. | ||
+ | |||
+ | This project was begun on feb 14 and is anticipated to take 2 weeks to complete. Project was completed on Feb 28th, 2012 | ||
+ | |||
+ | =====Objectives===== | ||
+ | We are making an encoder and decoder in C. | ||
+ | =====Prerequisites===== | ||
+ | In order to successfully accomplish/ | ||
+ | |||
+ | * successful completion of project #0 and solid understanding of pertinent topics | ||
+ | * familiarity with memory allocation via **malloc(3)** | ||
+ | * familiarity with looking up C function parameters/ | ||
+ | * familiarity with if statements, especially in use for error detection | ||
+ | * familiarity with pointers and pointer arithmetic | ||
+ | * familiarity with loops | ||
+ | * file I/O | ||
+ | |||
+ | =====Background===== | ||
+ | Writting a program that will encode a text file by chagning the characters.\\ | ||
+ | A different file will decode a text file by reversing the change in characters. \\ | ||
+ | It asks for a cypher key, and you will need to know that key when you decode it. | ||
+ | =====Scope===== | ||
+ | This project will test your familiarity with more involved algorithms, storage, and control structures. | ||
+ | |||
+ | Encoding is the process of converting a message into a coded form. | ||
+ | |||
+ | Decoding is the reverse- retrieving that original message from an encoded form. | ||
+ | |||
+ | In this project you will write a 2 programs: one that will take a plain text message and convert it into a coded form (largely indecipherable to the regular english expecting eye) using a given cipher key, and another that will reverse the process (or decipher), when given the appropriate key. | ||
+ | |||
+ | The key is a numeric used to rotate the alphabet a set amount. Where A would normally equal 1, and Z 26, using a cipher key of 1 to shift 1 position to the right; A would now equal 2 (or B), B is now 3 (or C), and Z would be 1 (A). | ||
+ | |||
+ | Your encoding program can operate as follows: | ||
+ | |||
+ | * obtains its cipher key from a text file called " | ||
+ | * or, if you prefer, use command-line arguments to provide the key | ||
+ | * obtains the input message from a file called " | ||
+ | * if " | ||
+ | * outputs the ciphertext message to STDOUT **AND** saves it to a file called " | ||
+ | * implement error checking to avoid segfaults | ||
+ | |||
+ | The decoding operation is essentially the reverse (shifting to the left). I'd suspect you could reuse much of the same logic. | ||
+ | |||
+ | Your decoding program: | ||
+ | |||
+ | * obtains its cipher key from a text file called " | ||
+ | * or, if you prefer, use command-line arguments to provide the key | ||
+ | * obtains the input cipher from a file called " | ||
+ | * if " | ||
+ | * outputs the plaintext message to STDOUT **AND** saves it to a file called " | ||
+ | * implement error checking to avoid segfaults | ||
+ | |||
+ | If you want, you may implement both functionalities into one program so long as you provide a mechanism for the user to access both operations (but note this is not required for successful completion of this project). | ||
+ | |||
+ | =====Code===== | ||
+ | |||
+ | The encipher code: | ||
+ | |||
+ | <code c> | ||
+ | /*encodes*/ | ||
+ | # | ||
+ | # | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | FILE *original, *encoded; | ||
+ | int key = 0; //number of asci spaces moved | ||
+ | int holder = 0; //used as a temp space holder | ||
+ | char letter; | ||
+ | original = fopen(" | ||
+ | encoded = fopen(" | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | letter = fgetc(original); | ||
+ | while(letter != EOF) | ||
+ | { | ||
+ | if ((letter > | ||
+ | { | ||
+ | holder = letter - 65; | ||
+ | holder = holder + key; | ||
+ | holder = holder % 26; | ||
+ | letter = holder + 65; | ||
+ | } | ||
+ | if ((letter >= ' | ||
+ | { | ||
+ | holder = letter - 97; | ||
+ | holder = holder + key; | ||
+ | holder = holder % 26; | ||
+ | letter = holder + 97; | ||
+ | } | ||
+ | fprintf(encoded, | ||
+ | letter = fgetc(original); | ||
+ | } | ||
+ | printf(" | ||
+ | fclose(original); | ||
+ | fclose(encoded); | ||
+ | return(0); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Deciphering code. | ||
+ | |||
+ | <code c> | ||
+ | //decodes | ||
+ | # | ||
+ | # | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | FILE *encoded, *decoded; | ||
+ | char typed; | ||
+ | int key = 0; | ||
+ | char letter; | ||
+ | unsigned char holder = 0; | ||
+ | encoded = fopen(" | ||
+ | decoded = fopen(" | ||
+ | |||
+ | printf(" | ||
+ | scanf(" | ||
+ | letter = fgetc(encoded); | ||
+ | while(letter != EOF) | ||
+ | { | ||
+ | if ((letter >= ' | ||
+ | { | ||
+ | holder = letter - 65; | ||
+ | holder = holder - key - 230; | ||
+ | holder = holder % 26; | ||
+ | letter = holder + 65; | ||
+ | } | ||
+ | if ((letter >= ' | ||
+ | { | ||
+ | holder = letter - 97; | ||
+ | holder = holder - key - 230; | ||
+ | holder = holder % 26; | ||
+ | letter = holder + 97; | ||
+ | } | ||
+ | fprintf(decoded, | ||
+ | letter = fgetc(encoded); | ||
+ | } | ||
+ | printf(" | ||
+ | fclose(encoded); | ||
+ | fclose(decoded); | ||
+ | return(0); | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | =====Execution===== | ||
+ | |||
+ | An example run of the enciphering process: | ||
+ | |||
+ | <cli> | ||
+ | |||
+ | Please pick your encyphering number: 4 | ||
+ | Encoding complete | ||
+ | </ | ||
+ | |||
+ | Example run of the decoding process: | ||
+ | |||
+ | <cli> | ||
+ | |||
+ | Please enter the original key number: 4 | ||
+ | Decoding complete, check decoded.txt | ||
+ | </ | ||
+ | =====Reflection===== | ||
+ | I learned that this project was a pain in the butt. I also know, I don't want to mess with stuff like this again. | ||
+ | =====References===== | ||
+ | In this project, I had a lot of help from Matt, our teacher. |