TKYM's profile※ 画面は開発中のものですPhotosBlogListsMore Tools Help

Blog


    June 23

    if(0<i<100) という書き方 (C#)

    もちろん、 if(0<i<100) なんていう書き方は不可能である。
    しかし、何とかしようと思えばできなくもなさそうである。

       1: struct keisan
       2: {
       3:     public int i;
       4:  
       5:     List<range> condition;
       6:  
       7:     //tostring
       8:     public override string ToString()
       9:     {
      10:         return "" + i;
      11:     }
      12:  
      13:     //コンストラクタ
      14:     public keisan(int i) {
      15:         this.i = i;
      16:         condition = new List<range>();
      17:     }
      18:  
      19:     //新しく追加するとき
      20:     public keisan(keisan ks) {
      21:         this.i = ks.i;
      22:         condition = new List<range>();
      23:  
      24:         foreach(range r in ks.condition){
      25:             condition.Add(r);
      26:         }
      27:     }
      28:  
      29:     //bool へのキャスト
      30:     public static implicit operator bool(keisan ks){
      31:         foreach(range r in ks.condition){
      32:             if(r.min > ks.i || r.max < ks.i){
      33:                 return false;
      34:             }
      35:         }
      36:         return true;
      37:     }
      38:  
      39:     //int へのキャスト
      40:     public static implicit operator int(keisan ks) {
      41:         return ks.i;
      42:     }
      43:  
      44:     //int からのキャスト
      45:     public static implicit operator keisan(int i)
      46:     {
      47:         return new keisan(i);
      48:     }
      49:  
      50:     //オペレーター
      51:  
      52:  
      53:     //<
      54:     public static keisan low(int i, keisan ks) {
      55:         keisan result = new keisan(ks);
      56:         result.condition.Add(new range(i, int.MaxValue));
      57:  
      58:         return result;
      59:     }
      60:     public static keisan high(int i, keisan ks)
      61:     {
      62:         keisan result = new keisan(ks);
      63:         result.condition.Add(new range(int.MinValue, i));
      64:  
      65:         return result;
      66:     }
      67:  
      68:     public static keisan operator <(keisan ks, int i) {
      69:         return high(i, ks);
      70:     }
      71:     public static keisan operator <(int i, keisan ks){
      72:         return low(i, ks);
      73:     }
      74:  
      75:     //>
      76:     public static keisan operator >(int i, keisan ks){
      77:         return high(i, ks);
      78:     }
      79:     public static keisan operator >(keisan ks, int i){
      80:         return low(i, ks);
      81:     }
      82:  
      83:     //<=
      84:     //>=
      85: }
      86:  
      87: struct range{
      88:     public range(int min, int max) {
      89:         this.min = min;
      90:         this.max = max;
      91:     }
      92:  
      93:     public int min;
      94:     public int max;
      95: }

    int 型の値と、条件を保持する構造体を作ってみて、

    • 比較演算子をオーバーロードする。
    • int , bool へのキャストを書く
    • int からのキャストを書く(代入用)
    • ToString を書く

     

    細かいことは考えていないが、

       1: keisan k = 100;
       2:  
       3: if (200 > k > 0) {
       4:     MessageBox.Show(""+k);
       5: }

     

    と、書くことができた。


    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://prog-city.spaces.live.com/blog/cns!E5379F11D9E8BB3F!1637.trak
    Weblogs that reference this entry
    • None