大数【加减乘除】算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/amd123456789/article/details/78290709

大数加减法,主要处理进位就行。
大数乘法,按照普通的乘法计算方式,写程序便可。
大数除法,实际上就是转换为不断做减法,看从被除数里面最多能减去多少个除数,商就是多少。逐个减显然太慢,要判断一次最多能减少多少个(除数*10的n次方)。

以7546除23为例。
先减去23的100倍,就是2300,可以减3次,余下646。 此时商就是300;
然后646减去23的10倍,就是230,可以减2次,余下186。此时商就是320;
然后186减去23,可以减8次,此时商就是328.

源码如下

#include<stdio.h>

#define MAX (33)
#define rint register int

char t1[MAX];
char t2[MAX];

int a[MAX];
int b[MAX];
int sum[MAX];

int cnt = 0;
bool mark = false;

char btn;

void clearChar(char *str)
{
       if(str == NULL)
             return;
       while(*str != '\0')
             *str++ = 0;
}

void clearInt(int *s)
{
       for(rint i = 0; i < MAX; i++)
             s[i] = 0;
}

void copyChar(char* from, char* to)
{
       clearChar(to);
       if(from == NULL || to == NULL)
             return;
       while(*from != '\0')
             *to++ = *from++;
}

int lenChar(char* str)
{
       int ret = 0;
       if(str == NULL)
             return ret;
       while (*str++ != '\0')
             ret++;
       return ret;
}

int str2int(char* from, int *to)
{
       if(from == NULL || to == NULL)
             return 0;
       clearInt(to);
       int len = lenChar(from);
       for(rint i = 0; i < len; i++)
       {
             to[len - 1 - i] = from[i] - '0';
       }
       return len;
}

void int2str(int *from, char* to, int len)
{
       if(from == NULL || to == NULL)
             return;
       clearChar(to);
       for(int i = 0; i < len; i++)
       {
             to[i] = from[len - 1 - i] + '0';
       }
}

/*加法计算*/
void plusChar(char *s1, char *s2, char *s)
{

       int len1 = str2int(s1, a);
       int len2 = str2int(s2, b);
       int len = len1;
       if(len1 < len2)
       {
             len = len2;
       }
       for(rint i = 0; i < len1 || i < len2; i++)
       {
             sum[i] = a[i] + b[i];
       }
       for(int i = 0; i < len; i++)
       {
             if(sum[i] >= 10 )
             {
                    sum[i + 1]++;
                    sum[i] %= 10;
                    if(i == len - 1)
                    {
                           len++;
                           break;
                    }
             }
       }

       int2str(sum, s, len);
}

/*减法计算*/
void minusChar(char* s1, char* s2, char* s)
{
       int len1 = str2int(s1, a);
       int len2 = str2int(s2, b);
       for(rint i = 0; i < len1; i++)
       {
             sum[i] = a[i] - b[i];
       }
       for(rint i = 0; i < len1 || i < len2; i++)
       {
             if(sum[i] < 0 )
             {
                    sum[i + 1]--;
                    sum[i] += 10;
             }
             else if(sum[i] == 0 && i == len1 - 1)
             {
                    len1--;
                    break;
             }
       }
       for(int i = len1 - 1; i >= 0; i--)
       {
             if(sum[i] == 0)
                    len1 --;
             else
                    break;
       }
       if(len1 == 0)
             len1 = 1;

       int2str(sum, s, len1);
}

/*减法计算*/
int minusChar(int* s1, int len1, int* s2, int len2)
{
       if(len1 < len2)
       {
             return -1;
       }
       else if(len1 == len2)
       {
             for(int i = len1 - 1; i >= 0; i--)
             {
                    if(s1[i] > s2[i])
                           break;
                    else if(s1[i] < s2[i])
                           return -1;
                    else
                           continue;
             }
       }
       for(int i = 0; i < len1; i++)
       {
             s1[i] -= s2[i];
             if(s1[i] < 0)
             {
                    s1[i]+=10;
                    s1[i+1]--;
             }
       }
       for(int i = len1 - 1; i >=0; i--)
       {
             if(s1[i])
                    return (i+1);
       }
       return 0;
}

/*除法计算,实际上就是转化为减法*/
void divideChar(char* s1, char* s2, char* s)
{
       int len1 = str2int(s1, a);
       int len2 = str2int(s2, b);
       int times = len1 - len2;
       int c[MAX] = {0};
       int d[MAX] = {0};
       int len = 1;
       bool flag = false;
       for(rint i = times; i >= 0; i--)
       {
             clearInt(d);
             for(rint j = len2 - 1; j >= 0; j--)
                    d[j + i] = b[j];
             for(rint j = 0; j < i; j++)
                    d[j] = 0;
             int temp = 0;
             while((temp = minusChar(a, len1, d, len2+i)) >= 0)
             {
                    c[i]++;
                    len1 = temp;
             }
             if(c[i] != 0 && flag == false)
             {
                    len = i+1;
                    flag = true;
             }
       }
       int2str(c, s, len);
}

/*乘法计算*/
void multiply(char* s1, char* s2, char* s)
{
       int len1 = str2int(s1, a);
       int len2 = str2int(s2, b);
       int c[MAX*2] = {0};
       for(rint i = 0; i < len1; i++)
       {
             for(rint j = 0; j < len2; j++)
             {
                    c[i+j] += a[i] * b[j];
             }
       }
       for(rint i = 0; i < len1+len2; i++)
       {
             c[i+1] += c[i]/10;
             c[i] = c[i]%10;
       }
       int len = 1;
       for(rint i = len1+len2; i>=0; i--)
       {
             if(c[i])
             {
                    len = i+1;
                    break;
             }
       }
       int2str(c, s, len);
}
/*处理函数*/
void handle(char button,char* t1, char* t2, char* display)
{
       if(lenChar(t1) == 0)
       {
             copyChar(t2, display);
             btn = button;
             return;
       }
       if(btn == '+')
       {
             plusChar(t1, t2, display);
       }
       else if(btn == '-')
       {
             minusChar(t1, t2, display);
       }
       else if(btn == '/')
       {
             divideChar(t1, t2, display);
       }
       else if (btn == '*')
       {
             multiply(t1, t2, display);
       }
       btn = button;
       clearChar(t1);
       copyChar(display, t2);
}
void Run(char button, char display[32 + 1])
{
       if(button >= '0' && button <= '9')
       {
             if(cnt == 0)
             {
                    copyChar(t2, t1);
                    clearChar(t2);
                    clearChar(display);
             }
             t2[cnt] = button;
             display[cnt] = button;
             cnt++;
             return;
       }
       mark = false;
       cnt = 0;
       if(button == 'C')
       {
             clearChar(display);
             clearChar(t1);
             clearChar(t2);
             clearInt(a);
             clearInt(b);
             clearInt(sum);
             display[0] = '0';
             return;
       }
       else
       {
             handle(button, t1, t2, display);
       }
}

没有更多推荐了,返回首页