#include<stdio.h>
#include<stdlib.h>
char * makeMorse(char ch, char *m){
int i;
for(i=0;i<8;i++){
m[7 - i]=(ch%2)?'.':'-';
ch = ch >>1;
}
return m;
}
char getMorse(char *m){
int i;
char ch = 0;
for(i=0;i<8;i++){
ch = ch << 1;
ch = ch |((m[i] == '.')?1:0);
}
return ch;
}
int main(int argc,char *argv[]){
FILE *fp;
char buff[8],ch;
long filelen;
if(argc<2){
system("pause");
return 1;
}
if(argv[1][0] == 'm'){
puts("hello");
fp = fopen("Morse.y1","wb");
}else if(argv[1][0] == 'u'){
fp = fopen("Morse.y1","rb");
}else{
fp = NULL;
}
//if(argv[1][0] == 'm'){
// fp = fopen("Morse.y1","rb");
//}else{
// fp = NULL;
//}
if(argv[1][0] == 'm'){
//encode
while((ch = getchar())!='&'){
//printf("%d",sizeof(char));
fwrite(makeMorse(ch,buff),sizeof(char),8,fp);
}
}else{
//decode
fseek(fp,0,SEEK_END);
filelen = ftell(fp);
fseek(fp,0,SEEK_SET);
while(ftell(fp)<filelen){
fread(buff,sizeof(char),8,fp);
putchar(getMorse(buff));
}
system("pause");
}
if(!fp){
return 2;
}
fclose(fp);
system("pause");
return 0;
}