我想以不同的组合排列和打印用户输入的单词?谁能告诉我如何实现这一目标?

I want to arrange and print words entered by the user in different combinations? Can anybody tell me how can I achieve this?

示例:

输入:

用户要输入的字数:3

字号1: 猫

字号2: 老鼠

字号3: 狗

输出:

猫鼠狗

猫狗鼠

鼠猫狗

鼠狗猫

狗猫鼠

狗鼠猫

像这样。

这是另一个程序的代码,我在其中以不同的组合排列了字母。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char character_swap(char*, char*);
void permutation(char*, int, int);

int main()
{
 char str[99];
 
 printf("Enter a string to find its permutations.\n");
 scanf("%s",str);
 
 system("clear");

 int n = strlen(str);
 permutation(str, 0, n-1);

 return 0;
}

char character_swap(char *x, char *y)
{
 char temp;

 temp = *x;
 *x = *y;
 *y = temp;
}

void permutation(char *a, int l, int r)
{
 if (l == r)
 printf("%s\n", a);

 else
 for (int i = l; i <= r; i++)
 {
 character_swap((a+l), (a+i));
 permutation(a, l+1, r);
 character_swap((a+l), (a+i));
 }
}

谁能告诉我,我怎样才能用文字做同样的事情?

您很好地将 permutation 实现与 main 接口分开,因此可以相当轻松地完成此操作。首先,有一些不一致,

  • character_swap中,有人声明了return类型char,但从来没有 return 任何东西;必须是 void。它只在这个文件中使用,所以它应该是static;这将避免命名空间冲突,让编译器执行更积极的优化。只要在上面声明,它也可以防止原型警告。
  • int 并不是处理索引的正确类型; size_t比较合适。
  • 解释一下 permutation 总是以 0 索引作为第二个参数来调用有点尴尬。最好把它丢掉,以后再填,我已经接受了 character_permutation 中的大小,它调用 character_permutation_r.

我使用 typedef 将类型从 char 概括为 type;我刚刚用 type 替换了 char。将 type 替换为等效的 char * 会更简单明了,但在下一次迭代中,我们将用完全不透明的类型替换它; typedef 是有保证的,因为它将所有类型信息放在一起。

#include <stdio.h>
#include <string.h>

/* This is part of the interface. */

void character_array_output(const char *a, const size_t size) {
 (void)size; /* Don't use the size -- it's null-terminated. */
 printf("%s\n", a);
}

/* This is the implementation. */

typedef char type;

typedef void (*character_array_action)(const type *, size_t);

static character_array_action character_print = &character_array_output;

static void character_swap(type *x, type *y)
{
 type temp;

 temp = *x;
 *x = *y;
 *y = temp;
}

static void character_permutation_r(type *a, const size_t l, const size_t r)
{
 if (l == r)
  character_print(a, r + 1);

 else
 for (size_t i = l; i <= r; i++)
 {
  character_swap((a+l), (a+i));
  character_permutation_r(a, l+1, r);
  character_swap((a+l), (a+i));
 }
}

static void character_permutation(type *a, const size_t size) {
 if(size == 0) return;
 else character_permutation_r(a, 0, size - 1);
}

/* This is part of the interface. */

int main(void)
{
 char str[99];

 printf("Enter a string to find its permutations.\n");
 scanf("%98s", str);

 character_permutation(str, strlen(str));

 return 0;
}

对于你的问题,typechar,而你想要它是char *,你只需将它替换为typedef char *type。但是,可以泛化到任何可分配类型。

#include <stdio.h>
#include <string.h>

/* This is the implementation.
 `name` satisfies `C` naming conventions when mangled
 `nametype` is an assignable type
 `array_output` is a function satisfying `name##_array_action` */

#define DEFINE_PERMUTATION(name, nametype, array_output) \
typedef nametype name##type; \
\
typedef void (*name##_array_action)(const name##type *, size_t); \
\
static name##_array_action name##_array_print = &array_output; \
\
static void name##_swap(name##type *x, name##type *y) \
{ \
 name##type temp; \
\
 temp = *x; \
 *x = *y; \
 *y = temp; \
} \
\
static void name##_permutation_r(name##type *a, const size_t l, const size_t r) \
{ \
 if (l == r) \
  name##_array_print(a, r + 1); \
\
 else\
 for (size_t i = l; i <= r; i++)\
 { \
 name##_swap((a+l), (a+i)); \
 name##_permutation_r(a, l+1, r); \
 name##_swap((a+l), (a+i)); \
 } \
} \
\
static void name##_permutation(name##type *a, size_t size) { \
    if(size == 0) return; \
    else name##_permutation_r(a, 0, size - 1); \
}

/* This is the interface. */

void character_array_output(const char *a, const size_t a_size) {
    (void)a_size;
    printf("%s\n", a);
}

void string_array_output(char *const a[], const size_t a_size) {
    for(size_t i = 0; i < a_size; i++)
        printf("%s%s", i ? ", " : "", a[i]);
    printf("\n");
}

void integer_array_output(const int *a, const size_t a_size) {
    printf("{");
    for(size_t i = 0; i < a_size; i++)
        printf("%s%d", i ? ", " : "", a[i]);
    printf("}\n");
}

typedef int (*operation)(int);
static int value;
void operation_array_output(const operation *a, const size_t a_size) {
    printf("{");
    for(size_t i = 0; i < a_size; i++)
        printf("%s%d", i ? ", " : "", value = a[i](value));
    printf("}\n");
}

DEFINE_PERMUTATION(character, char, character_array_output)
DEFINE_PERMUTATION(string, char *, string_array_output)
DEFINE_PERMUTATION(integer, int, integer_array_output)
DEFINE_PERMUTATION(operation, operation, operation_array_output)

#include <math.h>
static int square_n(const int n) { return n * n; }
static int sqrt_n(const int n) { return sqrtl(n); }
static int double_n(const int n) { return 2 * n; }

int main(void)
{
 char str[] = "abc";
 character_permutation(str, strlen(str));

 char *strings[] = { "cat", "rat", "dog" };
 string_permutation(strings, sizeof strings / sizeof *strings);

 int numbers[] = { 42, 99, 1, 0 };
 integer_permutation(numbers, sizeof numbers / sizeof *numbers);

 operation ops[] = { &square_n, &sqrt_n, &double_n };
 value = 1;
 operation_permutation(ops, sizeof ops / sizeof *ops);
 value = 5;
 operation_permutation(ops, sizeof ops / sizeof *ops);

 return 0;
}