c - 在 C 中向 char *[] 添加单词

标签 c arrays

我有一个程序,可以将两个文件的单词(第一个是单词列表,第二个是古腾堡项目的电子书)读取到两个 char * 数组中。

我正在尝试添加第二个 char *array 中未出现的所有唯一单词 将第一个 char *array 转换为第三个 char *array,然后打印它们。

该程序添加了正确的单词,但添加了多次。 错误发生在 findOdds() 中。 请注意,当我使用非二进制搜索方法时,该程序可以正常工作,但需要很长时间。 我的程序有什么问题?我为我的英语道歉。

#include <stdio.h>
#include <stdlib.h> /* for malloc() */
#include <ctype.h>
#include <string.h>
#define MAXCHAR 24
#define MAXLINES 150000

int add2array(FILE *fp, char *lineptr[]);

int findOdds(char *lineptr[], char *lineptr1[], int nlines, int nlines1);
int binsearch1(char *val, char *lineptr[], int nlines);

char *lineptr2[MAXLINES]; /* The unique words not in the word list */

int main(int argc, char *argv[])
{
    FILE *my_stream, *my_stream1;

    char *lineptr[MAXLINES], *lineptr1[MAXLINES];
    int i, nlines, nlines1, nlines2;

    /* Load the wordlist. */
    my_stream = fopen("words.txt","r"); 
    if(my_stream == NULL) {
        printf("error: Couldn't open file\n");
        return 2;
    } else {
        nlines = add2array(my_stream, lineptr);
        fclose(my_stream);
    } 
    if(nlines==-1) {
        printf("error: Epic Failure to copy words to char *lineptr[]\n");
        return -1;
    }

    /* Load the ebook. */
    my_stream1 = fopen("horsemanship.txt","r");
    if(my_stream1 == NULL) {
        printf("error: Couldn't open file\n");
        return 2;
    } else {
        nlines1 = add2array(my_stream1, lineptr1);
        fclose(my_stream1);
    } 
    if(nlines1==-1) {
        printf("error: Epic Failure to copy words to char *lineptr[]\n");
        return -1;
    }

    /* Find and print the unique words from the ebook not in the wordlist */
    nlines2 = findOdds(lineptr, lineptr1, nlines, nlines1);
    for(i=0; i<nlines2; i++)
        printf("%s\n",lineptr2[i]);
    return 0;
}

/* add2array: read the words from the file into char *lineptr[] */
int add2array(FILE *fp, char *lineptr[])
{
    int nlines=0, c=0, pos=0;
    char temp[MAXCHAR];
    char *p;

    while((c = getc(fp)) != EOF) {
        if(isalpha(c))
            temp[pos++] = tolower(c);
        else if(!isalpha(c)) {
            temp[pos] = '\0';
            pos = 0;
            if(isalpha(temp[0])){
                if((p = malloc(sizeof(temp)))==NULL)
                    return -1;
                strcpy(p, temp);
                lineptr[nlines++] = p;
            }
        }
    }
    return nlines;
}

/* Add the unique words from lineptr1 not in lineptr to lineptr2 */ 
int findOdds(char *lineptr[], char *lineptr1[], int nlines, int nlines1)
{
    char *p;
    char temp[MAXCHAR];
    int i, nlines2=0;

    for(i=0; i<nlines1; i++) {
        if(binsearch1(lineptr1[i], lineptr, nlines)==-1) {
            if(binsearch1(lineptr1[i], lineptr2, nlines2)==-1) {
                if((p = malloc(sizeof(temp)))==NULL)
                    return -1;
                strcpy(p, lineptr1[i]);
                lineptr2[nlines2++] = p;
            }
        }
    }
    return nlines2;
}

int binsearch1(char *val, char *lineptr[], int nlines)
{
    int pos;
    int start = 0;
    int end = nlines-1;
    int cond = 0;

    while(start <= end){
        pos=(start + end)/2; 
        if((cond = strcmp(lineptr[pos],val)) == 0)
            return pos;
        else if(cond < 0)
            start = pos+1;
        else
            end = pos-1;
    }
    return -1;
}

最佳答案

如果要使用二分搜索,则必须对数组进行排序,如上面 n.m 所述。

    in main() ...

    shellsort1(lineptr1, nlines1);
    /* Find and print the unique words from the ebook not in the wordlist */
    nlines2 = findOdds(lineptr, lineptr1, nlines, nlines1);
    ...


int shellsort1(char *v[], int n)
{
    int gap, i, j;
    char temp[MAXCHAR];
    char *p;

    for(gap=n/2; gap>0; gap/=2)
        for(i=gap; i<n; i++)
            for(j=i-gap; j>=0 && strcmp(v[j],v[j+gap])>0; j-=gap) {
                if((p = malloc(sizeof(temp)))==NULL)
                    return -1;  
                p = v[j];
                v[j] = v[j+gap];
                v[j+gap] = p;
            }
    return 0;
}

关于c - 在 C 中向 char *[] 添加单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25595532/

相关文章:

c - Sonar C++ 社区插件

我们可以在结构声明中使用#define 常量作为数组大小吗?

不使用 << 或 >> 检查 32 位符号位

c - 为 mips 链接可执行文件时,我收到此错误重定位被截断以适合 : R_MIPS_TLS_GOTTPREL against `_nl_current_LC_COLLATE'

arrays - Redux match.params 更新状态后不起作用

arrays - 使用 Array.FindAll 方法查找满足条件的子数组

c - 如何在C中的printf语句中打印循环结果

c++ - 关于C++中数组的一些问题

java - 从字符串数组中删除重复的单词

Java:用一个字符减去一个字符是什么意思?