闲来无事写的摩斯码、英语的互译命令行工具:trans

longqzh posted @ 2011年2月14日 13:08 in project with tags unix 摩斯码 莫斯码 翻译器 , 2868 阅读

trans程序的源代码在Msys下用gcc编译调试通过。

使用说明:trans  [-r/-s] srcfile objfile

    其中,-R 将摩斯码翻译成英文;
          -S 将英语翻译成摩斯码。
          srcfile为需要翻译的文本文件。运行成功后结果会保存在objfile文件中。

    注意:若是翻译英文,须保证所有字母均为大写字母。且标点符号仅可为“.”和“,”

代码已上传至github https://github.com/longqzh/MorseCode_Translator

/* *******************************
 *
 * Translator for Morse Code
 *
 * Author: Long Qianzhi
 * Date:2010/1/28
 *
 * *******************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 39

struct table{
	char letter;
	char mor[8];
}table[]={
	{'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',"--.."},{'0',"-----"},
	{'1',".----"},{'2',"..---"},{'3',"...--"},
	{'4',"....-"},{'5',"....."},{'6',"-...."},
	{'7',"--..."},{'8',"---.."},{'9',"----."},
	{'.',".-.-.-"},{',',"--..--"},{' ',".--.-."},
	{'\n',""}
};

FILE *infp=NULL, *outfp=NULL;

void check_file(char *in, char *out)
{
	if((infp=fopen(in,"r"))==NULL){
		printf("\nCan't open source file : %s\n", in);
		exit(1);
	}
	else if ((outfp=fopen(out,"w"))==NULL){
		printf("\nCan't open output file : %s\n", out);
		exit(1);
	}
	return;
}

int file_size(FILE *fp)
{
	int fsize=0;
	char c;
	while((c=fgetc(fp))!=EOF)
		fsize++;
	rewind(fp);
	return fsize;
}

char* make_buf(int n)
{
	char *pstr;
	pstr=(char *)malloc(n*sizeof(char));
	if(pstr==NULL){
		printf("\nMalloc failed!\n");
		exit(1);
	}
	return pstr;
}

void m2t()
{
	char *buffer;
	int fnum,n=0,i=0;
	char tmp[8]="";

	/* Read all of the data from srcfile to buffer.*/
	fnum=file_size(infp);
	buffer=make_buf(fnum+1);
	if((fread(buffer,sizeof(char),fnum,infp))!=fnum){
		printf("\nSomething wrong about reading file!\n");
		exit(1);
	}
	*(buffer+fnum)='\0';
	fclose(infp);

	printf("%s\n",buffer);fflush(stdout);

	while(*buffer!='\0'){

		/* Read a word from buffer to tmp.*/
		while(*buffer!=' ' && *buffer!='\0'){
			tmp[n++]=*buffer++;
			if(n>7){
				tmp[7]='\0';
				printf(" %s : Bad MorseCode!\n", tmp);
				exit(1);
			}
		}
		tmp[n]='\0';
		n=0; 
		printf("%s\t",tmp);fflush(stdout);
		

		while(strcmp(tmp,table[i++].mor)){
			if(i>=MAX && *(buffer+1)!='\0'){
				printf(" %s : CANNOT find it!\n", tmp);
				exit(1);
			}
		}

		fputc(table[--i].letter,outfp);
		printf("%c\t",table[i].letter);fflush(stdout);
		i=0;
		buffer++;
	}
	fclose(outfp);
	printf("The translated file is created.\n");
	fflush(stdout);
}

