2016-03-29 13 views
0

Bir üniversite projesinin bir parçası olarak kendi dilim için bir derleyici oluşturmaya çalışıyorum ve bir dizede (alfa, sayısal, sınırlayıcı vb.) Giriş türünü belirlemek için bir modül yazarken bu soruna rastladım. . Belirli semboller için ip nasıl test edilir? (C#)

private static void readChar(ref string sentence, ref int inputType) 
    { 
     foreach (char test in sentence) 
     { 

      if ((test.CompareTo("|") == 0) | 
       (test.CompareTo("*") == 0) | 
       (test.CompareTo("/") == 0) | 
       (test.CompareTo("+") == 0) | 
       (test.CompareTo("-") == 0) | 
       (test.CompareTo("@") == 0) | 
       (test.CompareTo("#") == 0) | 
       (test.CompareTo("$") == 0) | 
       (test.CompareTo("%") == 0) | 
       (test.CompareTo("^") == 0) | 
       (test.CompareTo("&") == 0) | 
       (test.CompareTo("(") == 0) | 
       (test.CompareTo(")") == 0) | 
       (test.CompareTo(",") == 0) | 
       (test.CompareTo("`") == 0) | 
       (test.CompareTo("=") == 0)) 
      { 
       inputType = 5; // delimiter 
      } 
      else 
      { 
       if ((test.CompareTo("0") == 0) | 
        (test.CompareTo("1") == 0) | 
        (test.CompareTo("2") == 0) | 
        (test.CompareTo("3") == 0) | 
        (test.CompareTo("4") == 0) | 
        (test.CompareTo("5") == 0) | 
        (test.CompareTo("6") == 0) | 
        (test.CompareTo("7") == 0) | 
        (test.CompareTo("8") == 0) | 
        (test.CompareTo("9") == 0)) 
       { 
        inputType = 3; // numeric    
       } // end if compareTo Numeric 
       else 
       { 
        if ((test.CompareTo(" ") == 0)) inputType = 6; //space 

        else 
         if ((test.CompareTo(";") == 0)) inputType = 7; //semicolon   
         else 
         { 
          inputType = 1; // alpha 
         } // end alpha 
       } // end else 

      } // end if 
     } 

    } // end readChar 

bu hata verir deyimi eğer ilk ulaşır

: türü 'System.ArgumentException' işlenmeyen bir özel durum mscorlib.dll

oluştu Ek bilgi: Nesne tipi Char olmalıdır.

Sınama için kullandığımı ve hatanın hangi nesneye başvurduğunu bilmediğim için bunu nasıl çözeceğimi bilmiyorum.

Okuduğunuz için teşekkürler, yardıma ^^

+0

;' ve 'çift kullanabilirsiniz || ', tek bir' | ' anlamı yok. – Camo

+1

"string" string parametresi neden? Ve bir döngüde 'int int 'parametresini çok kez güncellemek için ne kullanır? Bu int değişkeni kadar hızlı okumaya çalışan bir şey var mı? Ses görünmüyor. –

+0

Tek bir düşünce, "her" giriş türü için "(...)" anahtarını değiştirip (daha sonra "her bir blokta 'case' etiketiyle) bir anahtar bloğuna sahip olmaktır. vb. –

cevap

3

şey değişmez bir dize olduğunu. Eğer değişmez bir char istiyorsanız Eğer CompareTo çözüm yöntemleri karşılaştırmak için kömürü kabul tek tırnak 'a'

private static void readChar(ref string sentence, ref int inputType) 
{ 
    foreach (char test in sentence) 
    { 

     if ((test.CompareTo('|') == 0) | 
      (test.CompareTo('*') == 0) | 
      (test.CompareTo('/') == 0) | 
      (test.CompareTo('+') == 0) | 
      (test.CompareTo('-') == 0) | 
      (test.CompareTo('@') == 0) | 
      (test.CompareTo('#') == 0) | 
      (test.CompareTo('$') == 0) | 
      (test.CompareTo('%') == 0) | 
      (test.CompareTo('^') == 0) | 
      (test.CompareTo('&') == 0) | 
      (test.CompareTo('(') == 0) | 
      (test.CompareTo(')') == 0) | 
      (test.CompareTo(',') == 0) | 
      (test.CompareTo('`') == 0) | 
      (test.CompareTo('=') == 0)) 
     { 
      ...... 
+1

Buradaki iki 'char' değeri arasında' == 'kullanmak daha iyidir. '.CompareTo', genel sıralama için değil, eşitlik kontrolleri içindir. –

1

dizeleri atfedilen tırnak işareti kullanılan çünkü hata alıyorsanız teşekkür ederiz. Tek tırnak kullanın. çift ​​tırnak "asdf..." arasında yazılmış

String s = "String"; 
char c = 'c'; 
0

kullanın. dizgisi dizesinde geçiyorsunuz.
kullanımı tek bir "string"

1

Sadece hızlı öneri için 'char'
çift tırnak için tırnak: Sen `Char.IsDigit ('9') kullanabilir

private static void ReadChar(string sentence, ref int inputType) 
{ 
    foreach (char test in sentence) { 
     if ("|*/[email protected]#$%^&(),`=".Contains(test)) { 
      inputType = 5; 
     } // delimiter 
     else if (Char.IsDigit(test)) { 
      inputType = 3; 
     } // numeric    
     else if (Char.IsWhiteSpace(test)) { 
      inputType = 6; 
     } // space 
     else if (test == ';') { 
      inputType = 7; 
     } // semicolon   
     else { 
      inputType = 1; 
     } // end alpha 
    } 
} 
+0

'' ”| */+ - @ # $%^&(),' = ". İçerir (test)' 'Linq'den geçer (ve' string' IE'syonu '0Enumerable 'uygular) kullanır Bu çözüm ile System.Linq; Linq'den kaçınmak için, '' * | */+ - @ # $%^&(), '=” diyebilirsiniz. IndexOf (test)! = -1'', ancak Linq iyi. –

İlgili konular