C++ 读取 .txt 文件 + 二进制搜索 + 排序;上课有问题

标签 c++

所以我需要从一个input.txt文件中读取10,000个数字,然后搜索它是否在数组中,如果不在,则将其插入并递增频率数组中的频率值。然后我需要按频率数组对数组进行降序排序。我知道我仍然缺少很多......但我的主要问题是在 main.cpp 中找到的。当我引用 TermTable.BinarySearch 和 TermTable.Sort 时,我得到“错误:需要一个标识符”。这是我的 main.cpp 文件。所以我最大的问题是:为什么我不能访问类 TermTable 中的方法??

#include <cstdlib>
#include <iostream>
#include <fstream>
#include "TermTable.h"

using namespace std;

int main(){
const int size = 10000;
int termArray[size];
int frequencyArray[size];
char * charArray = new char[size];
int position = 0;
ifstream fin("input.txt");
if (fin.is_open())
{
    cout << "Open" << endl;
    while (!fin.eof() && position < size){
        fin.get(charArray[position]);
        position++;
    }
    charArray[position - 1] = '\0';
    for (int i = 0; charArray[i] != '\0'; i++)
    {
        termArray[i] = charArray[i];
    }
    for (int i = 0; termArray[i] != '\0'; i++){
        int searchValue = termArray[i];
        TermTable.BinarySearch(int termArray, int size, int searchValue);
        if (position != -1){ frequencyArray[i] += 1; }
        else if (position == -1){ 
            frequencyArray[i] = 0;
        }

    }
    TermTable.Sort(int termArray, int size);
}
else 
{
    cout << "couldn't open" << endl;
}
return 0;
}

这是我的规范 .cpp 文件。

#include <iostream>
#include <cstdlib>
#include "TermTable.h"
using namespace std;


int TermTable::BinarySearch(int array[], int size, int searchValue){
int first, last, middle, position; bool found; first = 0; last = size - 1; found = false; position = -1;
while (!found && first <=last)
{
    middle = (first + last) / 2; 
    if (array[middle] == searchValue)
    {
        found = true;
        position = middle;
    }
    else if (array[middle] > searchValue)
        last = middle - 1;
    else
        first = middle + 1;
}
return position;
}

void TermTable::Sort(int array[], int size){
int temp; bool swapOccurred;
do{
    swapOccurred = false;
    for (int count = (size-1); count > 0; count--)
    {
        if (array[count] < array[count - 1])
        {
            temp = array[count];
            array[count] = array[count - 1];
            array[count - 1] = temp;
            swapOccurred = true;
        }
    }
} while (swapOccurred);
}

这是我的类(class)文件。

#include <cstdlib>
#include <iostream>
using namespace std;

//class specification
class TermTable {

public:
//constructor
TermTable();

//member functions
int BinarySearch(int array[],int size, int searchValue);
void Insert(int value);
void Sort(int array[],int size);

//destructor
~TermTable();

private:
//data
int currentAmount;

};

最佳答案

我发现了两件事:

  1. 方法 BinarySearch()Sort() 是类 TermTable成员函数,而不是 < em>静态方法。因此,您需要实例化 TermTable,然后使用该对象调用这些方法。

main() 中:

TermTable termTableObj;
termTableObj.BinarySearch(...);
...
termTableObj.Sort(...);
  1. 调用方法时,只需要传变量名,不传变量类型,如:

    TermTable termTableObj;
    termTableObj.BinarySearch(termArray, size, searchValue);
    termTableObj.Sort(termArray, size);
    

关于C++ 读取 .txt 文件 + 二进制搜索 + 排序;上课有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27977421/

相关文章:

c++ - 使用 make 编译 MPI,出现几个命名空间错误,例如 "error: unknown type name ‘using’?

c++ - 如何修复 C++ 中的 'Segmentation fault' 错误

c++ - 在具有不同网格单元格大小的网格中查找单元格

c++ - 快速时间函数 C/C++

c++ - 为嵌套自定义类型定义散列函数

c# - P/Invoke问题(栈不平衡)

c++ - 如何在 C++ 中执行 netstat 来查看端口是否正在使用?

c++ - 没有着色器的 OpenGL 实例化渲染

c++ - 我应该怎么做才能在 C++ 中扩展输出值

c++ - 在 C++ 中使用引用进行向上转换