No.5 亿以下的正整数互相转换 (阿拉伯数字与汉字)
写了一个亿以下的阿拉伯数字和汉字之间的转换
本来觉得非常简单,后来发现有很多细节值得去挖掘
互相的转换我都采用了以整型数字为中介的方法
以及要注意!!!汉字在字符串内占两个字节,所以我采用了二维字符数组来输入输出汉字字符串
但是很遗憾,我的程序可跑的范围小1~100000000,而且鲁棒性较差,规范输入才能得到相应的值,之后我会一点一点改进的!
#include<iostream>
#include<cmath>
#include<string>
//亿以下的正整数转换,有“元”字是因为原题要求为金融数字
using namespace std;
char* ItoC(int T) { //将数字转为字符数组
switch (T)
{
case 1:
return "一";
case 2:
return "二";
case 3:
return "三";
case 4:
return "四";
case 5:
return "五";
case 6:
return "六";
case 7:
return "七";
case 8:
return "八";
case 9:
return "九";
}
return "零";
}
int CtoI(char* T) { 将字符数组转为数字
if (strcmp(T, "一") == 0) {
return 1;
}
else if (strcmp(T, "二") == 0) {
return 2;
}
else if (strcmp(T, "三") == 0) {
return 3;
}
else if (strcmp(T, "四") == 0) {
return 4;
}
else if (strcmp(T, "五") == 0) {
return 5;
}
else if (strcmp(T, "六") == 0) {
return 6;
}
else if (strcmp(T, "七") == 0) {
return 7;
}
else if (strcmp(T, "八") == 0) {
return 8;
}
else if (strcmp(T, "九") == 0) {
return 9;
}
else {
return 0;
}
}
int turn(int base, char** ch, int begin, int end) { //base在元之前万之后为1,在万之前亿之后为10000
int sum = 0;
int i0 = 0;
for (int i = begin; i > end; i--) { //begin大于end,因为是从后往前转化,另外为防止访问错误,设置为大于end
if (strcmp(ch[i], "元") == 0 || strcmp(ch[i], "万") == 0) {
i0 = CtoI(ch[i - 1])*base;
sum += i0;
}
else if (strcmp(ch[i], "十") == 0) {
i0 = CtoI(ch[i - 1])*base * 10;
sum += i0;
}
else if (strcmp(ch[i], "百") == 0) {
i0 = CtoI(ch[i - 1])*base * 100;
sum += i0;
}
else if (strcmp(ch[i], "千") == 0) {
i0 = CtoI(ch[i - 1])*base * 1000;
sum += i0;
}
}
return sum;
}
void swap(char &a, char &b) { //引用
char temp;
temp = a;
a = b;
b = temp;
}
char *translater1(char **ch, int length) {
char *temp;
temp = new char[100];
for (int i = 0; i < 100; i++) {
temp[i] = '\0';
}
int i = length - 1;
int sum = 0;
int check = 0;//检查是否有万字
int begin = 0;
int end = 0;
for (; i > 0; i--) {
int i0 = 1; //防止访问错误
if (strcmp(ch[i], "元") == 0) {
begin = i;
}
if (strcmp(ch[i], "万") == 0) { //以万为界限分开
check = 1;
end = i + 1;
sum += turn(1, ch, begin, end);
begin = i;
end = 0;
sum += turn(10000, ch, begin, end);
}
}
if (!check) { //检查到没有万
end = 0;
sum += turn(1, ch, begin, end);
}
for (int j = 0;; j++) {
if (sum == 0) {
break;
}
temp[j] = sum % 10 + '0';
sum /= 10;
}
for (int j = 0; j < strlen(temp) / 2; j++) {
swap(temp[j], temp[strlen(temp) - j - 1]);
}
return temp;
}
char **translater2(char *ch, int length) { //整体思路是把每个单位都写出来,在存的过程中来进行比较是否有书写不规范的情况
char **temp;
temp = new char*[1000];
for (int i = 0; i < 1000; i++) {
temp[i] = new char[3];
temp[i][0] = temp[i][1] = temp[i][2] = '\0';
}
int sum = 0;
int c = 0;
for (int i = length - 1; i >= 0; i--) {
sum += (ch[i] - '0')*pow(10, length - 1 - i);
}
int i = 0;
for (;; i++) {
if (sum == 0) {
break;
}
if (c == 4) {
strcpy(temp[i], "万");
i++;
}
if (c == 1 || c == 5) {
if (strcmp(temp[i - 1], "零") == 0) {
i--;
}
strcpy(temp[i], "十");
i++;
}
if (c == 2 || c == 6) {
strcpy(temp[i], "百");
i++;
}
if (c == 3 || c == 7) {
strcpy(temp[i], "千");
i++;
}
strcpy(temp[i], ItoC(sum % 10));
if (strcmp(temp[i], "零") == 0 && i != 0) { //防止数组超界
if (strcmp(temp[i - 1], "百") == 0 || strcmp(temp[i - 1], "千") == 0 || strcmp(temp[i - 1], "十") == 0) {
i -= 2;
strcpy(temp[i + 1], ItoC(0)); //去掉“零百”“零千”“零十”
i++;
}
}
if (strcmp(temp[i], "零") == 0 && i != 0) {
if (strcmp(temp[i - 1], "零") == 0 || strcmp(temp[i - 1], "万") == 0) { //防止出现“零零”,“零万”(为什么万不写在上面呢,因为万作为分界线是一定要有的)
i--; //“零零”、“零万”->“零”“万”
}
}
sum /= 10;
c++;
}
strcpy(temp[i], "元");
for (int j = 0; j < i / 2; j++) {
swap(temp[j], temp[i - j - 1]);
}
strcpy(temp[i + 1], "\0");
if (strcmp(temp[i - 1], "零") == 0) { //去掉“零元”
strcpy(temp[i - 1], "元");
strcpy(temp[i], "\0");
}
return temp;
}
int main() {
char ch1[200] = { 0 };
char ch2[200] = { 0 };
cin >> ch1;
int length = strlen(ch1) / 2;//汉字变阿拉伯数字
char** temp;
temp = new char*[length];
for (int i = 0; i < length; i++) {
temp[i] = new char[3];
temp[i][0] = ch1[2 * i];
temp[i][1] = ch1[2 * i + 1];
temp[i][2] = '\0';
}
cout << translater1(temp, length) << endl;
//阿拉伯数字变汉字
cin >> ch2;
length = strlen(ch2);
for (int i = 0;; i++) {
char **t;
t = translater2(ch2, length);
if (t[i][0] == '\0') {
break;
}
cout << t[i][0] << t[i][1];
}
cout << endl;
return 0;
}
运行结果如下:
汉字变阿拉伯数字:
阿拉伯数字变汉字: