[英]Binary search on string array and extra feature
This is a program I have been working on to learn C++. 这是我一直在努力学习C ++的程序。 It inputs a file into two different arrays (string, 2d double) than sorts, averages, adds averages to ends of 2d double array than outputs.
它将文件输入到两个不同的数组(字符串,2d双精度数组)中,而不是进行排序,取平均值,然后将平均值添加到2d double数组末尾,而不是输出。 I'm trying to implement a search (preferably binary) that finds the name matching to searchItem variable than gets the scores associated with that name to output.
我正在尝试实现一个搜索(最好是二进制),该搜索找到与searchItem变量匹配的名称,而不是获取与该名称相关的分数以输出。 I have an idea of how to implement grabbing the scores.
我有一个如何实现得分的想法。 Store into a variable the element position the name was found and use that to reference the row where the right scores will be.
将找到名称的元素位置存储到变量中,并使用该名称引用正确分数所在的行。 Having trouble with the binary search as in it gives no compile errors and isn't doing anything.
二进制搜索遇到麻烦,不会产生编译错误,也不会做任何事情。 From what I understand the algorithm I'm using is the same as others I've seen working.
据我了解,我正在使用的算法与我见过的其他算法相同。 Also am I on the right track with the idea I had or is there a better method?
另外,我是否对自己的想法正确,还是有更好的方法? It seemed to me to be the simplest way but my experience with this language is limited.
在我看来,这是最简单的方法,但是我对这种语言的经验有限。 Thank you in advance.
先感谢您。
#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;
}
}
Input file: bowlers2.txt 输入文件: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
When you compare two std::string
objects using the equality operator, it compares the whole strings. 当您使用相等运算符比较两个
std::string
对象时,它将比较整个字符串。 If you're looking for "keith"
and compare it to the string "keith hallmark"
then it won't match. 如果您要查找
"keith"
并将其与字符串"keith hallmark"
进行比较,则它将不匹配。
You can use std::string::find
to find a substring in a string. 您可以使用
std::string::find
查找字符串中的子字符串。
Like 喜欢
if (x[middle].find(searchItem) != std::string::npos)
{
// Sub-string found
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.