Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * Solution to homework task 01
- * Introduction to programming course
- * Faculty of Mathematics and Informatics of Sofia University
- * 2017/2018 academic year
- *
- * @author Lilyana Stoyeva
- * @idnumber 855286
- * @task 1
- * @compiler GCC
- *
- */
- #include <iostream>
- #include <cstring>
- using namespace std;
- void ToUper(char& c) // we need to increase all letters in message, because Morse code is the same for small and capital letters
- {
- if(c >= 'a' && c <= 'z')
- c = c - ('a' - 'A');
- }
- bool isAlpha(char c) // check if the symbol in message is the letter
- {
- if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
- return true;
- return false;
- }
- bool isNumber(char c) // check if the symbol in message is number
- {
- if(c >= '0' && c <= '9')
- return true;
- return false;
- }
- void MorseCodeForAlpha(char c) // function for converting all letters to unique sequence of dot and dashes
- {
- const char alpha[] = {'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'};
- const char* morse[] = {".-","-...","-.-.","-..",".","..-.",
- "--.","....","..",".---","-.-",".-..","--","-.","---",".--.",
- "--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."
- };
- for(int i=0; i<27; i++)
- {
- if(c == alpha[i])
- {
- cout << morse[i] << " ";
- }
- }
- }
- void MorseCodeForNumber(char c) // function for converting all numbers to unique sequence of dot and dashes
- {
- const char number[] = {'0','1','2','3','4','5','6','7','8','9'};
- const char* morse[] = { "-----",".----","..---","...--","....-",
- ".....","-....","--...","---..","----."};
- for(int i=0; i<11; i++)
- {
- if(c == number[i])
- {
- cout << morse[i] << " ";
- }
- }
- }
- /* In Morse Code, the space between parts of the same letter is one unit;
- the space between letters is three units;
- the space between words is seven units */
- int main()
- {
- char message[100];
- cin.get(message,99);
- int length = strlen(message);
- for(int i=0; i<length; i++)
- {
- if(isAlpha(message[i]))
- {
- ToUper(message[i]);
- MorseCodeForAlpha(message[i]);
- }
- else if (isNumber(message[i]))
- {
- MorseCodeForNumber(message[i]);
- }
- else if(message[i] == ' ') // determines the word
- {
- cout << " "; continue;
- }
- else
- {
- cout << "Bad input!" << endl; break; // message may contain only Latin letters and Arabic numbers
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement