原题:
将 电话号码 one two 。。。nine zero
翻译成1 2 。。9 0
中间会有double
例如输入:OneTwoThree 输出:123
输入:OneTwoDoubleTwo 输出:1222
输入:1Two2 输出:ERROR
输入:DoubleDoubleTwo 输出:ERROR
第三题:有空格,非法字符,两个Double相连,Double位于最后一个单词 都错误
贴代码,不怎么简洁:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char s[100];
while(cin>>s)
{
int in=0,first=0,second=0 ,last=0;
char up='a';
while(s[in]!='\0')
{
if(s[in]==' ')
{
cout<<"error"<<endl;
return 0;
}
if(s[in]>='A' && s[in]<='Z')
{
up=s[in];
if(s[in]=='D')
{
if(first==0)
{
first=1;
}
else
{
cout<<"error"<<endl;
return 0;
}
}
else
{
first=0;
}
}
in++;
}
if(up=='D')
{
cout<<"error"<<endl;
break;
}
char str[11][11]={"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Double"};
int i=0,j=0,m,d=0;
char temp[11];
while (s[i]!='\0')
{
temp[j++]=s[i];
if (s[i]>='A' && s[i]<='Z')
{
if(i!=0)
{
temp[j-1]='\0';
j=0;
for(m=0;m<11;m++)
{
if(!strcmp(temp,str[m]) && m<10)
{
cout<<m;
if(d==1)
{
cout<<m;
d=0;
}
break;
}else if(!strcmp(temp,str[m]) && m==10)
{
d=1;
break;
}
}
temp[j++]=s[i];
}
}
i++;
}
temp[j]=s[i];
if(i!=0)
{
temp[j]='\0';
j=0;
for(m=0;m<11;m++)
{
if(!strcmp(temp,str[m]) && m<10)
{
cout<<m;
if(d==1)
{
cout<<m;
d=0;
}
}
}
}
cout<<endl;
}
return 0;
}