codice:
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <ctype.h>
#include <vector>
#define DEBUG
//#define TEST
using namespace std;
//123: Cifre[0]=3; Cifre[1]=2; Cifre[2]=1;
class Numero
{
public:
int* Cifre;
int Len;
int PosVirgola;
Numero()
{
Numero(0);
}
Numero (int len)
{
PosVirgola=0;
Len=len;
Cifre=new int[len];
//Azzera
for (int i=0;i<len;i++)
Cifre[i]=0;
}
//Si puo' non utilizzare len
Numero (int len,int n)
{
PosVirgola=0;
if (len==-1)
Len=Numero::LenNumero(n);
else
Len=len;
Cifre=new int[Len];
int i=0;
while (n>0)
{
Cifre[i++]=(n%10);
n/=10;
}
}
int& operator[](int a)
{
return Cifre[a];
}
static int LenNumero(int a);
};
ostream& operator<<(ostream& s, Numero a)
{
int Zero=0;
while (Zero<a.PosVirgola && a[Zero]==0) Zero++;
bool PrintVirgola=true;
if (Zero==a.PosVirgola)PrintVirgola=false;
int j=a.Len-1;
if (a.Len==a.PosVirgola) s<<"0.";
while (j>=Zero)
{
s<<a[j];
if ((j==a.PosVirgola) && PrintVirgola)
s<<".";
j--;
}
return s;
}
istream& operator>>(istream& s, Numero& a)
{
//Legge tutti gli eventuali spazi iniziali
char c;
while ((s.get(c)) && (isspace(c)));
int* Arr=new int[1000]; int i=0; int PosVirgolaEnd=-1;
do
{
if (c=='.')
PosVirgolaEnd=i;
else Arr[i++]=c-'0';
}
while((s.get(c)) && (!isspace(c)));
//Sistema la virgola
a=Numero(i);
if (PosVirgolaEnd==-1)
a.PosVirgola=0;
else
a.PosVirgola=i-PosVirgolaEnd;
//Sistema le cifre
for (int j=0;j<i;j++)
{
//Scambia
a.Cifre[i-j-1]=Arr[j];
}
return s;
}
int Numero::LenNumero(int a)
{
int Cont=0;
while (a>0)
{
a/=10;
Cont++;
}
return Cont;
}
Numero operator + (Numero a,Numero b)
{
//Inizializza
Numero Ret(max(a.Len,b.Len)+1);
//Somma cifra per cifra
int Resto=0;
int j=0;
for(j=0;j<min(a.Len,b.Len);j++)
{
int Somma=a.Cifre[j]+b.Cifre[j];
Ret.Cifre[j]=(Somma+Resto)%10;
if (Somma+Resto>=10)
Resto=1;
else
Resto=0;
}
//bool Enter=false;
while (j<max(a.Len,b.Len))
{
int Somma=(a.Len>b.Len)?(a.Cifre[j]):(b.Cifre[j]);
Ret.Cifre[j]=(Somma+Resto)%10;
if (Somma+Resto>=10)
Resto=1;
else
Resto=0;
j++;
}
if (Resto==0) Ret.Len--;
else
Ret[Ret.Len-1]=1;
return Ret;
}
Numero operator * (Numero a,Numero b)
{
#ifdef DEBUG
cout<<a<<b;
#endif
Numero ProdN(1,0); int ProdNDec=0;
for (int i=0;i<b.Len;i++)
{
Numero Somma(-1,0);
int Dec=0;
for(int j=0;j<a.Len;j++)
{
Numero TT(-1,b[i]*a[j]);int TTCont=TT.Len-1;
Numero Temp(TT.Len+Dec);
for (int k=Temp.Len-1;k>=0;k--)
{
if (TTCont>=0)
Temp[k]=TT[TTCont--];
else
Temp[k]=0;
}
Somma= Somma+Temp;
Dec++;
}
Numero SommaTemp(Somma.Len+ProdNDec);
int SommaCont=Somma.Len-1;
for (int k=SommaTemp.Len-1;k>=0;k--)
{
if(SommaCont>=0)
SommaTemp[k]=Somma[SommaCont--];
else
SommaTemp[k]=0;
}
#ifdef DEBUG
//cout<<"SommaTemp:"<<SommaTemp<<"\n";
#endif
ProdN=ProdN+SommaTemp;
ProdNDec++;
}
ProdN.PosVirgola=a.PosVirgola+b.PosVirgola;
return ProdN;
}
Numero Potenza(Numero r,int ex)
{
Numero Ret(1);
Ret[0]=1;
Ret.PosVirgola=0;
for(int i=0;i<ex;i++)
{
Ret=Ret*r;
#ifdef DEBUG
//cout<<r<<" "<<ex<<" Esponente("<<i<<"):"<<Ret<<"\n";
#endif
}
return Ret;
}
void Test()
{
/*Numero a(3);
Numero b(2);
a.PosVirgola=2;
b.PosVirgola=1;
a[0]=9;
a.Cifre[1]=0;
a.Cifre[2]=1;
b.Cifre[0]=1;
b.Cifre[1]=1;
Numero Res=a*b;*/
Numero a; Numero n;
cin>>a>>n;
cout<<a+n;
}
int main(void)
{
#ifdef TEST
Test();
#else
int cont=0;
int n;
Numero s;
vector<Numero> Res;
while(cin>>s>>n)
{
Res.push_back(Potenza(s,n));
cout<<Res[Res.size()-1]<<"\n";
}
for(int i=0;i<Res.size();i++)
cout<<Res[i];
#endif
return 0;
}