字符串处理,还是用string类比较容易,先将字符串用million和thousand切分成三段,然后用同样的方法处理这三段即可。
注意输入数据结尾有多余空行
View Code
#include
<
iostream
>
#include < cstdlib >
#include < cstring >
#include < cstdio >
using namespace std;
string name[] =
{ " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " };
int id[] =
{ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 };
int getid( string st)
{
for ( int i = 0 ; i < 27 ; i ++ )
if (st == name[i])
return id[i];
return - 1 ;
}
int toint( string st)
{
if (st == " zero " )
return 0 ;
int ret = 0 ;
int pos;
if ((pos = st.find( " hundred " )) != string ::npos)
{
ret += getid(st.substr( 0 , pos - 1 )) * 100 ;
if (st.length() >= pos + 8 )
st.erase( 0 , pos + 8 );
else
st.erase( 0 , pos + 7 );
}
if ((pos = st.find( " " )) != string ::npos)
{
ret += getid(st.substr( 0 , pos));
if (st.length() >= pos + 1 )
st.erase( 0 , pos + 1 );
else
st.erase( 0 , pos);
}
if (st.length() > 0 )
ret += getid(st);
return ret;
}
int main()
{
// freopen("D:\\t.txt", "r", stdin);
string st;
while (getline(cin, st) && st != "" )
{
int ans = 0 ;
int pos;
int ne = 1 ;
if (st.substr( 0 , 8 ) == " negative " )
{
ne = - 1 ;
st.erase( 0 , 9 );
}
if ((pos = st.find( " million " )) != string ::npos)
{
ans += 1000000 * toint(st.substr( 0 , pos - 1 ));
if (st.length() >= pos + 8 )
st.erase( 0 , pos + 8 );
else
st.erase( 0 , pos + 7 );
}
if ((pos = st.find( " thousand " )) != string ::npos)
{
ans += 1000 * toint(st.substr( 0 , pos - 1 ));
if (st.length() >= pos + 9 )
st.erase( 0 , pos + 9 );
else
st.erase( 0 , pos + 8 );
}
if (st.length() > 0 )
ans += toint(st);
ans *= ne;
printf( " %d\n " , ans);
}
return 0 ;
}
#include < cstdlib >
#include < cstring >
#include < cstdio >
using namespace std;
string name[] =
{ " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " };
int id[] =
{ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 };
int getid( string st)
{
for ( int i = 0 ; i < 27 ; i ++ )
if (st == name[i])
return id[i];
return - 1 ;
}
int toint( string st)
{
if (st == " zero " )
return 0 ;
int ret = 0 ;
int pos;
if ((pos = st.find( " hundred " )) != string ::npos)
{
ret += getid(st.substr( 0 , pos - 1 )) * 100 ;
if (st.length() >= pos + 8 )
st.erase( 0 , pos + 8 );
else
st.erase( 0 , pos + 7 );
}
if ((pos = st.find( " " )) != string ::npos)
{
ret += getid(st.substr( 0 , pos));
if (st.length() >= pos + 1 )
st.erase( 0 , pos + 1 );
else
st.erase( 0 , pos);
}
if (st.length() > 0 )
ret += getid(st);
return ret;
}
int main()
{
// freopen("D:\\t.txt", "r", stdin);
string st;
while (getline(cin, st) && st != "" )
{
int ans = 0 ;
int pos;
int ne = 1 ;
if (st.substr( 0 , 8 ) == " negative " )
{
ne = - 1 ;
st.erase( 0 , 9 );
}
if ((pos = st.find( " million " )) != string ::npos)
{
ans += 1000000 * toint(st.substr( 0 , pos - 1 ));
if (st.length() >= pos + 8 )
st.erase( 0 , pos + 8 );
else
st.erase( 0 , pos + 7 );
}
if ((pos = st.find( " thousand " )) != string ::npos)
{
ans += 1000 * toint(st.substr( 0 , pos - 1 ));
if (st.length() >= pos + 9 )
st.erase( 0 , pos + 9 );
else
st.erase( 0 , pos + 8 );
}
if (st.length() > 0 )
ans += toint(st);
ans *= ne;
printf( " %d\n " , ans);
}
return 0 ;
}