-
October 30th, 2013, 01:39 PM
#1
assignment is recursive call and placing strings in link list notes
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 Note class as link list note to link all letters arrangement. The permute class has Note pointers, firstNote and lastNote, to point to the beginning and ending Notes 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.
Note class
The Note 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 Note. The Note class constructor has two parameters, one is string and the other is Note pointer.
Permute class
The Permute class has five private data members (*firstNote, *lastNote, total, firstString and secondString). The firstNote and lastNote pointers are point to Note. 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 ="";
You are welcome to use more advance skills than the techniques
Last edited by Cruzer; October 30th, 2013 at 01:58 PM.
-
October 30th, 2013, 02:22 PM
#2
Re: assignment is recursive call and placing strings in link list notes
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);
}
}
-
October 30th, 2013, 02:31 PM
#3
Re: assignment is recursive call and placing strings in link list notes
So what's your c++ question?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 30th, 2013, 04:06 PM
#4
Re: assignment is recursive call and placing strings in link list notes
Originally Posted by Cruzer
You need to write a permute class
What will happen to me if I don't write this class?
You are welcome to use more advance skills than the techniques
How about just using std::next_permutation?
Regards,
Paul McKenzie
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|