Question

You need to write a permute class that will take first and second strings to rearrange...

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 ="";

Homework Answers

Answer #1

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!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined...
Write the implementation of a non-member function Node* deleteSecond(Node* head_ptr), where Node is a class defined on page 257. The function takes as input a pointer to the head of a linked list consisting of numbers. The function should remove the second item in the list. If the list had only one item, the function should delete that item. If the list was empty, then let the list remain empty. In all cases return the new head of the list...
Write a Python function that takes two parameters: the first a list strings and the second...
Write a Python function that takes two parameters: the first a list strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False.
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
We are using a raspberry pi in my computer systems class. We need to Write a...
We are using a raspberry pi in my computer systems class. We need to Write a statically allocated linked list in ARM assembly. Steps You Should Follow Step 1 You will statically allocate some space for your link list elements. You need to make 5 elements, each elements should have two components, the next pointer first then the value stored. First inialize next pointer to NULL. The value stored at the address will be a string pointer. It doesn't matter...
We are using a raspberry pi in my computer systems class. We need to Write a...
We are using a raspberry pi in my computer systems class. We need to Write a statically allocated linked list in ARM assembly. Steps You Should Follow Step 1 You will statically allocate some space for your link list elements. You need to make 5 elements, each elements should have two components, the next pointer first then the value stored. First inialize next pointer to NULL. The value stored at the address will be a string pointer. It doesn't matter...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor...
10. Number Array Class Design a class that has an array of floating-point numbers. The constructor should accept an integer argument and dynamically allocate the array to hold that many numbers. The private data members of the class should include the integer argument in a variable to hold the size of the array and a pointer to float type to hold the address of the first element in the array. The destructor should free the memory held by the array....
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const...
could you implement this function please, im having issues with it. void makeList (const ListNode::value_type [],const size_t& count) class ListNode { public: typedef int value_type; ListNode (value_type d = value_type(), ListNode* n = NULL) { datum = d; next = n; }    //Assessor value_type getDatum () const { return datum; } ListNode const* getNext () const { return next; }    //Mutator void setDatum (const value_type& d) {datum = d; } ListNode* getNext () { return next; } void...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT