| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 705 人关注过本帖
标题:【求简洁代码】:定义一个大整数类,进行 100 位加减运算。
只看楼主 加入收藏
lxsxd
Rank: 5Rank: 5
等 级:贵宾
威 望:14
帖 子:153
专家分:357
注 册:2014-4-15
结帖率:96.15%
收藏
已结贴  问题点数:100 回复次数:2 
【求简洁代码】:定义一个大整数类,进行 100 位加减运算。
一、题目:定义一个大整数类,进行100位加减运算。
二、我的努力
经过2个星期的努力,终于朴素地完成了。代码很长,不简洁。下面代码只有加法运算(减法运算添上,超出论坛要求的字数)
namespace 书本练习题
{
    public class BigInteger
    {
        public char[] chars;
        public BigInteger()           //无参构造函数
        {
        }
        public BigInteger(int i)      //有参构造函数
        {
            this.chars = new char[i];  
        }
        /// <summary>
        ///大整数相加 运算符+重载
        /// </summary>
        public static BigInteger operator +(BigInteger one, BigInteger two)
        {
            BigInteger bigIntThree = new BigInteger();

            #region   //两个数都是正数
            if (one.chars[0] == '+' && two.chars[0] == '+')   //两个大整数都是正数
            {
                int temp1 = one.chars.Length - 1;
                int temp2 = two.chars.Length - 1;
                int temp = (Math.Max(temp1, temp2)) + 1 + 1;  //1位用于保存+/-,1位用于保存最高位相加向前进1;
                bigIntThree.chars = new char[temp];
                bigIntThree.chars[0] = '+';

                int x, y, z, n = 0;

                for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                {

                    if (i - ((temp - 1) - temp1) >= 1)
                    {
                        x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                    }
                    else
                    {
                        x = 0;
                    }
                    if (i - ((temp - 1) - temp2) >= 1)
                    {
                        y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                    }
                    else
                    {
                        y = 0;
                    }

                    z = x + y + n;

                    if (z < 10)
                    {
                        bigIntThree.chars[i] = Convert.ToChar(z + 48); ;
                        n = 0;
                    }
                    else
                    {
                        bigIntThree.chars[i] = Convert.ToChar(z - 10 + 48); ;
                        n = z / 10;
                    }
                }
            }
            #endregion

            #region  //两个数都是负数

            if (one.chars[0] == '-' && two.chars[0] == '-')   //两个大整数都是负数
            {
                int temp1 = one.chars.Length - 1;
                int temp2 = two.chars.Length - 1;
                int temp = (Math.Max(temp1, temp2)) + 1 + 1;//1位用于保存+/-,1位用于保存最高位相加向前进1;
                bigIntThree.chars = new char[temp];
                bigIntThree.chars[0] = '-';
                int x, y, z, n = 0;
                for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                {

                    if (i - ((temp - 1) - temp1) >= 1)
                    {
                        x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                    }
                    else
                    {
                        x = 0;
                    }
                    if (i - ((temp - 1) - temp2) >= 1)
                    {
                        y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                    }
                    else
                    {
                        y = 0;
                    }

                    z = x + y + n;

                    if (z < 10)
                    {
                        bigIntThree.chars[i] = Convert.ToChar(z + 48); ;
                        n = 0;
                    }
                    else
                    {
                        bigIntThree.chars[i] = Convert.ToChar(z - 10 + 48); ;
                        n = z / 10;
                    }
                }
            }
            #endregion
            #region  //第一个是负数,第二个是正数
            if (one.chars[0] == '-' && two.chars[0] == '+')   //第一个是负数,第二个是正数
            {
                int temp1 = one.chars.Length - 1;
                int temp2 = two.chars.Length - 1;

                int temp = (Math.Max(temp1, temp2)) + 1 + 1;//1位用于保存+/-;
                bigIntThree.chars = new char[temp];
                if (temp1 > temp2)
                {
                    bigIntThree.chars[0] = '-';   //负数大,结果为负数
                    int x, y, z, n = 0;
                    for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                    {
                        if (i - ((temp - 1) - temp1) >= 1)
                        {
                            x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                        }
                        else
                        {
                            x = 0;
                        }
                        if (i - ((temp - 1) - temp2) >= 1)
                        {
                            y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                        }
                        else
                        {
                            y = 0;
                        }
                        if ((x - n) >= y)
                        {
                            z = x - y - n;
                            bigIntThree.chars[i] = Convert.ToChar(z + 48);
                            n = 0;
                        }
                        else
                        {
                            z = x + 10 - y - n;
                            bigIntThree.chars[i] = Convert.ToChar(z + 48);
                            n = 1;
                        }
                    }

                }
                else if (temp1 < temp2)
                {
                    bigIntThree.chars[0] = '+';   //正数大,结果为正数
                    int x, y, z, n = 0;
                    for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                    {
                        if (i - ((temp - 1) - temp2) >= 1)
                        {
                            x = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                        }
                        else
                        {
                            x = 0;
                        }
                        if (i - ((temp - 1) - temp1) >= 1)
                        {
                            y = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                        }
                        else
                        {
                            y = 0;
                        }
                        if ((x - n) >= y)
                        {
                            z = x - y - n;
                            bigIntThree.chars[i] = Convert.ToChar(z + 48);
                            n = 0;
                        }
                        else
                        {
                            z = x + 10 - y - n;
                            bigIntThree.chars[i] = Convert.ToChar(z + 48);
                            n = 1;
                        }
                    }

                }
                else   //数位相等
                {
                    for (int m = 1; m < temp1; m++)
                    {
                        if (one.chars[m] > two.chars[m])
                        {
                            bigIntThree.chars[0] = '-';
                            int x, y, z, n = 0;
                            for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                            {
                                if (i - ((temp - 1) - temp1) >= 1)
                                {
                                    x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                                }
                                else
                                {
                                    x = 0;
                                }
                                if (i - ((temp - 1) - temp2) >= 1)
                                {
                                    y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                                }
                                else
                                {
                                    y = 0;
                                }
                                if ((x - n) >= y)
                                {
                                    z = x - y - n;
                                    bigIntThree.chars[i] = Convert.ToChar(z + 48);
                                    n = 0;
                                }
                                else
                                {
                                    z = x + 10 - y - n;
                                    bigIntThree.chars[i] = Convert.ToChar(z + 48);
                                    n = 1;
                                }
                            }
                            break;
                        }
                        else if (one.chars[m] < two.chars[m])
                        {
                            bigIntThree.chars[0] = '+';
                            int x, y, z, n = 0;
                            for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                            {
                                if (i - ((temp - 1) - temp2) >= 1)
                                {
                                    x = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                                }
                                else
                                {
                                    x = 0;
                                }
                                if (i - ((temp - 1) - temp1) >= 1)
                                {
                                    y = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                                }
                                else
                                {
                                    y = 0;
                                }
                                if ((x - n) >= y)
                                {
                                    z = x - y - n;
                                    bigIntThree.chars[i] = Convert.ToChar(z + 48);
                                    n = 0;
                                }
                                else
                                {
                                    z = x + 10 - y;
                                    bigIntThree.chars[i] = Convert.ToChar(z + 48);
                                    n = 1;
                                }
                            }
                            break;
                        }
                        else
                        {
                            ;
                        }
                    }
                }
            }
            #endregion
            #region  //第一个是正数,第二个是负数
            if (one.chars[0] == '+' && two.chars[0] == '-')   //第一个是正数,第二个是负数
            {
                int temp1 = one.chars.Length - 1;
                int temp2 = two.chars.Length - 1;
                int temp = (Math.Max(temp1, temp2)) + 1 + 1;//1位用于保存+/-;
                bigIntThree.chars = new char[temp];
                if (temp1 > temp2)
                {
                    bigIntThree.chars[0] = '+';   //正数大,结果为正数
                    int x, y, z, n = 0;
                    for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                    {
                        if (i - ((temp - 1) - temp1) >= 1)
                        {
                            x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                        }
                        else
                        {
                            x = 0;
                        }
                        if (i - ((temp - 1) - temp2) >= 1)
                        {
                            y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                        }
                        else
                        {
                            y = 0;
                        }

                        if ((x - n) >= y)
                        {
                            z = x - y - n;
                            Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
                            bigIntThree.chars[i] = Convert.ToChar(z + 48);
                            n = 0;
                        }
                        else
                        {
                            z = x + 10 - y - n;
                            Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
                            bigIntThree.chars[i] = Convert.ToChar(z + 48);
                            n = 1;
                        }
                    }

                }
                else if (temp1 < temp2)
                {
                    bigIntThree.chars[0] = '-';   //正数小,结果为负数
                    int x, y, z, n = 0;
                    for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                    {
                        if (i - ((temp - 1) - temp2) >= 1)
                        {
                            x = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                        }
                        else
                        {
                            x = 0;
                        }
                        if (i - ((temp - 1) - temp1) >= 1)
                        {
                            y = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                        }
                        else
                        {
                            y = 0;
                        }

                        if ((x - n) >= y)
                        {
                            z = x - y - n;
                            Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
                            bigIntThree.chars[i] = Convert.ToChar(z + 48);
                            n = 0;
                        }
                        else
                        {
                            z = x + 10 - y - n;
                            Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
                            bigIntThree.chars[i] = Convert.ToChar(z + 48);
                            n = 1;
                        }
                    }

                }
                else   //数位相等
                {
                    for (int m = 1; m < temp1; m++)
                    {
                        if (one.chars[m] > two.chars[m])
                        {
                            bigIntThree.chars[0] = '+';
                            int x, y, z, n = 0;
                            for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                            {
                                if (i - ((temp - 1) - temp1) >= 1)
                                {
                                    x = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                                }
                                else
                                {
                                    x = 0;
                                }
                                if (i - ((temp - 1) - temp2) >= 1)
                                {
                                    y = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                                }
                                else
                                {
                                    y = 0;
                                }

                                if ((x - n) >= y)
                                {
                                    z = x - y - n;
                                    Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
                                    bigIntThree.chars[i] = Convert.ToChar(z + 48);
                                    n = 0;
                                }
                                else
                                {
                                    z = x + 10 - y - n;
                                    Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
                                    bigIntThree.chars[i] = Convert.ToChar(z + 48);
                                    n = 1;
                                }
                            }
                            break;
                        }
                        else if (one.chars[m] < two.chars[m])
                        {
                            bigIntThree.chars[0] = '-';
                            int x, y, z, n = 0;
                            for (int i = temp - 1; i >= temp - 1 - (Math.Max(temp1, temp2)); i--)
                            {
                                if (i - ((temp - 1) - temp2) >= 1)
                                {
                                    x = Convert.ToInt32(two.chars[i - ((temp - 1) - temp2)]) - 48;
                                }
                                else
                                {
                                    x = 0;
                                }
                                if (i - ((temp - 1) - temp1) >= 1)
                                {
                                    y = Convert.ToInt32(one.chars[i - ((temp - 1) - temp1)]) - 48;
                                }
                                else
                                {
                                    y = 0;
                                }

                                if ((x - n) >= y)
                                {
                                    z = x - y - n;
                                    Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
                                    bigIntThree.chars[i] = Convert.ToChar(z + 48);
                                    n = 0;
                                }
                                else
                                {
                                    z = x + 10 - y - n;
                                    Console.WriteLine("x={0},y={1},z={2},n={3}:", x, y, z, n);
                                    bigIntThree.chars[i] = Convert.ToChar(z + 48);
                                    n = 1;
                                }
                            }
                            break;
                        }
                        else
                        {
                            ;
                        }
                    }
                }
            }
            #endregion
            return bigIntThree;
        }
       public void show()
        {
            int x=0;
            for (int i = 0; i < chars.Length; i++)
            {
                    Console.Write("{0}", chars[i]);
                    x++;
            }
            Console.WriteLine("这个数是{0}位数。", x-1);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入第一个大整数,正数以+开头,负数以-开头:");
            string strOneNumber = Console.ReadLine();
            BigInteger bigIntOne = new BigInteger();
            bigIntOne.chars = strOneNumber.ToCharArray();
            bigIntOne.show();
            Console.WriteLine("请输入第二个大整数,正数以+开头,负数以-开头:");
            string strTwoNumber = Console.ReadLine();
            BigInteger bigIntTwo = new BigInteger();
            bigIntTwo.chars = strTwoNumber.ToCharArray();
            bigIntTwo.show();
            //100位大整数+运算
            BigInteger bigIntThree;
            bigIntThree = bigIntOne + bigIntTwo;
            bigIntThree.show();
            //100位大整数-运算
            BigInteger bigIntFour;
            bigIntFour = bigIntOne - bigIntTwo;
            bigIntFour.show();
            Console.ReadKey();
        }
    }
}
三、美中不足的地方是:
1)输入大整数,正数要以+开头输入,负数要以-开头输入;
2)最后的计算结果是,相加最高位没有进位也有一个空位,相减高位等于0时也显示。

[ 本帖最后由 lxsxd 于 2014-9-7 22:38 编辑 ]
搜索更多相关主题的帖子: 练习题 public 
2014-09-07 22:32
邓士林
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:淮河河畔
等 级:贵宾
威 望:61
帖 子:2392
专家分:13384
注 册:2013-3-3
收藏
得分:35 
不错,判断挺多,有空考虑考虑给你回复

Maybe
2014-09-09 10:32
午夜小学徒
Rank: 2
等 级:论坛游民
威 望:3
帖 子:52
专家分:40
注 册:2014-7-17
收藏
得分:35 
好长好长 啊!!!可惜与我编的cocos比了
2014-09-11 07:17
快速回复:【求简洁代码】:定义一个大整数类,进行 100 位加减运算。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.042824 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved