You need to write a permute class that will take first and second strings to rearrange letters in first, followed by second. For example, if the first is “CAT” string and second is “MAN” string, then the program would print the strings TACMAN, ATCMAN, CTAMAN, TCAMAN, ACTMAN, and CATMAN. The first and second strings can be any length of string or a null.
The permute class uses a Node class as link list node to link all letters arrangement. The permute class has Node pointers, firstNode and lastNode, to point to the beginning and ending Nodes of the link list as private data members. There are three other private data members (total, firstString and secondString) to store the total possible number of arrangements and strings pass into the class.
Write a driver to test the permute class to pass in any two strings of any sizes.
Other than mention in the following, you can add more classes, functions, and private data members to this program.
Node class
The Node class needs to have two private data members and a constructor. The two private data members are data and p pointer. The data’s data type is string and p pointer is Node. The Node class constructor has two parameters, one is string and the other is Node pointer.
Permute class
The Permute class has five private data members (*firstNode, *lastNode, total, firstString and secondString). The firstNode and lastNode pointers are point to Node. The total has integer data type. The firstString and secondString have string data type.
There should have at least three public member functions, Permute, permutation and print. The Permute function is the constructor which takes strings to initialize the private data members. The permutation function does the recursive call to arrange the strings and setup the link list. The print function will print out the private data member information.
Driver file
The driver file should declare a Permute eight elements pointer array. Instantiate eight Permute object with the following eight set of data and assign the object to the pointer array. Use a repetition to call the object’s print function to print out the private data member information. If the total of the permute private data member is less than 100 then print out the permutated letters four in a row, otherwise print out 9 in a row.
first = "", second="",
first = "", second ="CATMAN",
first = "C", second ="ATMAN",
first = "CA", second ="TMAN",
first = "CAT", second ="MAN",
first = "CATM", second ="AN",
first = "CATMA", second ="N",
first 1 = "CATMAN", second ="";
Hello dear student.. i have tried to answer your question as it seems a bit incomplete..Please go through the code and ask in comments if you have any doubts.
Do hit LIKE if you find the answer even a little bit useful because that shows your love and support which motivates us.
Here starts the code :-
#include <cstdlib> // Provides size_t // included in
permute_append.h
#include <sstream> // provides the << operator for
string concatenation. // not using
#include <string> //provides string type //included in
permute_append.h
#include "permute_append.h"
using namespace std;
namespace CISP430_A5
{
/*CONSTRUCTORS, DECONSTRUCTOR*/
permute_append::permute_append() {
firstString =
"CAT"; // x is just a default value
secondString =
MAN"; // x is just a default value
total = 0;
}
permute_append::permute_append(const char* first, const char*
second) {
firstString =
first;
secondString =
second;
total = 1; // at
least one permutation
for (int
i=firstString.length(); i>0; --i) { // number of permutations is
equal to factorial of the number of characters in firstString
total *=
i;
}
/*Turn string
into linked list of chars*/
for (int i=0;
i<firstString.length(); ++i) {
permute_me.add(firstString[i]);
}
}
permute_append::permute_append(const permute_append &source)
{
firstString =
source.firstString;
secondString =
source.secondString;
total =
source.total;
permute_me =
source.permute_me;
result_list =
source.result_list;
}
permute_append::~permute_append()
{
total = 0;
firstString =
"";
secondString =
"";
/*so I guess we
don't need to delete the linked_list object manually*/
// delete
permute_me;
// delete
result_list;
}
linked_list<string>
permute_append::permute(linked_list<char> charList) { //
permute the characters in the array (n items, n at a time)
/*Returns a linked_list of strings, each string a
permutation of the chars in charList.*/
static
linked_list<string> perms; static linked_list<char>
usedChars;
linked_list<char> character;
for (int i = 0;
i < charList.size(); ++i) {
character
= list_splice(charList, i, 1); //??? How do we pass charList to
list_splice so list_splice modifies charList directly? Answer: just
like I did.
usedChars.add( character.get(0) );
if
(charList.size() == 0) perms.add( charList_join("", usedChars) );
//??? Is charList_join() working? I believe so.
permute(charList);
list_splice( charList, i, 0, character.get(0) );
list_pop(
usedChars );
}
return
perms;
}
string permute_append::do_it() {
// appends secondString to each permutation of
firstString and stores each result in result_list
linked_list<string> perms( permute(permute_me) ); // ??? How
do you point to the returned linked_list properly?
// string result
= "";
for (size_t i=0;
i<perms.size(); ++i) {
result_list.add(perms.get(i) + secondString);
}
}
string permute_append::do_it(const char* first, const
char* second) {
// set firstString and secondString then appends
secondString to each permutation of firstString and stores each
result in result_list
firstString =
first; secondString = second;
do_it();
}
string permute_append::get(size_t position) { //
get a result
/*Get the item at position position from result_list
*/
return
result_list.get(position);
}
}
Thankyou!
Get Answers For Free
Most questions answered within 1 hours.