RT,假定数字位很长 整形位不够用 利用字符串相加方式 怎么用c#实现:
方法写到类里面;

解决方案 »

  1.   

    用decimal进行处理,
    decimal d1 =0;
    decimal  d2 =5;
    decimal d3;
    d3 =d1+d2;
      

  2.   

    这个什么意思??decimal类型够长?
      

  3.   

    http://www.google.com.hk/search?hl=zh-CN&newwindow=1&safe=strict&biw=1260&bih=837&q=%E9%AB%98%E7%B2%BE%E5%BA%A6%E5%A4%A7%E6%95%B0%E8%BF%90%E7%AE%97&aq=f&aqi=&aql=&oq=
      

  4.   

    参考
    http://topic.csdn.net/u/20090701/09/918423b3-fc8b-4446-b8c2-67a9488bc377.html
      

  5.   

    long a="121212121212121";
    long b="212121212121212";
    String c=a.toString()+b.toString();
      

  6.   


    ///我蛋疼写一个,没考虑有负数的情况
                string a0 = "14536544";
                string a2 = "647811351545481";            string a1=a0.PadLeft(a2.Length, '0');
                List<int> result = new List<int>();            int head = 0;//进位
                for (int i = a2.Length - 1; i >= -1; i--)
                {
                    if (i == -1)
                    {
                        if(head!=0)result.Add(head);
                        break;
                    }
                    int ha = int.Parse(a1[i].ToString());
                    int xi = int.Parse(a2[i].ToString());
                    if (ha + xi + head < 10)
                    {
                        result.Add(ha + xi + head);
                        head = 0;
                    }
                    else
                    {
                        result.Add(ha + xi + head - 10);
                        head = 1;
                    }
                }
                result.Reverse();
               string strResult= result.Aggregate<int, string>("",(agg,next)=>agg+next);
               Console.WriteLine(strResult);
      

  7.   

    长整型用double,长度比decimal大得多...整型不需要小数精度...再长.NET 4.0有专门的长整型System.Numerics.BigInteger结构,理论上支持无限大...
      

  8.   

    以前发的贴,之后就忘了再看了,谢谢大家的回复啊;
    现在我把之后自己写出来的代码贴出来 如果有需要的童鞋可以看看;
    包括字符相加减(小数和负数均可),乘法没弄出来 有待大虾指教!using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace jiafa
    {
        class Program
        {
            static void Main(string[] args)
            {
                Shuju sj = new Shuju(); //定义一个Shuju类对象sj;
                Yuansuan js = new Yuansuan();    //定义一个Yuansuan类对象js;
                Console.WriteLine("求两个长精度型的字符运算结果:");
                do
                {
                    Console.WriteLine("请输入运算符:加法(“+”)减法(“-”)乘法(“*”)");
                    char x=Convert.ToChar(Console.ReadLine());
                    Console.WriteLine("输入第一个数据:");
                    sj.S1 = Console.ReadLine();
                    Console.WriteLine("输入第二个数据:");
                    sj.S2 = Console.ReadLine();
                    switch (x)
                    {
                        case '+':
                            Console.Write("{0}+{1}=", sj.S1, sj.S2);
                            sj.Change();
                            Console.WriteLine(js.Jsresult(sj.S1, sj.S2, '+'));
                            break;
                        case '-':
                            Console.Write("{0}-{1}=", sj.S1, sj.S2);
                            sj.Change();
                            Console.WriteLine(js.Jsresult(sj.S1, sj.S2, '-'));
                            break;
                        case '*':
                            Console.WriteLine("由于技术原因乘法运算尚待完成!");
                            break;
                        default:
                            Console.WriteLine("符号输入不正确!");
                            break;
                    }
                    Console.WriteLine("继续(y);结束(n)");
                    string b = Console.ReadLine();
                    if (b != "y") { break; }
                }while(true);
                Console.WriteLine("请按任意键结束!");
                Console.Read();
            }
        }
        class Shuju
        {
            private string _s1;     
            private string _s2;
            public string S1
            {
                get { return _s1; }
                set { _s1 = value; }
            }
            public string S2
            {
                get { return _s2; }
                set { _s2 = value; }
            }
            public void Change() //调整两个字符串的位数
            {
                int m = Math.Abs(S1.Length - S2.Length);
                int d1 = S1.IndexOf('.');
                int d2 = S2.IndexOf('.');
                #region 两个都是整数
                if (d1 == -1 && d2 == -1)
                {
                    if (S1.Length > S2.Length)
                    {
                        S2=ChPoint(S2,S1.Length-S2.Length);
                    }
                    else if (S1.Length < S2.Length)
                    {
                        S1 = ChPoint(S1, S2.Length - S1.Length);
                    }            }
                #endregion
                #region 两个都是小数
                else if (d1 != -1 & d2 != -1)
                {
                    string la = S1.Substring(0, d1);
                    string ra = S1.Substring(d1 + 1);
                    string lb = S2.Substring(0, d2);
                    string rb = S2.Substring(d2 + 1);
                    if (la.Length > lb.Length)
                    {
                        lb = ChPoint(lb,la.Length - lb.Length);
                    }
                    else if(la.Length<lb.Length)
                    {
                        la = ChPoint(la,lb.Length - la.Length);
                    }                if(ra.Length>rb.Length)
                    {
                        rb=ChPoint1(rb,ra.Length-rb.Length);
                    }
                    else if(ra.Length<rb.Length)
                    {
                        ra=ChPoint1(ra,rb.Length-ra.Length);
                    }                S1=la+"."+ra;
                    S2=lb+"."+rb;
                }
                #endregion
                #region 一个是小数
                else if (d1 != -1 & d2 == -1)
                {
                    string la = S1.Substring(0, d1);
                    string ra = S1.Substring(d1 + 1);
                    string lb = S2;
                    string rb = "";
                    if (la.Length > lb.Length)
                    {
                        lb = ChPoint(lb, la.Length - lb.Length);
                    }
                    else if (la.Length < lb.Length)
                    {
                        la = ChPoint(la, lb.Length - la.Length);
                    }
                    rb = ChPoint1(rb, ra.Length - rb.Length);                S1 = la + "." + ra;
                    S2 = lb + "." + rb;
                }
                else if (d1 == -1 & d2 != -1)
                {
                    
                    string la = S1;
                    string ra = "";
                    string lb = S2.Substring(0,d2);
                    string rb = S2.Substring(d2+1);
                    if (la.Length > lb.Length)
                    {
                        lb = ChPoint(lb, la.Length - lb.Length);
                    }
                    else if (la.Length < lb.Length)
                    {
                        la = ChPoint(la, lb.Length - la.Length);
                    }
                    ra = ChPoint1(ra, rb.Length - ra.Length);                S1 = la + "." + ra;
                    S2 = lb + "." + rb;
                    
                }
                #endregion
            }
            private string ChPoint(string a,int b)
            {  
                for (int i = 0; i < b; i++)
                {
                    a = a.Insert(0, "0");
                }
                return a;
            }  //小数点前面凑0
            private string ChPoint1(string a,int b)
            {
                for (int i = 0; i < b; i++)
                {
                    a = a.Insert(a.Length,"0");
                }
                return a;
            } //小数点后面凑0
        }    public class Yuansuan
        {
            public string Jsresult(string SS1, string SS2, char y) 
            {
                //int m= S1.Length > S2.Length ? S1.Length : S2.Length;
                char[] ch1 = SS1.ToCharArray();
                char[] ch2 = SS2.ToCharArray();
                string result = "";
                int flag=0;            #region 加法运算
                if (y == '+')
                {
                    for (int i = ch1.Length - 1; i >= 0; i--)
                    {
                        if (ch1[i].ToString() != ".")
                        {
                            int a = Convert.ToInt32(ch1[i].ToString());
                            int b = Convert.ToInt32(ch2[i].ToString());
                            int s = a + b + flag;
                            if (s >=10)
                            {
                                s = s -10;
                                flag = 1;
                            }
                            else { flag = 0; }
                            result = s + result;
                            //int j=(s>=10?(s/10):0);
                        }
                        else
                        {
                            result = '.' + result;
                        }
                    }
                    if (flag == 1)
                    {
                        result = 1 + result;
                    }
                    while (result[result.Length - 1] == '0' || result[result.Length - 1] == '.')
                    {
                        if (result[result.Length - 1] != '.')
                        {
                           result=result.Remove(result.Length-1,1);
                        }
                        else if (result[result.Length - 1] == '.')
                        {
                            result = result.Remove(result.Length-1, 1);
                            break;
                        }
                    }
                }
                #endregion
                #region 减法运算
                else if (y == '-')
                {
                    int j=0,s=0;
                    do
                    {
                        if (ch1[j] != '.')
                        {
                            s = Convert.ToInt32(ch1[j].ToString()) - Convert.ToInt32(ch2[j].ToString());
                        }
                            j++;
                    } while (s == 0 && j < ch1.Length);
                    if (s >= 0)
                    {
                        result = Plus(ch1, ch2);
                    }
                    else if (s < 0)
                    {
                        result = "-" + Plus(ch2, ch1);
                    }
                }
                #endregion
                #region 乘法运算
                #endregion
                return result;
            }//方法;计算两个凑0后的字符串运算结果
            private  string Plus(char[] x,char[] y) 
            {
                string re = "";
                int flag = 0;
                for (int i = x.Length - 1; i >= 0; i--)
                {
                    if (x[i].ToString() != ".")
                    {
                        int a = Convert.ToInt32(x[i].ToString());
                        int b = Convert.ToInt32(y[i].ToString());
                        int s = a - b + flag;
                        if (s < 0)
                        {
                            s = s + 10;
                            flag = -1;
                        }
                        else { flag = 0; }
                        re = s + re;
                        //int j=(s>=10?(s/10):0);
                    }
                    else
                    {
                        re = '.' + re;
                    }
                }
                while (re[0] == '0'&&re[1]!='.') 
                {
                    re = re.Remove(0, 1);
                }
                while (re[re.Length - 1] == '0' || re[re.Length - 1] == '.')
                {
                    if (re[re.Length - 1] != '.')
                    {
                        re = re.Remove(re.Length - 1, 1);
                    }
                    else if (re[re.Length - 1] == '.')
                    {
                        re = re.Remove(re.Length - 1, 1);
                        break;
                    }
                }
                return re;
            }  // 两个字符数组相减方法
        }
    }
      

  9.   

    你这是啥回答啊?告诉人家 用GOOGLE搜 高精度大数运算!
    鄙视你,还微软MVP呢
      

  10.   

     TO caozhy:你看人家jiang_zm2009多实在,一上来就很生猛,代码都贴上了
      

  11.   

    你还是考虑下.net4.0里现成的方法吧
      

  12.   

    神马数据那么长。long 整形都不够吗?