| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 418 人关注过本帖
标题:想不明白,向前辈求教,小程序改错
只看楼主 加入收藏
xixiqiqi
Rank: 2
等 级:论坛游民
帖 子:22
专家分:71
注 册:2013-10-10
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
想不明白,向前辈求教,小程序改错
本来是想写一个类似于文本编辑器“查找与替换”的简单功能,但我发现我写的这个单词查找方法findw()有一个问题,是这样,查找单词没问题,查找字母好像也没问题,但在查找字母a时就有问题.... 不能完全查找到所有的a,调试发现在findw()的while()中word数组的内容被改变了,为什么呢?想不明白,请大家多多指点一下,告诉我错在哪里~

环境:MS Visual C++ 6.0

程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

// 测试文本
char str[1024]="When we are children, there are lots of people ask about our dreams, and our answers most are doctors, policemen, teachers etc. But when we grow up, we have to face the reality, not everyone would realize childhood dreams, and some of these dreams even are unrealistic which will never be able to achieve. Therefore, we need correctly treat the relationship between dream and reality.";

// 打印查找结果
void display(int* index,int wlen)
{
    char mark_start=16;
    char mark_end=17;
    char* sp=str;
    int* ip=index;
    int m=0;
    while(*sp!='\0')
    {
        if(m==*ip)
        {
            putchar(mark_start);
        }
        if(m==*ip+wlen)
        {
            putchar(mark_end);
            ip++;
        }
        putchar(*sp);
        sp++;
        m++;
    }
}

// 查找单词
void findw()
{
    char word[20]={0};           // 用于保存要查找单词
    int index[20]={0};           // 用于保存查找单词在文本中出现的位置
    char* p=str;
    char* wp=word;
    printf("\n请输入您想查找的单词: ");
    scanf("%s",&word);
    while(getchar()!='\n');         
    printf("您要查找的单词为: %s\n",word);
    
    int j=0;
    while(*p!='\0')
    {
        if(*p==*wp)
        {
            wp++;
        }
        else
        {
            wp=word;
            if(*p==*wp)
            {
                wp++;
            }                            
        }
                                      
        if(*wp=='\0')
        {
            index[j++]=p-str-strlen(word)+1; 
            wp=word;                    
        }

        p++;
    }
    printf("查找的单词数为%d个\n",j);    
    if(j!=0)
    {
        printf("是否用标记在文中显示?(y/n)");
        char c=getchar();
        while(getchar()!='\n');
        if(c=='y')
        {
            display(index,strlen(word));
        }
    }
}


int main(int argc,char* argv[])
{
    // test
    while(1)
    {
        findw();
    }
    return 0;
}
搜索更多相关主题的帖子: include 编辑器 单词 字母 
2013-11-26 12:26
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:0 
不是没找到,而是你的display只会输出index[0]


[fly]存在即是合理[/fly]
2013-11-26 12:46
xixiqiqi
Rank: 2
等 级:论坛游民
帖 子:22
专家分:71
注 册:2013-10-10
收藏
得分:0 
回复 2楼 azzbcc
index数组中保存的是查找单词出现的位置,在display()中..if(m==*ip+wlen)在输出了查找单词结束标记后有ip++,会在下一个查找单词出现时做标记.display()方法应该没有问题,错应该在findw()的while()中,但就是搞不明白...
2013-11-26 13:53
pink_duo
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:209
专家分:1054
注 册:2013-11-5
收藏
得分:10 
试试楼上的办法

埋头做牛,抬头做人,低头做狗
2013-11-28 16:35
azzbcc
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:江西财经大学
等 级:贵宾
威 望:81
帖 子:3293
专家分:12919
注 册:2012-11-4
收藏
得分:10 
恩,我没说清楚咯

在findW中,你给display的传参是 index,应该是 &index[i](i是循环变量)


[fly]存在即是合理[/fly]
2013-11-28 17:49
快速回复:想不明白,向前辈求教,小程序改错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018179 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved