C#高精度实现(半成品)

准备开学了没什么时间写了 只实现了+号的重载 其他的以后再说 using System; using System.Text; namespace high_precision_calculate { class number { public number(string str) { v = new StringBuilder(str); } public number() { v = new StringBuilder(0); } public StringBuilder v { get; set; } public static number operator +(number l, number r) { //补2位 int ll = l.v.Length; int rl = r.v.Length; int sub = ll - rl; if (sub > 0) { r.v.Insert(0, new string('0', sub + 1)); l.v.Insert(0, new string('0', 1)); } else if (sub < 0) { l.v.Insert(0, new string('0', -sub + 1)); r.v.Insert(0, new string('0', 1)); } //逐位加 int count = l.v.Length;//因为不知道补的是哪个,所以要重新获取长度 char[] result = new char[count + 1];//result多一位 bool flag = false; for (int i = count - 1; i >= 0; i--) { int a = Convert.ToInt32(l.v[i]) - 48; int b = Convert.ToInt32(r.v[i]) - 48; int c = a + b; if (flag) { c++; flag = false; }; if (c >= 10) { flag = true; c -= 10; } result[i] = Convert.ToChar(c + 48); } return new number(Convert.ToInt32(new string(result)).ToString()); } public static number operator *(number l, number r) { //TODO number result = new number(); return result; } } }

你可能感兴趣的:(C#)