这是我一直致力于学习 C++ 的程序。它将文件输入到两个不同的数组(字符串,2d double )而不是排序,平均,将平均值添加到 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
}
关于c++ - 字符串数组的二进制搜索和额外功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36818871/