資源簡介
#ifndef HUGEINT_H
#define HUGEINT_H
#include <iostream>
using std::ostream;
class HugeInt
{
friend ostream& operator<<(ostream&, const HugeInt&);
public:
HugeInt(long = 0);
HugeInt( const char * );
HugeInt operator ( const HugeInt & ) const;
HugeInt operator ( int ) const;
HugeInt operator ( const char * ) const;
bool operator==(const HugeInt&) const;
bool operator!=( const HugeInt & ) const;
bool operator<( const HugeInt & ) const;
bool operator<=( const HugeInt & ) const;
bool operator>( const HugeInt & ) const;
bool operator>=( const HugeInt & ) const;
HugeInt operator-( const HugeInt & ) const;
HugeInt operator*( const HugeInt & ) const;
HugeInt operator/( const HugeInt & ) const;
void output();
int getLength() const;
private:
int integer[40];
};
#endif
#define HUGEINT_H
#include <iostream>
using std::ostream;
class HugeInt
{
friend ostream& operator<<(ostream&, const HugeInt&);
public:
HugeInt(long = 0);
HugeInt( const char * );
HugeInt operator ( const HugeInt & ) const;
HugeInt operator ( int ) const;
HugeInt operator ( const char * ) const;
bool operator==(const HugeInt&) const;
bool operator!=( const HugeInt & ) const;
bool operator<( const HugeInt & ) const;
bool operator<=( const HugeInt & ) const;
bool operator>( const HugeInt & ) const;
bool operator>=( const HugeInt & ) const;
HugeInt operator-( const HugeInt & ) const;
HugeInt operator*( const HugeInt & ) const;
HugeInt operator/( const HugeInt & ) const;
void output();
int getLength() const;
private:
int integer[40];
};
#endif
代碼片段和文件信息
#include
#include“hugeint.h“
using?namespace?std;
ostream&?operator<<(ostream&?output?const?HugeInt&?n)
{
int?i;
for?(i?=?0;?(n.integer[i]?==?0)?&&?(i?<=?39);?i++);
if?(i?==?40)
output?<0;
else
for?(i;?i?40;?i++)
{
output?< }
return?output;
}
HugeInt::HugeInt(long?num)
{
for?(int?i?=?0;?i?40;?i++)
{
integer[i]?=?0;
}
for?(int?j?=?39;?num?!=?0?&&?j?>?0;?--j)
{
integer[j]?=?num?%?10;
num?/=?10;
}
}
HugeInt::HugeInt(const?char*?c)
{
for?(int?i?=?0;?i?40;?i++)
integer[i]?=?0;
int?lenth?=?strlen(c);
for?(int?j?=?40?-?lenth?k?=?0;?j?40;?j++?k++)
if?(isdigit(c[k]))
integer[j]?=?c[k]?-?‘0‘;
}
HugeInt?HugeInt::operator+(const?HugeInt&?n)?const
{
HugeInt?temp;
int?carry?=?0;
for?(i
- 上一篇:計算器 實現普通計算、進制轉換和階乘計算
- 下一篇:C語言基礎思維導圖
評論
共有 條評論