二进制搜索字符串数组和额外功能

时间:2016-04-24 03:11:36

标签: c++ arrays string algorithm binary-search

这是我一直在学习C ++的程序。它将文件输入两个不同的数组(字符串,2d double),而不是排序,平均值,将平均值添加到2d双数组的末尾而不是输出。我试图实现一个搜索(最好是二进制),找到与searchItem变量匹配的名称,而不是获取与该名称相关联的分数来输出。我知道如何实现抓取分数。将变量存储到变量中,找到名称的元素位置,并使用它来引用正确分数所在的行。像二进制搜索一样出现问题不会产生编译错误,也不会做任何事情。根据我的理解,我使用的算法与我见过的其他算法相同。我是否在正确的轨道上有我的想法或有更好的方法?在我看来,这是最简单的方式,但我对这种语言的经验是有限的。提前谢谢。

<table>
<?php
    $items = WC()->cart->get_cart();

    // Here begins the cart loop
    foreach($items as $item => $values) { 
        $_product = $values['data']->post;
        $product_id = $values['product_id']; // product Id
        $product_title = $_product->post_title // product title
        $product_price = get_post_meta($product_id , '_price', true); // product price
        $product_qty = $values['quantity']; // product quantity
        $productDetails = wc_get_product( $product_id ); // product details array
        // Here in this foreach loop you display each product of the cart
?>
    <tr>
        <td><?php echo $product_title; ?></td>
        <td><?php echo $product_qty; ?></td>
        <td><?php echo $product_price; ?></td>
    </tr>
<?php            
    } 
?>
</table>

输入文件:bowlers2.txt

#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;  

const int ROWS = 11;
const int COLS = 4;

void ArrayManip (string x[], double y[][COLS], int length);

int main() 
{
    ifstream inFile;

    inFile.open("bowlers2.txt");

    const int SIZE = 10;    

    double scores[ROWS][COLS];
    string names[SIZE];
    string mystring;

    if (!inFile)
    {
        cout << "Can not open the input file"
             << " This program will end."<< "\n";
        return 1;

    }

    for(int r = 0; r < SIZE; r++)
    {
        getline(inFile, names[r]);
        for(int c = 0; c < (COLS - 1); c++)
        {
            getline(inFile, mystring);
            scores[r][c] = atoi(mystring.c_str());
        }
    }

    inFile.close();

    ArrayManip (names, scores, SIZE);

    return 0;
}

void ArrayManip (string x[], double y[][COLS], int LENGTH)
{
    int i,j,k,
        first = 0,
        last = LENGTH - 1,
        middle,
        position = -1;    
    bool found = false;
    string searchItem = "keith";
    string sValue;
    double dValue;
    double dArray[COLS - 1];
    double sum = 0;
    double avr;

    //Sort names and scores
    for(i = 1; i < LENGTH; i++)
    {
        sValue = x[i];
        for (k = 0; k < (COLS - 1); k++)
        {
           dArray[k] = y[i][k];
        }
        for(j = i - 1; j >= 0 && x[j] > sValue; j--)
        {
            x[j + 1] = x[j];
            for (k = 0; k < (COLS - 1); k++)
            {
               y[j + 1][k] = y[j][k];
            }
        }
        x[j + 1] = sValue;
        for (k = 0; k < (COLS - 1); k++)
        {
           y[j + 1][k] = dArray[k];
        }
    }

    for(k = 0; k < LENGTH; k++)
        for(i = 1; i < (COLS - 1); i++)
        {
            dValue = y[k][i];
            for(j = i - 1; j >= 0 && y[k][j] > dValue; j--)
            {
                y[k][j + 1] = y[k][j]; 
            }
            y[k][j + 1] = dValue;
        }

    //average and store rows
    for(i=0;i<LENGTH;i++)
    {
        for(j = 0; j < (COLS - 1); j++)
        {
            sum += y[i][j];             
        }
        avr = sum/(COLS -1);
        y[i][j++] = avr;
        sum = 0;        
    }

    //average and store columns
    for(j=0;j<(COLS - 1);j++)
    {
        for(i=0;i<LENGTH;i++)
        {
            sum += y[i][j];
        }
        avr = sum/LENGTH;
        y[i++][j] = avr;
        sum = 0;
    }

    cout << fixed << showpoint << setprecision(2);
    cout << "Averages for all Players:\n";
    for(i=0;i<(COLS - 1);i++)
    {
        cout << "Score " << (i+1) << " average: " << y[LENGTH][i] << "\n";
    }

    cout << "\n";

    for(i=0;i<LENGTH;i++)
    {
        cout << "player " << (i+1) << ": " << x[i] << "\n";
        for(j=0;j<COLS;j++)
        {
            if(j < (COLS - 1))
            {
            cout << "score " << (j+1) << ": " << y[i][j] << "\n";            
            }
            else
            cout << "player average: " << y[i][j] << "\n\n";            
        }
    }    

    while(!found && first <= last)
    {
        middle = (first + last) / 2;
        if(x[middle] == searchItem)
        {
            found = true;
            cout << x[middle] << "\n";
        }
        else if (x[middle] > searchItem)
            last = middle - 1;
        else
            first = middle + 1;
    }
}

1 个答案:

答案 0 :(得分:1)

使用等于运算符比较两个std::string对象时,它会比较整个字符串。如果您正在寻找"keith"并将其与字符串"keith hallmark"进行比较,那么它将无法匹配。

您可以使用std::string::find在字符串中查找子字符串。

if (x[middle].find(searchItem) != std::string::npos)
{
    // Sub-string found
}