1

最初的提示是:编写一个跟踪扬声器局的程序。该程序应使用一种结构来存储有关演讲者的以下数据: 姓名 电话号码 演讲主题 费用

该程序应使用至少 10 个结构的数组。它应该让用户向数组中输入数据,更改任何元素的内容,并显示存储在数组中的所有数据。该程序应该有一个菜单驱动的用户界面。输入验证:输入新扬声器的数据时,请确保用户输入所有字段的数据。演讲者的费用不得输入负数。

添加的提示是:我需要这个来扩展搜索模式,其中可能包含字母或数字拼写错误的一个字符。只有一个字符可能是错字,在任何位置尝试这些测试模式应该得到以下结果:

0-9 is 0x30-0x39
a-z is 0x41-0x5A
A-Z is 0x61-0x7A (or lower case it)

而且我无法获得使用当前程序的附加提示。

搜索模式中的其他字符不得更改。

#include <iostream>
#include <cstring>
#include<string>

using namespace std;
bool print_one_typo(string input, string people[11], bool is_found[11])
{
    bool found = false;
    if (input[0] == '?')
    {
        char *strPtr = NULL;
        for (int i = 0; i < 11; i++)
        {
            strPtr = strstr(people[i].c_str(), input.substr(1).c_str());
            if (strPtr != NULL)
            {
                cout << "\t" << people[i] << endl;
                found = true;
                is_found[i] = true;
            }
        }
    }
    else
    {
        for (int i = 0; i < 11; i++)
        {
            bool match = true;
            string str = people[i];
            int value = str.find(input[0]);
            for (int k = 0; k < input.length(); k++)
            {
                if (input[k] != '?' && input[k] != str[value++])
                {
                    match = false;
                    break;
                }
            }
            if (match && !is_found[i])
            {
                found = true;
                cout << "\t" << people[i] << endl;
            }
        }
    }
    return found;
}

int main()
{
    string people[11] = { "Becky Warren, 555-1223",
        "Joe Looney, 555-0097",
        "Geri Palmer, 555-8787",
        "Lynn Presnell, 555-1225",
        "Holly Gaddis, 555-8878",
        "Sam Wiggins, 555-0998",
        "Bob Kain, 555-8712",
        "Tim Haynes, 555-7676",
        "Warren Gaddis, 555-9037",
        "Jean James, 555-9223",
        "Ron Palmer, 555-7227" };

    bool is_found[11] = { false };
    string lookUp;
    int i;
    cout << "\t People and Phone numbers" << endl;
    cout << "Enter name or phone number: ";
    cin >> lookUp;
    cout << "result: " << endl;
    bool found = false;
    bool output = false;
    for (int i = 0; i < lookUp.length(); i++)
    {
        string local = lookUp;
        found = print_one_typo(local.replace(i, 1, 1, '?'), people, is_found);
        if (found) output = true;
    }
    if (!output)
        cout << "No matching product was found" << endl;

    return 0;
}
4

1 回答 1

0

I think your code overthinks the problem. Also, you didn't specify if "typo" just means "wrong character", or a fuller range that includes dropped characters or inserted characters.

If you are only looking for matches with zero or one incorrect characters, but otherwise the same length, I think this code should do:

bool print_one_typo(string input, string people[11], bool is_found[11])
{
    for (int i = 0; i < 11; i++)
    {
        if ( is_found[i] )              // Skip if it had already been found?
            continue;

        if ( input.length() != people[i].length() )  // Are they same length?
            continue;                                // No:  Skip it.

        int    typos = 0;
        size_t len   = input.length();

        for (size_t j = 0; j != len && typos < 2; j++)
             if ( input[j] != people[i][j] )
                 typos++;

        if (typos < 2)    // Fewer than 2 typos:  We have a winner!  Return it.
        {
            is_found[i] = true;
            return true;
        }
     }

     return false;
} 

This code skips strings that differ in length, or which you're filtering out via the is_found[] array. Not sure why you're doing that, but I preserved that bit of your original code.

If it finds two strings that are the same length, it just compares them character by character, counting up typos. If it sees 2 or more typos, it skips to the next one. Otherwise, it takes the first string that's the same length but fewer than 2 typos.

于 2013-07-15T13:34:54.677 回答