Upcoming SlideShare
×

Killian Vigna Morse code

729 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total views
729
On SlideShare
0
From Embeds
0
Number of Embeds
160
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

Killian Vigna Morse code

1. 1. // Killian Vigna (10129758)// This program will help you with your first project.#include <iostream>#include <iomanip>#include <string>#include <windows.h>#include <algorithm>using namespace std;// Declare a structure of 2 parts - Character and string.struct Code{ char letter; stringmorse;};// Array of the structure Code.Code convert[] ={ { A, ".- " },{ B, "-... " },{ C, "-.-. " }, { D, "-.. " },{ E, ". " },{ F, "..-. " }, { G, "--. " },{ H, ".... " },{ I, ".. " }, { J, ".--- " },{ K, "-.- " },{ L, ".-.. " }, { M, "-- " },{ N, "-. " },{ O, "--- " }, { P, ".--. " },{ Q, "--.- " },{ R, ".-. " }, { S, "... " },{ T, "- " },{ U, "..- " }, { V, "...- " },{ W, ".-- " },{ X, "-..- " }, { Y, "-.-- " },{ Z, "--.. " },{ 0, "---- " }, { 1, ".--- " },{ 2, "..--- "},{ 3, "...-- "}, { 4, "....- "},{ 5, "..... "},{ 6, "-.... "}, { 7, "--... "},{ 8, "---.. " },{ 9, "----. "}, { ., ".-.-.- "},{ ,, "--..-- "},{ ?, "..--.. "}};voidconvert_to_morse(string); // This function uses the string type.voidmorse_out(string); // Performs the beeping.voidmorse_to_text(); // Converts string entered to textvoidtext_to_morse(); // Converts string entered to Morsevoidconvert_to_text(string); // Uses string type, function prototypeint main (){ int menu; int num1, num2; string text; while(1) { // Menu on screen outputs cout<< "What is your Selection: n"; cout<< " 1 - Convert Text to Morse: n"; cout<< " 2 - Convert Morse to Text: n"; cout<< " 3 - Exit nn"; cin>> menu; // Output of selected menu option switch (menu) {
2. 2. case 1: cout<<"Convert Text to Morse Selected: "; cout<<"Enter Your Conversion: "; cin>> text; convert_to_morse(text); break; case 2: cout<<"Convert Morse to Text Selected: "; cin>> text; morse_to_text(); break; case 3: return 0; break; } } system("pause"); // terminate program return 0;}voidtext_to_morse(){ // we loop until the menu is processed string text; // The next 4 lines use the structure (Code) above to convertto morse code. cout<< "Enter a string to convert to Morse Code: " ; fflush(stdin); getline(cin, text); // The function below performs the conversion to Morse convert_to_morse(text);}// the functions below convert text to morse using the structure Codeabove.voidconvert_to_morse(string s){ unsignedinti, j; // step through character by character up to length of input string s for ( i = 0; i<s.length(); i++ ) { // only 54 characters in the structure...expand this for ( j = 0; j < 54; j++ ) { // Convert characters in s to uppercase before checkingfor them if ( toupper(s[i]) == convert[j].letter ) { morse_out(convert[j].morse); // display morse codeof letter break; } } }
3. 3. cout<< "n";}voidmorse_out(string str){ int x; charch; for (x = 0; x!= str.length(); x++) // keep going until the end(length) is reached { ch = str[x]; cout<<ch; // Display on screen; // Shows the compiler the size and length of output switch(ch) { case .: Beep(523, 200); Sleep(100); break; case -: Beep(700, 400); Sleep(100); break; case : cout<< " "; Sleep(700); break; } }}// Part two: Morse - Textvoidmorse_to_text(){ stringmorse; cout<< "Enter a string to convert to Morse Code: " ; fflush(stdin); getline(cin, morse); if (morse[morse.length() - 1] != ) morse = morse + " "; // the variable starts by pointing to the beginning of the inputmores string //use the compare method to compare the inoutmorse string againstwhat tou have stored in the convert structure convert_to_text(morse);} // insert code here for comparing morsevoidconvert_to_text(string s){ unsignedint a, b; //step through all charachters in the structure
4. 4. for ( a = 0; a <s.length(); ) { for( b = 0; b < 54; b++) { //convert the characters in b to uppercase before youcheck them if(s.compare(a, convert[b].morse.length(),convert[b].morse) == 0) { a = a + convert[b].morse.length(); // this moved past the matching morse string and onto the next one cout<< convert[b].letter; //display letter break; // Ends output } } } cout<<"n";}