随机字符串生成器
作者:GWBailang
截止到当前最新版本都没有用 windows.h 头文件,所以任何系统都可以放心食用。
3.0:
变成能调用的函数才是最好的!
#include<bits/stdc++.h>
using namespace std;
const string lowlet="abcdefgjijklmnopqrstuvwxyz";
const string let="ABCDEFGHIGKLMNOPQRSTUVWXYZ";
const string number="0123456789";
const string symbol="!@#$%^&*()";
string strrand(const long long len,const string strr,const int seed){
if(seed!=-1)srand(seed);
string ans="",fu=" ";
int stl=strr.size();
for(long long i=1;i<=len;i++){
fu[0]=strr[rand()%stl];
ans+=fu;
}
return ans;
//时间复杂度O(len)
}
int main(){
cout<<strrand(10//生成的字符串长度,可以但不建议超过int范围
,"+-*/#$@"+lowlet//生成的字符串里的字符都来自于这个字符串
//,如果想有大小写字母或数字可以在字符串后面写+lowlet+let+number
,time(0)//rand函数的随机种子,为-1则不更换种子
)<<endl;
cout<<strrand(10,"01",114514)<<endl;
cout<<strrand(100,"-={}[]"+let+number+symbol,-1)<<endl;
for(int i=1;i<=10;i++){
cout<<strrand(20,"%$"+let+":;"+number,i)<<endl;
}
return 0;
}2.3 版本:
可以生成若干个满足条件的字符串,并且可以无限执行。还改了点细节。
#include<bits/stdc++.h>
using namespace std;
#define ZHONG 8
string gzfh[ZHONG+10]{//各种符号
"",//啥也没有
"ABCDEFGHIGKLMNOPQRSTUVWXYZ",
"abcdefgjijklmnopqrstuvwxyz",
"0123456789",
"+-*/%=",
".,`';:",
"!@#$^&|\~",
"()[]{}",
""//自定义
};
bool fuhao[ZHONG+10];//都有那些种类的符号
string s;
int geshu=0;//一共需要包含几种符号
bool zdy=false;//是否有自定义符号
int chang;//随机名字长度
int lei,lei2;//符号类型
int jige=1;//生成几个字符串
void guize(){
cout<<"随机名字生成器2.3\n";
cout<<"每运行一次程序可以随机无限次,输入一个不是1~"<<ZHONG<<"的数就结束程序\n";
cout<<"还可以自定义随机几个字符串\n";
cout<<"各种格式(是否包含这种符号):\n";
cout<<"1.大写字母\n";
cout<<"2.小写字母\n";
cout<<"3.阿拉伯数字\n";
cout<<"4.运算符("<<gzfh[4]<<")\n";
cout<<"5.英文标点符号("<<gzfh[5]<<")\n";
cout<<"6.特殊符号("<<gzfh[6]<<")\n";
cout<<"7.括号"<<gzfh[7]<<"\n";
cout<<"8.其他/自定义符号(后面会让你输入自定义符号)\n";
}
void shuru(){
cin>>s;
if(s.size()==1&&(s[0]<='0'||s[0]>'9')){
cout<<"结束程序\n";
exit(0);
}
for(int i=0;i<(int)s.size();i++){
if(s[i]>='1'&&s[i]<=ZHONG+'0'){
if(fuhao[s[i]-'0']==false){
geshu++;
fuhao[s[i]-'0']=true;
}
if(s[i]==ZHONG+'0')zdy=true;
}
}
if(zdy==true){
cout<<"请输入自定义符号(暂时只能有char类型的符号):\n";
cin>>gzfh[ZHONG];
}
// for(int i=0;i<ZHONG;i++)cout<<gzfh[i]<<endl;//调试代码
// cout<<geshu<<endl;//调试代码
cout<<"输入随机字符串的长度(输入小于1则代表随机长度):\n";
cin>>chang;
if(chang<=0)chang=rand()%30+1;
cout<<"输入需要生成几个随机字符串(小于1则只生成1个):\n";
cin>>jige;
jige=max(jige,1);
}
void suiji(){
cout<<jige<<"个长度为"<<chang<<"的随机字符串:\n";
s="";
int x=chang;
while(x--){
for(int i=1;i<=ZHONG;i++){
if(fuhao[i]==true)s+=gzfh[i];
}
}
while(jige--){
x=chang;
while(x--){
cout<<s[rand()%s.size()+1];
}
cout<<endl;
}
}
int main(){
guize();
while(true){
srand(time(0));
cout<<"请输入字符串要包含哪些种类的字符:\n";
shuru();
suiji();
}
return 0;
}2.2 版本:
输出每个字符的概率统一了,基本上不会再出现相邻两个字符相同的情况了。
#include<bits/stdc++.h>
using namespace std;
#define ZHONG 8
string gzfh[ZHONG+10]{//各种符号
"",//啥也没有
"ABCDEFGHIGKLMNOPQRSTUVWXYZ",
"abcdefgjijklmnopqrstuvwxyz",
"0123456789",
"+-*/%=",
".,`';:",
"!@#$^&|\~",
"()[]{}",
""//自定义
};
bool fuhao[ZHONG+10];//都有那些种类的符号
string s;
int geshu=0;//一共需要包含几种符号
bool zdy=false;//是否有自定义符号
int chang;//随机名字长度
int lei,lei2;//符号类型
void guize(){
cout<<"随机名字生成器2.2\n";
cout<<"将输出每种字符的概率统一了\n";
cout<<"各种格式(是否包含这种符号):\n";
cout<<"1.大写字母\n";
cout<<"2.小写字母\n";
cout<<"3.阿拉伯数字\n";
cout<<"4.运算符("<<gzfh[4]<<")\n";
cout<<"5.英文标点符号("<<gzfh[5]<<")\n";
cout<<"6.特殊符号("<<gzfh[6]<<")\n";
cout<<"7.括号"<<gzfh[7]<<"\n";
cout<<"8.其他/自定义符号(后面会让你输入自定义符号)\n";
}
void shuru(){
cin>>s;
for(int i=0;i<(int)s.size();i++){
if(s[i]>='1'&&s[i]<=ZHONG+'0'){
if(fuhao[s[i]-'0']==false){
geshu++;
fuhao[s[i]-'0']=true;
}
if(s[i]==ZHONG+'0')zdy=true;
}
}
if(zdy==true){
cout<<"请输入自定义符号(暂时只能有char类型的符号):\n";
cin>>gzfh[ZHONG];
}
// for(int i=0;i<ZHONG;i++)cout<<gzfh[i]<<endl;//调试代码
// cout<<geshu<<endl;//调试代码
cout<<"输入随机字符串的长度(输入小于1则代表随机长度):\n";
cin>>chang;
if(chang<=0)chang=rand()%30+1;
}
void suiji(){
cout<<"长度为"<<chang<<"的随机字符串:\n";
s="";
while(chang--){
for(int i=1;i<=ZHONG;i++){
if(fuhao[i]==true)s+=gzfh[i];
}
cout<<s[rand()%s.size()+1];
}
cout<<endl;
}
void zhuyao(){
guize();
shuru();
suiji();
}
int main(){
srand(time(0));
zhuyao();
return 0;
}2.1 版本:
改掉了 2.0 的亿堆 BUG,增加了自定义长度功能,长度可以自定义,也可以让系统随机一个 1~30 的长度。
#include<bits/stdc++.h>
using namespace std;
#define ZHONG 8
string gzfh[ZHONG+10]{//各种符号
"",//啥也没有
"ABCDEFGHIGKLMNOPQRSTUVWXYZ",
"abcdefgjijklmnopqrstuvwxyz",
"0123456789",
"+-*/%=",
".,`';:",
"!@#$^&|\~",
"()[]{}",
""//自定义
};
bool fuhao[ZHONG+10];//都有那些种类的符号
string s;
int geshu=0;//一共需要包含几种符号
bool zdy=false;//是否有自定义符号
int chang;//随机名字长度
int lei,lei2;//符号类型
void guize(){
cout<<"随机名字生成器2.1\n";
cout<<"将已知的BUG都调好了并将代码美化了一下\n";
cout<<"各种格式(是否包含这种符号):\n";
cout<<"1.大写字母\n";
cout<<"2.小写字母\n";
cout<<"3.阿拉伯数字\n";
cout<<"4.运算符("<<gzfh[4]<<")\n";
cout<<"5.英文标点符号("<<gzfh[5]<<")\n";
cout<<"6.特殊符号("<<gzfh[6]<<")\n";
cout<<"7.括号"<<gzfh[7]<<"\n";
cout<<"8.其他/自定义符号(后面会让你输入自定义符号)\n";
}
void shuru(){
cin>>s;
for(int i=0;i<(int)s.size();i++){
if(s[i]>='1'&&s[i]<=ZHONG+'0'){
if(fuhao[s[i]-'0']==false){
geshu++;
fuhao[s[i]-'0']=true;
}
if(s[i]==ZHONG+'0')zdy=true;
}
}
if(zdy==true){
cout<<"请输入自定义符号(暂时只能有char类型的符号):\n";
cin>>gzfh[ZHONG];
}
// for(int i=0;i<ZHONG;i++)cout<<gzfh[i]<<endl;//调试代码
// cout<<geshu<<endl;//调试代码
cout<<"输入随机字符串的长度(输入小于1则代表随机长度):\n";
cin>>chang;
if(chang<=0)chang=rand()%30+1;
}
void suiji(){
cout<<"长度为"<<chang<<"的随机字符串:\n";
while(chang--){
lei=rand()%geshu+1;
lei2=0;
while(lei>0){
lei2++;
if(fuhao[lei2]==true){
lei--;
}
}
cout<<gzfh[lei2][rand()%gzfh[lei2].size()];
}
cout<<endl;
}
void zhuyao(){
guize();
shuru();
suiji();
}
int main(){
srand(time(0));
zhuyao();
return 0;
}2.0 版本:
在 1.0 的基础上,可以自定义随机出来的字符。只不过有一堆 BUG,所以这里没有放出代码。
1.0 版本:
用随机函数生成随机长度,每一位都随机的字符串,只能是英文大小写字母。因为代码太简单所以没放。