1.需要分类考虑的情况比较多
2.123456789可以分为 1 2345 6789 ,其中2345和6789的处理方法相同,不同点就是2345处理完后需要加上Wan
3.需要处理0等特殊情况
4下面有些特别的测试例子,可以参考:
/*
-123456789
123400000
123000000
120000000
100000000
123000010
-23456789
23400000
23000000
20000000
00000000
23000010
*/
//#include<string>
//#include <iomanip>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;
/*
-123456789
123400000
123000000
120000000
100000000
123000010
-23456789
23400000
23000000
20000000
00000000
23000010
*/
string num2Chinese[] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
string deleteZero(string str)
{
string tmp = "";
bool firstZero = true;
for (int i = 0; i < str.size(); i++)
{
if (str[i] == '0'&&firstZero)
;
else
{
firstZero = false;
tmp += str[i];
}
}
return tmp;
}
string digit1(string str)
{//只有一位数字的时候
if (str == "") return str;
else return num2Chinese[str[0] - '0'];
}
string digit2(string str)
{//两位数组,10为yi Shi,11为yi Shi yi,处理这两种特殊情况:1)个位为0:2)各位不为0
string strTmp = num2Chinese[str[0] - '0'] + " Shi";
string next = deleteZero(str.substr(1));
if (next.size() == 1)
return strTmp + " " + digit1(next);
else return strTmp;
}
string digit3(string str)
{//100,101,110,111处理三种特殊情况,1)十位不为0;2)十位为0,个位不为0;3)十位、个位均为0
string strTmp = num2Chinese[str[0] - '0'] + " Bai";
string next = deleteZero(str.substr(1));
if (next.size() == 2)
return strTmp + " " + digit2(next);
else if (next.size() == 1)
return strTmp + " ling " + digit1(next);
else return strTmp;
}
string digit4(string str)
{//1000,1001,1011,1111处理四种特殊情况,1)百位不为0;2)百位为0,十位不为0,个位不为0;3)百位、十位均为0,个位不为0;4)百位、十位、个位均为0
string strTmp = num2Chinese[str[0] - '0'] + " Qian";
string next = deleteZero(str.substr(1));
if (next.size() == 3)
return strTmp + " " + digit3(next);
else if (next.size() == 2)
return strTmp + " ling" + digit2(next);
else if (next.size() == 1)
return strTmp + " ling" + digit1(next);
else return strTmp;
}
string digitProc(string str)
{
switch (str.size())
{
case 0:return digit1(str);
case 1:return digit1(str);
case 2:return digit2(str);
case 3:return digit3(str);
case 4:return digit4(str);
default:
return "";
}
}
int main(void)
{
string str;
cin >> str;
bool sign = true;
if (str[0] == '-')
{
sign = false;
str = str.substr(1);
}
str = deleteZero(str);
string ans = "";
if (str.size() == 9)
{//包括亿位
string a = str.substr(0, 1);
string b = str.substr(1, 4);
string c = str.substr(5, 4);
ans = num2Chinese[a[0] - '0'] + " Yi";
string tmpB = deleteZero(b);
string tmpC = deleteZero(c);
if (tmpB.size() == 4 && tmpC.size() == 4)
ans += " " + digitProc(tmpB) + " Wan " + digitProc(tmpC);
else if (tmpB.size() == 4 && tmpC.size() == 0)
ans += " " + digitProc(tmpB) + " Wan ";
else if (tmpB.size() == 4 && tmpC.size() < 4)
ans += " " + digitProc(tmpB) + " Wan ling " + digitProc(tmpC);
else if (tmpB.size() == 0 && tmpC.size() == 0)
;
else if (tmpB.size() == 0 && tmpC.size() <= 4)
ans += " ling " + digitProc(tmpC);
else if (tmpB.size() < 4 && tmpC.size() == 0)
ans += " ling" + digitProc(tmpB) + " Wan ";
}
else if (str.size() < 9 && str.size() > 4)
{//不包括亿位,包括万位
string b = str.substr(0, str.size() - 4);
string c = str.substr(str.size() - 4);
string tmpB = deleteZero(b);
string tmpC = deleteZero(c);
if (tmpB.size() == b.size() && tmpC.size() == c.size())
ans += digitProc(tmpB) + " Wan " + digitProc(tmpC);
else if (tmpB.size() == 0 && tmpC.size() == 0)
;
else if (tmpB.size() == 0 && tmpC.size() <= 4)
ans += digitProc(tmpC);
else if (tmpB.size() == 4 && tmpC.size() == 0)
ans += digitProc(tmpB) + " Wan ";
else if (tmpB.size() < 4 && tmpC.size() == 0)
ans += digitProc(tmpB) + " Wan ";
else if (tmpB.size() < 4 && tmpC.size() < 4)
ans += digitProc(tmpB) + " Wan ling " + digitProc(tmpC);
else if (tmpB.size() == 4 && tmpC.size() < 4)
ans += digitProc(tmpB) + " Wan ling " + digitProc(tmpC);
}
else if (str.size() <= 4 && str.size() != 0)
{
ans = digitProc(str);
}
else if (str.size() == 0)
ans = "ling";
if (!sign)
ans = "Fu " + ans;
cout << ans << endl;
return 0;
}