所以我需要從input.txt文件讀取10,000個數字,然後搜索它是否在數組中,如果它不在數組中,則插入它並將頻率值增加到頻率數組中。然後我需要按降序排列陣列。我知道我錯過了很多......但我的主要問題是在main.cpp中找到的。當我引用TermTable.BinarySearch和TermTable.Sort時,我得到「錯誤:預期的標識符」。這是我的main.cpp文件。所以我最大的問題是:爲什麼我無法訪問類TermTable中的方法?C++從.txt文件讀取+二進制搜索+排序;故障類
#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);
}
這裏是我的班級文件。
#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;
};
如果使用'new'運算符創建類實例,則必須使用類指針'TermTable *'。 – Ari0nhh
@ Ari0nhh我的壞!相應地更正:) – CinCout
哦,我的....現在我覺得愚蠢。感謝您及時的回覆! (: –