65,206
社区成员
发帖
与我相关
我的任务
分享
int main(int argc, char* argv[])
{
//const char* content = "1一A2二B3三C4四D五EF";
// 初始化文件
ofstream ofs("statistics1.txt");
if ( !ofs.bad() )
{
ofs << "1一A2二B3三C4四D五EF" << endl;
ofs.close();
}
// 读文件数据并统计数据
ifstream ifs("statistics1.txt");
if ( !ifs.bad() )
{
TCount count, line_count;
memset(&count, 0, sizeof(count));
string line;
while( getline(ifs, line) )
{
CountString(line.c_str(), line.length(), line_count);
count.add(line_count);
}
printf("chinese : %d\nenglish : %d\nnumeric : %d\n", count.chinese, count.english, count.numeric);
ifs.close();
}
system("pause");
return 0;
}
#include <memory.h>
#include <string.h>
#include <process.h>
struct TCount
{
int chinese;
int english;
int numeric;
};
#define BYTE unsigned char
#define WORD unsigned short
#define ByteToWord(h,l) ((BYTE)h<<8|(BYTE)l)
void CountString(const char* content, int length, TCount& count)
{
memset(&count, 0, sizeof(count));
int i;
WORD tmp;
for ( i=0; i<length; i++ )
{
if ( 'A' <= content[i] && content[i] <= 'Z'
|| 'a' <= content[i] && content[i] <= 'z' )
{
++count.english;
}
else if ( '0' <= content[i] && content[i] <= '9' )
{
++count.numeric;
}
else if ( i + 1 < length )
{
tmp = ByteToWord(content[i], content[i+1]);
if ( 0x8140 <= tmp && tmp <= 0xFEFE )
{
++count.chinese;
++i;
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
const char* content = "1一A2二B3三C4四D五EF";
TCount count;
CountString(content, strlen(content), count);
printf("chinese : %d\nenglish : %d\nnumeric : %d\n", count.chinese, count.english, count.numeric);
system("pause");
return 0;
}
int main(int argc, char* argv[])
{
// const char* content = "1一A2二B3三C4四D五EF";
ifstream ifs("statistics1.txt");
TCount count;
string line;
while(getline(ifs, line))
{
CountString(line, line.length(), count);
}
printf("chinese : %d\nenglish : %d\nnumeric : %d\n",
count.chinese, count.english, count.numeric);
system("pause");
ifs.close();
return 0;
}
int main(int argc, char* argv[])
{
// const char* content = "1一A2二B3三C4四D五EF";
ifstream ifs("static.txt");
TCount count;
string line;
while(getline(ifs, line))
{
CountString(line, line.length(), count);
}
printf("chinese : %d\nenglish : %d\nnumeric : %d\n", count.chinese,count.english, count.numeric);
system("pause");
ifs.close();
return 0;
}