随机名称生成器 [英] Random Name Generator

查看:138
本文介绍了随机名称生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找帮助以ASCII创建随机名称生成器.我是C ++的新手,实际上不是100%地确定如何进行攻击. 名字和姓氏应随机产生,并且必须以大写字母开头.此外,名字首字母应为辅音.名字的其余部分应在元音和辅音之间交替使用,其中每个偶数编号的字母都是元音,每个奇数带编号的字母是辅音."任何帮助都将是伟大的!.

I am looking for some help to create a random name generator with ASCII. I am new to C++ and really and not 100% sure how to attack this. "First and last names should be randomly generated and must begin with a capital letter. Additionally, the first letter should be a consonant. The remainder of the name should alternate between vowels and consonants, where every even numbered letter is a vowel and every odd numbered letter is a consonant." Any help would be GREAT!!.

推荐答案

我采用的方式是这样的:

1.定义返回随机元音和随机辅音的函数-这是一个随机元音函数:

The way I''d approach it is like this:

1. Define functions that return random voewls and random consonants - here''s a random vowel function:

char RandomVowel()<br />{<br />   const char vowels[] = {''a'', ''e'', ''i'', ''o'', ''u''};<br />   // Use rand() to get a number between 0 and RAND_MAX. Dividing<br />   // by RAND_MAX+1 gives a number n such that 0 <= n < 1, so<br />   // multiplying by the number of characters in vowels gives a random<br />   // index into vowels<br />   const int index = (int)((double)rand()/(RAND_MAX+1) * sizeof(vowels));<br />   return vowels[index];<br />}



2.使用对随机元音和辅音函数的交替调用来构建字符串-请注意,我记得大写的第一个字符!



2. Build up a string using alternating calls to the random vowel and consonant functions - note that I''ve remembered about the upper case first character!

std::string name;<br />   name += (char)toupper(RandomConsonant());<br />   for(int i=1;i<desired-name-length;++i)<br />   {<br />      if (i%2==1)<br />         name += RandomVowel();<br />      else<br />         name += RandomConsonant();<br />   }



显然,所需的名称长度应该也是随机选择的!



Obviously the desired name length should be chosen at random as well!


这是我的尝试:

Here''s my try:

#include <string>
using namespace std;

int main(int argc, char* argv[])
{
	string name;
	static const int namelen = 15;
	
	static const char* letters[2] = { "bcdfghjklmnpqrstvwxyz", "aeiouy" };
	static const letterlen[2] = { strlen(letters[0]), strlen(letters[1]) };
	
	for (int i=0; i<namelen; i++)
		name += letters[i%2][rand()%letterlen[i%2]];
	name[0] = toupper(name[0]);

	return 0;
}


很抱歉,每个人都在为您做功课.如果没有先阅读并理解代码,就不会通过复制和粘贴给出的代码来学习任何东西.您还应该记住,您的老师可以使用google,如果您有任何感触,他们将按照您在此处粘贴的内容搜索google的原始问题,并且会逐字逐句地使提交此处代码的任何人失败.您应该做的是尝试从已经提供的代码中学习,然后尝试编写自己的代码,并可能问*特定的*问题,如果您在尝试*做*作业时遇到困难,而不是要求我们为您做.
I am sorry that everyone is doing your homework for you. You won''t learn anything by copying and pasting the code you were given, without first reading through it and understanding it. You should also remember that your teacher has access to google, if they have any sense, they will google their original question, as pasted here by you, and will fail anyone who turns in the code presented here, verbatim. What you should do, is try to learn from the code you''ve been given, then try to write your own code, and perhaps ask *specific* questions if you get stuck while trying to *do* your homework, as opposed to asking us to do it for you.


这篇关于随机名称生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