#include <iostream>
#include <string>
using namespace std;
class BInt//宣告大數類
{
public:
BInt(){};
BInt(char s[]);
BInt(int i);//用普通整數構造大數類
public:
int operator=(int bn2);//多載賦值操作,允許用普通數給大數賦值
char * operator=(char *s1);//多載賦值操作,允許用字串形式的大數給其賦值
public:
friend istream & operator >> (istream & input, BInt& bn) {input >> bn.m_s;return input;}//多載輸入運算子
friend ostream & operator << (ostream & output, BInt& bn){output << bn.m_s;return output;}//多載輸出運算子
friend BInt operator + (const BInt& bn1,const BInt& bn2);//多載加法運算子
friend BInt operator - (const BInt& bn1,const BInt& bn2);//多載減法運算子
friend bool operator < (const BInt& bn1,const BInt& bn2);//多載小于運算子
friend bool operator <= (const BInt& bn1,const BInt& bn2);//多載小于等于運算子
friend bool operator > (const BInt& bn1,const BInt& bn2);
friend bool operator > (const BInt& bn1, int bn2);
friend bool operator >= (const BInt& bn1,const BInt& bn2);
friend bool operator == (const BInt& bn1,const BInt& bn2);
friend bool operator == (const BInt& bn1, int bn2);
private:
int size() const {return m_s.size();}//獲得大數的位數
string m_s;//內部用string來存盤大數
};
BInt::BInt(int i)//實作用普通數構造大數
{
char a[2]={0};
if (i==0)
{
m_s = "0";
return;
}
for (int tmp;i>0;i/=10)
{
tmp = i%10;
a[0] = tmp + '0';
m_s = a+m_s;
}
}
BInt::BInt(char s[]):m_s(s)//實作用字串構造大數
{
}
int BInt::operator=(int bn2)//實作賦值操作多載
{
char a[2]={0};
m_s="";
if (bn2==0)
{
m_s = "0";
return bn2;
}
for (int tmp;bn2>0;bn2/=10)
{
tmp = bn2%10;
a[0] = tmp + '0';
m_s = a+m_s;
}
return bn2;
}
char * BInt::operator=(char *s1)
{
m_s = s1;
return s1;
}
bool operator<(const BInt& bn1,const BInt& bn2)//實作小于操作多載
{
if (bn1.size()<bn2.size())
{
return true;
}
else if (bn1.size()>bn2.size())
{
return false;
}
for (int i=0, length = bn1.size();i<length;++i)
{
if (bn1.m_s[i]<bn2.m_s[i])
{
return true;
}
else if (bn1.m_s[i]>bn2.m_s[i])
{
return false;
}
}
return false;
}
bool operator==(const BInt& bn1,const BInt& bn2)//實作等于操作多載
{
if (bn1.size() != bn2.size())
{
return false;
}
for (int i = 0, length = bn1.size();i<length;++i)
{
if (bn1.m_s[i]!=bn2.m_s[i])
{
return false;
}
}
return true;
}
bool operator<=(const BInt& bn1,const BInt& bn2)//實作小與等于多載
{
if (bn1<bn2||bn1 == bn2)
{
return true;
}
else
{
return false;
}
}
bool operator==(const BInt& bn1, int bn2)//使大數可以跟普通數比較
{
BInt i(bn2);
if (bn1 == i)
{
return true;
}
return false;
}
bool operator>(const BInt& bn1,const BInt& bn2)
{
if (bn1<bn2==false&&(bn1 == bn2)==false)
{
return true;
}
else
{
return false;
}
}
bool operator>(const BInt& bn1,int bn2)
{
BInt b(bn2);
if (bn1 > b)
{
return true;
}
else
{
return false;
}
}
bool operator>=(const BInt& bn1,const BInt& bn2)
{
if (bn1>bn2||bn1==bn2)
{
return true;
}
else
{
return false;
}
}
BInt operator+(const BInt& bn1,const BInt& bn2)
{
BInt sum;
char a[2];
a[0] = '0';
a[1] = 0;
for ( int i=bn1.m_s.size() -1,j=bn2.m_s.size() -1;i>=0||j>=0;--i,--j)
{
int tmp;
if (i<0)
{
tmp = bn2.m_s[j] - '0' +a[0] - '0';
}
else if (j<0)
{
tmp = bn1.m_s[i] - '0' +a[0] - '0';
}
else
{
tmp = bn1.m_s[i] - '0' + bn2.m_s[j] - '0' +a[0] - '0';
}
a[0] = tmp%10 + '0';
sum.m_s = a + sum.m_s;
a[0] = tmp/10 +'0';
}
if (a[0] != '0')
{
sum.m_s = a + sum.m_s;
}
return sum;
}
int main()
{
BInt a(1234567);//測驗以整數構造大數
BInt b("123456789123456789123456789");//測驗以字串構造大數
cout<<"a 為:"<<a<<endl;
cout<<"b 為:"<<b<<endl;
a = 42335;//測驗以整數給大數賦值
b = "15365423432435343";//測驗以字串給大數賦值
cout<<"a 為:"<<a<<endl;
cout<<"b 為:"<<b<<endl;
cout<<"a 和 b 中較大的數為 :"<<(a>b?a:b)<<endl<<endl;//測驗大于運算子
cout<<"請重新輸入a的值:"<<endl;
cin>>a;//測驗輸入運算子
cout<<"請重新輸入b的值:"<<endl;
cin>>b;
cout<<endl<<"a 和 b 中較小的數為"<<(a<b?a:b)<<endl<<endl;//測驗小于運算子
cout<<"a 和 b 的和為:"<<a+b<<endl<<endl;//測驗+運算子
return 0;
}
uj5u.com熱心網友回復:
真心懶得編譯運行一下找哪里錯uj5u.com熱心網友回復:
大嬸運蒜都不懂,別說大叔運蒜了,擼過學習
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/133535.html
標籤:基礎類
