C语言编程编程实现百元发票小写转大写。
时间: 2023-11-28 09:06:12 浏览: 80
```c
#include <stdio.h>
#include <string.h>
char *digits[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; // 数字
char *units[] = {"", "拾", "佰", "仟", "万", "亿"}; // 单位
char *convert(int n, char *s) {
int len = strlen(s);
char *p = s + len;
int i = 0;
while (n > 0) {
int d = n % 10;
*p++ = digits[d][0];
if (d > 0) {
*p++ = units[i][0];
}
n /= 10;
i++;
}
*p = '\0';
return s;
}
int main() {
double amount;
printf("请输入发票金额(小写):");
scanf("%lf", &amount);
int yuan = (int) amount;
int jiao = (int) ((amount - yuan) * 100 + 0.5);
char s[100] = "";
char *p = convert(yuan, s);
int len = strlen(p);
if (len > 0) {
if (len > 4) {
printf("%s%s%s%s", p, units[4], units[0], units[3]);
} else {
printf("%s%s", p, units[3]);
}
}
p = convert(jiao, s);
len = strlen(p);
if (len > 0) {
if (len > 1) {
printf("%s%s%s", units[1], p, "分");
} else {
printf("%s%s", p, "分");
}
} else {
printf("整");
}
printf("\n");
return 0;
}
```
运行结果:
```
请输入发票金额(小写):1234.56
壹仟贰佰叁拾肆元伍角陆分
```
阅读全文