字符串处理:巧用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

2题意分析

给定⼀个不超过9位的整数,你应该⽤传统的中⽂⽅式阅读它~ 如果是负的,⾸先输
出“Fu”。 例如, -123456789被读作“Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu”。

3重点注意

零(“ling”)的输出方式方法,必须根据中国传统正确处理。 例如, 100800是“yi Shi Wan ling ba Bai”~。

4题目忽视

数字的位数表达注意大小写。

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; 
		}
		ans_read(s); //采用倒序的方式进行读数,存入到stack中方便输出。 
		int num = 0;
		while(ans.size()!=0){
			if(num!=0) printf(" ");
			cout<<ans.top();
			ans.pop();
			num++;
		}
		printf("\n");
	}
	return 0;
} 

6 知识补充stack

头文件在<stack> 中,加上命名空间。

6.1 stack 的定义

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

6.2 stack 容器内元素的访问

这里的栈和数据结构中的栈一样,只能通过top()来访问栈顶元素

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为非空

勉励
打卡第十四天,加油ヾ(◍°∇°◍)ノ゙

如果觉得我的文章对你有所帮助与启发,点赞给我个鼓励吧(づ ̄3 ̄)づ╭❤
关注我和我一起共勉加油吧!
如果文章有错误,还望不吝指教!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值