본문 바로가기
C 언어

C언어를 이용하여 무작위로 사람 이름 만들기

by treeCoder 2020. 12. 18.

C 언어를 이용하여 무작위로 영문으로 사람 이름을 만들어 보았다.

 

// C program to generate names and sort them
#include <stdio.h>
#include <stdlib.h>  // for rand
#include <time.h>    // for time
#include <string.h>
  
// Perform sort operation using bubble sort 
void sort(char** names, int n) {
 
    int i, j; 
  
    for (i = 0; i < n - 1; i++) 
        for (j = 0; j < n - i - 1; j++) 
            if (strcmp(names[j], names[j + 1]) > 0) { 
                char* temp; 
                temp = (char*)calloc(30, sizeof(char)); 
                strcpy(temp, names[j]); 
                strcpy(names[j], names[j + 1]); 
                strcpy(names[j + 1], temp); 
            }


// Defining comparator function as per the requirement 
static int myCompare(const void* a, const void* b) 

  
    // setting up rules for comparison 
    return strcmp(*(const char**)a, *(const char**)b); 

  
int main() { 

    char** fullName;
    char fullName2[40];

    int n, i, j, cnt=0; 
    int duplicated = 0;

    n = 50;
  
    char *first[]={ "Alex", "Amy", "Andrew", "Andy", "Anthony",
                      "Ashley", "Bill", "Bob", "Connie", "Daniel",
                      "David", "Debra", "Diane", "Donna", "Emily",
                      "Emma", "Eric", "Frank", "Fred", "Gary", "Gill",
                      "Harry", "Jack", "Jake", "Jeffrey", "Joe",
                      "John", "Julie", "Kate", "Kevin", "Larry",
                      "Liz", "Marie", "Mary", "Michael", "Michelle",
                      "Monica", "Nancy", "Natalie", "Nicole", "Paul",
                      "Peter", "Rachel", "Ray", "Rick", "Richard", "Robert",
                      "Ryan", "Sam", "Scott", "Stephanie", "Steve",
                      "Susan", "Timothy", "Tom"
    };
    
    char *last[]={ "Abrahams", "Adams", "Anderson", "Ayers", "Blackwood",
                     "Brown", "Bryant", "Cooper", "Davis", "Ddavidson",
                     "Edgington", "Elmore", "Ferguson", "Fortini", "Gates",
                     "Gibson", "Gillmore", "Gray", "Hamington", "Harris",
                      "Henderson", "Hendrix", "Hudson", "Jackson",
                     "Johnston", "Jones", "Kuligowski", "Long", "Murray",
                     "Nance", "Netherly", "Petersen", "Putnam", "Robertson",
                     "Sampson", "Schmitz", "Smalley", "Smith", "Stanley", "Taylor",
                     "Washington", "Watson", "White", "Williams", "Wilson"
    };

    int size1 = sizeof(first)/sizeof(first[0]);
    int size2 = sizeof(last)/sizeof(last[0]);

    if( n > size1 * size2 ) n= size1 * size2;

    fullName = (char**)calloc(n, sizeof(char*));  // allocating memory for 1st dimension
    if(fullName==NULL) {
       puts("Memory allocation error");
       return 0;
    }

    for (i = 0; i < n; i++)  {
        // allocating memory for 2nd dimension 
        fullName[i] = (char*)calloc(30, sizeof(char)); 

        if(fullName[i]==NULL) {
           puts("Memory allocation error");
           return 0;
        }

        strcpy(fullName[i], "NONE");
    } 
        
    srand(time(0));
  
    for (i = 0; cnt < n; i++) {
        sprintf(fullName2, "%s %s", first[rand()%size1], last[rand()%size2]);
        
        duplicated = 0;
        for(j=0; j<n; j++) {
            if(strcmp(fullName[j], fullName2)==NULL) { //duplicated
               duplicated = 1;
               break;
            }
        }
        if( !duplicated && cnt < n) {
          if( strcmp( fullName[cnt], "NONE" )==NULL )
              strcpy( fullName[cnt++], fullName2);
        }
        if(i > 32767) break;
        
    } 
    sort( fullName, n); 
     
    puts("\n***** Names ****\n"); 
    for (i = 0; i < n; i++) 
       if( strcmp( fullName[i], "NONE" )!=NULL )
           printf("%04d %s\n", i+1, fullName[i]); 

    for (i = 0; i < n; i++)  {
        free(fullName[i]); 
    }

    free(fullName);

/*
    qsort(first, size1, sizeof(const char*), myCompare); 
    for( i = 0; i < size1; i++ ) {
        printf("\"%s\", ",first[i]);
        if( i % 5 == 4) puts("");
    }

 

puts(""); 

qsort(last, size2, sizeof(const char*), myCompare);  
    for( i = 0; i < size2; i++ ) { 
        printf("\"%s\", ",first[i]); 
        if( i % 5 == 4) puts(""); 
    } 
*/
    return 0; 

 

실행 결과는 아래의 그림과  같다.

 

 

댓글