#include
<iostream>
#include<cstring>
using namespace std;
class MData
{
private:
int n,m;
char *str1,*str2;
public:
char *mul();//乘法
char *add();//加法
char *sub();//减法
char *div();//除法
MData();
MData(const char *s1,const char * s2);
void init(const char *s1,const char * s2);
~MData();
};
MData::MData()
{
str1=nullptr;
str2 = nullptr;
n = 0;
m = 0;
}
MData::MData(const char *s1, const char *s2)
{
n = (int)strlen(s1);
m = (int)strlen(s2);
str1 = new char[n+1]{'\0'};
str2 = new char[m+1]{'\0'};
strcpy(this->str1,s1);
strcpy(this->str2,s2);
}
void MData::init(const char *s1,s2);
}
MData::~MData()
{
if(str1!=nullptr)
delete[] str1;
if(str2!=nullptr)
delete[] str2;
}
char* MData::mul()//乘法
{
int *s = new int[n+m];
memset(s,0,sizeof(int)*(n+m));
for(int i = 0;i<n;i++)
for(int k = 0;k<m;k++)
{
s[i+k+1] += (str1[i]-'0')*(str2[k]-'0');
}
for(int i = n+m-1;i>=0;i--)
{
if(s[i]>=10)
{
s[i-1] += s[i]/10;
s[i] %= 10;
}
}
int i = 0;
while(s[i] == 0)
{
i++;
if(i == n+m)
return "0";
}
char *sum = new char[n+m-i+1]{'\0'};
int k = 0;
int kn = n+m-i+1;
for(;k<kn&&i<n+m;k++,i++)
{
sum[k] = s[i]+'0';
}
sum[k] = '\0';
delete[] s;
return sum;
}
char *MData::add()//加法
{
int *s,temp;
if(n > m)
{
temp = n;
s = new int[n+1];
memset(s,sizeof(int)*(n+1));
for(int i = 0;i<n;i++)
{
if(i >= n-m)
s[i+1] = (str1[i]-'0')+(str2[i-(n-m)]-'0');
else
s[i+1] = str1[i]-'0';
}
}
else
{
temp = m;
s = new int[m+1];
memset(s,sizeof(int)*(m+1));
for(int i = 0;i<m;i++)
{
if(i >= m-n)
s[i+1] = (str2[i]-'0')+(str1[i-(m-n)]-'0');
else
s[i+1] = str2[i]-'0';
}
}
for(int i = temp;i>=0;i--)
{
if(s[i]>=10)
{
s[i-1] += s[i]/10;
s[i] %= 10;
}
}
int i = 0;
while(s[i] == 0)
{
i++;
if(i == temp+1)
return "0";
}
char *sum = new char[(temp+1)-i+1]{'\0'};
int k = 0;
int kn = (temp+1)-i+1;
for(;k<kn&&i<temp+1;k++,i++)
{
sum[k] = s[i]+'0';
}
sum[k] = '\0';
delete[] s;
return sum;
}
char *MData::sub()
{
int *s,temp;
bool bt = false;
if(n >= m)
{
s = new int[n+1];
temp = n;
memset(s,sizeof(int)*(n+1));
if(n > m)
{
for(int i = 0;i < n;i++)
{
if(i >= n-m)
s[i+1] = (str1[i]-'0')-(str2[i-(n-m)]-'0');
else
s[i+1] = str1[i]-'0';
}
}
else
{
if(strcmp(str1,str2)>0)
{
for(int i = 0;i < n;i++)
{
s[i+1] = (str1[i]-'0')-(str2[i]-'0');
}
}
else
{
bt = true;
for(int i = 0;i < n;i++)
{
s[i+1] = (str2[i]-'0')-(str1[i]-'0');
}
}
}
}
else
{
bt = true;
s= new int[m+1];
temp = m;
memset(s,sizeof(int)*(m+1));
for(int i = 0;i<m;i++)
{
if(i >= m-n)
s[i+1] = (str2[i]-'0')-(str1[i-(m-n)]-'0');
else
s[i+1] = str2[i]-'0';
}
}
for(int i = temp;i>=0;i--)
{
if(s[i] < 0)
{
s[i-1] -= 1;
s[i] += 10;
}
}
int i = 0;
while(s[i] == 0)
{
i++;
if(i == n+1)
{
return "0";
}
}
char *sum;
if(bt)
{
sum = new char[(temp+1)-(i-1)+1]{'\0'};
int k = 0;
int kn = (temp+1)-i+1;
for(;k<kn&&i<temp+1;k++,i++)
{
if(k == 0)
sum[k] = '-';
sum[k+1] = s[i]+'0';
}
sum[k+1] = '\0';
}
else
{
sum = new char[(temp+1)-i+1]{'\0'};
int k = 0;
int kn = (temp+1)-i+1;
for(;k<kn&&i<temp+1;k++,i++)
{
sum[k] = s[i]+'0';
}
sum[k] = '\0';
}
delete[] s;
return sum;
}
char *MData::div()
{
if(n < m)
return "0";
else
{
if(n == m)
{
if(strcmp(str1,str2) < 0)
return "0";
else
{
int i = 0;
char *st = this->sub();
while(st[0]!='-')
{
i++;
strcpy(str1,st);
st = this->sub();
}
char *str = new char[2]{0};
str[0] = i+'0';
str[1] = '\0';
return str;
}
}
else
{
int *s = new int[n];
for(int i = 0;i<n;i++)
s[i] = 0;
int i = 0;
char *str = new char[m]{0};
str = strncpy(str,str1,1);
str[1] = '\0';
char *tp = str1+1;
char *temp = str1;
str1 = str;
while(i<n)
{
if(strlen(str1) < m ||
(strcmp(str1,str2)<0 && strlen(str1) == m))
{
strncpy(str,tp,1);
str[1] = '\0';
str1 = strcat(str1,str);
tp++;
i++;
}
else
{
s[i]++;
str1 = this->sub();
}
if(i == n)
{
break;
}
}
for(int mm = n-1;mm >= 0;mm--)
{
if(s[mm] >= 10)
{
s[mm-1] += s[mm]/10;
s[mm] %= 10;
}
}
int nn = 0;
while(s[nn] == 0)
{
nn++;
if(nn == n)
return "0";
}
char *strchar = new char[n+1 -nn];
memset(strchar,sizeof(char)*(n+1-nn));
int k = 0;
for(;k<n && nn < n;k++,nn++)
{
strchar[k] = s[nn]+'0';
}
strchar[k] = '\0';
delete[] temp;
return strchar;
}
}
}
int main()
{
int i = 0;
while(i<100)
{
i++;
char *str1=new char[10000]{'\0'},*str2=new char[10000]{'\0'};
cin>>str1;
cin>>str2;
MData sk(str1,str2);
char *mul = sk.mul();//乘法
cout << "1:"<< mul << endl;
char *add = sk.add();//加法
cout<<"2:" << add<<endl;
char *sub = sk.sub();//减法
cout<<"3:"<< sub <<endl;
//char *div = sk.div();//除法
//cout<<"4:"<<div<<endl<<endl;
if(i == 100)
{
delete[]str1;
delete[]str2;
}
}
return 0;
}
大数除法还有问题,没有时间去完善,如果你完善了,请评论并回复代码哈,谢谢了
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。