/** Version 5 of Morse Coder program. Add reading from a file * Max file size is 1024 characters * Author : E.S.Boese * Date : Fall 2006 * Revised: D.C.Matthews, Spring 2017 */ #include #include #include void morseIt(char line[]); void morseCode(char character); void demorseIt(char line[]); char getLetter(char word[]); void printMenu(); int readFile(char contents[], int length); int main() { char line[1024]; char filecontents[2048]; int error = 0; printf("Welcome to the Morse Code Program (MCP)\n"); printMenu(); printf(" > "); fgets(line, sizeof(line), stdin); while (line[0] != 'q') { switch (line[0]) { case '?': printMenu(); break; case 'e': printf("Enter text to encode: "); fgets(line, sizeof(line), stdin); morseIt(line); break; case 'd': printf("Enter text to decode: "); fgets(line, sizeof(line), stdin); demorseIt(line); break; case 'i': error = readFile(filecontents, sizeof(filecontents)); printf("Original File:\n===============\n%s\n=================\n", filecontents); if (error >= 0) morseIt(filecontents); break; case 'f': error = readFile(filecontents, sizeof(filecontents)); printf("Original File:\n===============\n%s\n=================\n", filecontents); if (error >= 0) demorseIt(filecontents); break; } printf("\n > "); fgets(line, sizeof(line), stdin); } printf("\n"); return 0; } int readFile(char contents[], int length) { FILE *fp; char filename[50]; char c; // for reading in one character at a time int index=0; // index in contents string array printf("Enter name of file: "); fgets(filename, sizeof(filename), stdin); filename[strlen(filename)-1] = '\0'; // remove new line char if ((fp = fopen(filename,"r")) == NULL) { printf("Cannot open file %s\n", filename); return -1; // return an error code } while ((c = fgetc(fp)) != EOF) { contents[index++] = c; // add character to string } contents[index] = '\0'; fclose(fp); return 0; } void printMenu() { printf("*-*-*- MENU -*-*-*\n"); printf(" e - encode a line of text\n"); printf(" d - decode a line of text\n"); printf(" i - encode a text file\n"); printf(" f - decode a text file\n"); printf(" ? - print this menu\n"); printf(" q - quit\n"); } void demorseIt(char line[]) { #define MAX_WORD_LEN 5 char word[MAX_WORD_LEN]; int index=0; // index for word for (int i=0; line[i] != '\0'; i++) { if (line[i] == '#') // done with word { word[index] = '\0'; printf("%c", getLetter(word)); index = 0; // start over for next word } else word[index++] = line[i]; } } char getLetter(char word[]) { if (strcmp(word, ".-" ) == 0) return 'A'; if (strcmp(word, "-...") == 0) return 'B'; if (strcmp(word, "-.-.") == 0) return 'C'; if (strcmp(word, "-.." ) == 0) return 'D'; if (strcmp(word, "." ) == 0) return 'E'; if (strcmp(word, "..-.") == 0) return 'F'; if (strcmp(word, "--." ) == 0) return 'G'; if (strcmp(word, "....") == 0) return 'H'; if (strcmp(word, ".." ) == 0) return 'I'; if (strcmp(word, ".---") == 0) return 'J'; if (strcmp(word, "-.-" ) == 0) return 'K'; if (strcmp(word, ".-..") == 0) return 'L'; if (strcmp(word, "--" ) == 0) return 'M'; if (strcmp(word, "-." ) == 0) return 'N'; if (strcmp(word, "---" ) == 0) return 'O'; if (strcmp(word, ".--.") == 0) return 'P'; if (strcmp(word, "--.-") == 0) return 'Q'; if (strcmp(word, ".-." ) == 0) return 'R'; if (strcmp(word, "..." ) == 0) return 'S'; if (strcmp(word, "-" ) == 0) return 'T'; if (strcmp(word, "..-" ) == 0) return 'U'; if (strcmp(word, "...-") == 0) return 'V'; if (strcmp(word, ".--" ) == 0) return 'W'; if (strcmp(word, "-..-") == 0) return 'X'; if (strcmp(word, "-.--") == 0) return 'Y'; if (strcmp(word, "--..") == 0) return 'Z'; if (strcmp(word, "$$" ) == 0) return ' '; return word[0]; } void morseIt(char line[]) { for (int i=0; line[i] != '\0'; i++) { morseCode(line[i]); printf("#"); } } void morseCode(char character) { switch (toupper(character)) { case 'A': printf(".-"); break; case 'B': printf("-..."); break; case 'C': printf("-.-."); break; case 'D': printf("-.."); break; case 'E': printf("."); break; case 'F': printf("..-."); break; case 'G': printf("--."); break; case 'H': printf("...."); break; case 'I': printf(".."); break; case 'J': printf(".---"); break; case 'K': printf("-.-"); break; case 'L': printf(".-.."); break; case 'M': printf("--"); break; case 'N': printf("-."); break; case 'O': printf("---"); break; case 'P': printf(".--."); break; case 'Q': printf("--.-"); break; case 'R': printf(".-."); break; case 'S': printf("..."); break; case 'T': printf("-"); break; case 'U': printf("..-"); break; case 'V': printf("...-"); break; case 'W': printf(".--"); break; case 'X': printf("-..-"); break; case 'Y': printf("-.--"); break; case 'Z': printf("--.."); break; case ' ': printf("$$"); break; default: printf("%c", character); } }