使用ASCII字符65-91生成器的C ++ randdom字母 [英] C++ randdom Alphabet using ascii characters 65-91 generator

查看:103
本文介绍了使用ASCII字符65-91生成器的C ++ randdom字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用rand函数生成,构造一个大小写(至少20个)不超过30个大写字母的数组.

这就是我的程序..生成介于65到91之间的元素并将其强制转换,可以从A到Z获得任何字母...生成之后,仅使用一个循环,尝试将所有字母变小在数组的左侧要比字母"M"大,而在数组的右侧要比字母"M"大..

输出应为:

鉴于以下数组:
A D F Y H J I O L B G E M Y R W A Q(随机)

数组的迭代交换
??????

鉴于以下数组:
A D F Y H J I O L B G E M Y R W A Q(随机)

数组的递归交换
?????????

使用下面的代码帮助我

Using rand function to generate, Construct an array of upper case letters of size (at least 20) not more than 30.

This is what my program is.. to generate an element between the range from 65 to 91 and cast it, can get any letter from A to Z... After the generation, using only one loop, try to move all the letters smaller than letter ''M'' to the left hand side of the array and all the letters greater than ''M'' to the right side of the array..

the output should be:

Given the follwing array:
A D F Y H J I O L B G E M Y R W A Q (Random)

iterative swap of array
??????

Given the follwing array:
A D F Y H J I O L B G E M Y R W A Q (Random)

Recursive swap of array
????????

help me with my code below

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

void characters(char[]);
void printChar(char[]);
void swapChar(char[]);

int main ()
{
	char c[26];
	
	characters(c);
	cout << "Given the following array" << endl;
	printChar(c);
	swapChar(c);
	cout << "Iterative swap of array" << endl;
	printChar(c);

}

void characters(char c[])
{
	int seed;
	
	int sum = rand();
	srand(time(NULL));
	
	for(int i = 65; i <= 90; i++)
	{
		c[sum] = i;	   	   	   
		sum++;
	}

}

void printChar(char c[])
{
	for(int i = 0; i <= 25; i++)
	{
		
		cout << c[i] << " ";
	}	 
		cout << endl;
}

void swapChar(char c[])
{
	int temp;
	for(int i = 0; i <= 25; i++)
	{
		if(c[i] > c[12])
		{
			temp = c[i];
			c[i] = c[26-i-1];
			c[26-i-1] = temp;
		}
		
	}
}

推荐答案

我认为characters(char [])函数存在错误.

试试这个:
I think error is in characters(char []) function.

Try this:
void characters(char c[])
{
	bool used[25];

	memset(used, 0, 25 * sizeof(bool));
	srand(time(NULL));
	
	for(int i = 65; i <= 90; i++)
	{		
		int index;

		do {
			index = rand() % 25;
		} while (used[index]);

		used[index] = true;
		c[index] = i;
	}

}


没有swapChar(),它应该像这样

without swapChar() it should look like this

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

void characters(char[]);
void printChar(char[]);
void swapChar(char[]);

int main ()
{
	char c[26];
	
	characters(c);
	cout << "Given the following array" << endl;
	printChar(c);
	swapChar(c);
	cout << "Iterative swap of array" << endl;
	printChar(c);
	return 0;
}

void characters(char c[])
{
	bool used[26];

	memset(used, 0, 26 * sizeof(bool));
	srand(time(NULL));
	
	for(int i = 65; i <= 90; i++)
	{		
		int index;

		do {
			index = rand() % 26;
		} while (used[index]);

		used[index] = true;
		c[index] = i;
	}

}

void printChar(char c[])
{
	for(int i = 0; i <= 25; i++)
	{
		
		cout << c[i] << " ";
	}	 
	cout << endl;
}



剩下的唯一事情就是这个功能.在家工作? :)



The only thing left is this function. Homework? :)


这种方式,我认为:

Some kind of this, I think:

void swapChar(char c[])
{
	int temp;
	int l_index = 0;	

	for(int i = 0; i <= 25; i++)
	{
		if (c[i] < ''M'') {
			temp = c[i];
			c[i] = c[l_index];
			c[l_index++] = temp;
		}
	}
}


这篇关于使用ASCII字符65-91生成器的C ++ randdom字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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