2013-08-07 13 views
5

इसलिए मैंने सी # में एक बहुत ही सरल शब्द जनरेटर प्रोग्राम बनाया जो अपेक्षाकृत अच्छी तरह से काम करता है। यह उपयोगकर्ता द्वारा परिभाषित लंबाई के आधार पर एक शब्द उत्पन्न करता है।यादृच्छिक शब्द जनरेटर # 2

एल्गोरिथ्म एक व्यंजन और फिर दृश्य है, जो आदर्श नहीं है में प्रत्येक लगातार पत्र के लिए एक स्वर कहते हैं, लेकिन बुनियादी शब्द के लिए काफी अच्छी तरह से काम करता है।

मेरा एकमात्र मुद्दा यह है कि मैंने इसे "यू" अक्षर अनुक्रम में जोड़ने के लिए कहा था, यदि कोई "क्यू" इससे ठीक पहले दिखाई देता है, लेकिन इससे कोई फर्क नहीं पड़ता कि मैंने यह किया है कि यह कम से कम 1 अक्षर भी बनाता है लंबा।

मैं इसे ऊपर टिप्पणी में एक स्टार के साथ मेरी समस्या क्षेत्र चिह्नित किया है। यहाँ कोड है:

public void WordFinder() 
{ 
    string word = null; 
    int cons; 
    int vow; 
    //counter 
    int i = 0; 
    bool isword = false; 
    Random rnd = new Random(); 
    //set a new string array of consonants 
    string[] consonant = new string[]{"b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"}; 
    //set a new string array of vowels 
    string[] vowel = new string[]{"a","e","i","o","u"}; 
    while (isword == false) 
    { 
     word = null; 
     Console.WriteLine("Pick the length of a word"); 
     int num = Convert.ToInt32(Console.ReadLine()); 
     //set the counter "i" to 1 
     i = 1; 
     if (num%2 == 0) 
     { 
      while (i <= num) 
      { 
       if (num != 1) 
       { 
        // current consonant = random consonant 
        cons = rnd.Next(0, 20); 
        // get the consonant from the string array "consonant" and add it to word 
        word = word + consonant[cons]; 
        // add 1 to counter 
        i ++; 
        //* if the consonant is "q" 
        if (cons == 12) 
        { 
         // add "u" right after it 
         word = word + vowel[4]; 
         // add 1 to counter 
         i++; 
        } 
       } 
       vow = rnd.Next(0, 4); 
       word = word + vowel[vow]; 
       i ++; 
      } 
     } 
     if (num % 2 != 0) 
     { 
      while (i <= num - 1) 
      { 
       //repeat same code as done to even length 
       if (num != 1) 
       { 
        cons = rnd.Next(0, 20); 
        word = word + consonant[cons]; 
        i ++; 
        if (cons == 12) 
        { 
         word = word + vowel[4]; 
         i ++; 
        } 
       } 
       vow = rnd.Next(0, 4); 
       word = word + vowel[vow]; 
       i ++; 
      } 
      // if the length is not equal to 1 
      if (num != 1) 
      { 
       // add a consonant to the end of the word 
       cons = rnd.Next(0, 20); 
       word = word + consonant[cons]; 
      } 
      //if the length is 1 
      else if (num == 1) 
      { 
       // pick a vowel 
       vow = rnd.Next(0, 4); 
       word = word + vowel[vow]; 
      } 
     } 
     i = 1; 
     Console.WriteLine(word); 
     Console.WriteLine("Is this a word? (y/n)"); 
     string q = Console.ReadLine(); 
     q = q.ToLower(); 
     //if the answer is yes, then it is a word and end the loop 
     if (q == "y" || q == "yes") 
     { 
      isword = true; 
     } 
     //if the answer is no try the loop again 
     else if (q == "n" || q == "no") 
     { 
      isword = false; 
     } 
    } 
} 
// main method 
static void Main(string[] args) 
{ 
    Program prog = new Program(); 
    prog.WordFinder(); 
    //wait for user input 
    Console.ReadLine(); 
} 
} 
+17

