简体   繁体   English

C 中的匹配字符串模式平面文件

[英]matching string pattern flat file in C

Just to have a look a name in a list, when match, the value is discounted.只是为了查看列表中的名称,匹配时,价值会打折。

I tried to code, but the matching technique is fail.我尝试编码,但匹配技术失败。 Like I try to find "John", but it match with "John" and "Johnny", whether the expected match is just "John" (case is not sensitive)就像我试图找到“约翰”,但它与“约翰”和“约翰尼”匹配,预期匹配是否只是“约翰”(不区分大小写)

Just want to help my mom's store.只是想帮助我妈妈的商店。 What I want is something like: I have 3 set of flat file (list1.txt, list2.txt, list3.txt).我想要的是:我有 3 组平面文件(list1.txt、list2.txt、list3.txt)。 Each file has its name, for example:每个文件都有其名称,例如:

John
Rudy
Barrack Obama
John Travolta

List2.txt contained: List2.txt 包含:

Jeddi Sarah
Mute Sand

List3.txt contained: List3.txt 包含:

Greedy Black
Nevada Blue

The program when executed, ask:程序执行时,问:

Enter name: Greedy Black
Enter price: 1000

If the name is listed in list1.txt, he gets discount price 10%, list2.txt for 20%, and list3.txt for 30%.如果名字在 list1.txt 中列出,他将获得 10% 的折扣价,list2.txt 为 20%,list3.txt 为 30%。 example output:例如 output:

Enter name: Greedy Black
Enter price: 1000    
Greedy Black is found in list3.txt, got discount for 10%
price after discount: 900

But if he does not in any list, he gets normal price, which is 1000. How could I do that in C?但是如果他不在任何列表中,他会得到正常价格,即 1000。我怎么能在 C 中做到这一点? Thank you for the help...感谢您的帮助...

This Works Fine这很好用

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAX_C 111
char *trim(char *str);
int countlines(const char *filename);
char ** names(const char *filename);

int contains(const char* a)
{
    int l1 = countlines("list1.txt");
    int l2 = countlines("list2.txt");
    int l3 = countlines("list3.txt");

    char** c1 = names("list1.txt");
    char** c2 = names("list2.txt");
    char** c3 = names("list3.txt");



    for (int i = 0; i < l1; ++i) {
        if (strcmp(a,c1[i])== 0 )
            return 1;
    }
    for (int i = 0; i < l2; ++i) {
        if (strcmp(a,c2[i])== 0 )
            return 2;
    }
    for (int i = 0; i < l3; ++i) {
        if (strcmp(a,c3[i])== 0 )
            return 3;
    }
    for (int j = 0; j < l1; ++j) {
        printf("%s\t%s",a,c1[j]);
    }
    return 0;
}

int main()
{
    char  * a = (char * ) malloc(MAX_C);

    printf("Enter Name:\n");
    scanf("%[^\n]",a);
    int p;
    printf("Enter Price:\n");
    scanf("%d",&p);

    int c = contains(a);
    if(c)
    {
        printf("Greedy Black is found in list%d.txt, got discount for 10%%\nprice after discount: %f",c,p-p/10.0);
    }
else
    {
        printf("Greedy Black is found in list%d.txt, got discount for 10%%\nprice Without discount: %f",c,p);
    }


}


int countlines(const char *filename) {
    // count the number of lines in the file called filename
    FILE *fp = fopen(filename, "r");
    int ch = 0;
    int lines = 0;

    if (fp == NULL)
        return 0;

    lines++;
    while ((ch = fgetc(fp)) != EOF) {
        if (ch == '\n')
            lines++;
    }
    fclose(fp);
    return lines;
}
char ** names(const char *filename)
{
    int line = countlines(filename);
    char ** arr = (char **)malloc(line * sizeof(char *));
    for (int i=0; i<line; i++)
        arr[i] = (char *)malloc(MAX_C * sizeof(char));
    FILE * fp = fopen(filename, "r");
    if (fp == NULL) {
        printf("Could not open file %s", filename);
        return NULL;
    }
    for (int i = 0; i < line; ++i) {
        if(fgets(arr[i], MAX_C, fp)!=NULL)
        {
            trim(arr[i]);
        }
    }

    return arr;
}
char *trim(char *str)
{
    size_t len = 0;
    char *frontp = str;
    char *endp = NULL;

    if( str == NULL ) { return NULL; }
    if( str[0] == '\0' ) { return str; }

    len = strlen(str);
    endp = str + len;

    /* Move the front and back pointers to address the first non-whitespace
     * characters from each end.
     */
    while( isspace((unsigned char) *frontp) ) { ++frontp; }
    if( endp != frontp )
    {
        while( isspace((unsigned char) *(--endp)) && endp != frontp ) {}
    }

    if( frontp != str && endp == frontp )
        *str = '\0';
    else if( str + len - 1 != endp )
        *(endp + 1) = '\0';

    /* Shift the string so that it starts at str so that if it's dynamically
     * allocated, we can still free it on the returned pointer.  Note the reuse
     * of endp to mean the front of the string buffer now.
     */
    endp = str;
    if( frontp != str )
    {
        while( *frontp ) { *endp++ = *frontp++; }
        *endp = '\0';
    }

    return str;
}


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM