• String类型的二进制数求和


     1 public static string er(string a, string b)
     2         {
     3             int l;
     4             if (a.Length>b.Length)
     5             {
     6                 l = a.Length;
     7             }
     8             else
     9             {
    10                 l = b.Length;
    11             }
    12             char[] c1 = new char[l];
    13             char[] c2 = new char[l];
    14             char p='0';
    15             char[] c3 = new char[l + 1];
    16             for (int j = 0; j < l+1; j++)
    17             {
    18                 c3[j] = '0';
    19             }
    20             StringReader sra = new StringReader(a);
    21             sra.Read(c1, 0, l);
    22             StringReader srb = new StringReader(b);
    23             srb.Read(c2, 0, l);
    24             sra.Close();
    25             srb.Close();
    26             for (int i= l-1; i>=0; i--)
    27             {
    28                 if (c1[i]=='0'&&c2[i]=='0')
    29                 {
    30                     if (p=='1')
    31                     {
    32                         c3[i + 1] = '1';
    33                         p = '0';
    34                     }
    35                     else
    36                     {
    37                         c3[i + 1] = '0';
    38                     }
    39                 }
    40                 else if (!(c1[i] == '1' && c2[i] == '1'))
    41                 {
    42                     if (p == '1')
    43                     {
    44                         c3[i + 1] = '0';
    45                     }
    46                     else
    47                     {
    48                         c3[i + 1] = '1';
    49                     }
    50                 }
    51                 else
    52                 {
    53                     if (p == '1')
    54                     {
    55                         c3[i + 1] = '1';
    56                     }
    57                     else
    58                     {
    59                         c3[i + 1] = '0';
    60                         p = '1';
    61                     }
    62                 }
    63             }
    64             if (p=='1')
    65             {
    66                 c3[0] = '1';
    67             }
    68             StringBuilder c =new StringBuilder("");
    69             StringWriter sw = new StringWriter(c);
    70             sw.Write(c3, 0, l+1);
    71             sw.Close();
    72             return c.ToString();
    73         }
    1 string x = "1010101";
    2 string y = "1101010";
    3 Console.WriteLine(x+"+"+y+"=");
    4 string z = "";
    5 z = er(x, y);
    6 Console.WriteLine(z.ToString());
    7 Console.ReadLine();
  • 相关阅读:
    [读书计划]2015读书计划
    [整理]iOS开发学习
    nginx配置
    Nginx的使用
    Spring
    JSP的使用以及EL和JSTL的使用
    关于linux安装tomcat和mysql
    linux常用操作(安装jdk配置环境变量)
    redis的安装与使用
    Mybatis
  • 原文地址:https://www.cnblogs.com/xingye3327/p/5413892.html
Copyright © 2020-2023  润新知