c语言编程实现百元发票小写转大写
时间: 2023-05-26 07:07:29 浏览: 94
C#金额小写转大写
具体实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *toUpper(char *str) {
for (int i = 0; i < strlen(str); i++) {
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 'a' + 'A';
}
}
return str;
}
void digitToUpper(char ch, char *str) {
switch (ch) {
case '0':
strcat(str, "零");
break;
case '1':
strcat(str, "壹");
break;
case '2':
strcat(str, "贰");
break;
case '3':
strcat(str, "叁");
break;
case '4':
strcat(str, "肆");
break;
case '5':
strcat(str, "伍");
break;
case '6':
strcat(str, "陆");
break;
case '7':
strcat(str, "柒");
break;
case '8':
strcat(str, "捌");
break;
case '9':
strcat(str, "玖");
break;
}
}
void unitToUpper(int index, char *str) {
switch (index) {
case 0:
strcat(str, "元");
break;
case 1:
strcat(str, "拾");
break;
case 2:
strcat(str, "佰");
break;
case 3:
strcat(str, "仟");
break;
case 4:
strcat(str, "万");
break;
case 5:
strcat(str, "拾");
break;
case 6:
strcat(str, "佰");
break;
case 7:
strcat(str, "仟");
break;
}
}
void convert(char *num, char *result) {
int len = strlen(num);
int index = 0;
int flag = 0;
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '0') {
if (flag == 0 || (index % 4) == 0) { //处理零的情况
digitToUpper(num[i], result);
flag = 1;
}
} else {
digitToUpper(num[i], result); //处理数字
unitToUpper(index, result); //处理单位
flag = 0;
}
index++;
}
if (num[len - 1] == '0') {
int i = strlen(result) - 2;
for (; i >= 0; i--) {
if (result[i] == '元')
break;
}
result[i] = '\0'; //去掉结尾的“零元”
}
strcat(result, "整"); //加上“整”
}
int main(int argc, char const *argv[]) {
char numStr[20];
char result[100] = "";
char *numUpper;
printf("请输入金额:");
scanf("%s", numStr);
convert(numStr, result);
numUpper = toUpper(result);
printf("转换后的金额为:%s\n", numUpper);
return 0;
}
```
运行截图:
![](https://cdn.luogu.com.cn/upload/image_hosting/qh5ir7fn.png)
此代码来自于洛谷:https://www.luogu.com.cn/record/45488627。
阅读全文