//* struct used for lookup table - how to look up items and access them *

// __code needed in embedded system, not in Visual Studio // so let's define it as nothing #define __code // define the data type for the structure typedef struct A2MstructType { const char letter; __code const char *morse; }; // declare the gloabl constant array of structures static __code const struct A2MstructType cxTABLE[] = { { '0', "----- " }, //[0] { '1', ".---- " }, { '2', "..--- " }, { '3', "...-- " }, { '4', "....- " }, { '5', "..... " }, { '6', "-.... " }, { '7', "--... " }, { '8', "---.. " }, { '9', "----. " }, { 'A', ".- " }, //[10] { 'B', "-... " }, { 'C', "-.-. " }, { 'D', "-.. " }, { 'E', ". " }, { 'F', "..-. " }, { 'G', "--. " }, { 'H', ".... " }, { 'I', ".. " }, { 'J', ".--- " }, { 'K', ".-.- " }, //[20] { 'L', ".-.. " }, { 'M', "-- " }, { 'N', "-. " }, { 'O', "--- " }, { 'P', ".--. " }, { 'Q', "--.- " }, { 'R', ".-. " }, { 'S', "... " }, { 'T', "- " }, { 'U', "..- " }, //[30] { 'V', "...- " }, { 'W', ".-- " }, { 'X', "-..- " }, { 'Y', "-.-- " }, { 'Z', "--.. " }, { ' ', " " }, { '.', ".-.-.- " }, { ',', "--..-- " }, { '?', "..--.. " }, }; //[40] #include "stdio.h" unsigned char find_index1(char data) { unsigned char pos = -1; // Note: it is not really -1 if (data>='A' && data<='Z') pos = data-'A'+10; else if (data>='a' && data<='z') pos = data-'a'+10; else if (data>='0' && data<='9') pos = data-'0'; else if (data==' ') pos = 36; else if (data=='.') pos = 37; else if (data==',') pos = 38; else if (data=='?') pos = 39; // else it is already -1 return(pos); } unsigned char find_index2(char data) { unsigned char pos; if (data>='a' && data<='z') data = data - 'a' + 'A'; for (pos=0; pos<40; pos++) if ( cxTABLE[pos].letter == data ) break; if (pos==40) pos=-1; // NOT FOUND VALUE: it is not really -1 return(pos); } void test3() { unsigned char c; unsigned char i, j; while(1) { scanf("%c", &c); if (c=='\n') c = ' '; // handle \n character i = find_index2(c); if (i!=(unsigned char)(-1)) { for (j=0; cxTABLE[i].morse[j]!='\0'; j++) { printf("%c", cxTABLE[i].morse[j]); } printf("\n"); } else { printf("?\n"); } } } void main() { test3(); }