簡體   English   中英

二進制搜索字符串數組和其他功能

[英]Binary search on string array and extra feature

這是我一直在努力學習C ++的程序。 它將文件輸入到兩個不同的數組(字符串,2d雙精度數組)中,而不是進行排序,取平均值,然后將平均值添加到2d double數組末尾,而不是輸出。 我正在嘗試實現一個搜索(最好是二進制),該搜索找到與searchItem變量匹配的名稱,而不是獲取與該名稱相關的分數以輸出。 我有一個如何實現得分的想法。 將找到名稱的元素位置存儲到變量中,並使用該名稱引用正確分數所在的行。 二進制搜索遇到麻煩,不會產生編譯錯誤,也不會做任何事情。 據我了解,我正在使用的算法與我見過的其他算法相同。 另外,我是否對自己的想法正確,還是有更好的方法? 在我看來,這是最簡單的方法,但是我對這種語言的經驗有限。 先感謝您。

#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;
    }
}

輸入文件:bowlers2.txt

Linus too good
100
23
210
Charlie brown
1
2
12
Snoopy
300
300
100
Peperment Patty
223
300
221
Pig Pen
234
123
212
Red Headed Girl
123
222
111
Marcey
1
2
3
keith hallmark
222
300
180
anna hallmark
222
111
211
roxie hallmark
100
100
2

當您使用相等運算符比較兩個std::string對象時,它將比較整個字符串。 如果您要查找"keith"並將其與字符串"keith hallmark"進行比較,則它將不匹配。

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

喜歡

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM