Вопрос

I am now on part 4 of my homework, I am trying to figure out how to Binary Search just the first 14 characters from an array of strings. We were given two .csv files:

The 01110011100110 is a simulated barcode.

Inventory.csv: Inventory items separated by a comma - Barcode, product, price First Five Lines:

01001010100011,Air freshener,12.43
01101000111101,Alfredo sauce,10.71
10000010100101,All spice,4.14
00011011001111,Allergy medication,20.06
00101010111011,Aluminum foil,8.92

Carts.csv: A 'cart' containing barcodes of purchases First Five Lines:

01000011010100,00010011010100
00110111100000
00100110100000,01011000110111,01011101000110,01011101000000,01001110000010
01110101100101,00100111110101,00101101110110,00100110000000,00100000001100,00101101011100
00101100111110,01000110110000,01010110111110,00100111111001,01011101101010,01011011010011,00010011010100,01010111101001

I am working on Part4 of the homework: Part 4: Write a function to read in the Carts.csv file. Read in one line at a time and process it. The process steps are: a. Separate the barcodes in each cart based on the comma delimiter.
b. For each barcode, lookup the barcode in the product list. c. Extract the price from the product string data line and add it to a running total for the cart. d. After processing the line, output the total price for the cart. e. Repeat from step (a) until end-of-file.

When I try to lookup it's returning -1, it is comparing the whole line rather than just part of it. How do Binary Search just part of a string. Here is the excerpt of my code:

int binarySearch(string* list, int size, string value){
    int first = 0,
        last = size - 1, middle,
        position = -1;
        bool found = false;
    while (!found && first <= last){
        middle = (first + last) / 2;

        if (list[middle].compare(value) == 0){
            found = true;
            position = middle;
        }
        else if (list[middle].compare(value) > 0)
            last = middle - 1;
        else
            first = middle + 1;     
    }
    return position;
}

void processFile(string filename, string* list, int size){
    ifstream file(filename);
    string line;
    int count = 1;
    if (file.is_open()){
        while (!file.eof()){
            int ctr = 0;
            int start = 0;
            getline(file, line);
            string substring = "";
            int find = line.find(COMMA);
            cout << "Cart " << count << ": ";
            while (find != string::npos){
                substring = line.substr(start, find - start);
                int position = binarySearch(list, size, substring);
                cout << position << " ";
                start = find + 1;
                ctr++;
                find = line.find(COMMA, start);
            }
            if (ctr > 0 || find == -1){
                substring = line.substr(start, line.length() - start);
                int position = binarySearch(list, size, substring);
                cout << position << endl;
                count++;
            }
        }
    }
    file.close();
}
Это было полезно?

Решение

try the following function realization

int binarySearch( const std::string* list, int size, std::string value )
{
    int position = -1;
    int first = 0, last = size - 1;

    while ( first <= last )
    {
        int middle = ( first + last ) / 2;

        int result = list[middle].substr( 0, value.size() ).compare( value );
        if ( result == 0 )
        {
            position = middle;
            break;
        }
        else if ( result > 0 )
        {
            last = middle - 1;
        }
        else
        {
            first = middle + 1;
        }      
    }

    return position;
}

Of course array list must be sorted according to barcodes in the ascending order.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top