标签:
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();
标签:
原文地址:http://www.cnblogs.com/xingye3327/p/5413892.html