huge integer 加減運算 參考代碼

頭文件:huge.h
#include<iostream>
#include<cstdio>
#define M 40
using namespace std;

class huge
{
public:
huge();
void set();
void add(huge &);           //相加
void sub(huge &);          //相減
void compare(huge &);     //關係比較,相等或大小


private:
short a[M];

};



cpp文件:huge.cpp 
#include"huge.h"
//--------------------------------------------------------------
huge::huge()            //初始化數組
{
int i;
for(i=0;i<M;i++)
{
a[i]=0;
}

}
//--------------------------------------------------------------
void huge::set()               //給數組賦值
{
char ch;
short b[M];

int i=0;
for(i=0;i<M;i++)
{
b[i]=0;
}
i=0;
cout<<"please input a hugeinteger:";
scanf("%c",&ch);
while(ch>='0'&&ch<='9')
{
b[i]=ch-'0';                 //將字符轉化爲整形數
i++;
scanf("%c",&ch);
}
i=i-1;

int k;
for(k=0;k<=i;k++)              //將字符在數組中右對齊賦值
{
a[M-1-k]=b[i-k];
}





}
//----------------------------------------------------------
void huge::add(huge &p)               //兩個對象做加法
{
cout<<"addtion:"<<endl;
int i=0,k=0,x=0;

short b[M];
for(i=0;i<M;i++)
{
b[i]=0;
}


for(i=M-1;i>=0;i--)
{
b[i]=b[i]+a[i]+p.a[i];
if(b[i]>9)                //滿10進1
{
b[i]=b[i]-10;
b[i-1]=b[i-1]+1;
}

}
cout<<"result:";
for(i=0;i<M;i++)
{
k=b[i];
if(k==x) //第一個非零字符前所有的0,不顯示       
{
continue;
}
else
{
cout<<b[i];
x=100;
}
}
cout<<endl;

}
//----------------------------------------------------------
void huge::sub(huge &p)              //兩對象做減法
{
cout<<"substraction:"<<endl;
int i=0,k=0,m=0,x=0;
short c[M];


for(i=0;i<M;i++)
{
c[i]=0;
}

for(i=0;i<M;i++)                     //判斷兩個對象的大小
{


k=a[i]-p.a[i];

if(k==0)
{
continue;
}
else if(k>0)
{
m=1;
break;
}
else
{
m=-1;
break;
}

}
if(i==M-1)
{
cout<<"result:0"<<endl;
}

k=0;
if(m>0)                            //第一個對象大
{
for(i=M-1;i>=0;i--)
{
c[i]=c[i]+a[i]-p.a[i];

if(c[i]<0)                 
{
c[i]=k+a[i]+10-p.a[i];   //從前一位借10來完成正整數減法
c[i-1]-=1;
k=-1;
}
}
cout<<"result:";
}
else if(m<0)                  //第二個對象大
{
for(i=M-1;i>=0;i--)
{
c[i]=c[i]+p.a[i]-a[i];

if(c[i]<0)
{
c[i]=k+p.a[i]+10-a[i];
c[i-1]-=1;
k=-1;
}
}
cout<<"result:-";
}

for(i=0;i<M;i++)
{
k=c[i];
if(k==x) //第一個非零字符前所有的0,不顯示       
{
continue;
}
else
{
cout<<c[i];
x=100;
}
cout<<endl;
}
}
//---------------------------------------------------------------------
void huge::compare(huge &p)                   //比較兩個對象的關係
{
cout<<"relations:";
int i;
for(i=0;i<M;i++)
{
if(a[i]==p.a[i])
{
continue;
}
else
{
cout<<"Not equal!"<<endl;
break;
}
}
if(i==M)                  //相等,退出,不繼續運行
{
cout<<"Equal!"<<endl;
getchar();
getchar();
exit (0);
}
for(i=0;i<M;i++)            //不相等,比較大小
{
if(a[i]==0&&p.a[i]==0)
{
continue;
}
else if(a[i]>p.a[i])
{
cout<<"The first integer is bigger than the second one!"<<endl;
break;
}
else
{
cout<<"The first integer is smaller than the second one!"<<endl;
break;
}
}

}




cpp文件 主函數.cpp
 
 #include"huge.h"

void main()
{
huge x,y;               //定義兩個對象
x.set();
y.set();
x.add(y);
x.sub(y);
x.compare(y);

system("pause");
}



代碼僅供參考 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章