对候选字排列组合后输出的程序(用…

本文介绍了一个简单的命令行程序,用于生成随机的中文姓名。该程序接受用户输入的姓氏及名字候选字,支持随机顺序输出及去除叠字等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

babyname.bmp
如果懒得自己编译的就下载此“图片”,重命名为exe后缀即可直接使用
===============================================================================

#include <iostream>
#include <cstdio>
#include <ctime>

using namespace std;

void usage()
{
      cout << "usage : babyname.exe [姓] [名字候选字] [0 or other : 随机顺序 or 正序] [0 or other : 去除叠字名 or 不去除]" << endl;
      cout << "example : babyname.exe 张 三四五 0 0" << endl;
}

void output_one_name(char* family_name, char* names, int first_pos, int second_pos)
{
      cout << family_name << names[first_pos*2] << names[first_pos*2+1] << names[second_pos*2] << names[second_pos*2+1] << endl;
}

int rand_int(int min, int max)
{
      int i = ((rand() % 2) << 30);
      i |= (rand() << 15);
      i |= rand();
      return (i % (max - min + 1)) + min;
}

int main(int argc, char* argv[])
{
      if (argc != 5)
      {
            usage();
            return 1;
      }

      int name_cand_cnt = strlen(argv[2]);
      if (name_cand_cnt % 2 != 0)
      {
            cout << "名字候选字必须为中文" << endl;
            return 1;
      }
      name_cand_cnt /= 2;

      bool random_flag = (strcmp(argv[3], "0") == 0);
      bool forbid_double_flag = (strcmp(argv[4], "0") == 0);
      if (random_flag)
      {
            if (name_cand_cnt > 10240)
            {
                  cout << "名字候选字太多,请不要使用随机顺序" << endl;
                  return 1;
            }
            int cand_cnt = name_cand_cnt * name_cand_cnt;
            int* order = new int[cand_cnt];
            for (int i = 0; i < cand_cnt; i++)
            {
                  order[i] = i;
            }
            srand((unsigned)time(NULL));
            for (int i = 1; i < cand_cnt; i++)
            {
                  int swap_pos = rand_int(0, i);
                  if (swap_pos != i)
                  {
                        int tmp = order[i];
                        order[i] = order[swap_pos];
                        order[swap_pos] = tmp;
                  }
            }
            for (int i = 0; i < cand_cnt; i++)
            {
                  int first_pos = order[i] / name_cand_cnt;
                  int second_pos = order[i] % name_cand_cnt;
                  if (!forbid_double_flag ||  first_pos != second_pos)
                  {
                        output_one_name(argv[1], argv[2], first_pos, second_pos);
                  }
                     

            delete[] order;
      }
      else
      {
            for (int i = 0; i < name_cand_cnt; i++)
            {
                  for (int j = 0; j < name_cand_cnt; j++)
                  {
                        if (!forbid_double_flag ||  i != j)
                        {
                              output_one_name(argv[1], argv[2], i, j);
                        }
                  }
                     
      }

      return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值