PAT1082 Read Number in Chinese
坑太多了,调了一个多小时,最后发现tm 0的时候没有特判,艹
#include<iostream>
#include<cstring>
using namespace std;
string num[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string c1[5]={" ","Wan","Shi","Bai","Qian"};
string c2[5]={" "," ","Shi","Bai","Qian"};
int main()
{
string s;
bool ff=false;
cin>>s;
if(s=="0")
{
cout<<"ling";
return 0;
}
if(s[0]=='-')
{
cout<<"Fu";
ff=true;
s = s.erase(0,1);
}
bool flag1=false;
bool flag2=false;
bool flag3=false;
bool flag4=false;
if(s.length()==9)
{
int t=s[0]-'0';
if(ff)
{
cout<<" "<<num[t]<<" Yi";
}
else
{
ff=true;
cout<<num[t]<<" Yi";
}
s = s.erase(0,1);
}
if(s.length()<=8&&s.length()>4)
{
flag4=true;
int cnt=s.length()-4;
string s1=s.substr(0,cnt);
for(int i=0;i<cnt;i++)
{
int t=s[i]-'0';
if(t==0&&!flag1&&stoi(s.substr(i,s1.length()-i))!=0)
{
flag1=true;
cout<<" ling";
}
else if(t==0&&!flag1&&stoi(s.substr(i,s1.length()-i))==0)
{
cout<<" Wan";
s=s.erase(0,cnt);
flag3=true;
break;
}
else if(t==0&&stoi(s.substr(i,s1.length()-i))!=0)
{
}
else if(t==0&&stoi(s.substr(i,s1.length()-i))==0)
{
cout<<" Wan";
s=s.erase(0,cnt);
flag3=true;
break;
}
else{
if(ff)
{
cout<<" "<<num[t]<<" "<<c1[cnt-i];
}
else
{
ff=true;
cout<<num[t]<<" "<<c1[cnt-i];
}
}
}
}
if(!flag3&&flag4)s=s.erase(0,s.length()-4);
if(s.length()<=4)
{
for(int i=0;i<s.length();i++)
{
int t=s[i]-'0';
if(t==0&&!flag2&&stoi(s.substr(i,s.length()-i))!=0)
{
flag2=true;
cout<<" ling";
}
else if(t==0&&flag2)
{
}
else if(t==0&&flag2&&stoi(s.substr(i,s.length()-i))==0)
{
}
else if(t==0&&!flag2&&stoi(s.substr(i,s.length()-i))==0)
{
return 0;
}
else if(t!=0&&i==s.length()-1){
cout<<" "<<num[t];
}
else
{
if(ff)
{
cout<<" "<<num[t]<<" "<<c2[s.length()-i];
}
else
{
ff=true;
cout<<num[t]<<" "<<c2[s.length()-i];
}
}
}
}
}