|
1楼 (我不知道汉字怎么才能进行统计`````还有就是英文字母老统计不出来,请高手指教) #include<stdio.h> main() { FILE *fp; long number=0; long zimu=0; long hanzi=0; long other=0; char ch; if((fp=fopen("text.txt","r"))==NULL) { printf("cannot open file!\n"); exit(0); } while(!feof(fp)) {ch=fgetc(fp); if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') {zimu++;} else if(ch>='0'&&ch<='9') {number++;} else if(sizeof(ch)>128) {hanzi++;} else {other++;} } printf("number=%d zimu=%d hanzi=%d other=%d\n",number,zimu,hanzi,other); fclose(fp); } |
|
|
|
|
2楼 ==== 如果你不清楚运算的顺序,请加上括号。足够多的括号能减少很多不必要的麻烦。 至于中文,呵呵,怎么能用sizeof这样的运算符呢?好好看看书,sizeof是什么意思。 显然你的是作业,那么可以简单认为中文就是国标GB2312,即一个汉字是两个字节,与英文的差别在于这两个字节都是大于128的。 |
|
|
|
|
3楼 |
|
|
|
|
5楼 每次读入的字节如果大于等于128,就可以认为这个字节和下面一个字节组成了一个汉字 即 if(ch>=128) 剩下的可以自己发挥了 |
|
|
|
|
6楼 main() { FILE *fp; long number=0; long zimu=0; long hanzi=0; long other=0; char q; if((fp=fopen("text.txt","r"))==NULL) { printf("cannot open file!\n"); exit(0); } while(!feof(fp)) {q=fgetc(fp); if((q>='A'&&q<='Z')||(q>='a'&&q<='z')) {zimu++;} else if(q>='0'&&q<='9') {number++;} else if(q>=128) {hanzi++;} else {other++;} } printf("number=%d zimu=%d hanzi=%d other=%d\n",number,zimu,hanzi,other); fclose(fp); } 我按照你的改完了 我在文本里输入:a与1。 结果确是:number=1 zimu=0 hanzi=1 other=0 ```为什么 |
|
|
|
| 210.77.4.* |
7楼 else if(q>=128) { hanzi++; fgetc(fp);//因为汉字是两个字节。 } |
|
|
| 210.77.4.* |
8楼 fgetc(fp);//因为汉字是两个字节。而是在最后把hanzi除以2,得到正确的汉字数目。 |
|
|
|
10楼 else if(q>=128) {hanzi++;} 你这样判断一个字节就加? 字符要定义成 unsigned char q; 否则,不可能大于128了, |
|
|
|
|
11楼 main() { FILE *fp; long number=0; long zimu=0; long hanzi=0; long other=0; unsigned char q; if((fp=fopen("text.txt","r"))==NULL) { printf("cannot open file!\n"); exit(0); } while(!feof(fp)) {q=fgetc(fp); if((q>='A'&&q<='Z')||(q>='a'&&q<='z')) {zimu++;} else if(q>='0'&&q<='9') {number++;} else if(q>=128) {hanzi++; fgetc(fp);} else {other++;} } printf("number=%d zimu=%d hanzi=%d other=%d\n",number,zimu,hanzi,other); fclose(fp); } 按照你的改完了 运行后还是不对 |
|
|
|
|
12楼 while((q=fgetc(fp)) && !feof(fp)) { 把你的程序里feof判断改成这样。结果就应该是正确的了。 |
|
|
|
|
13楼 main() { FILE *fp; long number=0; long zimu=0; long hanzi=0; long other=0; unsigned char q; if((fp=fopen("text.txt","r"))==NULL) { printf("cannot open file!\n"); exit(0); } while((q=fgetc(fp))&&!feof(fp)) {q=fgetc(fp); if((q>='A'&&q<='Z')||(q>='a'&&q<='z')) {zimu++;} else if(q>='0'&&q<='9') {number++;} else if(q>=128) {hanzi++; fgetc(fp);} else {other++;} } printf("number=%d zimu=%d hanzi=%d other=%d\n",number,zimu,hanzi,other); fclose(fp); } 在text文件中输入:yu于12, 结果:number=0 zimu=0 hanzi=1 other=0 |
|
|
|
|
14楼 else {other++;}后面再加上一个 printf("q=%d\n",q);然后把结果传上来看看。 |
|
|
|
|
20楼 #include<stdio.h> int main() { FILE *fp; int number=0; int zimu=0; int hanzi=0; int other=0; unsigned char q; if((fp=fopen("t.txt","rb"))==NULL) { printf("cannot open file!\n"); exit(0); } while(!feof(fp)) {q=fgetc(fp); if((q>='A'&&q<='Z')||(q>='a'&&q<='z')) {zimu++;} else if(q>='0'&&q<='9') {number++;} else if(q>=128) {hanzi++; fgetc(fp);} else {other++;} } printf("number=%d zimu=%d hanzi=%d other=%d\n",number,zimu,hanzi-1,other); fclose(fp); return 0; } |
|
|
|
|
22楼 改成 if((fp=fopen("c:\\text.txt","r"))==NULL) 然后在C:下建立text.txt文件,在里面输入一些东西 ,再看看结果对不对!!! |
|
|
|
|
23楼 {q=fgetc(fp); === VC99,一定要先fgetc,再feof判断的。不然就会出现你的hanzi-1这种情况了,呵呵。因为文件文件的读取,最后一个字符是255,以表示结束。恰好255>128,所以hanzi多加了一个。 |
|
|
|
|
27楼 main() { FILE *fp; long number=0; long zimu=0; long hanzi=0; long other=0; unsigned char q; if((fp=fopen("c:\\text.txt","r"))==NULL) { printf("cannot open file!\n"); exit(0); } while((q=fgetc(fp))&&!feof(fp)) {q=fgetc(fp); if((q>='A'&&q<='Z')||(q>='a'&&q<='z')) {zimu++;} else if(q>='0'&&q<='9') {number++;} else if(q>=128) {hanzi++; fgetc(fp);} else {other++;} } printf("number=%d zimu=%d hanzi=%d other=%d\n",number,zimu,hanzi,other); fclose(fp); } 现在程序是这样 |
|
|
|
|
28楼 {q=fgetc(fp); ===老大啊,改成这样: while((q=fgetc(fp))&&!feof(fp)) { |
|
|
|