64,646
社区成员
发帖
与我相关
我的任务
分享
/****************************************************************************************
大数相加
调用形式:N.Add(A)
返回值:N+A
****************************************************************************************/
CBigInt CBigInt::Add(CBigInt& A)
{
CBigInt X;
X.Mov(*this);
unsigned carry=0;
unsigned __int64 sum=0;
if(X.m_nLength<A.m_nLength)X.m_nLength=A.m_nLength;
for(unsigned i=0;i<X.m_nLength;i++)
{
sum=A.m_ulValue[i];
sum=sum+X.m_ulValue[i]+carry;
X.m_ulValue[i]=(unsigned long)sum;
carry=(unsigned)(sum>>32);
}
X.m_ulValue[X.m_nLength]=carry;
X.m_nLength+=carry;
return X;
}
CBigInt CBigInt::Add(unsigned long A)
{
CBigInt X;
X.Mov(*this);
unsigned __int64 sum;
sum=X.m_ulValue[0];
sum+=A;
X.m_ulValue[0]=(unsigned long)sum;
if(sum>0xffffffff)
{
unsigned i=1;
while(X.m_ulValue[i]==0xffffffff){X.m_ulValue[i]=0;i++;}
X.m_ulValue[i]++;
if(m_nLength==i)m_nLength++;
}
return X;
}
#include <cstdlib>
#include <iostream>
using namespace std;
struct BigNum
{
char string[100];
int len;
int istr[100];
}a, b, c;
void CalLen(BigNum &a)
{
a.len = 0;
for(int i = 0; a.string[i] != '\0'; ++i, ++a.len);
}
void char_to_int(BigNum &a)
{
for(int i = 0; i <= a.len; ++i)
a.istr[i] = a.string[i] - 48;
}
void zero(BigNum &a)
{
for(int i = 0; i < a.len; ++i)
a.istr[i] = 0;
}
/////////////////////////////////////////////////////////////////
void CalPlus(BigNum &c, BigNum &a, BigNum &b)
{
c.len = (a.len>=b.len?a.len:b.len);
for(int i = 0; i <= c.len; ++i)
{
c.istr[i] += a.istr[i] + b.istr[i];
if(c.istr[i] >= 10)
{
c.istr[i - 1] += c.istr[i]%10;
c.istr[i]/= 10;
}
// printf("%d ", c.istr[i]);
}
}
void print(BigNum & a)
{
for(int i = 0; i < a.len; ++i)
{
printf("%d ", a.istr[i]);
}
printf("\n");
}
////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
printf("input a and b:\n");
scanf("%s", &a.string);
scanf("%s", &b.string);
CalLen(a);
CalLen(b);
zero(a);
zero(b);
char_to_int(a);
char_to_int(b);
CalPlus(c, a, b);
printf("The result is: \n");
print(c);
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}
以前写的,楼主看看,没考虑负数、小数的情况,不过应该差不多
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string one(1000,'9');
string two(800,'8');
int dalt=abs(one.size()-two.size());
string temp(dalt,'0');
int maxsize=one.size()>=two.size()?one.size()+1:two.size()+1;
if(one.size()>=two.size())
{
temp+=two.c_str();
two=temp;
}
else
{
temp+=one.c_str();
one=temp;
}
string result(maxsize,'0');
cout<<one<<endl;
cout<<'+'<<endl;
cout<<two<<endl;
cout<<'='<<endl;
size_t i=0,carry=0;
for( ;i<maxsize;++i)
{
result[i] =(one[i]-'0'+two[i]-'0'+carry)%10+'0';
carry=(one[i]-'0'+two[i]-'0'+carry)/10;
}
result[i]=carry+'0';
cout<<result<<endl;
return 0;
}
class Integer
{
private:
unsigned char buf[128]; // 够长了吧
public:
Integer & operator+(Integer & i)
{
int tmpi=0;
for(int i = 0; i < 128; i++)
{
tmpi = tmpi>>8; // 上次的进位
tmpi = buf[i] + i.buf[i] + tmpi;
buf[i] = tmpi;
}
}
}
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
using namespace std;
struct List{
int data;
List * next;
};
List *make_list(string &str)
{
List *head=new List;
List *last=head,*p;
string num;
int pos=str.size();
while(!str.empty()){
pos=str.rfind(',',pos);
if (pos==str.npos){
last->data=atoi(str.c_str());
str.clear();
}
else {
num=str.substr(pos+1,str.size()-pos); //pos还是正向搜索时在字符串中的位置
//str=str.substr(pos+1,str.size()-pos);
str.erase(pos,str.size()-pos);
last->data=atoi(num.c_str());
p=new List;
last->next=p;
last=p;
}
}
last->next=NULL;
return head;
}
List *Add(const List *list1,const List *list2)
{ //类似于链表合并操作
List *mylist=new List;
List *head,*last;
last=head=mylist;
int sum,inc=0;
const List *p=list1;
const List *q=list2;
while(p!=NULL&&q!=NULL){
sum=p->data+q->data+inc;
head->data=sum%1000;
inc=sum/1000;
p=p->next;
q=q->next;
last=new List;
head->next=last;
head=last;
}
while(p!=NULL){
sum=p->data+inc;
head->data=sum%1000;
inc=sum/1000;
p=p->next;
if(p==NULL)
head->next=NULL;
else{
last=new List;
head->next=last;
head=last;
}
}
while(q!=NULL){
sum=q->data+inc; //如果链表中结点的值与进位和大于1000,可能一直有进位
head->data=sum%1000;
inc=sum/1000;
q=q->next;
if(q==NULL)
head->next=NULL;
else{
last=new List;
head->next=last;
head=last;
}
}
head->next=NULL;
return mylist;
}
List *Minus(const List *list1,const List *list2)
{
List *mylist=new List;
List *head,*last;
last=head=mylist;
int sum,inc=0;
const List *p=list1;
const List *q=list2;
while(p!=NULL&&q!=NULL){
sum=p->data-q->data-inc;
if(sum<0)
{
head->data=1000+sum; //被减数不够减时
inc=1; //向上边最多借位为1,即1000
}
else {
head->data=sum;
inc=0;
}
p=p->next;
q=q->next;
if(p==NULL) head->next=NULL; //p指向空,head同时结束
else { //否则再建一新的结点
last=new List;
head->next=last;
head=last;
}
}
while(p!=NULL){
sum=p->data-inc; //如果链表中结点的值一直为1,可能一直借位
if(sum<0)
{
head->data=1000+sum;
inc=1;
}
else {
head->data=sum;
inc=0;
}
p=p->next;
if(p==NULL)
head->next=NULL;
else{
last=new List;
head->next=last;
head=last;
}
}
return mylist;
}
List *Mus(const List *list1,const List *list2,int &sign)
{
const List *p=list1;
const List *q=list2;
int inc=0;
while (p->next!=NULL&&q->next!=NULL)
{
if(p->data>q->data) inc=1; //当被减数与减数在链表中有相同的长度时,要找出其中较大者
else inc=0;
p=p->next;
q=q->next;
}
if(p->next==NULL&&q->next==NULL&&inc||p->next!=NULL) //被减数较大
return Minus(list1,list2);
else{
sign=1; //减数较大,标记为负
return Minus(list2,list1);
}
}
void show(const List *mylist,int sign=0)
{
vector<int> ivec;
while (mylist!=NULL)
{
ivec.push_back(mylist->data);
mylist=mylist->next;;
}
vector<int>::reverse_iterator ite=ivec.rbegin(); //反向迭代器
while(*ite==0) ite++;
cout<<"The result is :"<<endl;
cin.sync();
if(sign) cout<<'-';
cout<<*ite;
for (ite++;ite!=ivec.rend();ite++)
{
cout<<',';
cout<<setw(3)<<setfill('0')<<*ite;
}
cout<<endl;
}
int main()
{
List *list1,*list2,*list3;
string str;
char c;
int sign=0;
cout<<"Please input the first number."<<endl;
cin>>str;
list1=make_list(str);
cout<<"Please input the second number."<<endl;
cin>>str;
list2=make_list(str);
cout<<"Please input the operator."<<endl;
cin>>c;
if (c=='+')
list3=Add(list1,list2);
else if(c=='-')
list3=Mus(list1,list2,sign);
show(list3,sign);
system("pause");
return 0;
}