-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
117 lines (95 loc) · 3.51 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// FCI – Programming 1 – 2018 - Assignment 2
// Program Name: Morse code
// Last Modification Date: 2/28/2018
// Author1 and ID and Group: Name:mohamed hamdy mohamed ID:20170227 Group:G9
// Teaching Assistant:
//Purpose:encrypt or dycrypt a message
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
while (true)
{
cout <<endl;
cout <<"Ahlan ya user ua habibi. \n";
string choosenumber ;
cout <<"What do you like to do today? \n";
cout <<"1- Cipher a message \n";
cout <<"2- Decipher a message \n";
cout <<"3- End \n";
cin >> choosenumber ;
if (choosenumber == "1" )
{
string message ;
cout <<"Please enter the message to cipher: " ;
cin.ignore();
getline(cin, message);
string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string Morse[62] = {".- ","-... ","-.-. ","-.. ",". ","..-. ","--. ",".... ",".. ",".--- ","-.- ",".-.. ","-- ","-. ","--- ",".--. ","--.- ",".-. ","... ","- ","..- ","...- ",".-- ","-..- ","-.-- ","--.. ",".- ","-... ","-.-. ","-.. ",". ","..-. ","--. ",".... ",".. ",".--- ","-.- ",".-.. ","-- ","-. ","--- ",".--. ","--.- ",".-. ","... ","- ","..- ","...- ",".-- ","-..- ","-.-- ","--.. ","---- ",".--- ","..--- ","...-- ","....- ","..... ","-.... ","--... ","---.. ","----. "};
for (int i = 0 ; i < message.length() ; i++)
{
for (int j=0 ; j< 62; j++)
{
if (message[i] == alphabet[j])
{
cout << Morse[j];
}
if (message[i] == ' ')
{
cout <<" " ;
break;
}
}
}
}
else if (choosenumber == "2")
{
string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string Morse[62] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----","----.","---..","--...","-....",".....","-....","--...","---..","----."};
string decryption;
cin.ignore();
string message ;
cout <<"Enter your cipher (for example, ... --- ... ... --- ...) : "<<endl;
getline(cin,message);
string letter;
for(int i=0; i< message.length(); i++)
{
while (/*(int)*/message[i]!=32)
{
letter+=message[i];
i++;
}
for(int j=0 ; j<62; ++j)
{
if(letter == Morse[j])
{
decryption += alphabet[j];
letter="";
}
}
if (/*(int)*/message[i]==32)
{
if (/*(int)*/(message[i+1]==32 && message[i+2] == 32)||(message[i+1]==32))
{
decryption+=" ";
}
}
}
cout<<decryption;
}
else if (choosenumber == "3")
{
break ;
}
else
{
cout <<"Error \n";
string choosemunber ;
cout << "Enter any key to repeat the list :";
cin >> choosenumber ;
}
}
return 0 ;
}