translateMoney::translateMoney(){
str_chinese = "";
size = 0;
mark_c = 0;
mark_mod = 0;
}
std::string translateMoney::trans(std::string money){
mark_c = 0;
std::string bigLetter[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
std::string unit[] = {"元", "拾", "佰", "仟", "万", "亿"};
size = money.length();
bool z_flag = false;
int trans_num;
int c_count = 0;
bool c_flag = false;
for(int i = 0; i < size; i++){
z_flag = false;
trans_num = money.at(i) - 48;//逐位获取字符进行转换
mark_c = size - i - 1;
if(trans_num){//非零则直接转换
str_chinese += bigLetter[trans_num];
}
else{//否则检测是否为连续零,连续则缩为一个零,若为个或万或亿位(mark_c % 4 == 0)则不加零
if(mark_c % 4){
if(c_count){
c_count++;
if(c_count == 4){
c_flag = true;
c_count = 0;
}
}
if((int)(money.at(i + 1) - 48)){
str_chinese += bigLetter[trans_num];
}
}
else if(mark_c > 0){//为万或亿位后加零做判断
if((int)(money.at(i + 1) - 48)){
z_flag = true;
}
c_count++;
}
}
mark_mod = mark_c / 4;
if(trans_num){
if(mark_c % 4){
str_chinese += unit[mark_c % 4];
}
else{
if(mark_mod){
str_chinese += unit[mark_mod + 3];
}
else{
str_chinese += unit[mark_c % 4]; //+元
}
}
}
else{
if(!(mark_c % 4)){
if(mark_mod){
if(c_flag){
str_chinese += unit[mark_mod + 3];
c_flag = false;
}
if(z_flag){
str_chinese += bigLetter[trans_num];
}//+零
}
else{
str_chinese += unit[mark_c % 4]; //+元
}
}
}
}
return str_chinese;
}