Repository navigation
Expand file tree
/
Copy pathScript.js
More file actions
182 lines (158 loc) · 6.97 KB
/
Copy pathScript.js
File metadata and controls
182 lines (158 loc) · 6.97 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Morse Code Mappings
const morseEncodeMap = {
'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': '--..', '1': '.----',
'2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
'0': '-----', ' ': '/'
};
const morseDecodeMap = Object.fromEntries(Object.entries(morseEncodeMap).map(([k, v]) => [v, k]));
// Function to encode text to Morse code
function encodeToMorse(text) {
return text.toUpperCase().split('').map(char => morseEncodeMap[char] || '?').join(' ');
}
// Function to decode Morse code to text
function decodeFromMorse(morseCode) {
return morseCode.split(' ').map(code => morseDecodeMap[code] || '?').join('');
}
// Event listeners for buttons
document.getElementById('encodeBtn').addEventListener('click', () => {
const inputText = document.getElementById('inputText').value;
const encodedText = encodeToMorse(inputText);
document.getElementById('outputText').value = encodedText;
});
document.getElementById('decodeBtn').addEventListener('click', () => {
const inputText = document.getElementById('inputText').value;
const decodedText = decodeFromMorse(inputText);
document.getElementById('outputText').value = decodedText;
});
// Python and C++ code for download
const pythonCode = `
# Morse Code Mappings
morse_encode_map = {
'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': '--..', '1': '.----',
'2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.',
'0': '-----', ' ': '/'
}
morse_decode_map = {v: k for k, v in morse_encode_map.items()}
# Function to encode text to Morse code
def encode_to_morse(text):
return ' '.join(morse_encode_map.get(char.upper(), '?') for char in text)
# Function to decode Morse code to text
def decode_from_morse(morse_code):
return ''.join(morse_decode_map.get(code, '?') for code in morse_code.split())
# Main function
def main():
while True:
print("Morse Code Encoder/Decoder")
print("1. Encode to Morse Code")
print("2. Decode from Morse Code")
print("3. Exit")
choice = input("Choose an option (1, 2, or 3): ")
if choice == '1':
text = input("Enter text to encode: ")
encoded_text = encode_to_morse(text)
print("Encoded Morse Code:", encoded_text)
elif choice == '2':
morse_code = input("Enter Morse Code to decode (use space between characters and '/' for space between words): ")
decoded_text = decode_from_morse(morse_code)
print("Decoded Text:", decoded_text)
elif choice == '3':
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
`;
const cppCode = `
#include <iostream>
#include <unordered_map>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
// Morse Code Mappings
unordered_map<char, string> morseEncodeMap = {
{'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', "--.."}, {'1', ".----"},{'2', "..---"},{'3', "...--"},{'4', "....-"},
{'5', "....."},{'6', "-...."},{'7', "--..."},{'8', "---.."},{'9', "----."},
{'0', "-----"},{' ', " / "}
};
unordered_map<string, char> morseDecodeMap = {
{".-", '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'}, {".----", '1'},{"..---", '2'},{"...--", '3'},{"....-", '4'},
{".....", '5'},{"-....", '6'},{"--...", '7'},{"---..", '8'},{"----.", '9'},
{"-----", '0'},{" / ", ' '}
};
// Function to encode text to Morse code
string encodeToMorse(const string &text) {
stringstream morseStream;
for (const char &ch : text) {
char upperChar = toupper(ch);
if (morseEncodeMap.find(upperChar) != morseEncodeMap.end()) {
morseStream << morseEncodeMap[upperChar] << " ";
} else {
morseStream << "? "; // Unknown character
}
}
return morseStream.str();
}
// Function to decode Morse code to text
string decodeFromMorse(const string &morseCode) {
stringstream textStream;
stringstream morseStream(morseCode);
string morseChar;
while (morseStream >> morseChar) {
if (morseDecodeMap.find(morseChar) != morseDecodeMap.end()) {
textStream << morseDecodeMap[morseChar];
} else {
textStream << "?"; // Unknown Morse code
}
}
return textStream.str();
}
// Main function
int main() {
string input;
int choice;
cout << "Morse Code Encoder/Decoder\\n";
cout << "1. Encode to Morse Code\\n";
cout << "2. Decode from Morse Code\\n";
cout << "Choose an option (1 or 2): ";
cin >> choice;
cin.ignore(); // Ignore the newline character after choice
if (choice == 1) {
cout << "Enter text to encode: ";
getline(cin, input);
string morseCode = encodeToMorse(input);
cout << "Encoded Morse Code: " << morseCode << "\\n";
} else if (choice == 2) {
cout << "Enter Morse Code to decode (use space between characters and '/' for space between words): ";
getline(cin, input);
string decodedText = decodeFromMorse(input);
cout << "Decoded Text: " << decodedText << "\\n";
} else {
cout << "Invalid choice.\\n";
}
return 0;
}
`;
// Function to create a blob and set the href attribute for download
function createDownloadLink(code, elementId) {
const blob = new Blob([code], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
document.getElementById(elementId).href = url;
}
// Create download links for Python and C++ files
createDownloadLink(pythonCode, 'downloadPython');
createDownloadLink(cppCode, 'downloadCpp');