usaa20/a5/program.c

135 lines
2.9 KiB
C
Raw Normal View History

2020-12-03 13:45:07 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2020-12-03 14:00:02 +00:00
#define MAX 1000
2020-12-03 13:45:07 +00:00
struct morsetree{
char letter;
struct morsetree *dash;
struct morsetree *dot;
};
struct morsetree* create(char l){
struct morsetree* new = calloc(1, sizeof(struct morsetree));
new->letter = l;
new->dot = NULL;
new->dash = NULL;
return new;
}
struct morsetree* add(struct morsetree *tree, char *morse, char letter){
struct morsetree *this;
int i, size;
this = tree;
size = strlen(morse);
for(i = 0; i < size; i++) {
if(morse[i] == '.') {
if(this->dot == NULL){
this->dot = create('*');
}
this = this->dot;
}
else if(morse[i] == '-'){
if(this->dash == NULL){
this->dash = create('*');
}
this = this->dash;
}
}
this->letter = letter;
}
void destroy(struct morsetree *root){
if(root == NULL) return;
destroy(root->dot);
destroy(root->dash);
free(root);
root = NULL;
}
void search(struct morsetree* root, char* decode){
struct morsetree* this = root;
int i = 0;
for(i == 0; i <= strlen(decode); i++){
2020-12-03 13:56:44 +00:00
if(i == strlen(decode) && decode[strlen(decode)-1] == ' '){
return;
}
2020-12-03 13:45:07 +00:00
if(decode[i] == ' ' || decode[i] == '\n' || decode[i] == '\0'){
printf("%c", this->letter);
this = root;
}
if(decode[i] == '/'){
printf(" ");
}
else{
if(decode[i] == '.'){
this = this->dot;
}
else if(decode[i] == '-'){
this = this->dash;
}
}
}
2020-12-03 13:56:44 +00:00
2020-12-03 13:45:07 +00:00
}
int main (){
struct morsetree *root = create('*');
add(root, ".-", 'A');
add(root, "-..." ,'B' );
add(root, "-.-.", 'C' );
add(root, "-..", 'D');
add(root, ".", 'E');
add(root, "..-.", 'F');
add(root, "--.", 'G');
add(root, "....", 'H');
add(root, "..", 'I');
add(root, ".---", 'J');
add(root, "-.-", 'K');
add(root, ".-..", 'L');
add(root, "--", 'M');
add(root, "-.", 'N');
add(root, "---", 'O');
add(root, ".--.", 'P');
add(root, "--.-", 'Q');
add(root, ".-.", 'R');
add(root, "...", 'S');
add(root, "-", 'T');
add(root, "..-", 'U');
add(root, "...-", 'V');
add(root, ".--", 'W');
add(root, "-..-", 'X');
add(root, "-.--", 'Y');
add(root, "--..", 'Z');
add(root, ".----", '1');
add(root, "..---", '2');
add(root, "...--", '3');
add(root, "....-", '4');
add(root, ".....", '5');
add(root, "-....", '6');
add(root, "--...", '7');
add(root, "---..", '8');
add(root, "----.", '9');
add(root, "-----", '0');
char line[MAX];
fgets(line, MAX, stdin);
line[strlen(line)-1] = '\0';
2020-12-03 13:46:01 +00:00
2020-12-03 13:45:07 +00:00
search(root, line);
2020-12-03 13:57:42 +00:00
printf("\n");
2020-12-03 13:58:49 +00:00
printf("\n");
2020-12-03 13:45:07 +00:00
destroy(root);
}