2017-09-13 107 views
-1

我已經被賦予了重寫一個使用goto語句的程序的任務,這個程序的唯一目的是證明它們有多令人沮喪,並且我已經瞭解到他們確實非常令人沮喪。經過幾個小時翻閱調試器後,我仍然遇到問題,有沒有人可以爲我編寫僞代碼,以便我可以更好地理解它?除了爲每個goto創建if和else語句之外,我都迷路了。用於GoTo程序的C#Sudo代碼

namespace Assignment_03_Nasty_Code 
{ 
    class Assignment03 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Assignment 03 = " + new Assignment03().Solve()); 
     } 

     public int Solve() 
     { 
      int result = 0; 
      int upperSearchLimit = 1_000_000; 
      Console.WriteLine("Building list of primes..."); 
      ArrayList primes = Utils.BuildListOfPrimes(upperSearchLimit); 
      Console.WriteLine(" Done."); 
      // Step through the odd composite numbers 
      for (int i = 19; i < upperSearchLimit; i+= 2) 
      { 
       Label02: 
       if (primes.Contains(i)) 
        goto Label03; 
       // Is the number divisible by a prime? 
       int j = 0; 
       Boolean match = false; 
       Label01: 
       int tmp; 
       int prime = (int)primes[j]; 
       tmp = i - prime; 
       int half = tmp /= 2; 
       int squareRoot = (int) Math.Sqrt(half); 
       if (tmp != squareRoot * squareRoot) 
        goto Label04; 
       // We got one 
       //System.out.println(i + " is a composite that can be written as the sum of a prime and twice a square"); 
       match = true; 
       Console.WriteLine(i + " = " + (int)primes[j] + " + 2 * " + half); 
       Label04: // Second goto 
       j++; 
       if ((int)primes[j] < i) 
        goto Label01; 
       if (match == false) 
       { 
        Console.WriteLine("No match for " + i); 
        result = i; 
        break; 
       } 
       //goto Label02; 
       Label03: // First goto 
       int x; 
      } 
      return result; 
     } 
    } 
} 
+3

我做的第一件事將通過格式化來運行代碼! –

+0

這種打敗的目的,然後我想我的目的是教我從來沒有使用goto聲明,以及我不需要告訴爲什麼在此之後。 – shockemc

+1

他的意思是修正縮進,「文本牆」不必要的困難,因爲它在可維護性方面存在多個問題,「goto」語句只是其中一個問題。首先修復縮進,然後開始處理goto。 –

回答

0

我已經取代了在你的代碼goto,這就是結果

public int Solve() 
{ 
    var result = 0; 
    var upperSearchLimit = 1_000_000; 
    Console.WriteLine("Building list of primes..."); 
    ArrayList primes = Utils.BuildListOfPrimes(upperSearchLimit); 
    Console.WriteLine(" Done."); 

    for (var i = 19; i < upperSearchLimit; i += 2) 
    { 
     if (primes.Contains(i)) 
     { 
      continue; 
     } 

     var j = 0; 
     var match = false; 
     do 
     { 
      int tmp; 
      var prime = (int)primes[j]; 
      tmp = i - prime; 
      var half = tmp /= 2; 
      var squareRoot = (int)Math.Sqrt(half); 
      if (tmp == squareRoot * squareRoot) 
      { 
       match = true; 
       Console.WriteLine(i + " = " + (int)primes[j] + " + 2 * " + half); 
      } 

      j++; 
     } while ((int)primes[j] < i); 

     if (match == false) 
     { 
      Console.WriteLine("No match for " + i); 
      result = i; 
      break; 
     } 
    } 

    return result; 
} 
+0

OP明確表示**不希望**完成代碼解決方案。 – Fildor

+0

(沒有downvote,雖然^^) – Fildor

+0

我真的希望有一個大綱的代碼做什麼,而不是一個硬編碼的答案,但這不工作謝謝。儘管這很容易理解。 – shockemc