// File: decimal.cpp // Description: Decimal class - implementation // Project: - // Version: 1.00 // Revision: B // Content: class decimal // // -1- decimal::decimal(long int x) // -2- decimal::decimal(char *s) // -3- decimal::decimal(double y) // // -4- istream& operator>>(istream &in, decimal &d) // -5- ostream& operator<<(ostream &out, decimal &d) // // -6- decimal operator +(decimal &a, decimal &b) // -7- decimal operator *(decimal &a, decimal &b) // -8- decimal operator /(decimal &a, decimal &b) // -9- decimal operator -(decimal &a, decimal &b) // // -10- int decimal::decimalPartLenght() // -11- decimal& decimal::operator-() // -12- decimal::operator double () // -13- decimal& decimal::operator=(decimal &d) // // Language: Zortech C++ ver. 3.0 // OS: MS-DOS // Date: 04.04.1997 // Author: pf // Mail: fabo@fmph.uniba.sk // Phone: 00421 7 729248 // 00421 7 724000 kl. 516 // 00421 905 615536 // 00421 7 729248 // // Copyright Centaur s.r.o. // MFF UK, Mlynska dolina // 842 15 Bratislava // Slovakia // // The copyright to the computer program(s) herein // is the property of company Centaur s.r.o., Slovakia. // The program(s) may by used and/or copied only with // the written permission of company Centaur s.r.o. // or in accordance with the terms and conditions // stipulated in the agreement/contract under which // the program(s) have been suplied. // // History // Date Who V/R What // 04.04.1997 pf 1.00/A creation // 14.05.1997 pf 1.00/b operator / // #include "decimal.hpp" // Initializing for static members long int decimal::decimalLenght = DECIMAL_LENGHT; long int decimal::decimalMask = (long int) pow(10,DECIMAL_LENGHT); long int decimal::decimalMask_2 = (long int) pow(10,DECIMAL_LENGHT/2); // -1- // decimal::decimal(long int x) { decimalPart=0; integerPart=x; } // -2- // decimal::decimal(char *s) { // string format (for decimalLenght = 6) // "iiiiiiiiiiii.dddddd" // // "-iiiiiiiiiiii.dddddd" const int bufSize=decimalLenght+1; char *intBuf=new char[bufSize*2]; // buff for integer part = 2* dec. part char *decBuf=new char[bufSize]; // buff for decimal part for(int k=0; k<(bufSize-1); k++) decBuf[k]='0'; // init decimal buffer with "0 .. 0" decBuf[bufSize-1]=0; // end of buffer for(k=0; k<(bufSize*2-1); k++) intBuf[k]='0'; // init integer buffer with "0 .. 0" intBuf[bufSize-1]=0; // end of buffer int q=0; // flag for negative number, // 0 -> number is positiv if( s[0]=='-') q=1; // number is negativ for(k=q; k<(bufSize*2-1); k++) { if( isdigit(s[k]) ) intBuf[k]=s[k]; // read data for integer part else break; } intBuf[k]=0; int j=0; if( s[k]=='.') // read data for decimal part { k++; // next digit for(j=0; j<(bufSize-1); j++) { if( isdigit(s[k+j]) ) decBuf[j]=s[k+j]; else break; } } integerPart=atol(intBuf); // convert strings to long int decimalPart=atol(decBuf); if(q==1) // 1 -> negative number { integerPart*=-1; decimalPart*=-1; } delete intBuf; delete decBuf; } // -3- // decimal::decimal(double y) { integerPart=floor(y); if(y<0.0) integerPart+=1.0; // corect for negative values decimalPart=(y-integerPart)*decimalMask; } // -4- // istream& operator>>(istream &in, decimal &d) { char buff[64]; in>>buff; decimal tmp=buff; // converting form string to decimal d=tmp; return in; } // -5- // ostream& operator<<(ostream &out, decimal &d) { if ( d.integerPart==0 && d.decimalPart<0 ) cout<<'-' ; out<