Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ 팁&트릭
[18] bank class ..돈 계산 관련 클래스인디..표시할수 있는 자릿수가 한정되어 있네요..ㅡ,.ㅡ;;
오전&오후 [kkhhyy11] 4614 읽음    2002-06-24 11:37
자릿수야 늘릴수 있을거고..
콤파로 표현하는 방법은 배울수 있을듯...ㅅㅅ;

//**************************************
//    
// 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();
}

+ -

관련 글 리스트
18 bank class ..돈 계산 관련 클래스인디..표시할수 있는 자릿수가 한정되어 있네요..ㅡ,.ㅡ;; 오전&오후 4614 2002/06/24
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.