?? high point.cpp
字號:
/*本欄為本專題所有程序的公用部分,調(diào)用本專題任何一個程序必須加上本欄的代碼
input/print為高精度數(shù)輸入輸出,調(diào)用格式為input(hp HightPoint,"123456")/print(hp HighPoint)
*/
#include <iostream>
using namespace std;
#define maxsize 100
struct hp
{
int len;
int s[maxsize+1];
};
void input(hp &a,string str)
{
int i;
while(str[0]=='0' && str.size()!=1)
str.erase(0,1);
a.len=(int)str.size();
for(i=1;i<=a.len;++i)
a.s[i]=str[a.len-i]-48;
for (i=a.len+1;i<=maxsize;++i)
a.s[i]=0;
}
void print(const hp &y)
{
int i;
for(i=y.len;i>=1;i--)
cout<<y.s[i];
cout<<endl;
}
/*
高精度比較
語法:int result=compare(const hp &a,const hp &b);
參數(shù):
a,b:
進(jìn)行比較的高精度數(shù)字
返回值:
比較結(jié)果,a>b返回正數(shù),a=b返回0,a<b返回負(fù)數(shù)
*/
int compare(const hp &a,const hp &b)
{
int len;
if(a.len>b.len)
len=a.len;
else
len=b.len;
while(len>0 && a.s[len]==b.s[len]) len--;
if(len==0)
return 0;
else
return a.s[len]-b.s[len];
}
/*
高精度數(shù)加法
語法:plus(const hp &a,const hp &b,hp &c);
參數(shù):
a,b:
進(jìn)行加法的高精度數(shù)字
返回值:
返回相加結(jié)果到c中
*/
int plus(const hp &a,const hp &b,hp &c)
{
int i,len;
for(i=1;i<=maxsize;i++) c.s[i]=0;
if(a.len>b.len) len=a.len;
else len=b.len;
for(i=1;i<=len;i++)
{
c.s[i]+=a.s[i]+b.s[i];
if(c.s[i]>=10)//進(jìn)位
{
c.s[i]-=10;
c.s[i+1]++;
}
}
if(c.s[len+1]>0) len++;
c.len=len;
for (i=c.len;i<c.len;i--)
{
cout<<c.s[i];
}
return 0;
}
/*高精度數(shù)減法
語法:subtract(const hp &a,const hp &b,hp &c);
參數(shù):
a,b:
進(jìn)行減法的高精度數(shù)字,a是被減數(shù),b是減數(shù),不支持負(fù)數(shù)
返回值:
返回結(jié)果到c中
*/
void subtract(const hp &a,const hp &b,hp &c)
{
int i,len;
for(i=1;i<=maxsize;i++) c.s[i]=0;
if(a.len>b.len) len=a.len;
else len=b.len;
for(i=1;i<=len;i++)
{
c.s[i]+=a.s[i]-b.s[i];
if(c.s[i]<0) //借位
{
c.s[i]+=10;
c.s[i+1]--;
}
}
while(len>1&&c.s[len]==0) len--;
c.len=len;
}
/*高精度乘10
語法:multiply10(hp &a);
參數(shù):
a:
進(jìn)行乘法的高精度數(shù)字
返回值:
返回結(jié)果到 a 中
*/
void multiply10(hp &a)
{
int i;
for(i=a.len;i>=1;i--)
a.s[i+1]=a.s[i];
a.s[1]=0;
a.len++;
while(a.len>1&&a.s[a.len]==0) a.len--;
}
/*高精度乘高精度
語法:multiplyh(const hp &a,const hp &b,hp &c);
參數(shù):
a,b:
進(jìn)行乘法的高精度數(shù)字
返回值:
返回結(jié)果到 c 中
*/
void multiplyh(const hp &a,const hp &b,hp &c)
{
int i,j,len;
for(i=1;i<=maxsize;i++) c.s[i]=0;
for(i=1;i<=a.len;i++)
for(j=1;j<=b.len;j++)
{
c.s[i+j-1]+=a.s[i]*b.s[j];
c.s[i+j]+=c.s[i+j-1]/10;
c.s[i+j-1]%=10;
}
len=a.len+b.len+1;
while(len>1&&c.s[len]==0) len--;
c.len=len;
}
/*高精度除高精度
語法:divideh(const hp &a,const hp &b,hp &c,hp &d);
參數(shù):
a,b:
進(jìn)行除法的高精度數(shù)字
返回值:
返回商到 c 中,余數(shù)到 d 中
注意:
需要compare、multiply10、subtract
*/
void divideh(const hp &a,const hp &b,hp &c,hp &d)
{
hp e;
int i,len;
for(i=1;i<=maxsize;i++)
{
c.s[i]=0;
d.s[i]=0;
}
len=a.len;
d.len=1;
for(i=len;i>=1;i--)
{
multiply10(d);
d.s[1]=a.s[i];
while(compare(d,b)>=0)
{
subtract(d,b,e);
d=e;
c.s[i]++;
}
}
while(len>1&&c.s[len]==0) len--;
c.len=len;
}
void main()
{
hp a,b,c;
string str1,str2;
str1="123456";str2="345678";
//cout<<"請輸入要計(jì)算的數(shù)字1:";
//cin>>str1;
input(a,str1);
cout<<endl;
cout<<"您輸入的數(shù)字1為:";
print(a);
cout<<endl;
//cout<<"請輸入要計(jì)算的數(shù)字2:";
//cin>>str2;
input(b,str2);
cout<<endl;
cout<<"您輸入的數(shù)字2為:";
print(b);
cout<<endl;
cout<<"比較兩個數(shù)字:";
compare(a,b);
cout<<endl;
cout<<"兩個數(shù)字相加的結(jié)果為:";
plus(a,b,c);
for (int i=c.len;i<c.len;i--)
{
cout<<c.s[i];
}
cout<<endl;
cout<<"兩個數(shù)字相減的結(jié)果為:";
subtract(a,b,c);
cout<<endl;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -