(个人水平有限,请见谅!)
描述:
实现一个算法,可以将小写数字转换成大写数字。
输入:
输入一个整数。范围在0~450亿之间。
输出:
输出对应的大写数字,以“元整”结尾。 大写数字要符合汉语读写习惯。
输入样例:
0
5
233
1001
40607
8900000000
输出样例:
零元整
伍元整
贰佰叁拾叁元整
壹仟零壹元整
肆万零陆佰零柒元整
捌拾玖亿元整
代码示例:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
char line[1000001];
// 循环读入多行数据
while (cin.getline(line, 1000000)) {
string str = line;
string result = "", result_t = "", tt1 = "零", tt2 = "万", tt3 = "亿";
string num[10] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
if (str == "0")
cout << "零元整" << endl;
else
{
for (int i = str.size() - 1; i >= 0; i--)
{
int j = str.size() - 1 - i;
if (str[i] == '0')
result += "零";
if (j % 4 == 3 && str[i] != '0')
result += "仟" + num[str[i] - '0'];
if (j % 4 == 2 && str[i] != '0')
result += "佰" + num[str[i] - '0'];
if (j % 4 == 1 && str[i] != '0')
result += "拾" + num[str[i] - '0'];
if (j % 4 == 0 && str[i] != '0')
result += num[str[i] - '0'];
if (j % 8 == 3 && j != str.size() - 1)
result += "万";
if (j % 8 == 7 && j != str.size() - 1)
result += "亿";
}
for (int i = 0; i < result.size() - 3;)
{
if (result[0] == tt1[0] && result[1] == tt1[1] && result[2] == tt1[2])
{
result.erase(i, 3);
continue;
}
if (result[i] == tt1[0] && result[i + 1] == tt1[1] && result[i + 2] == tt1[2] && result[i + 3] == tt1[0] && result[i + 4] == tt1[1] && result[i + 5] == tt1[2] ) // 零零
result.erase(i + 3, 3);
else if (result[i] == tt2[0] && result[i + 1] == tt2[1] && result[i + 2] == tt2[2] && result[i + 3] == tt1[0] && result[i + 4] == tt1[1] && result[i + 5] == tt1[2] ) // 万零
result.erase(i + 3, 3);
else if (result[i] == tt3[0] && result[i + 1] == tt3[1] && result[i + 2] == tt3[2] && result[i + 3] == tt1[0] && result[i + 4] == tt1[1] && result[i + 5] == tt1[2] ) // 亿零
result.erase(i + 3, 3);
else if (result[i] == tt2[0] && result[i + 1] == tt2[1] && result[i + 2] == tt2[2] && result[i + 3] == tt3[0] && result[i + 4] == tt3[1] && result[i + 5] == tt3[2] ) // 万亿
result.erase(i, 3);
else
i = i + 3;
}
for (int i = result.size() - 1; i >= 2; i = i - 3)
cout << result[i - 2] << result[i - 1] << result[i];
cout << "元整" << endl;
}
}
return 0;
}