C语言:统计一个文件中大写字符、小写字符、数字个数
“Project15122.exe”: 已加载“C:\Windows\System32\sechost.dll”,已加载符号(去除源信息)。
“Project15122.exe”: 已加载“C:\Windows\System32\rpcrt4.dll”,已加载符号(去除源信息)。
“Project15122.exe”: 已加载“C:\Windows\System32\cryptbase.dll”,已加载符号(去除源信息)。
“Project15122.exe”: 已加载“C:\Windows\System32\bcryptprimitives.dll”,已加载符号(去除源信息)。
线程 ‘Win64 线程’ (0x3674) 已退出,返回值为 0 (0x0)。
线程 ‘Win64 线程’ (0x206c) 已退出,返回值为 0 (0x0)。
线程 ‘Win64 线程’ (0x3264) 已退出,返回值为 0 (0x0)。
“Project15122.exe”: 已加载“C:\Windows\System32\kernel.appcore.dll”,已加载符号(去除源信息)。
程序“[10208] Project15122.exe: 本机”已退出,返回值为 0 (0x0)。
代码如下:
#include <iostream>
#include <fstream>
using namespace std;
void chapterstatistic()
{
printf_s("统计文件开始\r\n");
char *path = "E:\\VS2015\\shujuji\\shuju_SVM\\SAVE_wine.txt";
FILE *f;
int r = fopen_s(&f, path, "r");
if (r>0)
{
printf_s("读取文件错误");
}
int capletter = 0, lowercase = 0, num = 0, other = 0;
char temp = fgetc(f);
while (!feof(f))
{
if (temp >= 'a' && temp <= 'z')
{
lowercase++;
}
else if (temp >= 'A' && temp <= 'Z')
{
capletter++;
}
else if (temp >= '0' && temp <= '9')
{
num++;
}
else
{
other++;
}
temp = fgetc(f);
}
printf_s("大写字符:%d 小写字符个数:%d 数字个数:%d 其他字符个数:%d", capletter, lowercase, num, other);
fclose(f);
printf_s("统计文件结束\r\n");
}
int main()
{
chapterstatistic();
system("pause");
}
效果如图:
另外的,附上数字大小写数值金额统计自定义处理DIY
int csdsdd()
{
int money;
int i = 0;
int count = 0;//记录用户输入数字位数
int money_s[N];
int temp = 0;//临时存放变量
char num[10][4] = { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };
char unit[6][8] = { "拾万","万","仟","佰","拾","元整" };
//一个中文占4个字节
printf("请输入整数为6位及以内的金额(小数点后忽略不计):\n");
for (;;)
{
scanf("%d", &money);
if (money / 1000000 >= 1)
{
printf("输入有误!\n");
}
else { break; }
}
printf("您输入的金额为:%d。\n", money);
for (i = 0; i<10; i++)//测试num数组正确性
{
printf("num数组第%d位为:%s\n", i + 1, num[i]);
}
for (i = 0; i<6; i++)//测试unit数组正确性
{
printf("数组第%d位为:%s\n", i + 1, unit[i]);
}
for (i = 0; i<N; i++)//取出用户每一位数字
{
money_s[i] = money % 10;
money /= 10;
count++;
if (money == 0)
{
break;
};
}
for (i = 0; i<count; i++)//打印数组中用户每一位数字
{
printf("%d\n", money_s[count - i - 1]);
};
printf("\n");
for (i = 0; i<count; i++)
{
temp = money_s[count - i - 1];
printf("%s\t", num[temp]);
printf("%s\t", unit[6 - count + i]);
temp = 0;
}
return 0;
}
整体效果如图:
全部代码运行如下:
#include <iostream>
#include <fstream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#define N 6
void chapterstatistic()
{
printf_s("统计文件开始\r\n");
char *path = "E:\\VS2015\\shujuji\\shuju_SVM\\save_data_1V.txt";
FILE *f;
int r = fopen_s(&f, path, "r");
if (r>0)
{
printf_s("读取文件错误");
}
int capletter = 0, lowercase = 0, num = 0, other = 0;
char temp = fgetc(f);
while (!feof(f))
{
if (temp >= 'a' && temp <= 'z')
{
lowercase++;
}
else if (temp >= 'A' && temp <= 'Z')
{
capletter++;
}
else if (temp >= '0' && temp <= '9')
{
num++;
}
else
{
other++;
}
temp = fgetc(f);
}
printf_s("大写字符:%d 小写字符个数:%d 数字个数:%d 其他字符个数:%d", capletter, lowercase, num, other);
fclose(f);
printf_s("统计文件结束\r\n");
}
int csdsdd()
{
int money;
int i = 0;
int count = 0;//记录用户输入数字位数
int money_s[N];
int temp = 0;//临时存放变量
char num[10][4] = { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };
char unit[6][8] = { "拾万","万","仟","佰","拾","元整" };
//一个中文占4个字节
printf("请输入整数为6位及以内的金额(小数点后忽略不计):\n");
for (;;)
{
scanf("%d", &money);
if (money / 1000000 >= 1)
{
printf("输入有误!\n");
}
else { break; }
}
printf("您输入的金额为:%d。\n", money);
for (i = 0; i<10; i++)//测试num数组正确性
{
printf("num数组第%d位为:%s\n", i + 1, num[i]);
}
for (i = 0; i<6; i++)//测试unit数组正确性
{
printf("数组第%d位为:%s\n", i + 1, unit[i]);
}
for (i = 0; i<N; i++)//取出用户每一位数字
{
money_s[i] = money % 10;
money /= 10;
count++;
if (money == 0)
{
break;
};
}
for (i = 0; i<count; i++)//打印数组中用户每一位数字
{
printf("%d\n", money_s[count - i - 1]);
};
printf("\n");
for (i = 0; i<count; i++)
{
temp = money_s[count - i - 1];
printf("%s\t", num[temp]);
printf("%s\t", unit[6 - count + i]);
temp = 0;
}
return 0;
}
int main()
{
chapterstatistic();
csdsdd();
system("pause");
}