时间紧,写的不好,不要见怪啊~~
头文件 :myint.h
---------------------------
#ifndef _MYINT_H
#define _MYINT_H
#include<iostream>
#include<string>
using namespace std;
const int MAX=20;
class MyInt
{
private:
int arry[MAX];
int plus;
int size;
protected:
int bigger(const MyInt & lhs)
{
if(size>lhs.size)
return 1;
else if(size<lhs.size)
return 0; //lhs bigger
else if(size=lhs.size)
{
for(int i=size-1;i>=0;i--)
{
if(arry[i]>lhs.arry[i])
return 1;
else if(arry[i]<lhs.arry[i])
return 0;
}
}
return 0;
}
void add(const MyInt &lhs,int pp)
{
int pp_plus=lhs.plus;
if(pp==1)
pp_plus=(pp_plus+1)%2;
int temp=0;
//-----------------
if(plus==0&&pp_plus==0)
{
for(int i=0;i<MAX;i++)
{ temp=arry[i]+lhs.arry[i];
if(temp>9)
{
temp=temp-10;
arry[i+1]=arry[i+1]+1;
}
arry[i]=temp;
}
}
//--------------------
if(plus==0&&pp_plus==1)
{
if(bigger(lhs)==1)
{
for(int i=0;i<MAX;i++)
{
temp=arry[i]-lhs.arry[i];
if(temp<0)
{
temp=temp+10;
arry[i+1]=arry[i+1]-1;
}
arry[i]=temp;
}
}
else
{
int swap[MAX];
for(int i=0;i<MAX;i++)
swap[i]=arry[i];
for(i=0;i<MAX;i++)
arry[i]=lhs.arry[i];
for( i=0;i<MAX;i++)
{
temp=arry[i]-swap[i];
if(temp<0)
{
temp=temp+10;
arry[i+1]=arry[i+1]-1;
}
arry[i]=temp;
}
}
}
//-------------------------
if(plus==1&&pp_plus==1)
{
for(int i=0;i<MAX;i++)
{ temp=arry[i]+lhs.arry[i];
if(temp>9)
{
temp=temp-10;
arry[i+1]=arry[i+1]+1;
}
arry[i]=temp;
}
}
//-------------------
if(plus==1&&pp_plus==0)
{
if(bigger(lhs)==1)
{
for(int i=0;i<MAX;i++)
{
temp=arry[i]-lhs.arry[i];
if(temp<0)
{
temp=temp+10;
arry[i+1]=arry[i+1]-1;
}
arry[i]=temp;
}
}
else
{
int swap[MAX];
for(int i=0;i<MAX;i++)
swap[i]=arry[i];
for(i=0;i<MAX;i++)
arry[i]=lhs.arry[i];
for( i=0;i<MAX;i++)
{
temp=arry[i]-swap[i];
if(temp<0)
{
temp=temp+10;
arry[i+1]=arry[i+1]-1;
}
arry[i]=temp;
}
}
}
}
public:
MyInt() : plus(0),size(0) {
for(int i=0;i<MAX;i++)
arry[i]=0;
}
MyInt(string str)
{
int temp=0;
for(int i=0;i<MAX;i++)
arry[i]=0;
if(str[0]=='-')
{ plus=1;
size=str.size()-1;
for(i=str.size()-1;i>0;i--)
{ temp=int(str[i])-48;
arry[size-i]=temp;
}
}
else
{ plus=0;
size=str.size();
for(i=str.size()-1;i>=0;i--)
{ temp=int(str[i])-48;
arry[size-1-i]=temp;
}
}
}
void operator =(const MyInt &lhs)
{
plus=lhs.plus;
size=lhs.size;
for(int i=0;i<MAX;i++)
{ arry[i]=0;
arry[i]=lhs.arry[i];
}
}
void operator +=( const MyInt &lhs)
{
add(lhs,0);
}
void operator -=(const MyInt &lhs)
{
add(lhs,1);
}
void display()
{
if(plus==1)
cout<<"-";
int flag=0;
for (int i=MAX-1;i>=0;i--)
{
if(arry[i]!=0||flag==1)
{
flag=1;
if(flag==1)
cout<<arry[i];
}
}
}
};
#endif
//----------
头文件 complex.h
#ifndef _COMPLEX_H
#define _COMPLEX_H
#include"myint.h"
class ComplexInt
{
private:
MyInt real;
MyInt unreal;
public:
ComplexInt() : real("0"),unreal("0"){}
ComplexInt(string r,string ur) :real(r),unreal(ur) {}
ComplexInt(string r) :real(r),unreal("0") {}
void operator +=(const ComplexInt & lhs)
{
real+=lhs.real;
unreal+=lhs.unreal;
}
void operator -=(const ComplexInt & lhs)
{
real-=lhs.real;
unreal-=lhs.unreal;
}
void operator =(const ComplexInt & lhs)
{
real=lhs.real;
unreal=lhs.unreal;
}
void display()
{
real.display();
cout<<"+";
unreal.display();
cout<<"i";
}
};
#endif
主函数:main.cpp
#include "complex.h"
#include "myint.h"
void Display(MyInt myint)
{
cout<<"This is MyInt::Display, the result is: ";
myint.display();
cout<<endl;
}
void Display(ComplexInt complex )
{
cout<<"This is ComplexInt::Display, the result is: ";
complex.display();
cout<<endl;
}
void main(int argc, char* argv[])
{
MyInt iM("1234567890123"); // 整数对象初始化
MyInt iN("-4445555544444");
MyInt iResult1;
iResult1 = iM;
iResult1 += iN; // 整数加法操作
MyInt iResult2 = iM;
iResult2 -= iN; // 整数减法操作
// 结果输出
Display(iResult1); // iResult1: -1976419764198
Display(iResult2); // iResult2: 4445555544444
// 实部: 1234567890123;虚部: 4445555544444
ComplexInt ciM("1234567890123", "4445555544444"); // 复数对象初始化
// 实部: -4445555544444;虚部: 0
ComplexInt ciN("-4445555544444"); // 复数对象初始化
ComplexInt ciResult1 = ciM;
ComplexInt ciResult2 = ciM;
ciResult1 += ciN; // 复数加法操作
ciResult2 -= ciN; // 复数减法操作
// 结果输出
Display(ciResult1);
Display(ciResult2);
}