*翻译电话号码

题目描述:

将电话号码 one two...nine zero翻译成1  2...9 0

中间会有double

例如输入:onetwothree

输出:123

输入:onetwodoubletwo

输出:1222

输入:1two2 输出:ERROR

输入:doubledoubletwo 输出:ERROR

有空格,非法字符,两个double相连,double位于最后一个单词,都错误,输出:ERROR

代码:

#include<stdio.h>  
#include<string.h>  
#include<stdlib.h>  
int main()  
{  
    char a[100]="\0",out[100]="\0";  
    char temp[10]="\0";  
    gets(a);  
    //对有非法字符打印错误  
    for(int i=0;a[i]!='\0';i++)  
    {  
        if(a[i]<'a'||a[i]>'z')  
        {  
            printf("ERROR\n");  
            return 0;  
        }  
    }  
    char b[11][11]={"zero","one","two","three","four","five","six","seven","eight","nine","double"};  
    //将对应的电话号码变成字符存到out数组中,输入错误时打印错误  
    int i,k=0;  
    for(i=0;a[i]!='\0';)  
    {  
        strncpy(temp,a+i,6);  
        if(strncmp(temp,b[0],4)==0)  
        {  
            out[k++]='0';  
            i=i+4;  
            continue;  
        }  
        else if(strncmp(temp,b[1],3)==0)  
        {  
            out[k++]='1';  
            i=i+3;  
            continue;  
        }  
        else if(strncmp(temp,b[2],3)==0)  
        {  
            out[k++]='2';  
            i=i+3;  
            continue;  
        }  
        else if(strncmp(temp,b[3],5)==0)  
        {  
            out[k++]='3';  
            i=i+5;  
            continue;  
        }  
        else if(strncmp(temp,b[4],4)==0)  
        {  
            out[k++]='4';  
            i=i+4;  
            continue;  
        }  
        else if(strncmp(temp,b[5],4)==0)  
        {  
            out[k++]='5';  
            i=i+4;  
            continue;  
        }  
        else if(strncmp(temp,b[6],3)==0)  
        {  
            out[k++]='6';  
            i=i+3;  
            continue;  
        }  
        else if(strncmp(temp,b[7],5)==0)  
        {  
            out[k++]='7';  
            i=i+5;  
            continue;  
        }  
        else if(strncmp(temp,b[8],5)==0)  
        {  
            out[k++]='8';  
            i=i+5;  
            continue;  
        }  
        else if(strncmp(temp,b[9],4)==0)  
        {  
            out[k++]='9';  
            i=i+4;  
            continue;  
        }  
        else if(strncmp(temp,b[10],6)==0)  
        {  
            out[k++]='d';  
            i=i+6;  
            continue;  
        }  
        else  
        {  
            printf("ERROR\n");  
            return 0;  
        }  
    }  
    //排除连续d和最后d的情况  
    if(out[k-1]=='d')  
    {  
        printf("ERROR\n");  
        return 0;  
    }  
    for(i=0;i<k-1;i++)  
    {  
        if(out[i]=='d'&&out[i]==out[i+1])  
        {  
            printf("ERROR\n");  
            return 0;  
        }  
    }  
    //将d转换为d后面的数字符号  
    for(i=0;i<k-1;i++)  
    {  
        if(out[i]=='d')  
            out[i]=out[i+1];  
    }  
    printf("%s\n",out);  
    return 0;  
}
或者

#include <iostream>   
#include <string>         
using namespace std;      
void process(string str)      
{      
    //各字符串的下标即为其对应的阿拉伯数字      
    char *ch[]={"zero","one","two","three","four","five","six","seven","eight","nine","double"};  
    char *out[100];//存储结果集  
  
    int f=0;  
    int i,j,k;    
      
    string substr;      
    int len=str.length();   
    int flag=0;//判断double出现  
    int flag2=0;//判断内循环有无break  
  
    for(i=0;i<len;i++)      
    {   
        if(flag2)  
            break;  
        else if(str[i]<97 || str[i]>122)//非字母字符  
        {  
            out[f++]="error";  
            break;  
        }  
        else{  
            //可以看出拼音最短为3个字符,最长为6个字符      
            for(j=i+2;j<len&&(j-i)<6;j++)      
            {      
                substr=str.substr(i,j-i+1);      
                if(!substr.compare(ch[10]))//出现double  
                {  
                    if(i==len-6)//最后一个double  
                    {  
                        out[f++]="error";  
                        flag2=1;  
                        break;  
                    }else if(flag){//连续两个double  
                        out[f++]="error";  
                        flag2=1;  
                        break;  
                    }else  
                        flag=1;  
                }  
                else{  
                    for(k=0;k<10;k++)   
                    {  
                        if(!substr.compare(ch[k]))      
                        {      
                            if(flag){  
                                out[f++]=ch[k];  
                                out[f++]=ch[k];  
                                flag=0;  
                                break;                                
                            }  
                            else{  
                                out[f++]=ch[k];     
                                break;  
                            }  
                        }   
                    }  
                }  
            }   
        }  
    }    
      
    int flag3=0;//看有无error,默认没有error  
    for(int r=0;r<f;r++){  
        if(out[r]=="error")//有error  
            flag3=1;  
    }  
  
    if(!flag3)  
        for(r=0;r<f;r++)  
            for(k=0;k<10;k++)   
            {  
                if(out[r]==ch[k])      
                    printf("%d",k);  
            }  
    else  
        printf("error");  
  
    printf("\n");    
}      
      
void main()    
{    
    string str;      
    while(getline(cin,str))    
        process(str);   
}    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值