‘壹’ c语言统计字数
在你的基础上做了点修改, 以#结束
#include<stdio.h>
int main(void)
{
int nc=0; /* number of characters */
int nq=0; /* numerical quantities */
int bs=0; /* number of blank spaces */
char e;
int nw=0,word=0,nl=1;
printf("Text Analysis\n");
printf("please input the Text:\n");
e=getchar();
while (e!='#')
{
if((e>='a'&&e<='z')||(e>='A'&&e<='Z')) nc++;
if(e>='0'&&e<='9') nq++;
if(e==' ') bs++;
if(e=='\n') nl++;
if(e==' ') word=0;
else if(word==0)
{
word=1;
nw++;
}
if(e=='\n') word=0;
else if(word==0)
{
word=1;
nw++;
}
e=getchar();
}
printf("Number of Words:%d \n",nw);
printf("Number of Characters:%d\n",nc);
printf("Number of blank spaces:%d\n",bs);
printf("Number of numerical characters:%d\n",nq);
printf("Number of lines:%d \n",nl);
return 0;
}
‘贰’ c语言统计文件中字符的个数
while(!feof(fp)){
fgetc(fp);//最后一个收到的是文件结束符号,当然不是字符串的一部分!
num++;//而此处加了1
}
所以最后字符串的长度要减一。
而你修改后,条件先不满足,不进入循环了,没有num++的动作。
‘叁’ C语言怎么样连续读取文件数据,统计单个文件的数据量
#include<iostream>
#include<fstream>
using namespace std;
#define n 3
int main()
{
int i;
char res[]="result",
suffix[]=".txt",
go_on;
for(i=1;i<=n;i++)
{
char Rname[10],Wname[20];
itoa(i,Rname,10);
strcpy(Wname,Rname);
strcat(Rname,suffix);
strcat(Wname,res);
strcat(Wname,suffix);
cout<<Rname<<':'<<endl;
ifstream infile(Rname,ios::binary);
ofstream outfile(Wname,ios::binary);
if(!infile||!outfile)
{
cerr<<"error"<<endl;
exit(1);
}
int j=0,count=0;
double d,
*dp;
cout<<"数组读取:"<<endl;
while(!infile.eof())
{
infile>>d;
count++;
}
infile.clear();
infile.seekg(0);
dp=new double[--count];
while(j<count)
{
infile>>dp[j++];
cout<<dp[j-1]<<' ';
if(j%(count/8)==0) cout<<endl;
}
cout<<endl<<"共"<<count<<"个数"<<endl;
infile.clear();
infile.seekg(0);
while(!infile.eof())
{
infile.read((char *)&d,sizeof(d));
outfile.write((char *)&d,sizeof(d));
}
delete []dp;
infile.close();
outfile.close();
cout<<"是否继续(是:y 否:anykey):";
cin>>go_on;
if(go_on!='Y'&&go_on!='y') break;
system("cls");
}
return 0;
}
‘肆’ 用.net或c语言编码,读取txt文件,并对txt文件中的字数统计个数,计算概率并排序。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(Charactor)
typedef struct charactor
{
char key[3];
int count;
struct charactor *next;
}Charactor;
Charactor *add(char *c,Charactor *head) //查找记数
{
int flag=1;
Charactor *p=head;
while(p!=NULL&&flag==1)
{
if(strcmp(p->key,c) == 0)
{
flag=0; break;
}
else
{
p=p->next;
}
}
if(flag==0)
p->count++;
else
{
p=head;
head=(Charactor *)malloc(LEN);
strcpy(head->key,c);
head->count=1;
head->next=p;
}
return head;
}
void display(Charactor *p) //输出
{
printf("\n The result are:\n");
while(p!=NULL)
{
printf("%s: %d\n",p->key,p->count);
p=p->next;
}
}
void main()
{
Charactor *head=NULL;
char ch[2];
char str[100];
FILE *fstream;
FILE *fp=fopen("d:\\1.txt","w");
printf("此程序只能统计中文\n");
printf("请输入一个中文字符串\n");
fflush(stdin);
gets(str);
fprintf(fp,str); fclose(fp);
if((fstream = fopen("d:\\1.txt","r")) == NULL)
{ exit(-1); }
while( !feof(fstream) )
{ ch[0]=32;
ch[1]=32;
fscanf(fstream,"%2s",ch);
if(ch[0]==32&&ch[1]==32)
{
break;
}
head=add(ch,head);
}
display(head);
system("PAUSE");
}
c语言编码 vc6.0通过 统计您输入的字符出现的个数
觉得满意 联系我 然后我给你完善程序
‘伍’ C语言//从另一文件中读取数字 和 字符串,并进行统计。
/*
统计文件中重复的单词
*/
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#defineFILEOPEN(x)fopen(#x,"r")
#defineWORD_LEN15
#defineWORDS_LEN1000
intmain()
{
FILE*pfile=FILEOPEN(E:/2.txt);//fopen函数是第一个变元是文件路径第二个是模式
char*parr[WORDS_LEN];//记录每个单词的内存地址
intwords_count[WORDS_LEN];//生命记录单词的字符数组
chartemp_char=0;//用于记录fgetc从文件得到的字符
intword_index=0;//记录单词的字符索引
intwords_index=0;//用于记录文本的单词索引
inti,has_repeat;
if(pfile)
{
char*pcurrent_char=(char*)malloc(sizeof(char)*WORD_LEN);//分配第一个单词的内存
while(!feof(pfile))//循环一直到文件末尾
{
temp_char=fgetc(pfile);//从文件读取一个char
if(isalpha(temp_char))//判断这个字符是不是字符
//如果是字符就把当前的单词的字符索引位置对应的字符赋值为temp_char
pcurrent_char[word_index++]=temp_char;
else
{
//如果不是字母,就认为刚才的那个单词已经结束。因为把pcurrent_char末尾添加
pcurrent_char[word_index]='