簡體   English   中英

C 程序將 2D 數組的每個字與順序 2D 數組的所有字進行比較

[英]C program to compare each word of a 2D array to all words of the order 2D array

我創建了 2 個包含單詞的二維數組。 例如:

loadKey[25][30] = 
{
   {'J','a','v','a','\0'},
   {'P','y','t','h','o','n','\0'},
   {'C','+','+','\0'},
   {'H','T','M','L','\0'},
   {'S','Q','L','\0'}
   // ... 20 other words here
};
resume[189][30] = 
{
   {'L','a','l','a','\0'},
   {'H','i','h','i','h','i','\0'},
   {'C','+','+','\0'},
   {'Y','o','Y','o','\0'},
   {'S','Q','L','\0'}
   // ... 184 other words here
};

我想將 loadKey[] 的每個單詞與 resume[] 的所有單詞進行比較,以計算 loadKey[] 的 25 個單詞與 resume[] 單詞匹配的次數。 我嘗試了 strcmp(loadKey[i], resume[j]) 但它是數組的指針。 任何人都可以幫我解決這個問題嗎? 非常感謝! 我的程序代碼:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PAUSE myPause()
#define KEYWORD 25
#define WORDS 250
#define MAX_LETTER 30

//*********************************************
// FUNCTION PROTOTYPES
void compAndCount(char loadKey[][MAX_LETTER], char resume[][MAX_LETTER]);
void myPause();
void readAndLoadKeyword(char loadKey[][MAX_LETTER]);

// MAIN FUNCTION
int main() {
    char loadKey[KEYWORD][MAX_LETTER];
    char resume[WORDS][MAX_LETTER];
    // load keywords from keywords file into array loadKey[] 
    readAndLoadKeyword(loadKey);
    for (int j = 0; j < KEYWORD; j++)
    {
        puts(loadKey[j]);
    }
    puts("\n");
    // compare and count the occurrences of keyword in resumes file
    compAndCount(loadKey, resume);
}

// FUNTIONS

void compAndCount(char loadKey[][MAX_LETTER], char resume[][MAX_LETTER]) {
    FILE* fpr;
    fpr = fopen("resumes.txt", "r");
    int r = 0, count = 0, num = 0, res = 0;
    char temp;
    while ((temp = fgetc(fpr)) != EOF) {
        if (temp != ' ' && temp != '\n') {
            resume[res][r] = temp;
            r++;
        }
        else
        {
            resume[res][r] = '\0';
            r = 0;
            res++;
        }
    }
    printf("words in resume file %i\n", res);
    for (int j = 0; j < res; j++)
    {
        puts(resume[j]);
    }
    puts("\n");
/*
    // way 1 to compare and count (WRONG?)
    for (int i = 0; i < res; i++) {
        if (i < KEYWORD) {
            scanf(" %[^\n]", loadKey[i]);
        }   
        scanf(" %[^\n]", resume[i]);
    }
    for (int k = 0; k < KEYWORD; k++) {
        for (int l = 0; l < res; l++) {
            if (strcmp(loadKey[k], resume[l]) == 0)
                count++;
        }
    }*/
    /*
    // way 2 to compare and count (WRONG?)
    char key[MAX_LETTER] = {'\0'}, r[MAX_LETTER] = {'\0'};
    for (int i = 0; i < KEYWORD; i++) {
        strcpy(key, loadKey[i]);
        for (int l = 0; l < res; l++) {
            strcpy(r, resume[l]);
            if (strcmp(key, r) == 0)
                count++;
        }
    }
    */
    printf("Resume Rating: %i\n", count);
    fclose(fpr);
} // end compAndCount

void myPause() {
    puts("\nPress ENTER to continue\n");
    exit(0);
}

void readAndLoadKeyword(char loadKey[][MAX_LETTER]) {
    FILE* fp;
    fp = fopen("keywords.txt", "r");
    char ch;
    int row = 0, col = 0;   
    if (fp == NULL) {
        puts("Not able to open keyword file!");
        PAUSE;
    }
    // load 25 keywords and ',' into an array line[]  
    char line[181]; // 180 characters + '\0'
    fgets(line, 181, fp);
    puts(line);
    puts("\n");
    // load 25 words in array line[] into array loadKey[]
    for (int i = 0; i < 180; i++) {
        ch = line[i];
        if (ch != ',') {
            loadKey[row][col] = ch;
            col++;
        }
        else {
            loadKey[row][col] = '\0';
            col = 0;
            row++;
        }
    }
    fclose(fp);
} // end readAndLoadKeyword

您可以使用字符串文字代替char{}

數組可以有不同數量的列和行。

char loadKey[25][30] = 
{
   "Java",
   "Python",
   // ... more words here
};
char resume[189][30] = 
{
   "Lala",
   "Hihihi",
   "C++",
   // more
};

//lkr - number of loadKer rows
//lkr - number of loadKer columns
//rr  - number of resume rows
//rc  - number of resume columns
//rep - count duplicates
size_t count(size_t lkr, size_t lkc, size_t rr, size_t rc, char (*loadKey)[lkc], char (*resume)[rc], int rep)
{
    size_t result = 0;
    for(size_t lkrow = 0; lkrow < lkr; lkrow++)
    {
        for(size_t rrow = 0; rrow < rr; lrow++)
        {
            if(!strcmp(loadKey[lkrow], resume[rrow])) 
            {
                result++;
                if(!rep) break;
            }
        }
    }
    return result;
}

如果相同的字符串可以多次出現在resume數組中,並且 oyu 也想計算重復次數,那么rep參數應該是非零的。

示例用法:

int main(void)
{
    size_t cnt = count(25, 30, 189, 30, loadKey, resume, 0);
    printf("%zu\n", count);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM