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

来源:互联网 发布:gnome和x windows 编辑:程序博客网 时间:2024/05/01 02:19

算法提高 9-3摩尔斯电码
时间限制:1.0s 内存限制:256.0MB
提交此题
问题描述
  摩尔斯电码破译。类似于乔林教材第213页的例6.5,要求输入摩尔斯码,返回英文。请不要使用”zylib.h”,只能使用标准库函数。用’ * ‘表示’ . ‘,中间空格用’ | ‘表示,只转化字符表。

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

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

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

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

这里写图片描述

这里的输入要注意了,每次用’\0’判断输入

#include <cstdio>#include <iostream>#include <cstring>using namespace std;void check(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[110];    char str[110];    int k=0,i;    while(cin>>ch)    {        i=0;        while(1)        {            for(; ch[i]!='|'&&ch[i]; i++)                if(ch[i]!='|')                    str[k++]=ch[i];            str[k]='\0';            check(str);            i++;            k=0;            if(ch[i]=='\0')                break;        }        cout<<endl;    }    return 0;}
0 0