उस कोड में लगभग सभी टिप्पणियां सहायक नहीं हैं। टिप्पणियां आपको यह बताने के लिए वहां होनी चाहिए कि कोड * आपको * नहीं बताता (या यह कोड से स्पष्ट नहीं है)। एक टिप्पणी जो सिर्फ बताती है कि कोड क्या करता है, किसी की भी मदद नहीं करता है, यह सिर्फ लोगों के समय को बर्बाद कर देता है। – Servy

+2

मुझे समझ में नहीं आता कि * उसने शब्द को एक पत्र (या "क्यू" को "यू" लंबा * मतलब के बाद जोड़ा गया था। आप 'व्यंजन [विपक्ष] ==' q 'का उपयोग करने का प्रयास कर सकते हैं। 'आपकी' if' स्थिति के रूप में, आपके पास जादू संख्या 12 की जगह है। –

+2

जिस तरह से मैं इसे पढ़ रहा हूं, लूप के माध्यम से प्रत्येक पास के लिए आप 2 अक्षर जोड़ रहे हैं। एक व्यंजन और स्वर। उत्पन्न करने के मामले में एक 'क्यू' आप कुल 3 अक्षरों को जोड़ रहे हैं: 'QU' प्लस एक यादृच्छिक अन्य स्वर। यहां तर्क दिखाई देता है। – NotMe

उत्तर

8

मैं आपका जवाब पुनर्संशोधित और कुछ डिबगिंग के बाद मैं यह काम करने के लिए मिला है। क्षमा करें मैं इसे ठीक करने के लिए बस एक चिमटा नहीं कर सका। मेरा मानना ​​है कि यह "qu" या "q" में किसी शब्द को समाप्त करने की अनुमति नहीं देता है।

public void WordFinder() 
{ 
    bool isWord = false; 
    Random rnd = new Random(); 
    string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" }; 
    string[] vowels = { "a", "e", "i", "o", "u" }; 


    while (isWord == false) 
    { 
     string word = ""; 

     Console.WriteLine("Pick the length of a word"); 
     int requestedLength = Convert.ToInt32(Console.ReadLine()); 

     // Generate the word in consonant/vowel pairs 
     while (word.Length < requestedLength) 
     { 
      if (requestedLength != 1) 
      { 
       // Add the consonant 
       string consonant = GetRandomLetter(rnd, consonants); 

       if (consonant == "q" && word.Length + 3 <= requestedLength) // check +3 because we'd add 3 characters in this case, the "qu" and the vowel. Change 3 to 2 to allow words that end in "qu" 
       { 
        word += "qu"; 
       } 
       else 
       { 
        while(consonant == "q") 
        { 
         // Replace an orphaned "q" 
         consonant = GetRandomLetter(rnd, consonants); 
        } 

        if (word.Length + 1 <= requestedLength) 
        { 
         // Only add a consonant if there's enough room remaining 
         word += consonant; 
        } 
       } 
      } 

      if (word.Length + 1 <= requestedLength) 
      { 
       // Only add a vowel if there's enough room remaining 
       word += GetRandomLetter(rnd, vowels); 
      } 
     } 

     Console.WriteLine(word); 
     Console.WriteLine("Is this a word? (y/n)"); 
     string q = Console.ReadLine().ToLower(); 

     if (q == "y" || q == "yes") 
     { 
      isWord = true; 
     } 
    } 
} 

private static string GetRandomLetter(Random rnd, string[] letters) 
{ 
    return letters[rnd.Next(0, letters.Length - 1)]; 
} 

संपादित करें: हालांकि, यह अभी भी बहुत बेकार है। एक यादृच्छिक स्ट्रिंग उत्पन्न करने के बारे में, और उसके बाद "क्यू" को "क्यू" के साथ बदलने के बाद कैसे करें?

public string WordFinder2(int requestedLength) 
{ 
    Random rnd = new Random(); 
    string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" }; 
    string[] vowels = { "a", "e", "i", "o", "u" }; 

    string word = ""; 

    if (requestedLength == 1) 
    { 
     word = GetRandomLetter(rnd, vowels); 
    } 
    else 
    { 
     for (int i = 0; i < requestedLength; i+=2) 
     { 
      word += GetRandomLetter(rnd, consonants) + GetRandomLetter(rnd, vowels); 
     } 

     word = word.Replace("q", "qu").Substring(0, requestedLength); // We may generate a string longer than requested length, but it doesn't matter if cut off the excess. 
    } 

    return word; 
} 

private static string GetRandomLetter(Random rnd, string[] letters) 
{ 
    return letters[rnd.Next(0, letters.Length - 1)]; 
} 
+0

+1। – Bobo

+0

मैं इन उत्तरों ग्रेग की सराहना करता हूं। मैं सी # और सामान्य रूप से प्रोग्रामिंग के लिए अपेक्षाकृत नया हूं और सामान्य रूप से सी # के साथ खेल रहा हूं और मैं इस कोड के साथ आया हूं। सी # पुस्तकालयों और कक्षाओं के अपने न्यूनतम ज्ञान के कारण, मुझे अपना कोड इस तरह के क्रूर और सरल तरीके से लिखना पड़ा। मैं सराहना करता हूं कि आपने दोनों विकल्पों को दिखाया, एक स्रोत कार्यक्रम के करीब और दूसरा अधिक कुशल तरीके से। –

1

आपकी समस्या है क्योंकि जिस तरह से आप अपने छोरों का निर्माण कर रहे हैं का हो रहा है।

आप दो अलग-अलग छोरों का उपयोग, चाहे लंबाई सम या विषम है पर निर्भर करता है, और प्रत्येक कि पाश दो अक्षर जोड़ देगा में मान। हालांकि, जब एक क्यू का सामना किया जाता है, तो लूप 3 वर्ण जोड़ता है, जो लूप को एक अतिरिक्त समय निष्पादित करने का कारण बनता है, और आप एक अतिरिक्त चरित्र के साथ समाप्त होते हैं।

इस विधि का प्रयास करें:

string GenerateWord(int length) 
    { 
     if (length < 1) // do not allow words of zero length 
      throw new ArgumentException("Length must be greater than 0"); 

     string word = string.Empty; 

     if (rand.Next() % 2 == 0) // randomly choose a vowel or consonant to start the word 
      word += cons[rand.Next(0, 20)]; 
     else 
      word += vowel[rand.Next(0, 4)]; 

     for (int i = 1; i < length; i += 2) // the counter starts at 1 to account for the initial letter 
     { // and increments by two since we append two characters per pass 
      char c = cons[rand.Next(0, 20)]; 
      char v = vowel[rand.Next(0, 4)]; 

      if (c == 'q') // append qu if the random consonant is a q 
       word += "qu"; 
      else // otherwise just append a random consant and vowel 
       word += c + v; 
     } 

     // the word may be short a letter because of the way the for loop above is constructed 
     if (word.Length < length) // we'll just append a random consonant if that's the case 
      word += cons[rand.Next(0, 20)]; 

     return word; 
    } 
+0

अच्छा पहला पास, हालांकि यह आवश्यकता को छोड़ देता है कि एक स्वर हमेशा 'QU' का पालन करता है। इसके अलावा, एक प्रश्न – NotMe

+0

पर समाप्त होने की संभावना मौजूद है, मुझे उस आवश्यकता को परिभाषित नहीं किया गया है। मैं देखता हूं कि ओपीएस कोड इसका तात्पर्य है, लेकिन मुझे पूरी तरह से यकीन नहीं है कि इरादे से है। – Michael

+0

@ माइकल - मेरे पास एक जवाब था [कि मैंने हटा दिया] (http://i.stack.imgur.com/TWa5L.png) जहां ओपी ने उस आवश्यकता को बनाया। गलतफहमी के लिए खेद है। 'Qu' के साथ 'q' को प्रतिस्थापित करने के सुझाव के लिए – Greg

0

क्षमा करें, मुझे बहुत देर हो चुकी है। लेकिन किसी को भी समस्या होने के लिए, यह सबसे अच्छा और सबसे आसान समाधान है .. System.Net का उपयोग करना सुनिश्चित करें;

 public string RandomWord() 
     { // add a dispose to the webclient to make things go more smoothly 
     return new WebClient().DownloadString("http://setgetgo.com/randomword/get.php"); 
     } 
संबंधित मुद्दे