搜索
查看: 1529|回复: 10
打印 上一主题 下一主题

【急】诚心求解,关于重复调用一个嵌套函数的问题。

[复制链接]
跳转到指定楼层
楼主
发表于 2013-11-17 20:35:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
15啊哈币
本帖最后由 redpanda 于 2013-11-17 20:38 编辑

帮朋友做的一道题目,计算一行英文作文里某个特定的单词出现的次数,【ps那篇英文作文我以附件的形式上传上来了】要求:需要搜索的几个单词在一次输入后的到其出现次数,作文也只是一行的英文。这个程序写完没语法错误,但却无法运行,我c学得渣渣的,搞了很久都不知道到底应该怎么改进。但我肯定应该是程序最后几行中的
“for(m=0;m<=2;m++){   getsentence(str1,fp,str2[m]);
        printf("The word %s which you want to search in the file have been appeared %d times in total!\n",str2[m],sum);}”
出现了 错误,但又不知道应该怎么改,跪求好心人搭救orz。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define N 200
#define M 20
static int sum=0;              /*sum是用来记录目标单词总共出现的次数,必须用static型*/


/************************************************************/

/*sum是用来记录目标单词总共出现的次数,必须用static型*/

void checkword(char* p1,char* objectword){   

     char temp[M];/*这个数组是用来存取从句子中提取出来的单词*/

     int i=0,j,count=0;

     while(p1!='\0'){

        j=0;

        while(isalpha(p1)){/*用来检测p1是不是字母*/

           temp[j]=p1;

           i++;

           j++;

        }

        temp[j]='\0';

        if(strcmp(temp,objectword)==0)/*判断提取出来的单词是否与要查找的单词一致*/

           count++;

        while(!isalpha(p1)&&p1!='\0'){

           i++;

        }

     }

     if(count!=0){

        printf("%s\n",p1);

        sum+=count;

     }         

}

void getsentence(char* p1,FILE* p2,char* objectword){

     int i;

     char ch;

     ch=fgetc(p2);

     while(ch!=EOF){

        i=0;

        while(ch!='.'&&ch!=';'&&ch!=EOF){

            p1=ch;

            i++;

            ch=fgetc(p2);


        }

        if(ch=='.'||ch==';'){

           p1=ch;

           p1[i+1]='\0';   

           checkword(p1,objectword);

           ch=fgetc(p2);

        }

     }

}

/*******************************************************************/
int main()
{
    char str1[M];
        char str2[2][M];
        char filename[M];
        int m;


    FILE* fp;
    printf("Please input the name of the file:\n");
    scanf("%s",filename);
        printf("\n");
         printf("Please input the word which you want to search:\n");

    scanf("%s %s %s",str2[0],str2[1],str2[2]);        
    fp=fopen(filename,"r");
    if(fp==NULL){
       printf("cannot open the file\n");
       exit(0);
    }
for(m=0;m<=2;m++)
{   getsentence(str1,fp,str2[m]);
        printf("The word %s which you want to search in the file have been appeared %d times in total!\n",str2[m],sum);}
    fclose(fp);
    system("pause");
    return 0;
}


a.zip

256 Bytes, 下载次数: 3

需要用到的英文作文

沙发
发表于 2013-11-17 21:04:25 | 只看该作者
太多错误了:
1.system("pause");没用在特定地方
2.exit()返回值错误
3.代码完全被你繁化了
4.从终端输入时的scanf()你打出空格
5.你这样写根本没办法打开文件
6.fclose()函数使用不当
板凳
发表于 2013-11-17 21:44:59 | 只看该作者
LZ告诉你个我自己认为比较好的技巧!最复杂的编程可以把它用最简单的东西拼凑出来....
地板
发表于 2013-11-17 21:59:03 | 只看该作者
其实单词一直用fscanf扫就行,你这样处理IO太麻烦了……
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>

  5. #define LEN 30

  6. void strip(char*);

  7. int main()
  8. {
  9.   char w[3][LEN];
  10.   char filename[LEN];

  11.   scanf("%29s", filename);
  12.   scanf("%29s %29s %29s", w[0], w[1], w[2]);

  13.   FILE* fp = fopen(filename, "r");
  14.   assert(fp);

  15.   char buf[LEN];
  16.   int count[3] = {0};
  17.   while(fscanf(fp, "%29s", buf) == 1){
  18.     strip(buf);
  19.     for(int i=0; i!=3; ++i)
  20.       if(strcmp(buf, w[i]) == 0)
  21.         count[i] += 1;
  22.   }

  23.   for(int i=0; i!=3; ++i)
  24.     printf("%d\n", count[i]);

  25.   return 0;
  26. }

  27. void strip(char* buf)
  28. {
  29.   int start = 0;
  30.   int end = strlen(buf) - 1;
  31.   while(start < LEN && !isalpha(buf[start]))
  32.     ++start;
  33.   while(end >= 0 && !isalpha(buf[end]))
  34.     --end;

  35.   if(start < end)
  36.     return;
  37.   /* not a word, ignore */

  38.   memmove(buf, buf+start, end - start + 1);
  39. }
  40.    
