Visual C++ 6.0 如何生成随机字符

Visual C++ 6.0 如何生成随机字符

函数名:randomstring(char *options,int length)

options形参参数 

T生成一个大写字母t生成一个小写字母
N生成一个数字n生成一个数字
S生成一个符号s生成一个符号
G随机生成一个大、小写字母或数字g随机生成一个大、小写字母或数字
M随机生成一个大写字母或数字m随机生成一个小写字母或数字
X随机生成一个字符(大小写字母、数字、符号)x随机生成一个字符(大小写字母、数字、符号)
NULL未指定生成的字符格式""未指定生成的字符格式

 

length形参为正整数的生成长度

函数原型:

#include <stdlib.h>
#include <time.h>

char *randomstring(char *options,int length)
{
    char *saverandomstring;//保存随机生成的字符,并组合成字符串
    char unknowsoptions;
    int saverandomstringcontrol=0;//控制saverandomstring指针变量的位置
    int optionscontrol=0;//控制形参options指针变量的位置
    int branchselect;//生成分支选择
    saverandomstring=(char *)malloc(sizeof(char));
    srand((unsigned)time(NULL));//复位和初始化随机状态
    //进入if语句判断
    if(options==NULL && options=="" && length=='0')//如果options为空和length为零
    {
        printf("Error!\n");
        return(NULL);
    }
    else if(options!=NULL && length==0)//相反,options不为空,但length为零
    {
        while(1)
        {
            if(optionscontrol>2700)
            {
                printf("Options Out!\n");
                saverandomstring=NULL;
                break;
            }
            else if(options[optionscontrol]=='\0')
            {
                saverandomstring[saverandomstringcontrol]='\0';//在saverandomstring指针变量的末尾加入结束符
                break;//终止while循环
            }
            else
            {
                switch(options[optionscontrol])
                {
                case 'T':
                    saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                    break;
                case 't':
                    saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                    break;
                case 'S':
                    saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                    break;
                case 's':
                    saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                    break;
                case 'N':
                    saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                    break;
                case 'n':
                    saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                    break;
                case 'G':
                    branchselect='1'+ rand()%3;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'g':
                    branchselect='1'+ rand()%3;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'M':
                    branchselect='1'+ rand()%2;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'm':
                    branchselect='1'+ rand()%2;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'X':
                    branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    break;
                case 'x':
                    branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    break;
                default:
                    unknowsoptions=options[optionscontrol];
                    saverandomstring[saverandomstringcontrol]=unknowsoptions;
                    break;
                }
                optionscontrol++;
                saverandomstringcontrol++;
                continue;//继续while循环
            }
        }
    }
    else if(options==NULL || options=="")
    {
        while(1)
        {
            if(length>2700)
            {
                printf("Length Out!\n");
                saverandomstring=NULL;
                break;
            }
            else if(saverandomstringcontrol<length)
            {
                branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    saverandomstringcontrol++;
                    continue;
            }
            else
            {
                saverandomstring[saverandomstringcontrol]='\0';
                break;
            }
        }
    }
    else//相反,options和length都有数据
    {
        while(1)
        {
            if(length>2700)
            {
                printf("Length Out!\n");
                saverandomstring=NULL;
                break;
            }
            else if(options[optionscontrol]=='\0')
            {
                optionscontrol=0;
                continue;
            }
            else if(saverandomstringcontrol<length)
            {
                switch(options[optionscontrol])
                {
                case 'T':
                    saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                    break;
                case 't':
                    saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                    break;
                case 'S':
                    saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                    break;
                case 's':
                    saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                    break;
                case 'N':
                    saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                    break;
                case 'n':
                    saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                    break;
                case 'G':
                    branchselect='1'+ rand()%3;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'g':
                    branchselect='1'+ rand()%3;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'M':
                    branchselect='1'+ rand()%2;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'm':
                    branchselect='1'+ rand()%2;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'X':
                    branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    break;
                case 'x':
                    branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    break;
                default:
                    unknowsoptions=options[optionscontrol];
                    saverandomstring[saverandomstringcontrol]=unknowsoptions;
                    break;
                }
                optionscontrol++;
                saverandomstringcontrol++;
                continue;//继续while循环
            }
            else
            {
                saverandomstring[saverandomstringcontrol]='\0';
                break;//终止while循环
            }
        }
    }
    return(saverandomstring);//返回已存储在saverandomstring字符串指针变量中的字符串
    free(saverandomstring);//释放saverandomstring字符串指针变量
}

//**************************************************************************************************************************************

调用示例:

#include <stdlib.h>
#include <time.h>

