Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Permute
#4
I don't think there is something similar to permute function. Better try this:

Code:
Copy      Help
/* swap(s,t): swap characters.
* Swaps characters *s and *t. */
void swap(char *s, char *t)
{
  int c;

  /* Rotate through temp variable c. */
  c = (int) *s; *s = *t; *t = (char) c;
}

/* perm(s,n,l): print permutations.
* Prints string s for all permutations of characters
* in positions n through l-1.
*/
void perm(char *s, int n, int l)
{
    int i;

    if (n == l)
    {
        /* No characters to permute . . print string. */
        printf("%s", s);
    }
    else
    {
        /* Work through all characters of string. */
        for (i = n; i < l; i++)
        {
            /* Swap this character with the first one. */
            swap(s + i, s + n);
            /* Permute remainder of string. */
            perm(s, n+1, l);
            /* Swap back again. */
            swap(s + i, s + n);
        }
    }
}

Hints for converting C/C++ code to QM:
Here are two functions - perm and swap. That is, you need to create two QM items of Function type. But maybe better would be to integrate swap into perm.
char* in QM is lpstr.
(int), (char) and similar are type casts. In QM they can be omitted, or use + if gives error.
{ } marks code blocks. In QM, code blocks are marked by tab-indenting.
// and /* ... */ are comments.
, and ; in most cases can be leaved, although in QM they are not always necessary.
printf in QM is out.
for (i = n; i < l; i++) in QM is for i n l
s + i an similar arguments in QM must not contain spaces or must be enclosed: s+i or (s + i).
void is used instead of nothing, that is, the function does not return a value.
perm is function name, then follow (arguments) and {function body}.
Function argument definitions also must not contain spaces. Convert void perm(char *s, int n, int l) to function lpstr's int'n int'l or to shorter form function $s n l.
Convert *s to s[0], which is the same in C, but in QM lpstr is not interpreted as pointer.


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)