本文主要是介绍正整数的汉语读法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述:现在让你在普通的计算器(位数最多12位)上增加一个功能button,点击这个button之后能进行数字显示模式的切换,即能显示我们汉语对数字表达的习惯。比如计算器最初显示“零”,你按“1”这个按键之后,屏幕显示的是“一”,继续按”9”这个键,显示的是”十九”,继续按“0”这个键,显示的是“一百九十”… …而且,还要增加两个功能button,“退格键”和“清空键”。继续按退格键,显示的是“十九”,继续按“清空建”,显示的是“零”。
正确代码置顶:
------------------------------------------------------------------------------------
如果位数很大,兆以上的单位可以用getBase函数来获得,那么代码可以这么写:
1. 分段,如果不存在0,在len-i ==5 , == 9, == 13的位置输出万,亿,兆,其他位置输出数字+十百千即可
2. 如果存在0,遍历这段连续的0,如果中间经历len-i == 13, 9, 5的位置,输出相应的第一个单位兆>亿>万,之后break
·这里得强调一下: 当前一个非0输出一个base后,这个时候连续4个0是不用被输出的!!!!所以有了(k-i)%4 != 3
3. 如果0是末位,中间不用加0,否则要加一个0
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
string getBase(int num) {if (num == 5)return "万";else if (num == 9)return "亿";else if (num == 13)return "兆";elsereturn "";
}
Another version:int main() {string input = "101030100105";string res = "";string num[] = {"零","一","二","三","四","五","六","七","八","九"};string unit[] = {"千","","十","百"};int len = input.length(), i, j;for (i = 0; i < len; ) {if (input[i] != '0') {res += num[input[i]-'0'];res += unit[(len-i)%4];res += getBase(len-i);++i;}else {int j = i;while (j < len && input[j] == '0')++j; for (int k = i; k < j; ++k)if (getBase(len-k) != "" && (k-i)%4 != 3) {res += getBase(len-k);break;}if (j != len)res += "零";i = j;}}return 0;
}
-------------------------------------------------------------------------------------
注意连续的0的位置即可:
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
string print(const string& num) {string str[] = {"零","一","二","三","四","五","六","七","八","九","十","百","千"};string unit[] = {"十","百","千"};string res = "";int i, j, len = num.length();for (i = 0; i < len; ++i) {if (num[i] == '0') {for (j = i + 1; num[j] == '0' && j < len; ++j);if (len - (i - 1) >= 6 && len - j <= 4)res += "万";if (j != len)res += "零";i = j - 1;continue;}else {res += str[num[i] - '0'];//cout << (len - i) % 5 << endl;// 2 3 4 6 7 8if ((len - i) % 4 != 1)res += unit[(len - i - 2) % 4];}if (len - i == 5)res += "万";if (len - i == 9)res += "亿";}return res;
}
int main() {string s = "1010";cout << print(s) <<endl;return 0;
}
如果位数很大,兆以上的单位可以用getBase函数来获得,那么代码可以这么写:
1. 分段,如果不存在0,在len-i ==5 , == 9, == 13的位置输出万,亿,兆,其他位置输出数字+十百千即可
2. 如果存在0,遍历这段连续的0,如果中间经历len-i == 13, 9, 5的位置,输出相应的第一个单位兆>亿>万,之后break
3. 如果0是末位,中间不用加0,否则要加一个0
把握这三点即可。。。
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
string getBase(int i) {if (i == 5)return "万";else if (i == 9)return "亿";else if (i == 13)return "兆";elsereturn "";
}string print(const string& num) {string str[] = {"零","一","二","三","四","五","六","七","八","九"};string unit[] = {"十","百","千"};string res = "";int i, j, len = num.length();for (i = 0; i < len; ++i) {if (num[i] == '0') {for (j = i + 1; num[j] == '0' && j < len; ++j);for (int k = len - i; k >= len - j; --k) {string tmp = getBase(k);if (tmp != "") {res += tmp;break;}}if (j != len)res += "零";i = j - 1;continue;}else {res += str[num[i] - '0'];//cout << (len - i) % 5 << endl;// 2 3 4 6 7 8if ((len - i) % 4 != 1)res += unit[(len - i - 2) % 4];}res += getBase(len - i);}return res;
}
int main() {string s = "101030100105";cout << print(s) <<endl;return 0;
}
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
string getBase(int num) {if (num == 5)return "万";else if (num == 9)return "亿";else if (num == 13)return "兆";elsereturn "";
}
Another version:int main() {string input = "101030100105";string res = "";string num[] = {"零","一","二","三","四","五","六","七","八","九"};string unit[] = {"千","","十","百"};int len = input.length(), i, j;for (i = 0; i < len; ) {if (input[i] != '0') {res += num[input[i]-'0'];res += unit[(len-i)%4];res += getBase(len-i);++i;}else {int j = i;while (j < len && input[j] == '0')++j; for (int k = i; k < j; ++k)if (getBase(len-k) != "" && (k-i)%4 != 3) {res += getBase(len-k);break;}if (j != len)res += "零";i = j;}}return 0;
}
#include <stdio.h>
#include <string>
#include <string.h>
#include <iostream>
using namespace std;
string getBase(int num) {if (num == 5)return "万";else if (num == 9)return "亿";else if (num == 13)return "兆";elsereturn "";
}
Another version:int main() {string input = "101030100105";string res = "";string num[] = {"零","一","二","三","四","五","六","七","八","九"};string unit[] = {"千","","十","百"};int len = input.length(), i, j;for (i = 0; i < len; ) {if (input[i] != '0') {res += num[input[i]-'0'];res += unit[(len-i)%4];res += getBase(len-i);++i;}else {int j = i;while (j < len && input[j] == '0')++j; for (int k = i; k < j; ++k)if (getBase(len-k) != "" && (k-i)%4 != 3) {res += getBase(len-k);break;}if (j != len)res += "零";i = j;}}return 0;
}
这篇关于正整数的汉语读法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!