简体   繁体   English

将字符串c ++与通配符值进行比较

[英]comparing strings c++ with wildcard values

I've been lurking around here for a long time, thank you for all your help in the past, even if this is the first question I've had to ask. 我一直潜伏在这里很长一段时间,感谢你过去的所有帮助,即使这是我不得不问的第一个问题。

I'm trying to make a simple database program, and I am stuck the search requirement for it. 我正在尝试制作一个简单的数据库程序,但我仍然坚持它的搜索要求。 When searching, the user needs to be able to enter a question mark if they don't know a value. 搜索时,如果用户不知道某个值,则需要输入问号。 If you knew a movie was from the 90's you could enter 199? 如果你知道一部电影来自90年代你可以输入199? and it would find all the movies that matched 199_. 它会找到所有匹配199_的电影。 I keep getting errors when I compile, "cannot convert 'char*' to 'char ( )[5] for argument '2' to 'bool compareYears(const char , char (*)[5]" I am trying to figure most of it out on my own, and I like to separate the functions and make them work in a separate .cpp file before adding them to the main file, just to make debugging easier. 我在编译时不断收到错误,“无法将'char *'转换为'char( )[5]以将参数'2'转换为'bool compareYears(const char ,char(*)[5]”我试图解决大多数问题它本身就是我自己的,我喜欢将它们分开并使它们在一个单独的.cpp文件中工作,然后再将它们添加到主文件中,只是为了使调试更容易。

#include <iostream>
#include <cstring>
#include <fstream>
#include <cctype>
using namespace std;

const int yearLength = 4;
typedef char year[yearLength + 1];

bool compareYears(const year year1, year year2[]);

int main()
{
    year year1 = "1992"; //year from database, will be assigned the 
                         //  variable when implemented in main program. 
    year year2;          //year entered by user, it will be compared to year1.

    cout << "Enter a year to search for: ";
    cin >> year2;
    cout << endl;

    if((compareYears(year1, year2)) == true)
        cout << "they match\n";
    if((compareYears(year1, year2)) == true)
        cout << "they do not match\n";

    return 0;
}

bool compareYears(const year year1, year year2[])
{
    for(int i = 0; i < 4; i++)
    {
        if (strncom(year1, year2[i], 4) ==0)
            return true;
        else if (strncmp(year1, "????", 4) == 0)
            return true;
        else
            return false;
    }
}

Thanks for helping me out with this, usually the most help I get from others is useless or insulting. 感谢您帮助我解决这个问题,通常我从别人那里获得的最大帮助是无用的或侮辱性的。 What I need help with most is getting rid of that compiler error. 我最需要帮助的是摆脱编译器错误。 I cannot figure it out for the life of me. 我无法理解我的生活。

First of all read this: typedef fixed length array 首先阅读: typedef固定长度数组

Then, use this typedef: 然后,使用此typedef:

typedef struct year { char characters[4]; } year;

and change the code this way: 并以这种方式更改代码:

int main()
{
    year year1; 
    year1.characters= "1992"; //year from database, will be assigned the variable when implemented in main program. 
    year year2;          //year entered by user, it will be compared to year1.

    cout << "Enter a year to search for: ";
    cin >> year2.characters;
    cout << endl;

    if((compareYears(year1, year2)) == true)
        cout << "they match\n";
    else
        cout << "they do not match\n";

    return 0;
}

bool compareYears(year year1, year year2)
{
    if (strncom(year1.characters, year2.characters, 4) ==0)
        return true;
    else if (strncmp(year1, "????", 4) == 0)
        return true;
    return false;
}

I also fixed some logical bugs 我还修复了一些逻辑错误

Just change these lines and it will work...the function declaration needs an array of year and you are trying to pass a variable.. 只需更改这些行,它将工作...函数声明需要一年的数组,你试图传递一个变量..

if((compareYears(year1, &year2)) == true) //this changed from year2 to &year2
cout << "they match\n";
if((compareYears(year1, &year2)) == true) //this changed from year2 to &year2
cout << "they do not match\n";

The type of the year2 argument for compareYears looks strange. year2参数的compareYears看起来很奇怪。 It looks like this argument is the mask to test against, ie a literal year like 1992 or something using wildcards. 看起来这个参数是要测试的mask ,即1992的字面1992或使用通配符的东西。 Hence, how about making it a char array of yearLength bytes? 因此,如何使它成为yearLength字节的char数组?

It might be even easier to write just a generic function which is given two strings or arbitrary length (the second of which may use wildcards) and sees whether they are equal. 编写一个通用函数可能更容易,该函数有两个字符串或任意长度(第二个可以使用通配符)并查看它们是否相等。 The quality test could first see whether both strings are the same length and if so, whether the character at every position in both strings is either equal or the character at the given position in the second string is a '?'. 质量测试首先可以看出两个字符串是否长度相同,如果是,两个字符串中每个位置的字符是否相等,或者第二个字符串中给定位置的字符是否为“?”。 You could use a single loop to walk over both strings in parallel to do this. 您可以使用单个循环并行遍历两个字符串来执行此操作。

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

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