자릿수야 늘릴수 있을거고..
콤파로 표현하는 방법은 배울수 있을듯...ㅅㅅ;
//**************************************
//
// Name: Cash Handling Class (1234 to 1,
// 234)
// Description:C\C++ cannot handle bussi
// ness type number format e.g. 1,234,567.4
// 4. moneyString class allows you to pass
// it a number which it formats, adds, subt
// racts using overloaded operators
// By: Mohammed Ali Akbani
//
//This code is copyrighted and has// limited warranties.Please see
http://
// www.1CPlusPlusStreet.com/xq/ASP/txtCodeI
// d.3154/lngWId.3/qx/vb/scripts/ShowCode.h
// tm//for details.//**************************************
//
/*
Author: Mohammed Ali Akbani
Level : Intermediate
E-mail: duke@samwonline.com
Web Site:
http://www.akbani.20m.com
Please do not repost it in your name.
*/
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
class moneyString
{
private:
long double amount;
long integer;
float fraction;
char amountStr[30];
char fracStr[10];
private:
void format();
void insertComma();
public:
moneyString();
moneyString(long double);
void newValue(long double);
//DISPLAY THE AMOUNT
void display()
{
cout << amountStr;
}
//ALLOWS THE SUBTRACTION OF TWO AMOUNTS
moneyString operator -(moneyString &money)
{
amount = amount - money.amount;
return moneyString(amount);
}
//ALLOWS ADDITION OF TWO AMOUNTS
moneyString operator +(moneyString &money)
{
amount = amount + money.amount;
return moneyString(amount);
}
};
//DEFAULT CONSTRUCTOR
moneyString :: moneyString()
{
amount = 0;
integer = 0;
fraction = 0.0;
for(int i=0; i<=30; i++)
{
amountStr[i] = '\0';
}
for(int j=0; j<=10; j++)
{
fracStr[j] = '\0';
}
}
//ONE ARGUMENT CONSTRUCTOR
moneyString :: moneyString(long double cash)
{
amount = cash;
integer = floor(cash);
fraction = cash - integer;
for(int i=0; i<=30; i++)
{
amountStr[i] = '\0';
}
for(int j=0; j<=10; j++)
{
fracStr[j] = '\0';
}
format();
}
//CHANGE THE CURRENT AMOUNT TO NEW ONE
void moneyString :: newValue(long double cash)
{
amount = cash;
integer = floor(cash);
fraction = cash - integer;
for(int i=0; i<=30; i++)
{
amountStr[i] = '\0';
}
for(int j=0; j<=10; j++)
{
fracStr[j] = '\0';
}
format();
}
//CONVERT THE AMOUNT TO STRING AND FORMA
// T IT
void moneyString :: format()
{
//convert the integer part to string
ltoa(integer, amountStr, 10);
//insert dot in the string
amountStr[strlen(amountStr)] = '.';
//multiply the fraction part by hundred to make it integer
fraction *= 100;
//convert the fraction part to string
itoa(fraction, fracStr, 10);
//add the fraction part after the dot
strcat(amountStr, fracStr);
insertComma();
}
//INSERT THE COMMAS IN THE STRING
void moneyString :: insertComma()
{
int count = 0;
int counter = 1;
//increment counter to index where . is fount
while( amountStr[count] != '.' )
{
count++;
}
while( count>0 )
{
//if three digit have passed insert comma
if( (counter==3) && (amountStr[count-1] != '.') && (count!=1))
{
count--;
//move the digits forward
for(int i=strlen(amountStr); i>=count; i--)
{
amountStr[i+1] = amountStr[i];
}
amountStr[count] = ',';
counter = 1;
}
count--;
if(counter==3) counter=1; else counter++;
}
}
void main()
{
// clrscr();
//Create and initialize objects
moneyString sales(75453.89);
moneyString purchases(60278.50);
moneyString profit;
//Using overloaded operator calculate profit
profit = sales - purchases;
cout << "\nSales: "; sales.display();
cout << "\nPurchase : "; purchases.display();
cout << "\nProfit: "; profit.display();
cout << endl;
//Change the values
sales.newValue(102560.25);
purchases.newValue(85642.66);
profit = sales - purchases;
cout << "\nSales: "; sales.display();
cout << "\nPurchase : "; purchases.display();
cout << "\nProfit: "; profit.display();
getch();
}