1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
#include <iostream>
#include <cstring>
using std::istream;
using std::ostream;
using std::cin;
using std::cout;
using std::endl;
#define M 10100
#define N 10100 //10100
class bigInteger
{
private:
char *Int;
public:
bigInteger(char *num);
bigInteger(char*num,int size);
~bigInteger();
void Insert(char num);
bool operator !=( bigInteger &num);
bigInteger & operator +=( bigInteger &num);
friend istream & operator>>(istream &input, bigInteger &num);
friend ostream & operator<<(ostream &output, bigInteger &num);
friend void tranCtoN(char Int[]);
friend void tranNtoC(char Int[]);
};
int main()
{
// while(cin)
{
bigInteger sum("0",N);//累加和
bigInteger end("0");//结束数据
bigInteger num("\0",M); //存放读入的大整数
while(cin>>num && num!=end)
{
sum+=num;
}
cout<<sum<<endl;
}
return 0;
}
bigInteger::bigInteger(char *num)
{
Int=new char [strlen(num)+1];
strcpy(Int,num);
}
bigInteger::bigInteger(char *num,int size)
{
int temp;
temp=size>(strlen(num))?size:(strlen(num));
Int=new char [temp+1];
strcpy(Int,num);
}
bigInteger::~bigInteger()
{
delete [] Int;
}
void tranCtoN(char Int[])
{
int i;
for(i=0;i<N && Int[i]!='\0';i++)
{
Int[i]=Int[i]-'0';
}
}
void tranNtoC(char Int[])
{
int i;
for(i=0;i<N && Int[i]!='\0';i++)
{
if(Int[i]=='*')
{
Int[i]=Int[i]-'*'+'0';
}
else
{
Int[i]=Int[i]+'0';
}
}
}
void bigInteger:: Insert(char num)
{
int i;
for(i=0;Int[i]!='\0';i++);
for(;i>=0;i--)
{
Int[i+1]=Int[i];
}
Int[i+1]=num;
}
bool bigInteger:: operator !=( bigInteger &num)
{
int i;
if(strlen(Int)!=strlen(num.Int))
{
return true;
}
else
{
for (i=0;Int[i]!='\0';i++)
{
if(Int[i]!=num.Int[i])
{
return true;
}
}
return false;
}
}
bigInteger & bigInteger:: operator +=(bigInteger &num)
{
int i,j;
int temp;
i=strlen(Int);
j=strlen(num.Int);
temp=i>j?i:j;
if(temp>i)
{
for(i;i<temp;i++)
{
Insert('0');
}
}
else if(temp>j)
{
for(j;j<temp;j++)
{
num.Insert ('0');
}
}
tranCtoN(Int);
tranCtoN(num.Int);
i=temp;
i--;
for(;i>=0;i--)
{
if(i==0)
{
if((num.Int[i]+Int[i])/10)
{
char tempchar=Int[i];
Int[i]=(num.Int[i]+Int[i])%10;
Insert((num.Int[i]+tempchar)/10);
}
else
{
Int[i]=(num.Int[i]+Int[i])%10;
}
}
else
{
Int[i-1]=(num.Int[i]+Int[i])/10+Int[i-1];
Int[i]=(num.Int[i]+Int[i])%10;
}
if(Int[i]=='\0')
{
Int[i]='*';
}
}
tranNtoC(Int);
return *this;
}
istream & operator>>(istream &input, bigInteger &num)
{
char temp[1000];
input >> temp;
delete [] num.Int;
num.Int = new char[strlen(temp)+1];
strcpy(num.Int,temp);
return input;
}
ostream & operator<<(ostream &output, bigInteger &num)
{
output << num.Int;
return output;
}
|