算法提高 9-3摩尔斯电码

来源:互联网 发布:淘宝的潘多拉是真的吗 编辑:程序博客网 时间:2024/04/30 21:42

问题描述
  摩尔斯电码破译。类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文。请不要使用"zylib.h",只能使用标准库函数。用' * '表示' . ',中间空格用' | '表示,只转化字符表。

  摩尔斯码定义见:http://baike.baidu.com/view/84585.htm?fromId=253988。

提示
  清橙进行评测时,输入是以EOF结尾的,而不是换行符。(EOF不是一个字符,“以EOF结尾”是一种通俗但不严谨的说法。)因此可以通过以下方式之一获取输入:

  1. 一次读入整行字符串,再进行后续解析。

  2. 使用getchar或scanf一次读入一个字符,通过它们的返回值判断输入结束。
样例输出

思路:

        将每种情况罗列出来,将 | 符号前的符号与自己罗列的情况对比,符合就输出

代码:

#include<iostream>#include<cstdio>#include<cstring>using namespace std;void pp(char *s){if (strcmp("*-",s)==0)  cout<<"a";else if (strcmp("-***",s)==0)   cout<<"b";else if (strcmp("-*-*",s)==0)  cout<<"c";else if (strcmp("-**",s)==0)   cout<<"d";else if (strcmp("*",s)==0)  cout<<"e";else if (strcmp("**-*",s)==0)   cout<<"f";   else if (strcmp("--*",s)==0)  cout<<"g";else if (strcmp("****",s)==0)   cout<<"h";   else if (strcmp("**",s)==0)  cout<<"i";else if (strcmp("*---",s)==0)   cout<<"j";   else if (strcmp("-*-",s)==0)  cout<<"k";else if (strcmp("*-**",s)==0)   cout<<"l";   else if (strcmp("--",s)==0)  cout<<"m";else if (strcmp("-*",s)==0)   cout<<"n";   else if (strcmp("---",s)==0)  cout<<"o";else if (strcmp("*--*",s)==0)   cout<<"p";   else if (strcmp("--*-",s)==0)  cout<<"q";else if (strcmp("*-*",s)==0)   cout<<"r";   else if (strcmp("**",s)==0)  cout<<"s";else if (strcmp("-",s)==0)   cout<<"t";   else if (strcmp("**-",s)==0)  cout<<"u";else if (strcmp("***-",s)==0)   cout<<"v";   else if (strcmp("*--",s)==0)  cout<<"w";else if (strcmp("-**-",s)==0)   cout<<"x";   else if (strcmp("-*--",s)==0)  cout<<"y";else if (strcmp("--**",s)==0)   cout<<"z";}int main(){char ch[100],s[10],k=0;int i=0,j;while (cin>>ch){i=0;while (1){for ( ;ch[i]!='|'&&ch[i];i++)  if (ch[i]!='|')  s[k++]=ch[i];  s[k]='\0';  pp(s);  i++;  k=0;if (ch[i]=='\0')   break;    }cout<<endl; }return 0;}


0 0
原创粉丝点击