【PAT】1082. Read Number in Chinese (25)

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu". Note: zero ("ling") must be handled correctly according to the Chinese tradition. For example, 100800 is "yi Shi Wan ling ba Bai".

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:
-123456789
Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input 2:
100800
Sample Output 2:

yi Shi Wan ling ba Bai


分析:要注意0的转换,如0, 10,  100,  108, 1008, 100008 等情况。

代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <stack>
using namespace std;

void toChinese(vector<string> &v){
	v.push_back("ling");
	v.push_back("yi");
	v.push_back("er");
	v.push_back("san");
	v.push_back("si");
	v.push_back("wu");
	v.push_back("liu");
	v.push_back("qi");
	v.push_back("ba");
	v.push_back("jiu");
}

void setOther(vector<string> &v){
	v.push_back("Shi");
	v.push_back("Bai");
	v.push_back("Qian");
	v.push_back("Wan");
}

int main(int argc, char** argv) {
	vector<string> chinese;
	toChinese(chinese);
	vector<string> other; //进制 
	setOther(other);
	
	int i, num, cnt, t;
	string str;
	cin>>str;	 
	if( (str.size()==1&&str[0]=='0') || (str.size()==2 && str[0]=='-' && str[1]=='0') ){
		printf("ling\n");
		return 0;
	}
	
	stack<string> sta;
	cnt = 0;
	t = 0;
	for(i=str.size()-1; i>=0; i--){
		t++;
		num = str[i]-'0';
		if(t==9){
			//亿 
			sta.push("Yi");	
			sta.push(chinese[num]);
			continue;	 
		}
		
		if(str[i]=='-'){
		    sta.push("Fu");
			break;
		}	
			
		if(num != 0){
			if(cnt >= 1){
				sta.push(other[cnt-1]);//进制	
			}			  
			sta.push(chinese[num]);//数字					
		}else{
			if(sta.empty() || sta.top()=="ling"){
				 if(!sta.empty() && sta.top()=="ling" && cnt==4){
				 	sta.push("Wan");
				 }
			}else{
				if(sta.top()!="Wan")
				  sta.push("ling");			 
			}
		} 					
		if(cnt == 4){
			cnt=0;
		}		
		cnt++;		
	}	
	
	cout<<sta.top();
	sta.pop();
	while(!sta.empty()){
		cout<<" "<<sta.top();
		sta.pop();
	}
	cout<<endl;	
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值