First,我写了一个_int8192类,高精度,整数 Second,寻求改进与Bug
Third,难道有人能全看完吗(4760行)
Fourth,最后一篇有注释
Fifth,1.5和1.4其实改动不大
Sixth,正文开始
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
class _int8192{
private:
static const int BIT=1000;
short num[BIT+1];
void getsize(){
int i=BIT;
while(i>=0 && num[i]==0) i--;
if(i==-1) size=1;
else size=i+1;
}
public:
int size;
bool porn=true;
void init(){
for(register int i=0; i<=BIT; i++){
num[i]=0;
}
}
void read(){
init();
stack<short> st;
size=0;
memset(num,0,sizeof(num));
char a;
int x;
int p=0;
bool c=true;
bool fr0=true;
a=getchar();
while(1){
if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
if(a=='-'){
size--;
porn=false;
}
else if(fr0==false) st.push(a-'0');
else if(fr0==true && a!='0'){
st.push(a-'0');
fr0=false;
}
else size--;
if(c==true) c=false;
size++;
a=getchar();
}
for(register int i=0; i<size; i++){
num[i]=st.top();
st.pop();
}
if(size==0){
size=1;
num[0]=0;
}
}
void print(){
if(porn==false) printf("-");
int p=size-1;
while(p!=-1){
printf("%d",num[p]);
p--;
}
}
bool operator=(const _int8192 a){
memcpy(num,a.num,sizeof(short)*BIT);
porn=a.porn;
size=a.size;
}
};
int main(){
_int8192 n,m;
n.read();
m.read();
n.print();
m.print();
return 0;
}
这个版本仅编写了 read() 和 print(),似乎没什么用,至少先定义一下class _int8192
定义:
1. _int8192 含义为可表示数据范围为(2−8191)~(28191)−1的整数
2. 成员 num[] 存数字
3. bool变量 porn: true,该数非负 false,该数为负
4. size 表示该数绝对值的位数
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
class _int8192{
private:
static const int BIT=1000;
short num[BIT+1];
inline void getsize(){
int i=BIT;
while(i>=0 && num[i]==0) i--;
if(i==-1) size=1;
else size=i+1;
}
public:
int size;
bool porn=true;
inline void init(){
for(register int i=0; i<=BIT; i++){
num[i]=0;
}
}
void read(){
init();
stack<short> st;
size=0;
memset(num,0,sizeof(num));
char a;
int x;
int p=0;
bool c=true;
bool fr0=true;
a=getchar();
while(1){
if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
if(a=='-'){
size--;
porn=false;
}
else if(fr0==false) st.push(a-'0');
else if(fr0==true && a!='0'){
st.push(a-'0');
fr0=false;
}
else size--;
if(c==true) c=false;
size++;
a=getchar();
}
for(register int i=0; i<size; i++){
num[i]=st.top();
st.pop();
}
if(size==0){
size=1;
num[0]=0;
}
}
void print(){
if(porn==false) printf("-");
int p=size-1;
while(p!=-1){
printf("%d",num[p]);
p--;
}
}
void operator^(_int8192 &a){
_int8192 t;
t=*this;
*this=a;
a=t;
}
void operator=(const _int8192 a){
memcpy(num,a.num,sizeof(short)*BIT);
porn=a.porn;
size=a.size;
}
_int8192 abs(){
_int8192 ret;
ret=*this;
ret.porn=true;
return ret;
}
_int8192 opp(){
_int8192 ret;
ret=*this;
ret.porn=!ret.porn;
return ret;
}
bool operator>(const _int8192 a) const{
if(porn==true && a.porn==false) return true;
if(porn==false && a.porn==true) return false;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]>a.num[i]){
ret=true;
break;
}
else if(num[i]<a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator<(const _int8192 a) const{
if(porn==true && a.porn==false) return false;
if(porn==false && a.porn==true) return true;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]<a.num[i]){
ret=true;
break;
}
else if(num[i]>a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator==(const _int8192 a) const{
if(porn!=a.porn) return false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]!=a.num[i]) return false;
}
return true;
}
_int8192 operator+(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
ans.porn=porn;
int bit=max(size,a.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+a.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
else{
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()>_this.abs())?a.porn:porn;
int bit=max(size,a.size);
label:
for(register int i=0; i<bit; i++){
ans.num[i]+=b.num[i]-_this.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
if(ans.num[bit]<0){
memset(ans.num,0,sizeof(short)*BIT);
b^_this;
goto label;
}
}
ans.getsize();
return ans;
}
_int8192 operator-(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()<_this.abs())?porn:!porn;
int bit=max(size,a.size);
if(ans.porn!=porn) b^_this;
for(register int i=0; i<bit; i++){
ans.num[i]+=_this.num[i]-b.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
}
else{
ans.porn=porn;
_int8192 b=a;
b=b.opp();
int bit=max(size,b.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+b.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
ans.getsize();
return ans;
}
};
int main(){
_int8192 n,m;
n.read();
m.read();
(n+m).print();
(n-m).print();
return 0;
}
这个版本最重要的is加入了加法和减法,
but为了加减法的实现,我增加了取绝对值(abs)、取相反数(opp)、交换(^) 等等,以及大于(>)、小于(<)、等于(==) 等比较符
至于为何use ^ 表示交换,主要是one time 也找不到其他运算符了
PS:
AC P1601 :https://www.luogu.com.cn/record/180348561
Code:
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
class _int8192{
private:
static const int BIT=1000;
short num[BIT+1];
inline void getsize(){
int i=BIT;
while(i>=0 && num[i]==0) i--;
if(i==-1) size=1;
else size=i+1;
}
short cmp[BIT+1];
bool PrintBool(){
memset(cmp,0,sizeof(cmp));
if(memcmp(num,cmp,sizeof(short)*BIT)==0) return true;
return false;
}
public:
int size;
bool porn=true;
void init(){
for(register int i=0; i<=BIT; i++){
num[i]=0;
}
}
static _int8192 intTO_int8192(int n){
_int8192 ans;
ans.init();
if(n<0){
ans.porn=false;
n=-n;
}
else ans.porn=true;
int p=0;
while(n!=0){
ans.num[p++]=n%10;
n/=10;
}
ans.getsize();
return ans;
}
#define _int8192(x) _int8192::intTO_int8192(x)
void read(){
init();
stack<short> st;
size=0;
memset(num,0,sizeof(num));
char a;
int x;
int p=0;
bool c=true;
bool fr0=true;
a=getchar();
while(1){
if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
if(a=='-'){
size--;
porn=false;
}
else if(fr0==false) st.push(a-'0');
else if(fr0==true && a!='0'){
st.push(a-'0');
fr0=false;
}
else size--;
if(c==true) c=false;
size++;
a=getchar();
}
for(register int i=0; i<size; i++){
num[i]=st.top();
st.pop();
}
if(size==0){
size=1;
num[0]=0;
}
}
void print(){
if(num[0]==-8) return;
if(PrintBool()==true){
printf("0");
return;
}
if(porn==false) printf("-");
int p=size-1;
while(p!=-1){
printf("%d",num[p]);
p--;
}
}
void operator^(_int8192 &a){
_int8192 t;
t=*this;
*this=a;
a=t;
}
bool operator=(const _int8192 a){
memcpy(num,a.num,sizeof(short)*BIT);
porn=a.porn;
size=a.size;
}
_int8192 abs(){
_int8192 ret;
ret=*this;
ret.porn=true;
return ret;
}
_int8192 opp(){
_int8192 ret;
ret=*this;
ret.porn=!ret.porn;
return ret;
}
bool operator>(const _int8192 a) const{
if(porn==true && a.porn==false) return true;
if(porn==false && a.porn==true) return false;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]>a.num[i]){
ret=true;
break;
}
else if(num[i]<a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator<(const _int8192 a) const{
if(porn==true && a.porn==false) return false;
if(porn==false && a.porn==true) return true;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]<a.num[i]){
ret=true;
break;
}
else if(num[i]>a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator==(const _int8192 a) const{
if(porn!=a.porn) return false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]!=a.num[i]) return false;
}
return true;
}
bool operator>=(const _int8192 a) const{
return *this>a || *this==a;
}
bool operator<=(const _int8192 a) const{
return *this<a || *this==a;
}
_int8192 operator+(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
ans.porn=porn;
int bit=max(size,a.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+a.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
else{
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()>_this.abs())?a.porn:porn;
int bit=max(size,a.size);
label:
for(register int i=0; i<bit; i++){
ans.num[i]+=b.num[i]-_this.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
if(ans.num[bit]<0){
memset(ans.num,0,sizeof(short)*BIT);
b^_this;
goto label;
}
}
ans.getsize();
return ans;
}
_int8192 operator-(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()<_this.abs())?porn:!porn;
int bit=max(size,a.size);
if(ans.porn!=porn) b^_this;
for(register int i=0; i<bit; i++){
ans.num[i]+=_this.num[i]-b.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
}
else{
ans.porn=porn;
_int8192 b=a;
b=b.opp();
int bit=max(size,b.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+b.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
ans.getsize();
return ans;
}
void operator+=(const _int8192 a){
*this=*this+a;
}
void operator-=(const _int8192 a){
*this=*this-a;
}
void operator++(){
*this=*this+_int8192(1);
}
_int8192 operator++(int){
_int8192 *ret=this;
++*this;
return *ret;
}
void operator--(){
*this=*this-_int8192(1);
}
_int8192 operator--(int){
_int8192 *ret=this;
--*this;
return *ret;
}
_int8192 operator*(const _int8192 a) const{
_int8192 ans;
ans.init();
ans.porn=(porn==a.porn)?true:false;
for(register int i=0; i<size; i++){
for(register int j=0; j<a.size; j++){
ans.num[i+j]+=num[i]*a.num[j];
}
}
for(register int i=0; i<BIT; i++){
ans.num[i+1]+=ans.num[i]/10;
ans.num[i]%=10;
}
ans.getsize();
return ans;
}
_int8192 operator/(const _int8192 a) const{
if(a==_int8192(0)){
cout<<"Runtime Error! Zero(0) cannot be a divisor!";
_int8192 re;
re.init();
re.num[0]=-8;
return re;
}
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
ans=_int8192(0);
while(_this>=b){
_this-=b;
ans++;
}
ans.porn=ans.porn=(porn==a.porn)?true:false;
ans.getsize();
return ans;
}
_int8192 operator%(const _int8192 a) const{
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
while(_this>=b) _this-=b;
if(porn==a.porn) ans=_this;
else ans=b-_this;
ans.porn=a.porn;
ans.getsize();
return ans;
}
};
int main(){
_int8192 n,m;
n.read();
m.read();
(n+m).print();
return 0;
}
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
class _int8192{
private:
static const int BIT=1000;
short num[BIT+1];
inline void getsize(){
int i=BIT;
while(i>=0 && num[i]==0) i--;
if(i==-1) size=1;
else size=i+1;
}
short cmp[BIT+1];
bool PrintBool(){
memset(cmp,0,sizeof(cmp));
if(memcmp(num,cmp,sizeof(short)*BIT)==0) return true;
return false;
}
public:
int size;
bool porn=true;
void init(){
for(register int i=0; i<=BIT; i++){
num[i]=0;
}
}
static _int8192 intTO_int8192(int n){
_int8192 ans;
ans.init();
if(n<0){
ans.porn=false;
n=-n;
}
else ans.porn=true;
int p=0;
while(n!=0){
ans.num[p++]=n%10;
n/=10;
}
ans.getsize();
return ans;
}
#define _int8192(x) _int8192::intTO_int8192(x)
void read(){
init();
stack<short> st;
size=0;
memset(num,0,sizeof(num));
char a;
int x;
int p=0;
bool c=true;
bool fr0=true;
a=getchar();
while(1){
if(!((a>='0' && a<='9') || a=='-') || (c==false && #include<iostream>
#include<string.h>
#include<stack>
using namespace std;
class _int8192{
private:
static const int BIT=1000;
short num[BIT+1];
inline void getsize(){
int i=BIT;
while(i>=0 && num[i]==0) i--;
if(i==-1) size=1;
else size=i+1;
}
short cmp[BIT+1];
bool PrintBool(){
memset(cmp,0,sizeof(cmp));
if(memcmp(num,cmp,sizeof(short)*BIT)==0) return true;
return false;
}
public:
int size;
bool porn=true;
void init(){
for(register int i=0; i<=BIT; i++){
num[i]=0;
}
}
static _int8192 intTO_int8192(int n){
_int8192 ans;
ans.init();
if(n<0){
ans.porn=false;
n=-n;
}
else ans.porn=true;
int p=0;
while(n!=0){
ans.num[p++]=n%10;
n/=10;
}
ans.getsize();
return ans;
}
#define _int8192(x) _int8192::intTO_int8192(x)
void read(){
init();
stack<short> st;
size=0;
memset(num,0,sizeof(num));
char a;
int x;
int p=0;
bool c=true;
bool fr0=true;
a=getchar();
while(1){
if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
if(a=='-'){
size--;
porn=false;
}
else if(fr0==false) st.push(a-'0');
else if(fr0==true && a!='0'){
st.push(a-'0');
fr0=false;
}
else size--;
if(c==true) c=false;
size++;
a=getchar();
}
for(register int i=0; i<size; i++){
num[i]=st.top();
st.pop();
}
if(size==0){
size=1;
num[0]=0;
}
}
void print(){
if(num[0]==-8) return;
if(PrintBool()==true){
printf("0");
return;
}
if(porn==false) printf("-");
int p=size-1;
while(p!=-1){
printf("%d",num[p]);
p--;
}
}
void operator^(_int8192 &a){
_int8192 t;
t=*this;
*this=a;
a=t;
}
bool operator=(const _int8192 a){
memcpy(num,a.num,sizeof(short)*BIT);
porn=a.porn;
size=a.size;
}
_int8192 abs(){
_int8192 ret;
ret=*this;
ret.porn=true;
return ret;
}
_int8192 opp(){
_int8192 ret;
ret=*this;
ret.porn=!ret.porn;
return ret;
}
bool operator>(const _int8192 a) const{
if(porn==true && a.porn==false) return true;
if(porn==false && a.porn==true) return false;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]>a.num[i]){
ret=true;
break;
}
else if(num[i]<a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator<(const _int8192 a) const{
if(porn==true && a.porn==false) return false;
if(porn==false && a.porn==true) return true;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]<a.num[i]){
ret=true;
break;
}
else if(num[i]>a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator==(const _int8192 a) const{
if(porn!=a.porn) return false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]!=a.num[i]) return false;
}
return true;
}
bool operator>=(const _int8192 a) const{
return *this>a || *this==a;
}
bool operator<=(const _int8192 a) const{
return *this<a || *this==a;
}
_int8192 operator+(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
ans.porn=porn;
int bit=max(size,a.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+a.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
else{
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()>_this.abs())?a.porn:porn;
int bit=max(size,a.size);
label:
for(register int i=0; i<bit; i++){
ans.num[i]+=b.num[i]-_this.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
if(ans.num[bit]<0){
memset(ans.num,0,sizeof(short)*BIT);
b^_this;
goto label;
}
}
ans.getsize();
return ans;
}
_int8192 operator-(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()<_this.abs())?porn:!porn;
int bit=max(size,a.size);
if(ans.porn!=porn) b^_this;
for(register int i=0; i<bit; i++){
ans.num[i]+=_this.num[i]-b.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
}
else{
ans.porn=porn;
_int8192 b=a;
b=b.opp();
int bit=max(size,b.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+b.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
ans.getsize();
return ans;
}
void operator+=(const _int8192 a){
*this=*this+a;
}
void operator-=(const _int8192 a){
*this=*this-a;
}
void operator++(){
*this=*this+_int8192(1);
}
_int8192 operator++(int){
_int8192 *ret=this;
++*this;
return *ret;
}
void operator--(){
*this=*this-_int8192(1);
}
_int8192 operator--(int){
_int8192 *ret=this;
--*this;
return *ret;
}
_int8192 operator*(const _int8192 a) const{
_int8192 ans;
ans.init();
ans.porn=(porn==a.porn)?true:false;
for(register int i=0; i<size; i++){
for(register int j=0; j<a.size; j++){
ans.num[i+j]+=num[i]*a.num[j];
}
}
for(register int i=0; i<BIT; i++){
ans.num[i+1]+=ans.num[i]/10;
ans.num[i]%=10;
}
ans.getsize();
return ans;
}
_int8192 operator/(const _int8192 a) const{
if(a==_int8192(0)){
cout<<"Runtime Error! Zero(0) cannot be a divisor!";
_int8192 re;
re.init();
re.num[0]=-8;
return re;
}
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
ans=_int8192(0);
while(_this>=b){
_this-=b;
ans++;
}
ans.porn=ans.porn=(porn==a.porn)?true:false;
ans.getsize();
return ans;
}
_int8192 operator%(const _int8192 a) const{
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
while(_this>=b) _this-=b;
if(porn==a.porn) ans=_this;
else ans=b-_this;
ans.porn=a.porn;
ans.getsize();
return ans;
}
};
int main(){
_int8192 n,m;
n.read();
m.read();
(n+m).print();
(n-m).print();
(n*m).print();
(n/m).print();
(n%m).print();
return 0;
}
乘除 and MOD闪亮登场(除法好像not very fast)
对于除以0:cout<<"Runtime Error! Zero(0) cannot be a divisor!"; 然后传递特殊信号 −8 给print(),print()不再输出,return
一堆 ++、--、+=、-= 之类的符号,
++a: void operator++();
a++: _int8192 operator++(int);
第一次接触
另有一段将 int 转换为 _int8192 ,用处很广:
static _int8192 intTO_int8192(int n){
_int8192 ans;
ans.init();
if(n<0){
ans.porn=false;
n=-n;
}
else ans.porn=true;
int p=0;
while(n!=0){
ans.num[p++]=n%10;
n/=10;
}
ans.getsize();
return ans;
}
#define _int8192(x) _int8192::intTO_int8192(x)
对MOD已知问题进行修改
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
class _int8192{
private:
static const int BIT=1000;
short num[BIT+1];
inline void getsize(){
int i=BIT;
while(i>=0 && num[i]==0) i--;
if(i==-1) size=1;
else size=i+1;
}
short cmp[BIT+1];
bool PrintBool(){
memset(cmp,0,sizeof(cmp));
if(memcmp(num,cmp,sizeof(short)*BIT)==0) return true;
return false;
}
public:
int size;
bool porn=true;
void init(){
for(register int i=0; i<=BIT; i++){
num[i]=0;
}
}
static _int8192 intTO_int8192(int n){
_int8192 ans;
ans.init();
if(n<0){
ans.porn=false;
n=-n;
}
else ans.porn=true;
int p=0;
while(n!=0){
ans.num[p++]=n%10;
n/=10;
}
ans.getsize();
return ans;
}
#define _int8192(x) _int8192::intTO_int8192(x)
void read(){
init();
stack<short> st;
size=0;
memset(num,0,sizeof(num));
char a;
int x;
int p=0;
bool c=true;
bool fr0=true;
a=getchar();
while(1){
if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
if(a=='-'){
size--;
porn=false;
}
else if(fr0==false) st.push(a-'0');
else if(fr0==true && a!='0'){
st.push(a-'0');
fr0=false;
}
else size--;
if(c==true) c=false;
size++;
a=getchar();
}
for(register int i=0; i<size; i++){
num[i]=st.top();
st.pop();
}
if(size==0){
size=1;
num[0]=0;
}
}
void print(){
if(num[0]==-8) return;
if(PrintBool()==true){
printf("0");
return;
}
if(porn==false) printf("-");
int p=size-1;
while(p!=-1){
printf("%d",num[p]);
p--;
}
}
void operator^(_int8192 &a){
_int8192 t;
t=*this;
*this=a;
a=t;
}
bool operator=(const _int8192 a){
memcpy(num,a.num,sizeof(short)*BIT);
porn=a.porn;
size=a.size;
}
_int8192 abs(){
_int8192 ret;
ret=*this;
ret.porn=true;
return ret;
}
_int8192 opp(){
_int8192 ret;
ret=*this;
ret.porn=!ret.porn;
return ret;
}
bool operator>(const _int8192 a) const{
if(porn==true && a.porn==false) return true;
if(porn==false && a.porn==true) return false;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]>a.num[i]){
ret=true;
break;
}
else if(num[i]<a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator<(const _int8192 a) const{
if(porn==true && a.porn==false) return false;
if(porn==false && a.porn==true) return true;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]<a.num[i]){
ret=true;
break;
}
else if(num[i]>a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator==(const _int8192 a) const{
if(porn!=a.porn) return false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]!=a.num[i]) return false;
}
return true;
}
bool operator>=(const _int8192 a) const{
return *this>a || *this==a;
}
bool operator<=(const _int8192 a) const{
return *this<a || *this==a;
}
_int8192 operator+(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
ans.porn=porn;
int bit=max(size,a.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+a.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
else{
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()>_this.abs())?a.porn:porn;
int bit=max(size,a.size);
label:
for(register int i=0; i<bit; i++){
ans.num[i]+=b.num[i]-_this.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
if(ans.num[bit]<0){
memset(ans.num,0,sizeof(short)*BIT);
b^_this;
goto label;
}
}
ans.getsize();
return ans;
}
_int8192 operator-(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()<_this.abs())?porn:!porn;
int bit=max(size,a.size);
if(ans.porn!=porn) b^_this;
for(register int i=0; i<bit; i++){
ans.num[i]+=_this.num[i]-b.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
}
else{
ans.porn=porn;
_int8192 b=a;
b=b.opp();
int bit=max(size,b.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+b.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
ans.getsize();
return ans;
}
void operator+=(const _int8192 a){
*this=*this+a;
}
void operator-=(const _int8192 a){
*this=*this-a;
}
void operator++(){
*this=*this+_int8192(1);
}
_int8192 operator++(int){
_int8192 *ret=this;
++*this;
return *ret;
}
void operator--(){
*this=*this-_int8192(1);
}
_int8192 operator--(int){
_int8192 *ret=this;
--*this;
return *ret;
}
_int8192 operator*(const _int8192 a) const{
_int8192 ans;
ans.init();
ans.porn=(porn==a.porn)?true:false;
for(register int i=0; i<size; i++){
for(register int j=0; j<a.size; j++){
ans.num[i+j]+=num[i]*a.num[j];
}
}
for(register int i=0; i<BIT; i++){
ans.num[i+1]+=ans.num[i]/10;
ans.num[i]%=10;
}
ans.getsize();
return ans;
}
_int8192 operator/(const _int8192 a) const{
if(a==_int8192(0)){
cout<<"Runtime Error! Zero(0) cannot be a divisor!";
_int8192 re;
re.init();
re.num[0]=-8;
return re;
}
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
ans=_int8192(0);
while(_this>=b){
_this-=b;
ans++;
}
ans.porn=ans.porn=(porn==a.porn)?true:false;
ans.getsize();
return ans;
}
_int8192 operator%(const _int8192 a) const{
if(a==_int8192(0)){
cout<<"Runtime Error! Zero(0) cannot be a divisor!";
_int8192 re;
re.init();
re.num[0]=-8;
return re;
}
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
while(_this>=b) _this-=b;
if(porn==a.porn) ans=_this;
else ans=b-_this;
ans.porn=a.porn;
ans.getsize();
return ans;
}
};
int main(){
_int8192 n,m;
n.read();
m.read();
(n+m).print();
(n-m).print();
(n*m).print();
(n/m).print();
(n%m).print();
return 0;
}
问题主要在MOD未考虑被模数为0或负数的情况 (虽然被模数为负数我不会算是全靠windows的计数器的)
Question Code:
_int8192 operator%(const _int8192 a) const{
if(a==_int8192(0)){
cout<<"Runtime Error! Zero(0) cannot be a divisor!";
_int8192 re;
re.init();
re.num[0]=-8;
return re;
}
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
while(_this>=b) _this-=b;
if(porn==a.porn) ans=_this;
else ans=b-_this;
ans.porn=a.porn;
ans.getsize();
return ans;
}
变化微弱,增加了 ! 和 !=
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
class _int8192{
private:
static const int BIT=1000;
short num[BIT+1];
inline void getsize(){
int i=BIT;
while(i>=0 && num[i]==0) i--;
if(i==-1) size=1;
else size=i+1;
}
short cmp[BIT+1];
bool PrintBool(){
memset(cmp,0,sizeof(cmp));
if(memcmp(num,cmp,sizeof(short)*BIT)==0) return true;
return false;
}
public:
int size;
bool porn=true;
void init(){
for(register int i=0; i<=BIT; i++){
num[i]=0;
}
}
static _int8192 intTO_int8192(int n){
_int8192 ans;
ans.init();
if(n<0){
ans.porn=false;
n=-n;
}
else ans.porn=true;
int p=0;
while(n!=0){
ans.num[p++]=n%10;
n/=10;
}
ans.getsize();
return ans;
}
#define _int8192(x) _int8192::intTO_int8192(x)
void read(){
init();
stack<short> st;
size=0;
memset(num,0,sizeof(num));
char a;
int x;
int p=0;
bool c=true;
bool fr0=true;
a=getchar();
while(1){
if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
if(a=='-'){
size--;
porn=false;
}
else if(fr0==false) st.push(a-'0');
else if(fr0==true && a!='0'){
st.push(a-'0');
fr0=false;
}
else size--;
if(c==true) c=false;
size++;
a=getchar();
}
for(register int i=0; i<size; i++){
num[i]=st.top();
st.pop();
}
if(size==0){
size=1;
num[0]=0;
}
}
void print(){
if(num[0]==-8) return;
if(PrintBool()==true){
printf("0");
return;
}
if(porn==false) printf("-");
int p=size-1;
while(p!=-1){
printf("%d",num[p]);
p--;
}
}
void operator^(_int8192 &a){
_int8192 t;
t=*this;
*this=a;
a=t;
}
bool operator=(const _int8192 a){
memcpy(num,a.num,sizeof(short)*BIT);
porn=a.porn;
size=a.size;
}
bool operator!(){
return *this==_int8192(0);
}
_int8192 abs(){
_int8192 ret;
ret=*this;
ret.porn=true;
return ret;
}
_int8192 opp(){
_int8192 ret;
ret=*this;
ret.porn=!ret.porn;
return ret;
}
bool operator>(const _int8192 a) const{
if(porn==true && a.porn==false) return true;
if(porn==false && a.porn==true) return false;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]>a.num[i]){
ret=true;
break;
}
else if(num[i]<a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator<(const _int8192 a) const{
if(porn==true && a.porn==false) return false;
if(porn==false && a.porn==true) return true;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]<a.num[i]){
ret=true;
break;
}
else if(num[i]>a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator==(const _int8192 a) const{
if(porn!=a.porn) return false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]!=a.num[i]) return false;
}
return true;
}
bool operator!=(const _int8192 a) const{
return !(*this==a);
}
bool operator>=(const _int8192 a) const{
return *this>a || *this==a;
}
bool operator<=(const _int8192 a) const{
return *this<a || *this==a;
}
_int8192 operator+(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
ans.porn=porn;
int bit=max(size,a.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+a.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
else{
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()>_this.abs())?a.porn:porn;
int bit=max(size,a.size);
label:
for(register int i=0; i<bit; i++){
ans.num[i]+=b.num[i]-_this.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
if(ans.num[bit]<0){
memset(ans.num,0,sizeof(short)*BIT);
b^_this;
goto label;
}
}
ans.getsize();
return ans;
}
_int8192 operator-(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()<_this.abs())?porn:!porn;
int bit=max(size,a.size);
if(ans.porn!=porn) b^_this;
for(register int i=0; i<bit; i++){
ans.num[i]+=_this.num[i]-b.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
}
else{
ans.porn=porn;
_int8192 b=a;
b=b.opp();
int bit=max(size,b.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+b.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
ans.getsize();
return ans;
}
void operator+=(const _int8192 a){
*this=*this+a;
}
void operator-=(const _int8192 a){
*this=*this-a;
}
void operator++(){
*this=*this+_int8192(1);
}
_int8192 operator++(int){
_int8192 *ret=this;
++*this;
return *ret;
}
void operator--(){
*this=*this-_int8192(1);
}
_int8192 operator--(int){
_int8192 *ret=this;
--*this;
return *ret;
}
_int8192 operator*(const _int8192 a) const{
_int8192 ans;
ans.init();
ans.porn=(porn==a.porn)?true:false;
for(register int i=0; i<size; i++){
for(register int j=0; j<a.size; j++){
ans.num[i+j]+=num[i]*a.num[j];
}
}
for(register int i=0; i<BIT; i++){
ans.num[i+1]+=ans.num[i]/10;
ans.num[i]%=10;
}
ans.getsize();
return ans;
}
_int8192 operator/(const _int8192 a) const{
if(a==_int8192(0)){
cout<<"Runtime Error! Zero(0) cannot be a divisor!";
_int8192 re;
re.init();
re.num[0]=-8;
return re;
}
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
ans=_int8192(0);
while(_this>=b){
_this-=b;
ans++;
}
ans.porn=ans.porn=(porn==a.porn)?true:false;
ans.getsize();
return ans;
}
_int8192 operator%(const _int8192 a) const{
if(a==_int8192(0)){
cout<<"Runtime Error! Zero(0) cannot be a divisor!";
_int8192 re;
re.init();
re.num[0]=-8;
return re;
}
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
while(_this>=b) _this-=b;
if(porn==a.porn) ans=_this;
else ans=b-_this;
ans.porn=a.porn;
ans.getsize();
return ans;
}
};
int main(){
_int8192 n,m;
n.read();
m.read();
//不知道输出啥
return 0;
}
变化微弱,*= /= %= 有了
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
class _int8192{
private:
static const int BIT=1000;
short num[BIT+1];
inline void getsize(){
int i=BIT;
while(i>=0 && num[i]==0) i--;
if(i==-1) size=1;
else size=i+1;
}
short cmp[BIT+1];
bool PrintBool(){
memset(cmp,0,sizeof(cmp));
if(memcmp(num,cmp,sizeof(short)*BIT)==0) return true;
return false;
}
public:
int size;
bool porn=true;
void init(){
for(register int i=0; i<=BIT; i++){
num[i]=0;
}
}
static _int8192 intTO_int8192(int n){
_int8192 ans;
ans.init();
if(n<0){
ans.porn=false;
n=-n;
}
else ans.porn=true;
int p=0;
while(n!=0){
ans.num[p++]=n%10;
n/=10;
}
ans.getsize();
return ans;
}
#define _int8192(x) _int8192::intTO_int8192(x)
void read(){
init();
stack<short> st;
size=0;
memset(num,0,sizeof(num));
char a;
int x;
int p=0;
bool c=true;
bool fr0=true;
a=getchar();
while(1){
if(!((a>='0' && a<='9') || a=='-') || (c==false && a=='-')) break;
if(a=='-'){
size--;
porn=false;
}
else if(fr0==false) st.push(a-'0');
else if(fr0==true && a!='0'){
st.push(a-'0');
fr0=false;
}
else size--;
if(c==true) c=false;
size++;
a=getchar();
}
for(register int i=0; i<size; i++){
num[i]=st.top();
st.pop();
}
if(size==0){
size=1;
num[0]=0;
}
}
void print(){
if(num[0]==-8) return;
if(PrintBool()==true){
printf("0");
return;
}
if(porn==false) printf("-");
int p=size-1;
while(p!=-1){
printf("%d",num[p]);
p--;
}
}
void operator^(_int8192 &a){
_int8192 t;
t=*this;
*this=a;
a=t;
}
bool operator=(const _int8192 a){
memcpy(num,a.num,sizeof(short)*BIT);
porn=a.porn;
size=a.size;
}
bool operator!(){
return *this==_int8192(0);
}
_int8192 abs(){
_int8192 ret;
ret=*this;
ret.porn=true;
return ret;
}
_int8192 opp(){
_int8192 ret;
ret=*this;
ret.porn=!ret.porn;
return ret;
}
bool operator>(const _int8192 a) const{
if(porn==true && a.porn==false) return true;
if(porn==false && a.porn==true) return false;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]>a.num[i]){
ret=true;
break;
}
else if(num[i]<a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator<(const _int8192 a) const{
if(porn==true && a.porn==false) return false;
if(porn==false && a.porn==true) return true;
bool ret=false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]<a.num[i]){
ret=true;
break;
}
else if(num[i]>a.num[i]){
ret=false;
break;
}
}
if(porn==false) return !ret;
return ret;
}
bool operator==(const _int8192 a) const{
if(porn!=a.porn) return false;
int bit=max(size,a.size);
for(int i=bit-1; i>=0; i--){
if(num[i]!=a.num[i]) return false;
}
return true;
}
bool operator!=(const _int8192 a) const{
return !(*this==a);
}
bool operator>=(const _int8192 a) const{
return *this>a || *this==a;
}
bool operator<=(const _int8192 a) const{
return *this<a || *this==a;
}
_int8192 operator+(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
ans.porn=porn;
int bit=max(size,a.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+a.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
else{
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()>_this.abs())?a.porn:porn;
int bit=max(size,a.size);
label:
for(register int i=0; i<bit; i++){
ans.num[i]+=b.num[i]-_this.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
if(ans.num[bit]<0){
memset(ans.num,0,sizeof(short)*BIT);
b^_this;
goto label;
}
}
ans.getsize();
return ans;
}
_int8192 operator-(const _int8192 a) const{
_int8192 ans;
ans.init();
if(porn==a.porn){
_int8192 b=a;
_int8192 _this=*this;
ans.porn=(b.abs()<_this.abs())?porn:!porn;
int bit=max(size,a.size);
if(ans.porn!=porn) b^_this;
for(register int i=0; i<bit; i++){
ans.num[i]+=_this.num[i]-b.num[i];
if(ans.num[i]<0){
ans.num[i]+=10;
ans.num[i+1]--;
}
}
}
else{
ans.porn=porn;
_int8192 b=a;
b=b.opp();
int bit=max(size,b.size);
for(register int i=0; i<bit; i++){
ans.num[i]+=num[i]+b.num[i];
ans.num[i+1]=ans.num[i]/10;
ans.num[i]%=10;
}
}
ans.getsize();
return ans;
}
void operator+=(const _int8192 a){
*this=*this+a;
}
void operator-=(const _int8192 a){
*this=*this-a;
}
void operator++(){
*this=*this+_int8192(1);
}
_int8192 operator++(int){
_int8192 *ret=this;
++*this;
return *ret;
}
void operator--(){
*this=*this-_int8192(1);
}
_int8192 operator--(int){
_int8192 *ret=this;
--*this;
return *ret;
}
_int8192 operator*(const _int8192 a) const{
_int8192 ans;
ans.init();
ans.porn=(porn==a.porn)?true:false;
for(register int i=0; i<size; i++){
for(register int j=0; j<a.size; j++){
ans.num[i+j]+=num[i]*a.num[j];
}
}
for(register int i=0; i<BIT; i++){
ans.num[i+1]+=ans.num[i]/10;
ans.num[i]%=10;
}
ans.getsize();
return ans;
}
_int8192 operator/(const _int8192 a) const{
if(a==_int8192(0)){
cout<<"Runtime Error! Zero(0) cannot be a divisor!";
_int8192 re;
re.init();
re.num[0]=-8;
return re;
}
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
ans=_int8192(0);
while(_this>=b){
_this-=b;
ans++;
}
ans.porn=ans.porn=(porn==a.porn)?true:false;
ans.getsize();
return ans;
}
_int8192 operator%(const _int8192 a) const{
if(a==_int8192(0)){
cout<<"Runtime Error! Zero(0) cannot be a divisor!";
_int8192 re;
re.init();
re.num[0]=-8;
return re;
}
_int8192 ans;
ans.init();
_int8192 _this=*this;
_int8192 b=a;
_this=_this.abs();
b=b.abs();
while(_this>=b) _this-=b;
if(porn==a.porn) ans=_this;
else ans=b-_this;
ans.porn=a.porn;
ans.getsize();
return ans;
}
void operator*=(const _int8192 a){
*this=*this*a;
}
void operator/=(const _int8192 a){
*this=*this/a;
}
void operator%=(const _int8192 a){
*this=*this%a;
}
};
int main(){
_int8192 n,m;
n.read();
m.read();
//不知道输出啥
return 0;
}
为了 int 与 _int8192 更好地结合,使 _int8192 类型也可以直接+-*/%整型常数,我将几乎所有 operator 多加了1~2个包含 int 的功能相同的 operator
New Code:
_int8192 operator+(const int a) const{
return *this+_int8192(a);
}
_int8192 operator-(const int a) const{
return *this-_int8192(a);
}
_int8192 operator*(const int a) const{
return *this*_int8192(a);
}
_int8192 operator/(const int a) const{
return *this/_int8192(a);
}
_int8192 operator%(const int a) const{
return *this%_int8192(a);
}
bool operator<(const int a) const{
return *this<_int8192(a);
}
bool operator>(const int a) const{
return *this>_int8192(a);
}
bool operator==(const int a) const{
return *this==_int8192(a);
}
bool operator!=(const int a) const{
return *this!=_int8192(a);
}
bool operator>=(const int a) const{
return *this>=_int8192(a);
}
bool operator<=(const int a) const{
return *this<=_int8192(a);
}
void operator+=(const int a){
*this+=_int8192(a);
}
void operator-=(const int a){
*this-=_int8192(a);
}
void operator*=(const int a){
*this*=_int8192(a);
}
void operator/=(const int a){
*this/=_int8192(a);
}
void operator%=(const int a){
*this%=_int8192(a);
}
void operator=(int a){
*this=_int8192(a);
}
friend _int8192 operator+(int a,_int8192 b){
return _int8192(a)+b;
}
friend _int8192 operator-(int a,_int8192 b){
return _int8192(a)-b;
}
friend _int8192 operator*(int a,_int8192 b){
return _int8192(a)*b;
}
friend _int8192 operator/(int a,_int8192 b){
return _int8192(a)/b;
}
friend _int8192 operator%(int a,_int8192 b){
return _int8192(a)%b;
}
Code:
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
class _int8192{
private:
static const int BIT=1000;
short num[BIT+1];
inline void getsize(){
int i=BIT;
while(i>=0 && num[i]==0) i--;
if(i==-1) size=1;
else size=i+1;
}
short cmp[BIT+1];
bool PrintBool(){
memset(cmp,0,sizeof(cmp));
if(memcmp(num,cmp,sizeof(short)*BIT)==0) return true;
return false;
}
public:
int size;
bool porn=true;
void init(){
for(register int i=0; i<=BIT; i++){
num[i]=0;
}
}
static _int8192 intTO_int8192(int n){
_int8192 ans;
ans.init();
if(n<0){
ans.porn=false;
n=-n;
}
else ans.porn=true;