usaa20/a5/program.c

145 lines
3.1 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:36 +00:00
#define MAX 10000
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] == ' '){
2020-12-03 14:12:11 +00:00
printf("\n");
2020-12-03 13:56:44 +00:00
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 14:12:11 +00:00
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');
2020-12-03 14:03:22 +00:00
while(1){
char line[MAX];
fgets(line, MAX, stdin);
line[strlen(line)-1] = '\0';
2020-12-03 14:09:10 +00:00
if(line[0] == '\0'){
2020-12-03 14:12:11 +00:00
printf("\n");
2020-12-03 14:04:15 +00:00
destroy(root);
return 0;
2020-12-03 14:03:22 +00:00
}
search(root, line);
2020-12-03 14:10:09 +00:00
//printf("\n");
//printf("\n");
2020-12-03 14:03:22 +00:00
}
2020-12-03 14:09:10 +00:00
2020-12-03 14:04:15 +00:00
//destroy(root);
2020-12-03 14:09:10 +00:00
return 0;
2020-12-03 13:45:07 +00:00
}