69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1, *fp2; /*定义两个指向FILE类型结构体的指针变量*/
char filename1[100],filename2[100],ch; /*定义数组变量为字符型*/
long character,word,sentence,paragraph; /*定义变量为长整型*/
character=word=sentence=paragraph=0; /*长整型变量的初值均为0*/
printf("Enter file name \n");
scanf("%s", filename1); /*输入要进行统计的文件的路径及名称*/
if((fp1=fopen(filename1, "r"))==NULL) /*以只读方式打开指定文件*/
{
printf("cannot open file.\n");
exit(1);
}
printf ( "Enter file name for write data:\n");
scanf("%s",filename2); /*输入文件名即将统计结果放到那个文件中*/
if((fp2=fopen(filename2, "w"))==NULL) /*已可写方式要存放统计结果的文件*/
{
printf("cannot open file \n");
exit(1);
}
while((ch=fgetc(fp1))!=EOF) /*知道文件内容结束处停止while循环*/
{
if(ch>='A' && ch<'Z'||ch>='a' && ch<='z')
{
character++;
} /*当遇到字母时字符个数+1*/
else if(ch==' ')
{
character++;
word++;
} /*当遇到空格时字符数+1*/
else if(ch>='0' && ch<='9')
{
character++;
} /*当遇到数字时数字个数+1*/
else if(ch=='.'||ch=='!'||ch=='?')
{
sentence++;
character++;
} /*当遇到.!?时句子数+1,字符数+1*/
else if ((ch=='\r')||(ch=='\n'))
{ paragraph++;
} /*当遇到换行符时段落数+1*/
else
{
character++;
word++;
} /*遇到其他字符时字符个数+1*/
ch++;
}
fclose(fp1); /*关闭fp1指向的文件*/
fprintf(fp2,"character:%ld word:%ld sentence:%ld paragraph:%ld\n", character, word, sentence,paragraph);/*将统计结果写入fp指向的磁盘文件中*/
fclose(fp2);
}
else
{
character++;
word++;
} /*遇到其他字符时字符个数+1*/
ch++;
ch2=ch;
}
if(ch2=='\n')
{
printf("a");
character++;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp1, *fp2; /*定义两个指向FILE类型结构体的指针变量*/
int ch;// 定义整型,用来读取字符
long paraglen =0;//定义段长
char filename1[100],filename2[100] ; /*定义数组变量为字符型*/
long character,word,sentence,paragraph; /*定义变量为长整型*/
character=word=sentence=paragraph=0; /*长整型变量的初值均为0*/
printf("Enter file name \n");
scanf("%s", filename1); /*输入要进行统计的文件的路径及名称*/
if((fp1=fopen(filename1, "r"))==NULL) /*以只读方式打开指定文件*/
{
printf("cannot open file.\n");
exit(1);
}
printf ( "Enter file name for write data:\n");
scanf("%s",filename2); /*输入文件名即将统计结果放到那个文件中*/
if((fp2=fopen(filename2, "w"))==NULL) /*已可写方式要存放统计结果的文件*/
{
printf("cannot open file \n");
exit(1);
}
while((ch=fgetc(fp1))!=EOF) /*知道文件内容结束处停止while循环*/
{
paraglen++;//每读取一个字符,段长加一
if(ch>='A' && ch<'Z'||ch>='a' && ch<='z')
{
character++;
} /*当遇到字母时字符个数+1*/
else if(ch==' ')
{
character++;
word++;
} /*当遇到空格时字符数+1*/
else if(ch>='0' && ch<='9')
{
character++;
} /*当遇到数字时数字个数+1*/
else if(ch=='.'||ch=='!'||ch=='?')
{
sentence++;
character++;
} /*当遇到.!?时句子数+1,字符数+1*/
else if ((ch=='\r')||(ch=='\n'))
{ paragraph++;
paraglen = 0;//段长,实际上是行长,每当换行就清零
} /*当遇到换行符时段落数+1*/
else
{
character++;
word++;
} /*遇到其他字符时字符个数+1*/
ch =0;
}
if(paraglen)
paragraph++; /*遇到段长非零,表示读到 文件结束,最后一个段(行),没有计算所以,段(行)数加一*/
/* (或者读出错,这里不考虑读文件出错),需要的话,使用ferror 函数,读取文件错误码 */
fclose(fp1); /*关闭fp1指向的文件*/
fprintf(fp2,"character:%ld word:%ld sentence:%ld paragraph:%ld\n", character, word, sentence,paragraph);/*将统计结果写入fp指向的磁盘文件中*/
fclose(fp2);
while(getchar()!='q');
}
这是修改后的代码
ch ++ 是不必要的,因为ch 是从文件里读出来的,需要的话,重置为 0 #include <stdio.h>
#include <string.h>
#define MAXLEN 1000
char ln[MAXLEN];
FILE *f;
int i,z;
int b,n,L;
int main(int argc,char **argv) {
if (argc<2) {
printf("Usage:%s fullpathfilename.ext\nget total blank/non-blank/total linenumbers.\n",argv[0]);
return 1;
}
f=fopen(argv[1],"r");
if (NULL==f) {
printf("Can not open file [%s]!\n",argv[1]);
return 2;
}
z=0;
b=0;
n=0;
L=0;
while (1) {
if (NULL==fgets(ln,MAXLEN,f)) break;
L=strlen(ln);
if ('\n'==ln[L-1]) {
if (0==z) {
for (i=0;i<L-1;i++) {
if (!(' '==ln[i] || '\t'==ln[i])) break;
}
if (i<L-1) z=1;//当前行不是空行
}
if (0==z) b++; else n++;
z=0;
} else {
if (0==z) {
for (i=0;i<L;i++) {
if (!(' '==ln[i] || '\t'==ln[i])) break;
}
if (i<L) z=1;//当前行不是空行
}
}
}
fclose(f);
if (L>0 && '\n'!=ln[L-1]) {
if (0==z) b++; else n++;//最后一行末尾无'\n'也计算
}
printf("File:[%s] total blank/non-blank/total linenumbers is %d/%d/%d\n",argv[1],b,n,b+n);
return 0;
}
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
while (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
while (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。
[/quote]
那请问基于我的程序怎么改呢不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
while (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
while (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。