2017-09-13 15 views
-1

私はgotoステートメントを使用するプログラムを書き直すという仕事を与えられました。それは、いかにイライラさせることができるかを示す唯一の目的であり、実際には非常にイライラしていることが分かりました。私はまだ問題に遭遇しているデバッガをフリップして数時間後、私は潜在的にそれをよりよく理解することができる私のためにこれを擬似コードできる人はいますか?それぞれのgotoのif文とelse文を作成することとは別に、私は失われています。GoToプログラムの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

そのような目的を破ったのですが、やはり私はゴートステートメントを決して使わないように教えることを目的としていたと思います。 – shockemc

+1

"goto"ステートメントは、これらの問題の1つにすぎません。字下げを修正することを意味していましたが、 "テキストの壁"は不必要に難しいです。インデントを修正してから、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

(しかし、^^)downhoteはしませんでした – Fildor

+0

ハードコードされた回答とは対照的に、コードの概要を本当に期待していましたが、これはうまくいきます。しかし、これははるかに理解しやすいです。 – shockemc