复制代码
对单词的定义不同会导致结果不同,这里用的方法在你给出的测试数据集上是可行的(也是最简单的……)

另外比较是大小写敏感的,例如要求统计Is的个数,就不会统计is
如果你要大小写不敏感可以先把buf和w都转化成小写再strcmp
5#
发表于 2013-11-17 22:26:46 | 只看该作者
本帖最后由 河蟹 于 2013-11-17 22:29 编辑
rosynirvana 发表于 2013-11-17 21:59
其实单词一直用fscanf扫就行,你这样处理IO太麻烦了……对单词的定义不同会导致结果不同,这里用的方法在你 ...

额,拿你的代码去运行了下,两个for循环出错,额,不能在循环里声明 i ,要单独声明
6#
发表于 2013-11-17 22:28:22 | 只看该作者
河蟹 发表于 2013-11-17 22:26
额,拿你的代码去运行了下,两个for循环出错

-std=c99
不然就把i的定义移出去
7#
发表于 2013-11-17 22:30:33 | 只看该作者
rosynirvana 发表于 2013-11-17 22:28
-std=c99
不然就把i的定义移出去

嘛,对于英文什么的一直是苦手,然后咱也最终发现了是这个问题
8#
发表于 2013-11-17 22:35:55 | 只看该作者
河蟹 发表于 2013-11-17 22:26
额,拿你的代码去运行了下,两个for循环出错,额,不能在循环里声明 i ,要单独声明

C99之后是允许在for的第一句初始化时定义变量的,C99之前很多编译器也有扩展支持的
当然如果你只用啊哈C就当我没说
9#
发表于 2013-11-17 22:42:57 | 只看该作者
rosynirvana 发表于 2013-11-17 22:35
C99之后是允许在for的第一句初始化时定义变量的,C99之前很多编译器也有扩展支持的
当然如果你只用啊哈C ...

额,目前就只用啊哈c的说,然后稍微添加了点
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #define LEN 30

  7. void strip(char*);

  8. int main()
  9. {
  10.   char w[3][LEN];
  11.   char filename[LEN];
  12.   int i;
  13.   printf("请输入文件名(包括文件后缀名):");
  14.   scanf("%29s", filename);
  15.   printf("请输入搜索关键字:");
  16.   scanf("%29s%29s%29s", w[0], w[1], w[2]);

  17.   FILE* fp = fopen(filename, "r");
  18.   assert(fp);

  19.   char buf[LEN];
  20.   int count[3] = {0};
  21.   while(fscanf(fp, "%29s", buf) == 1)
  22.   {
  23.     strip(buf);
  24.     for(i=0;i<3;i++)
  25.     {
  26.       if(strcmp(buf, w[i]) == 0)
  27.         count[i] += 1;
  28.     }
  29.   }

  30.   for(i=0;i<3;i++)
  31.     {
  32.     printf("%d\n", count[i]);
  33.     }
  34.     system("pause");
  35.   return 0;
  36. }

  37. void strip(char* buf)
  38. {
  39.   int start = 0;
  40.   int end = strlen(buf) - 1;
  41.   while(start < LEN && !isalpha(buf[start]))
  42.     ++start;
  43.   while(end >= 0 && !isalpha(buf[end]))
  44.     --end;

  45.   if(start < end)
  46.     return;
  47.   /* not a word, ignore */

  48.   memmove(buf, buf+start, end - start + 1);
  49. }
  50.    
复制代码
10#
发表于 2013-11-19 19:16:12 | 只看该作者
河蟹 发表于 2013-11-17 22:42
额,目前就只用啊哈c的说,然后稍微添加了点

呵~~~//你单单用啊哈C是不行的,我安装了c++、WIN-TC、啊哈C、TC2.0
11#
发表于 2013-11-19 19:16:32 | 只看该作者
4399APPLE 发表于 2013-11-19 19:16
呵~~~//你单单用啊哈C是不行的,我安装了c++、WIN-TC、啊哈C、TC2.0

以上那句注释就当我没说
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

广播台
特别关注
快速回复 返回顶部 返回列表