void t2m()
{
	char *buffer;
	int fnum,i=0;

	/* Read all of the data from srcfile to buffer.*/
	fnum=file_size(infp);

	buffer=make_buf(fnum+1);
	if((fread(buffer,sizeof(char),fnum,infp))!=fnum){
		printf("\nSomething wrong about reading file!\n");
		exit(1);
	}
	*(buffer+fnum)='\0';
	fclose(infp);

	printf("%s %d\n", buffer,(int)(*buffer));fflush(stdout);

	while(*buffer!='\0'){
		/* find the MorseCode according to character.*/
		while(*buffer!=table[i].letter){
			
			i++;
			fflush(stdout);
			if(i>MAX && *buffer!=EOF){
				printf(" %c %d: Bad character\n", *buffer,(int)(*buffer));
				exit(1);
			}
		}
		buffer++;
		printf("%c-->%s\t\n",table[i].letter,table[i].mor);fflush(stdout);

		fputs(table[i].mor,outfp);
		fputc(' ',outfp);
		i=0;
	}
	fclose(outfp);
	printf("The translated file is created.\n");
	fflush(stdout);
}

int main(int argc,char *argv[])
{
	if (argc!=4){
		printf("Usage: trans [-R/-S] srcfile outfile\n");
		printf("\n\t -r translate Morse to English.");
		printf("\n\t -s translate English to Morse.");
		printf("\n\t -h show this help information.\n");
		fflush(stdout);
		return 1;
	}

	if (!strcmp(argv[1],"-r") || !strcmp(argv[1],"-R")){
		check_file(argv[2],argv[3]);
		m2t();
	}
	else if (!strcmp(argv[1],"-s") || !strcmp(argv[1],"-S")){
		check_file(argv[2],argv[3]);
		t2m();
	}else{
		printf("You input wrong arguments!\n");
		printf("Please type \"trans -h\".\n");
		fflush(stdout);
	}
	return 0;
}
Avatar_small
AP SSC fa 4 Question 说:
2022年9月09日 03:10

Formative Assessment means not only an examination, it includes various aspects such as Examination in completed lessons, Reflections, Project work done on the allotted topic and Self also Prepared notes etc. AP SSC fa 4 Question Paper Candidates of Telugu Medium, English Medium & Urdu Medium of the AP State can download the AP 10th Class FA 4 Model Paper 2023 Pdf with answers for the regular exams conducted by the Directorate of Government Examinations, Andhra Pradesh.

Avatar_small
instagram profil lös 说:
2023年7月19日 22:33

Instagram ist eine weltweit bekannte Anwendung für soziale Netzwerke. Es ermöglicht Benutzern, mit Personen auf der ganzen Welt zu sprechen und sich mit ihnen zu verbinden. Benutzer von Instagram haben die Möglichkeit, Fotos, Videos, Geschichten usw. instagram profil löschen zu teilen, um Follower zu gewinnen, was dazu beiträgt, als Berühmtheit zu wachsen, wenn Sie eine gewöhnliche Person sind.

Avatar_small
BOSEM 10th Class Sy 说:
2023年7月20日 17:28

BOSEM Board 10th Class Exam date Sheet 2024 available at Official Website, Manipur Board as they are Preparing for Their 10th Class Exam 2024, A Careful Analysis of the Latest Syllabus gives them an idea of the Chapters and Topics which need to be Prepared for the Public Examinations,Manipur Board Regulates and Supervises the System of Manipur Board High School BOSEM 10th Class Syllabus 2024 Leaving Certificate Examination, Students This Webpage, we are Providing the Manipur Class syllabus 2024 All English, Hindi, Bengali, Sanskrit, Nepali Medium Subjects can be Downloaded Pdf Format.

Avatar_small
pavzi.com 说:
2024年1月10日 02:13

Pavzi.com is a startup by passionate webmasters and bloggers who have a passion for providing engaging content that is accurate, interesting, and worthy to read. pavzi.com We are more like a web community where you can find different information, resources, and topics on day-to-day incidents or news. We provide you with the finest web content on every topic possible with the help of the editorial and content team.

Avatar_small
civaget 说:
2024年1月18日 02:29

Your path to relaxation starts with 펀초이스. Choose from a wide range of top-rated massage businesses.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter