简体   繁体   English

C 生成一串字母数字然后排序的程序

[英]C program that generates a string of alphanumericals and then sorts

I am trying to write a script in c that generates a string of alphanumerical values,based on the desired size of the user input, and then it sorts it.我正在尝试在 c 中编写一个脚本,该脚本根据用户输入的所需大小生成一串字母数字值,然后对其进行排序。

#include <stdio.h>
#include <stdlib.h>

#define FIRST_CHAR 'a'
#define LAST_CHAR 'z'
#define NULL_CHAR '\0'


char *randomString( int minSize, int maxSize){
int i,strSize;
char *newStr;
    strSize = minSize + ( rand() % (maxSize-minSize + 1 ) );
    newStr = (char*)malloc(strSize + 1 );
    if (newStr == NULL) {
        printf( " \n Error in memory allocation. Cannot create random string. \n " );
        return NULL;
    }
    for (i = 0; i < strSize; i++) {
        newStr[i] = FIRST_CHAR + (rand() % (LAST_CHAR - FIRST_CHAR + 1));
    }
    newStr[i] = NULL_CHAR;
return newStr;
}

void printStrings(char **strArray, int strArraySize)
{
    int i;
    for(i=0;i<strArraySize;++i)
    {
        printf("%s",strArray[i]);
    }
    printf("/n");
}
char *sort(char **strArray, int strArraySize)
{int i,j;
 char *sortedArray;
    for(i=0;i<strArraySize;++i)
    {
        for(j=FIRST_CHAR;j<=LAST_CHAR;++i)
        {
            if(strArray[i]==j)
            {
                strArray[i]=sortedArray[i];
            }
        }
    }
    sortedArray[i]= NULL_CHAR;
return sortedArray;
}

int main()
{int size,minSize=5,maxSize=20;
 char** str;
    printf("Dwse plithos alpharithmitikon\n");
    scanf("%d",&size);
    **str=*randomString(minSize,maxSize);

    printf("Seira pou dimiourgithike.\n");
    printStrings(str,size);

    printf("Seira pou taksinomithike.\n");
    printStrings(sort(str,size),size);

    return 0;
}

the main issue is that i cant wrap my head around the proper use of pointers in c. I am aware that it is of important use,especially while working with alphanumerical strings, but i am just not there yet.主要问题是我无法在 c 中正确使用指针。我知道它有重要用途,尤其是在使用字母数字字符串时,但我还没有做到这一点。 Thanks in advance !提前致谢 !

While the following generator is not truly random (what is?), it delivers a random number of random length ASCII "strings" (actually, just one string sprinkled with LF characters) created in sorted order.虽然下面的生成器不是真正随机的(什么是随机的?),但它提供了按排序顺序创建的随机长度的 ASCII“字符串”(实际上,只是一个散布着 LF 字符的字符串)。 If nothing else, it provides a starting point from which to refine a better solution.如果不出意外,它提供了一个起点,可以从中改进更好的解决方案。

char palette[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

int main( void ) {
    srand( time( 0 ) );

    // some constants
    const int obufsz = 512;
    const int maxStrLen = 15;

    // the output buffer
    char obuf[ obufsz ], *op = obuf;

    // random number of strings (min: 5, max 20) 
    int nStrs = 5 + rand()%(obufsz/maxStrLen);
    if( nStrs > 20 ) nStrs = 20;

    // increment so that successive first chars are always larger
    int incr = sizeof palette / nStrs;

    for( int strNo = 0; strNo < nStrs; strNo++ ) {
        // assign the 'ascending' first char
        *op++ = palette[ strNo * incr + (rand() % incr) ];

        // assign more chars (variable length)
        for( int charNo = 1 + rand()%(maxStrLen-2); charNo; charNo-- )
            *op++ = palette[ rand() % (sizeof palette - 1 ) ];

        // a LF for eventual printing
        *op++ = '\n';
    }
    *op++ = '\0';

    puts( obuf );

    return 0;
}

If you want to wrap your head around using C's pointers, grab a pencil and paper and start drawing boxes and arrows.如果你想用 C 的指针环绕你的头脑,拿起铅笔和纸,开始画方框和箭头。 A box would be a memory location (a pointer variable) that contains the address of another box that is (often) the first element of a contiguous array of boxes each holding either a 'primitive' datatype (ie: a 'char' or an 'int', etc.), or holding a pointer to another memory location (ie: a pointer to a pointer.) "Boxes and arrows" - that's how most people come-to-terms with C's pointers.一个框将是一个 memory 位置(指针变量),它包含另一个框的地址,该地址(通常)是连续框数组的第一个元素,每个框都包含一个“原始”数据类型(即:一个“字符”或一个'int' 等),或持有指向另一个 memory 位置的指针(即:指向指针的指针。)“方框和箭头”- 这就是大多数人接受 C 指针的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM