我正在编写一个获取输入文件并对其进行解析的程序,它应该创建两个数组WORDS
,FREQUENCY
然后打印出它在其中得到的内容。Words 将包含文件中的所有单词而不重复它们。频率将具有每个单词在文件中出现的次数。
但是,我似乎无法让 main 正确使用文件,并且在尝试运行程序时不断收到这些错误。
main.cpp:21: error: no matching function for call to 'FileParser::parseFile(Analysis& (&)())'
fileParser.h:56: note: candidates are: void FileParser::parseFile(Analysis&)
main.cpp:25: error: request for member 'printListAll' in 'a', which is of non-class type 'Analysis& ()()'
我的代码:
//MAIN
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
#include "analysis.h"
#include "fileParser.h"
int main(int argc, char *argv[])
{
Analysis a();
//Input Parse
FileParser input("input.txt");
input.parseFile(a);
//Output
ofstream outP("output.txt");
a.printListAll(outP);
return 0;
}
====================
using namespace std;
#include "word.h"
#ifndef ANALYSIS_H
#define ANALYSIS_H
class Analysis{
private:
Word** wordList;
int wordListSize;
int wordListCapacity;
//Resizes the wordList array
void resize()
{
Word** temp = new Word* [wordListCapacity + 10];
for(int i = 0; i < wordListSize; i++)
temp[i] = wordList[i];
delete [] wordList;
wordList = temp;
wordListCapacity += 10;
}
//Sorts the words in the analysis
void sortAna()
{
for (int i = 0; i < wordListSize - 1; i++)
{
int curSmallest = i;
for (int j = i + 1; j < wordListSize; j++)
if (*wordList[j] < *wordList[curSmallest])
curSmallest = j;
if(curSmallest != i)
{
Word* temp = wordList[i];
wordList[i] = wordList[curSmallest];
wordList[curSmallest] = temp;
}
}
}
Analysis(const Analysis&){}
public:
Analysis(){
wordListSize = 0;
wordListCapacity = 10;
wordList = new Word*[wordListCapacity];
}
~Analysis()
{
for (int i = 0; i < wordListSize; i++)
delete wordList[i];
delete [] wordList;
}
bool isInAnalysis(char str[]){
for (int i = 0; i < wordListSize; i ++)
{
if (wordList[i]->equals(str))
return true;
}
return false;
}
void addWord(char str[], int frequency)
{
if (wordListSize == wordListCapacity)
resize();
//Check to see if the exists already, if it does just
//add to the frequency
else if (isInAnalysis(str))
{
for (int i = 0; i < wordListSize; i++)
{
if (wordList[i]->equals(str))
{
wordList[i]->addFrequencyToList(str);
break;
}
}
}
//If its a new word
else
{
wordList[wordListSize] = new Word(str);
wordList[wordListSize] -> addFrequencyToList(str);
wordListSize++;
}
}
//Print the Analysis in all output form (-a)
void printListAll(ostream& out)
{
sortAna();
int * tempFrequency;
for (int i = 0; i < wordListSize; i++)
{
char* str = wordList[i] -> getWord();
int numFrequency = wordList[i] -> getFrequency();
tempFrequency = new int[numFrequency];
wordList[i] -> getFrequencyList(tempFrequency);
//Handle wrapping
char tempOutput[55];
strcpy(tempOutput, str);
strcat(tempOutput, " = ");
char tempPageNumber[50];
for (int i = 0; i < numFrequency; i++){
sprintf(tempPageNumber, "%d", tempFrequency[i]);
int currPageLen = strlen(tempPageNumber);
if(strlen(tempOutput) + currPageLen > 49)
{
out << tempOutput << endl;
strcpy(tempOutput, " ");
}
strcat(tempOutput, tempPageNumber);
if(i != numFrequency - 1)
strcat(tempOutput, " ");
}
//Flush the last set of contents of the buffer
out << tempOutput << endl;
delete[] tempFrequency;
delete[] str;
}
}
};
#endif
==========
#include "analysis.h"
#include "word.h"
#ifndef FILEPARSER_H
#define FILEPARSER_H
class FileParser
{
private:
char* fileName;
ifstream cin;
public:
FileParser(char* fName)
{
fileName = new char[strlen(fName) + 1];
strcpy(fileName, fName);
cin.open(fileName);
if (!cin.is_open())
cerr << "****ERROR: Cannot open Input file to parse it. ****" << endl;
}
~FileParser()
{
cin.close();
}
bool isFileOpen(){
return cin.is_open();
}
//Returns the length of the string given as an int
int getFrequencyNumber(char* str)
{
return atoi(str);
}
//Parses the given File
void parseFile(Analysis& a)
{
char currentWord[50] = {0};
char currentFrequencyString[10] = {0} ;
int currentFrequency;
cin >> currentWord;
while (currentWord[1] != '-')
{
if(currentWord[0] == ' ')
{
//copy from 2nd character of currentWord to before the space
//which is at strlen(currentWord)
int count = 1;
while (currentWord[count] != ' ')
{
currentFrequencyString[count - 1] = currentWord[count];
count++;
}
currentFrequencyString[count - 1] = '\0';
currentFrequency = getFrequencyNumber(currentFrequencyString);
}
else
{
//Convert the word to lower case
Word::toLower(currentWord);
//add to the index (passed in as param) with current freq.
a.addWord(currentWord, currentFrequency);
}
//get next word
cin >> currentWord;
}
}
};
#endif
===============
using namespace std;
#ifndef WORD_H
#define WORD_H
class Word{
private:
char* string;
int* frequencyList;
int frequencySize; //Actual number of frequencies in the list.
int frequencyCap; //Total size of frequency
int wordFrequency; //Frequency of a certain word.
void resizeFrequency(){
int* temp = new int[frequencyCap+10];
for (int i = 0; i < frequencyCap; i++)
temp[i] = frequencyList[i];
delete [] frequencyList;
frequencyCap += 10;
frequencyList = temp;
}
public:
/**
* This constructor constructs the word
* with the parameter value and initializes
* the frequencyList
*/
Word(char* input)
{
string = new char[strlen(input) + 1];
strcpy (string, input);
frequencyList = new int[10];
frequencySize = 0;
frequencyCap= 10;
wordFrequency = 0;
}
// Copy constructor for word
Word (const Word& rhs)
{
string = new char[strlen(rhs.string) + 1];
strcpy(string, rhs.string);
frequencyList = new int[rhs.frequencyCap];
for (int i = 0; i < rhs.frequencyCap; i++)
frequencyList[i] = rhs.frequencyList[i];
frequencyCap = rhs.frequencyCap;
frequencySize = rhs.frequencySize;
wordFrequency = rhs.wordFrequency;
}
//Destructor
~Word()
{
delete[]string;
delete[]frequencyList;
}
//Overloaded assignment operator
Word& operator=(const Word& rhs)
{
if(this != &rhs)
{
delete [] string;
delete [] frequencyList;
string = new char[strlen(rhs.string) + 1];
strcpy(string, rhs.string);
frequencyList = new int[rhs.frequencyCap];
for (int i = 0; i < rhs.frequencyCap; i++)
frequencyList[i] = rhs.frequencyList[i];
frequencyCap = rhs.frequencyCap;
frequencySize = rhs.frequencySize;
wordFrequency = rhs.wordFrequency;
}
return *this;
}
//Seeing if given word matches another word.
bool equals(char* temp)
{
if(strcmp(string, temp) == 0)
return true;
return false;
}
//Adds the frequency of the given word to the list
//and if it already exits, frequency++
void addFrequencyToList(char* temp)
{
if (frequencySize == frequencyCap)
resizeFrequency();
//Only adds a new frequency if the word doesn't
//already appear in the list.
if (equals(temp))
{
frequencyList[frequencySize] = wordFrequency;
frequencySize++;
wordFrequency++;
}
}
//Gets the total size of the frequency list.
int getFrequency()
{
return frequencySize;
}
//Gets the frequency of a word
/*int getFrequencyOfWord(char* temp)
{
for(int i=0; i<=frequencySize; i++)
{
if (*temp == string[i])
return frequencyList[i];
}
}*/
void getFrequencyList(int* tempFrequencyList)
{
sort(frequencyList, frequencyList + frequencySize);
for (int i = 0; i < frequencySize; i++)
tempFrequencyList[i] = frequencyList[i];
}
//Returns a copy of the word
char* getWord()
{
char* temp = new char[strlen(string) + 1];
strcpy(temp, string);
return temp;
}
//Helper function that converts to lower case.
static void toLower(char* str)
{
int len = strlen(str);
for (int i = 0; i < len; i++)
str[i] = tolower(str[i]);
}
bool operator < (const Word& rhs)
{
return (strcmp(this->string, rhs.string) < 0);
}
};
#endif