#include <stdio.h>

char *randomstring(char *options,int length);//声明randomstring函数

void main()

while(1)

{

printf("%s\n"ramdomstring("MMMMM-MMMMM-MMMMM-MMMMM-MMMMM",29));

system("pause");

}

char *randomstring(char *options,int length)
{
    char *saverandomstring;//保存随机生成的字符,并组合成字符串
    char unknowsoptions;
    int saverandomstringcontrol=0;//控制saverandomstring指针变量的位置
    int optionscontrol=0;//控制形参options指针变量的位置
    int branchselect;//生成分支选择
    saverandomstring=(char *)malloc(sizeof(char));
    srand((unsigned)time(NULL));//复位和初始化随机状态
    //进入if语句判断
    if(options==NULL && options=="" && length=='0')//如果options为空和length为零
    {
        printf("Error!\n");
        return(NULL);
    }
    else if(options!=NULL && length==0)//相反,options不为空,但length为零
    {
        while(1)
        {
            if(optionscontrol>2700)
            {
                printf("Options Out!\n");
                saverandomstring=NULL;
                break;
            }
            else if(options[optionscontrol]=='\0')
            {
                saverandomstring[saverandomstringcontrol]='\0';//在saverandomstring指针变量的末尾加入结束符
                break;//终止while循环
            }
            else
            {
                switch(options[optionscontrol])
                {
                case 'T':
                    saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                    break;
                case 't':
                    saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                    break;
                case 'S':
                    saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                    break;
                case 's':
                    saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                    break;
                case 'N':
                    saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                    break;
                case 'n':
                    saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                    break;
                case 'G':
                    branchselect='1'+ rand()%3;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'g':
                    branchselect='1'+ rand()%3;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'M':
                    branchselect='1'+ rand()%2;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'm':
                    branchselect='1'+ rand()%2;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'X':
                    branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    break;
                case 'x':
                    branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    break;
                default:
                    unknowsoptions=options[optionscontrol];
                    saverandomstring[saverandomstringcontrol]=unknowsoptions;
                    break;
                }
                optionscontrol++;
                saverandomstringcontrol++;
                continue;//继续while循环
            }
        }
    }
    else if(options==NULL || options=="")
    {
        while(1)
        {
            if(length>2700)
            {
                printf("Length Out!\n");
                saverandomstring=NULL;
                break;
            }
            else if(saverandomstringcontrol<length)
            {
                branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    saverandomstringcontrol++;
                    continue;
            }
            else
            {
                saverandomstring[saverandomstringcontrol]='\0';
                break;
            }
        }
    }
    else//相反,options和length都有数据
    {
        while(1)
        {
            if(length>2700)
            {
                printf("Length Out!\n");
                saverandomstring=NULL;
                break;
            }
            else if(options[optionscontrol]=='\0')
            {
                optionscontrol=0;
                continue;
            }
            else if(saverandomstringcontrol<length)
            {
                switch(options[optionscontrol])
                {
                case 'T':
                    saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                    break;
                case 't':
                    saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                    break;
                case 'S':
                    saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                    break;
                case 's':
                    saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                    break;
                case 'N':
                    saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                    break;
                case 'n':
                    saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                    break;
                case 'G':
                    branchselect='1'+ rand()%3;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'g':
                    branchselect='1'+ rand()%3;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'M':
                    branchselect='1'+ rand()%2;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'm':
                    branchselect='1'+ rand()%2;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    }
                    break;
                case 'X':
                    branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    break;
                case 'x':
                    branchselect='1'+ rand()%4;
                    switch(branchselect)
                    {
                    case '1':
                        saverandomstring[saverandomstringcontrol]='A'+ rand()%26;
                        break;
                    case '2':
                        saverandomstring[saverandomstringcontrol]='a'+ rand()%26;
                        break;
                    case '3':
                        saverandomstring[saverandomstringcontrol]='0'+ rand()%10;
                        break;
                    case '4':
                        saverandomstring[saverandomstringcontrol]='!'+ rand()%15;
                        break;
                    }
                    break;
                default:
                    unknowsoptions=options[optionscontrol];
                    saverandomstring[saverandomstringcontrol]=unknowsoptions;
                    break;
                }
                optionscontrol++;
                saverandomstringcontrol++;
                continue;//继续while循环
            }
            else
            {
                saverandomstring[saverandomstringcontrol]='\0';
                break;//终止while循环
            }
        }
    }
    return(saverandomstring);//返回已存储在saverandomstring字符串指针变量中的字符串
    free(saverandomstring);//释放saverandomstring字符串指针变量
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值