I want to help me combine the following three codes to gether so that the can ru
ID: 3630140 • Letter: I
Question
I want to help me combine the following three codes to gether so that the can run as one file
/************************************************************
*************************************************************
Programmer: Ivan Ssensalire
Class: CMIS315 C++ Programming
Semester: Fall 2011
*************************************
This program allows the user to enter a
file name
and its location on the machine, then
recogonize it
and then asks the user to eneter the
number of words
to be display from the file. the
program will then
display alist of words, then arrange
the alphabetical
order of each word, aside by the actual
word read.
*************************************************************
*************************************************************/
#include <iostream>
#include <fstream>
#include <string.h>
#include<iomanip>
using namespace std;
char** read(const char* fileName, int& count);
class KeyedWord {
public:
KeyedWord(char* = NULL);
char* getWord();
char* getKey();
void setKey(char*);
private:
char * _word;
char* _key;
};
void sort(KeyedWord** keyedWords, int numberOfObjects);
void printAnagrams(KeyedWord** keyedWords, int numberOfObjects);
int main() {
int numberOfWords = 0;
char *filename = new char[30];
cout << "Please enter a file name to be read by the program:: ";
cin >> filename;
cout << "Please enter number a number of words to be displayed:: ";
cin >> numberOfWords;
char** words = read(filename, numberOfWords);
KeyedWord **keyedWords = new KeyedWord*[numberOfWords];
for (int i = 0; i< numberOfWords; i++)
keyedWords[i] = new KeyedWord(words[i]);
for (int i = 0; i< numberOfWords; i++)
cout << keyedWords[i]->getWord() << endl;
sort(keyedWords, numberOfWords);
printAnagrams(keyedWords, numberOfWords);
getchar()
getchar();
}
KeyedWord::KeyedWord(char*s) {
_word =s
}
char *KeyedWord::getWord() {
return _word;
}
char *KeyedWord::getKey() {
return _key;
}
void KeyedWord::setKey(char*s) {
_key = s;
}
char** read(const char* fileName, int& count) {
fstreamFile;
File.open(fileName,ios::in);
char **s; s = new char*[count + 1];
for (int i = 0; i< count + 1; i++)
*(s+ i) = new char[30];
for (int i = 0; i< count; i++) {
File >> s[i]
}
File.close();
return s;
}
void sort(KeyedWord** keyedWords, int
numberOfObjects) {
char *s, c = 'd';
for (int i = 0; i
< numberOfObjects; i++) {
s
= new char[strlen(keyedWords[i]->getWord()
+ 1)];
strcpy(s,
(keyedWords[i]->getWord()));
for (int k = 0; k
< strlen(s); k++) {
for (int j = 0; j
< strlen(s) - 1; j++) {
if (s[j] > s[j + 1]) {
c
= s[j];
s[j]
= s[j + 1];
s[j
+ 1] = c;
}
}
}
(keyedWords[i])->setKey(s);
}
}
void printAnagrams(KeyedWord** keyedWords, int numberOfObjects) {
cout.setf(ios::left);
cout
<< "--------**************************--------------"
<< endl;
cout
<< "------------------------------------------------"
<< endl;
cout
<< setw(20) << "WORDS"
<< setw(20) << "ALPHABETICAL"
<< endl;
cout
<< "------------------------------------------------"
<< endl;
cout
<< "------------------------------------------------"
<< endl;
for (int i = 0; i
< numberOfObjects; i++) {
cout
<< setw(20) << keyedWords[i]->getWord() << setw(20)
<<
keyedWords[i]->getKey() << endl;
}
cout
<< "------*********************************---------"
<< endl;
cout
<< "------*********************************---------"
<< endl;
cout
<< "------------------------------------------------"
<< endl;
system("PAUSE");
}
/***********************************************************************
************************TESTING
PLAN************************************
*****************************TEST
ONE***********************************
The first thing to do is to locate the
word.txt file on the machine and
make sure that it has the data we need
for these program to run.
************************************************************************
1. STEP : START THE PROGRAM.
2. STEP : ENTER THE FILE LOCATION AND
ITS NAME i.e G:words.txt
note "G:" is file location
and "words.txt" is the file name
3. STEP : ENTER THE NUMBER OF WORD TO
BE DISPLAYED: 105
4. STEP : THE PROGRAM WILL FIRST
DISPLAY 105 WORDS AND THEN
DISPLAY EACH WORD ASIDE WITH ITS
ALPHABETICAL LETTERS.
THE WORDS WILL BE FROM A TO B UPTO THE
LAST VALUE OF 105.
*************************************************************************
*******************TESTING
TWO*******************************************
1. STEP : START THE PROGRAM.
2. STEP : ENTER THE FILE AND ITS
LOCATION
3. STEP : ENTER 1000.
4. STEP : THE PROGRAM WILL DISPLAY
WORDS IN THE FILE AND
ARRANGE THEIR ALPHABETICAL.
*************************************************************************
*************************************************************************
*******************TESTING
THREE*****************************************
1. STEP : START THE PROGRAM.
2. STEP : ENTER A NON EXIST FILE
3. STEP : ENTER VALUE TO BE READ.
4. STEP : THE PROGRAM WILL RUN BUT
WON'T DISPLAY ANYTHING.
******************END THE TESTING********************
*************************************************************************/
/*****************************************************
******************************************************
Programmer: Ivan SSensalire
Class: CMIS315 C++ Programming
Semester: FALL 2011
This program read data from a given
file and then
displays the data if the file is
available.
******************************************************
*****************************************************/
#include <iostream>
#include<string>
#include <fstream>
using namespace std;
char** read(const char* fileName, int&
count)
{
std::ifstream countingStream(fileName);
// first count them
count = 0;
while (true)
{
char line[100];
countingStream.getline(line, 100);
for(int
i=0;i<strlen(line);i++)
cout<<line[i]<<"
";
if (strlen(line)== 0)
{
break;
}
count += 1;
}
countingStream.close();
std::ifstream readingStream(fileName);
char** words = new char* [count];
for (int index = 0;index
< count; ++index) {
char line[100];
readingStream.getline(line, 100);
words[index] = _strdup(line);
}
readingStream.close();
return words;
}
int main()
{
string filename;
int count;
string line;
char *ch;
cout << "Please
enter a file name to display the file information:"<< endl;
cin>>filename;
ch=new char[filename.length()];
for(int i=0;
i<filename.length();i++)
ch[i]=filename[i];
read(ch,count);
/*********************************************
the function below will get the
information
from the file identified and display it
********************************************
*/
ifstream myfile;
myfile.open(filename.c_str());
while(!myfile.eof())
{
getline(myfile,line);
cout<<line<<endl;
}
system("PAUSE");
}
/***************TESTING*****************
****************************************
*****Testing plan for this
program******
1. STEP: RUN THE PROGRAM.
********
2. STEP: IDENTIFY FILE LOCATION
ON THE COMPUTER. i.e C:
********
3. STEP:IDENTIFY THE FILE TO READ
"words.txt"
********
4. STEP: AT THE COMMAND LINE TYPE IN
C:words.txt
********
5. STEP: PRESS ENTER TO DISPLAY DATA
FROM THE FILE.
********
6. STEP: PRESS ANY KEY TO EXIT THE
PROGRAM.
***************************************
***************************************
************TESTING TWO****************
TESTING A NON EXISTING FILE.
1. STEP : START THE PROGRAM
2. STEP : ENTER ANY FILE NAME
SPECIFYING POSSIBLE LOCATION
3. STEP : PRESS ENTER TO EXECUTE
NOTE: THE PROGRAM WILL DISPLAY
NOTHING.
***************************************
***************************************/
#include <iostream>
#include <string.h>
using namespace std;
class KeyedWord
{
public:
KeyedWord(char*
word);
char* getWord();
char* getKey();
private:
char* _word;
char* _key;
};
//constructor definition
KeyedWord::KeyedWord(char*
word)
{
int length = strlen(word);
//allocating memory for class variables
//with the size of given word
_word = new char[length];
_key = new char[length];
//copies given word to class variable
_word
strcpy(_word,word);
//copies given word to class variable
_key
strcpy(_key,word);
//arranging characters in the key
//are in alphabetical order
for(int i = 0; i < length
-1 ;i++)
{
for(int j = i+1; j <
length; j++)
if( _key[i] > _key[j] )
{
//swapping
char tmp = _key[i];
_key[i] = _key[j];
_key[j] = tmp;
}
}
}
//returns _word
char* KeyedWord::getWord()
{
return _word;
}
//returns _key
char* KeyedWord::getKey()
{
return _key;
}
//main function
int main()
{
//array of KeyedWord objects
KeyedWord keyedWords[] = {"rude","dure","word","letter","ivna","iavn","from","form"};
cout<<"Anagrams
are: "<<endl<<endl;
//finding anagrams in the array
//and displaying
for(int i=0;i<5;i++)
for(int j=i+1;j<6;j++)
{
char *word1 = keyedWords[i].getWord();
char *key1 = keyedWords[i].getKey();
char *word2 = keyedWords[j].getWord();
char *key2 = keyedWords[j].getKey();
//comparing keys
//if they area equal then the
corresponding
//words are anagrams
if( strcmp(key1, key2) == 0)
cout<<word1<<"
"<<word2<<endl;
}
cout<<endl;
system("PAUSE");
return 0;
}
/**********************************************
TEST PLAN
1. STEP : ADD THE WORDS IN ARRY TO
"JOBS","SBOJ" FIRST IN THE BRAKETS
THEY WILL BE DISPLAYED AND
"from","form" IGNORED.
IF "rude","dure" IS
REPLACED BY
"LOVE", "VELO", THIS WILL
BE
DISPLAYED.
*/
Explanation / Answer
just place all the functions into the file that has a main function. You can only have one main function per class. If you are going to have different main classes per file you can't combine them. Don't forget to add all the includes onto the top. Sorry I would do it but your 3 programs are displayed really messy when I copy them and not formated correctly
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.