Skip to content

Commit f240fff

Browse files
committed
[code] refactoring 61, 62
1 parent ee504af commit f240fff

File tree

2 files changed

+68
-48
lines changed
  • 061-二叉树遍历
  • 062-浮点数转换为字符串

2 files changed

+68
-48
lines changed

061-二叉树遍历/61.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ void createbitree(BiTree *t, int *n){
1313
*n = *n + 1;
1414
printf(" Input %d DATA: ", *n);
1515
x = getchar();
16-
if (x != '\n')
17-
getchar();
18-
19-
if (x == '^')
20-
return;
16+
if (x != '\n') getchar(); // 吃掉 \n
17+
if (x == '^') return;
2118

2219
q = (BiTree)malloc(sizeof(bitnode));
2320
q->data = x;
@@ -29,7 +26,6 @@ void createbitree(BiTree *t, int *n){
2926

3027
createbitree(&q->lchild, n);
3128
createbitree(&q->rchild, n);
32-
return;
3329
}
3430

3531
void visit(BiTree e) {
@@ -56,9 +52,9 @@ void countleaf(BiTree t, int *c) {
5652

5753
int treehigh(BiTree t) {
5854
int lh, rh, h;
59-
if (t == NULL)
55+
if (t == NULL) {
6056
h = 0;
61-
else {
57+
} else {
6258
lh = treehigh(t->lchild);
6359
rh = treehigh(t->rchild);
6460
h = (lh > rh ? lh : rh) + 1;

062-浮点数转换为字符串/62.c

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
#include <stdio.h>
2-
#include <string.h>
2+
#include <string.h> // strcat
3+
#include <stdlib.h> // atoi
34

4-
char c_je[51]; // 大写金额字符变量
5+
#define MAX_CHAR_LEN 255
6+
#define ZH_CHAR_LEN 3
7+
typedef enum {
8+
NOT_SET,
9+
NORMAL,
10+
ALL_ZERO_END,
11+
ZERO_IN_MID,
12+
} Flag;
513

614
// 数字金额转换为大写金额子程序
7-
char *zh(double x) {
8-
int i, n, bz;
9-
char je[14]; // 数字金额的字符变量
10-
char temp[13];
15+
char* zh(char zh_money[], double x) {
16+
int i = 0, n = 0, nth = 0;
17+
Flag flag = NOT_SET;
18+
char je[MAX_CHAR_LEN] = {0}; // 数字金额的字符变量
19+
char temp[MAX_CHAR_LEN] = {0};
1120
char f1[10][3] = {
1221
"零", "壹", "贰", "叁", "肆",
1322
"伍", "陆", "柒", "捌", "玖"
@@ -16,48 +25,62 @@ char *zh(double x) {
1625
"亿", "仟", "佰", "拾", "万",
1726
"仟", "佰", "拾", "元", "角", "分"
1827
}; // 每位数字对应单位数组变量
19-
sprintf(je, "%.01f", 100 * x); // 转换成字符
28+
29+
// i => 012 3456;
30+
// 100 => 100_00.0; n = 7
31+
// n-i => 765 4321;
32+
sprintf(je, "%.01f", 100 * x); // 扩大 100 倍后转为字符串
2033
n = strlen(je);
21-
c_je[0] = '\0';
22-
bz = 1;
34+
35+
zh_money[0] = '\0'; // 初始化为空字符串
36+
flag = NORMAL;
2337
for (i = 0; i < n; i++) {
24-
strcpy(temp, &je[i]); // 复制到临时数组
25-
if (atoi(temp) == 0) // 判断第i位后是否全为0
26-
{
27-
bz = 2;
38+
// i => 012 3456;
39+
// n-i => 765 4321;
40+
// 100.00 => 100_00.0; n = 7
41+
// nth => 321 01
42+
nth = n - i - 4;
43+
strcpy(temp, &je[i]); // 复制到临时数组
44+
if (atoi(temp) == 0) { // 判断第i位后是否全为0
45+
flag = ALL_ZERO_END;
2846
break;
2947
}
30-
if (je[i] != '0')
31-
{
32-
if (bz == 0)
33-
strcat(c_je, f1[0]);
34-
strcat(c_je, f1[je[i] - '0']); // 数字串转化字符串
35-
bz = 1;
36-
strcat(c_je, f2[13 - n + i]);
37-
}
38-
else
39-
{
40-
if (n - i == 7 && (je[i - 1] != '0' || je[i - 2] != '0' || je[i - 3] != '0')) // 判断万位位置
41-
strcat(c_je, "万");
42-
if (n - i == 3) // 判断个位数的元位置
43-
strcat(c_je, "元");
44-
bz = 0;
48+
if (je[i] != '0') {
49+
if (flag == ZERO_IN_MID) { // x十/百/千/... [零] x元/角/分/...
50+
strncat(zh_money, f1[0], ZH_CHAR_LEN);
51+
}
52+
strncat(zh_money, f1[je[i] - '0'], ZH_CHAR_LEN); // 数字串转化字符串
53+
strncat(zh_money, f2[13 - n + i], ZH_CHAR_LEN);
54+
flag = NORMAL;
55+
} else {
56+
// 判断万位位置
57+
if (
58+
5 == nth && (
59+
je[i - 1] != '0' ||
60+
je[i - 2] != '0' ||
61+
je[i - 3] != '0'
62+
)
63+
) strcat(zh_money, "万");
64+
if (1 == nth) strcat(zh_money, "元"); // 判断个位数的元位置
65+
flag = ZERO_IN_MID;
4566
}
4667
}
47-
if (bz == 2)
48-
{
49-
if (n - i >= 7 && n - i < 10)
50-
strcat(c_je, "万"); // 万位数字为0,加‘万’
51-
if (n - i >= 3)
52-
strcat(c_je, "元");
53-
strcat(c_je, "正"); // 最后不是分位,加“正”
68+
if (flag == ALL_ZERO_END) {
69+
// i => 01234 5678; 01234 5678 9012;
70+
// n - i => 98765 4321; 32109 8765 4321;
71+
// 10000 => 10000_00.0; 1_0000_0000 => 10000_0000_00.0;
72+
// nth => 54321 01 ; 98765 4321 01
73+
// 万位数字为 0,加‘万’
74+
if (5 <= nth && nth < 8) strcat(zh_money, "万");
75+
if (1 <= nth) strcat(zh_money, "元");
76+
strcat(zh_money, "正"); // 最后不是分位,加“正”
5477
}
55-
return c_je; // 返回大写金额
78+
return zh_money; // 返回大写金额
5679
}
5780

58-
main()
59-
{
60-
double count;
81+
int main() {
82+
double count = 0.0;
83+
char zh_money[MAX_CHAR_LEN] = {0}; // 大写金额字符变量
6184
//clrscr();
6285
printf("*********************************************************\n");
6386
printf("* *\n");
@@ -70,9 +93,10 @@ main()
7093
scanf("%lf", &count);
7194
printf("* 您输入的金额为:%10.2lf 。 *\n", count);
7295
printf("* *\n");
73-
printf("* 转换为大写金额是:%s\n", zh(count));
96+
printf("* 转换为大写金额是:%s\n", zh(zh_money, count));
7497
printf("* *\n");
7598
puts("* 请按任意键退出... *");
7699
printf("*********************************************************\n");
77100
getchar();
101+
return 0;
78102
}

0 commit comments

Comments
 (0)