Last active
May 9, 2017 11:52
-
-
Save henrybear327/90ce642e61495a1edd2cb28a18caa603 to your computer and use it in GitHub Desktop.
Midterm-homework.c
This file contains hidden or 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 <stdio.h> | |
#include <string.h> | |
#define BUFFER_SIZE 10000 | |
int isValid(char c) | |
{ | |
return ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9'); | |
} | |
char toUpper(char c) // change into capitalization | |
{ | |
if ('a' <= c && c <= 'z') | |
return c - 'a' + 'A'; | |
return c; | |
} | |
char decodeCipher(char key, char secret) | |
{ | |
key = toUpper(key); | |
secret = toUpper(secret); | |
if (!('A' <= secret && secret <= 'Z')) | |
return secret; | |
int offset = key - 'A'; | |
return (secret - 'A' - offset + 26) % 26 + 'A'; | |
} | |
char encodeCipher(char key, char character) | |
{ | |
key = toUpper(key); | |
character = toUpper(character); | |
if (!('A' <= character && character <= 'Z')) | |
return character; | |
int offset = key - 'A'; | |
return (character - 'A' + offset) % 26 + 'A'; | |
} | |
char mosLetter[40][10]; | |
char mosCode[40][10]; | |
int totalMos = 0; | |
void readInput() | |
{ | |
FILE *mosTXT = fopen("mos.txt", "r"); | |
if (mosTXT == NULL) { | |
printf("File mos.txt can't be opened\n"); | |
return; | |
} | |
while (fscanf(mosTXT, "%s %s", mosLetter[totalMos], mosCode[totalMos]) != | |
EOF) { | |
totalMos++; | |
} | |
fclose(mosTXT); | |
} | |
int decodeIndex = 0; | |
char decode_mos[BUFFER_SIZE] = {'\0'}; | |
void decodeMos() | |
{ | |
FILE *decodeTXT = fopen("decode.txt", "r"); | |
if (decodeTXT == NULL) { | |
printf("File decode.txt can't be opened\n"); | |
return; | |
} | |
// decode mos input | |
char inp[BUFFER_SIZE]; | |
while (fscanf(decodeTXT, "%s", inp) != EOF) { | |
// printf("Trying to match %s\n", inp); | |
int hasMatch = 0; | |
for (int i = 0; i < totalMos; i++) { | |
if (strcmp(mosCode[i], inp) == 0) { | |
decode_mos[decodeIndex++] = mosLetter[i][0]; | |
hasMatch = 1; | |
break; | |
} | |
} | |
if (hasMatch == 0) { | |
// printf("No match\n"); | |
decode_mos[decodeIndex++] = inp[0]; | |
} else { | |
// printf("Matched\n"); | |
} | |
} | |
fclose(decodeTXT); | |
} | |
int main() | |
{ | |
readInput(); | |
/*printf("================================\n"); | |
printf("Mos code table\n"); | |
for (int i = 0; i < totalMos; i++) | |
printf("%c %s\n", mosLetter[i][0], mosCode[i]); | |
printf("================================\n"); | |
*/ | |
decodeMos(); | |
printf("CHOOSE A FUNCTION\n\n(1)Encode\n(2)Decode\n"); | |
int choose; | |
scanf("%d", &choose); | |
if (choose == 2) { | |
printf("================================\n"); | |
printf("The data is\n"); | |
printf("%s\n", decode_mos); | |
printf("================================\n"); | |
// decod cipher | |
printf("\nKeyword: "); | |
char keyword[BUFFER_SIZE]; | |
scanf("%s", keyword); | |
int len = strlen(decode_mos); | |
int lenk = strlen(keyword); | |
int idxKeyWord = 0; | |
FILE *output_decode = fopen("output_decode.txt", "w"); | |
for (int i = 0; i < len; i++) { | |
char res = decodeCipher(keyword[idxKeyWord], decode_mos[i]); | |
if (isValid(res) == 1) { | |
idxKeyWord++; | |
idxKeyWord = idxKeyWord % lenk; | |
} | |
printf("%c", res == '|' ? ' ' : res); | |
fprintf(output_decode, "%c", res == '|' ? ' ' : res); | |
} | |
printf("\n"); | |
return 0; | |
} else if (choose == 1) { | |
FILE *encodeTXT = fopen("encode.txt", "r"); | |
char encode[BUFFER_SIZE]; | |
fscanf(encodeTXT, "%s", encode); | |
printf("String: %s\n", encode); | |
printf("Keyword: "); | |
char keyword[BUFFER_SIZE]; | |
scanf("%s", keyword); | |
int len = strlen(encode); | |
int lenk = strlen(keyword); | |
int idxKeyWord = 0; | |
FILE* outputEncode = fopen("output_encode.txt", "w"); | |
for (int i = 0; i < len; i++) { | |
char res = encodeCipher(keyword[idxKeyWord], encode[i]); | |
if (isValid(res) == 1) { | |
idxKeyWord++; | |
idxKeyWord %= lenk; | |
} | |
printf("%c", res == '-' ? '|' : res); | |
if(isValid(res) == 1) { | |
for (int i = 0; i < totalMos; i++) { | |
if(mosLetter[i][0] == res) { | |
fprintf(outputEncode, "%s ", mosCode[i]); | |
break; | |
} | |
} | |
} else { | |
fprintf(outputEncode, "%c ", res == '-' ? '|' : res); | |
} | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment