All pastes #71045 Raw Edit

Anonymous

public cpp v1 · immutable
#71045 ·published 2006-06-25 04:45 UTC
rendered paste body
/*  *  *	Project 2  * */#include <iostream>#include <fstream>#include <iomanip>#include <string>using namespace std;void get_params( int argc, char *argv[], string FilesToLoad[], string FilesToExclude[], string &OutputFile, int &FilesToLoadCount, int &FilesToExcludeCount);void LoadFileToString(string FileName, string &Words);void FindOnlyChars(string &data);void SplitIntoArray(string data, string Words[]);void Split(string data, string Words[], int &WordArrayPosition);void CountWords(string Words[], int WordCount[], int ArraySize);int main(int argc, char *argv[]){	string Words[1000];	int WordCount[1000];	int NumWords = 0;	for(int i = 0; i < 1000; i++)		WordCount[i] = 0;		string Files[1000];	string FilesToLoad[1000];  // Array of files to load	int FilesToLoadCount = 0;	string FilesToExclude[1000];	int FilesToExcludeCount = 0;	string OutputFile;	int DisplayOutputCount = 0;	get_params(argc, argv, &FilesToLoad[0], &FilesToExclude[0], OutputFile, FilesToLoadCount, FilesToExcludeCount);	for( int i = 0; i < FilesToLoadCount; i++ ) 	{		LoadFileToString(FilesToLoad[i], Files[i]);		FindOnlyChars(Files[i]);		Split(Files[i], &Words[0], NumWords);	}		CountWords(&Words[0], &WordCount[0], NumWords);	for(int i = 0; i < NumWords; i++)	{		cout << Words[i] << " " << WordCount[i] << endl;	}	return 0;}void get_params( int argc, char *argv[], string FilesToLoad[], string FilesToExclude[], string &OutputFile, int &FilesToLoadCount, int &FilesToExcludeCount){	if(argc == 1)	{		cout << "Need More Arguments!" << endl;		exit(2);	}	bool NoNext = false;	for(int i = 1; i < argc; i++)	{			string my_arg = argv[i];		string my_arg_next;		if( (i+1) < argc )		{			my_arg_next = argv[i + 1];			}		else		{			my_arg_next = "";			NoNext = true;		}				if( my_arg == "-x" )		{			if(NoNext)			{				cout << "Error No Exclude file specified" << endl;				exit(1);			}			else			{				FilesToExclude[FilesToExcludeCount] = my_arg_next;				FilesToExcludeCount++;				i++;			}		}		else if ( my_arg == "-o" )		{			if(NoNext)			{				cout << "Error No Output File, Using Default!";				OutputFile = "output.txt";				i++;			}			else			{				OutputFile = my_arg_next;				i++;			}		}		else		{			FilesToLoad[FilesToLoadCount] = my_arg;			FilesToLoadCount++;				}	}}void LoadFileToString(string FileName, string &Words){	ifstream ifs(FileName.c_str());	string text, s;	if(!ifs)	{		cout << "Error. Can't not open file " << FileName << " for reading " << endl;		return;		}	getline(ifs, s);	while (ifs)	{			text += s + "\n";			getline(ifs,s);	}	Words = text;}void FindOnlyChars(string &data){	for ( int i = 0; i < data.size(); i++ )	{		if( data[i] < 65 || data[i] > 90)			if( data[i] < 97 || data[i] > 122)				data[i] = ' ';	}}void Split(string data, string Words[], int &WordArrayPosition){	bool WordFound = true;	int Count = 0;	for( int i = 0; i < data.size(); i++)	{		if( data[i] == ' ' )		{			if(!WordFound)				WordArrayPosition++;			WordFound = true;		}		else		{			if(WordFound)			{				Count = 0;				Words[WordArrayPosition] = data[i];				WordFound = false;				Count++;			}			else			{				Words[WordArrayPosition] += data[i];				Count++;			}		}	}}void CountWords(string Words[], int WordCount[], int ArraySize){	string FindMe;	for(int i = 0; i < ArraySize; i++)	{		FindMe = Words[i];		WordCount[i]++;				for( int y = i + 1; y < ArraySize; y++)		{			if( FindMe == Words[y] )			{				WordCount[i]++;				Words[y] = "";			}		}	}}