C编程题(8)

 

      题目:

     输入两个正整数X,Y,将X,Y化为二进制数,然后将这两个二进制数作二进制加法运算,再将结果化为十进制数输出。

[C#]

        static int N = 18;
        static void Main(string[] args)
        {
            int[] bx = new int[N];
            int[] by = new int[N];
            int[] br = new int[N];
            string sx="", sy="", sr="";
            int x = 12345, y = 567;

            DtoB(x, bx, ref sx);
            DtoB(y, by, ref sy);
            //DtoB(x+y, br, ref sr);
            BAdd(bx, by, br, ref sr);

            Console.WriteLine("    x {0,18}", sx);
            Console.WriteLine("+   y {0,18}", sy);
            for (int i = 0; i < 24; i++)
                Console.Write("-");
            Console.Write("/n");
            Console.WriteLine("Result{0,18}", sr);

            Console.WriteLine("{0}+{1}={2}", x, y, BtoD(br));
            Console.ReadLine();
        }

        static void DtoB(int num, int[] tmb,ref string sb)
        {
            int i = 0;
            while (num > 0)
            {
                tmb[i] = num & 1;
                num = num >> 1;
                sb = tmb[i++].ToString() + sb;
            }
            tmb[i] = -1;
        }

        static void BAdd(int[] tmbx, int[] tmby, int[] tmbr,ref string tmsr)
        {
            bool b1=true, b2=true;
            int i=0;
            while((b1 || b2)&& i<N)
            {
                if (tmbx[i] == -1)
                    b1 = false;
                if (tmby[i] == -1)
                    b2 = false;

                if (b1 && !b2)
                    tmbr[i] = tmbx[i];
                if (!b1 && b2)
                    tmbr[i] = tmby[i];
                if (b1 && b2)
                    tmbr[i] = tmbx[i] + tmby[i];
                if (!b1 && !b2)
                    tmbr[i] = -1;
                i++;
            }

            i = 0;
            while (i < N)
            {
                if (tmbr[i] == -1)
                    break;               
                if (tmbr[i] >= 2)                  
                    tmbr[i + 1]++;
                tmbr[i] %= 2;   
                tmsr = tmbr[i].ToString() + tmsr;
                i++;
            }   
        }

        static int BtoD(int[] tmbarr)
        {
            int sum=0;
            for (int i = 0; i < N; i++)
            {
                if (tmbarr[i] == -1)
                    break;
                sum += tmbarr[i] *(int) Math.Pow(2.0, i);
            }
            return sum;
        }

[C++]

#include "stdio.h"
#include "math.h"
#include <stdlib.h>
#define N 18

void dtob(unsigned int, int*, char*);//10==>2
unsigned int  btod(int*);//2==>10
void badd(int*, int*, int*);
void show(int*, char*);

void main()
{
    unsigned int x, y;
    static int xb[N], yb[N], resultb[N], i;
    printf("input the decimal value X and Y:");
    scanf("%d %d", &x, &y);
    dtob(x, xb, "X");
    dtob(y, yb, "Y");
    badd(xb, yb, resultb);
    printf("/nResult convert to be decimal is:%4d/n", btod(resultb));

 system("pause");
}
void dtob(unsigned int n, int *nb, char *s)
{
    int i = N;
    while(n)
    {
        nb[--i] = n & 1;
        n >>= 1;
    }

    nb[--i] = -1;
    show(nb, s);
}
void badd(int *xb, int *yb, int *resultb)
{
    int i = 0;
    while(xb[i] != -1 && yb[i] != -1)
        i++;
    resultb[i++] = -1;
    for(; i < N; i++)
    {
        if(xb[i] == -1)
            resultb[i] = yb[i];
        else if(yb[i] == -1)
            resultb[i] = xb[i];
        else
            resultb[i] = xb[i] + yb[i];
    }
    for(i = N; resultb[i] != -1; i--)
    {
        if(resultb[i] > 1)
        {
            resultb[i] %= 2;
            if(resultb[i-1] == -1)
            {
                resultb[i-1] = 1;
                resultb[i-2] = -1;
            }
            else
                resultb[i-1] += 1;
        }
    }
    show(resultb, "Result");
}
unsigned int btod(int* nb)
{
    static unsigned int i, sum;
    for(; nb[i] != -1; i++);
    for(++i; i < N; i++)
 {
  int tmi=N-1-i;
        //sum += nb[i]*(int)pow(2, N-1-i);
  sum += nb[i]*pow(2.0, tmi);
 }
    return sum;
}

void show(int *nb, char *s)
{
    int i;
    printf("/n%s/t= ", s);
    for(i = 0; nb[i] != -1; i++);
    for(++i; i < N; i++)
        printf("%d", nb[i]);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值