# 字符串处理：巧用stack: 1082 Read Number in Chinese (25)分)

## 1试题内容：

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

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

output

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

-123456789

Sample Output

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input

100800

Sample Output

yi Shi Wan ling ba Bai

## 5测试数据:自己想的一些容易错的测试数据

1. 50000015
2. 10000000
3. 100000000
4. 0
5. 00015468
6. -000151548
#include <cstdio>
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int input;
string s;
string order_digit[4]={"none","Shi","Bai","Qian"}; //注意开头字母要大写
string digit[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
stack <string> ans;
void ans_read(string t){  //主要注意0的读取方式，和非零的读取方式
int len = t.length();
int flag =0,sign = 0;
for(int i=0;i<len;i++){
if(t[i]=='0'){ //其中零的读取中需要注意从个位数开始连续为零，与其他为开始连续为0 的差别
if(i==0)  flag = 1;
if(t[i+1]=='0'&&!flag){
if(!sign) {
ans.push("ling");
sign = 1;
}
if(!flag&&i==4) ans.push("Wan");
}
else if(t[i+1]=='0'&&flag)  {
if(len<9&&i==4) ans.push("Wan");//特殊数字 1000000 和yi Yi的差别
continue;
}
else {
if(flag) {
flag = 0;
continue;  //实现yi Yi的操作
}
else{
if(!sign) ans.push("ling");
if(i==4) ans.push("Wan");
sign = 0;
}
}
}
else{
if(i%4==0) {
if(i==4) ans.push("Wan");
if(i==8) ans.push("Yi");
ans.push(digit[t[i]-'0']);
}
else{
ans.push(order_digit[i%4]);
ans.push(digit[t[i]-'0']);
}
}
}
}
int main(){
scanf("%d",&input); //直接用整数输入可以避免000005265的数据处理
if(input<0) {
printf("Fu "); //注意大写开头
input = -input;
}
if(input==0) printf("ling");
else{
while(input!=0){
char t = input%10+'0';
input /= 10;
s += t;
}
int num = 0;
while(ans.size()!=0){
if(num!=0) printf(" ");
cout<<ans.top();
ans.pop();
num++;
}
printf("\n");
}
return 0;
}


## 6 知识补充stack

### 6.1 stack 的定义

stack <int> s; // 类型和上面的容器相同，可以是基本数据类型或者容器

### 6.2 stack 容器内元素的访问

for (int i = 0; i < 6; i++) {
s.push(i); // 将元素i压⼊栈s中
}
while(s.size()!=0){
cout << s.top() << endl; // 访问s的栈顶元素
s.pop(); //弹出栈顶元素
} // 相当于遍历所有的元素，并且遍历完后s 中的元素会被清空。


### 6.3stack常用函数：

1. push()
2. pop()
3. top()
4. size()
5. empty() // 检测stack内是否为空，返回ture为空，返回false为非空

06-25 799

08-25 49

09-04 69

10-21 416

09-10 124

03-04 41

03-19 1284

11-08 52

12-19 16

10-01 143

01-21 6

08-19 88

07-31 85

12-10 78

09-11 30

04-14 58万+

03-13 14万+

03-04 13万+

03-05 5688

03-08 4万+

03-08 7万+

03-10 12万+

03-12 11万+

03-13 11万+

03-14 9969

03-18 5412

03-18 8669

03-19 3万+

03-19 8万+

03-20 7441

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-29 21万+

03-29 1万+

03-29 10万+

03-30 16万+

05-28 5130

05-28 1万+

04-02 4万+

04-03 3066

04-06 7万+

04-07 5万+

04-09 8万+

04-09 2万+

05-17 7393

04-10 2465

04-11 3万+

04-15 5万+

04-18 4万+

04-20 4万+

04-24 3万+

04-29 5812

04-29 5618

04-30 4568

04-30 8849

04-30 8355

05-16 5万+

05-08 4万+

05-11 3万+

05-12 1万+

05-13 419

05-14 5630

05-14 1478

05-16 1万+

05-20 497

05-18 7631

05-27 527

05-18 2940

05-19 9788

05-21 5592

05-21 5602

05-25 3327

05-22 151

05-23 1万+

05-23 5903

05-24 1575

#### 自律不熬夜真的那么难吗？

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客