home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CHKBOOK.PAK / DOLLCENT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  6.4 KB  |  295 lines

  1. // dollcent.cpp : implementation of DDX_DollarsCents
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "chkbook.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. void SetDollarsCents(HWND hWnd, DWORD dwCents);
  19. CString HundredsTensOnes(DWORD dwHundredsTensOnes);
  20. CString TensOnes(DWORD dwTensOnes);
  21.  
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // Public functions
  25.  
  26. void AFXAPI DDX_DollarsCents(CDataExchange* pDX, int nIDC, DWORD& dwCents)
  27. {
  28.     HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
  29.     if (pDX->m_bSaveAndValidate)
  30.     {
  31.         if (!GetDollarsCents(hWndCtrl, dwCents))
  32.         {
  33.             AfxMessageBox(IDS_INVALID_DOLLAR_CENT);
  34.             pDX->Fail();
  35.         }
  36.     }
  37.     else
  38.     {
  39.         SetDollarsCents(hWndCtrl, dwCents);
  40.     }
  41. }
  42.  
  43.  
  44. BOOL GetDollarsCents(CWnd* pWnd, DWORD& dwCents)
  45. {
  46.     ASSERT(pWnd != NULL);
  47.     return GetDollarsCents(pWnd->m_hWnd, dwCents);
  48. }
  49.  
  50. BOOL GetDollarsCents(HWND hWnd, DWORD& dwCents)
  51. {
  52.     TCHAR szWindowText[20];
  53.     ::GetWindowText(hWnd, szWindowText, 19);
  54.     DWORD dwDollars;
  55.     int nCents;
  56.     TCHAR* psz;
  57.     TCHAR* pszDollars;
  58.     TCHAR* pszCents;
  59.  
  60.     // strip leading blanks
  61.     for (pszDollars = szWindowText;  *pszDollars == ' ';  pszDollars++)
  62.     {
  63.         if (*pszDollars == 0)
  64.         {
  65.             dwCents = 0;
  66.             return TRUE;
  67.         }
  68.     }
  69.  
  70.     // parse dollar amount, before optional decimal point
  71.     for (psz = pszDollars; (*psz != '.') && (*psz != ' ') && (*psz != 0); psz++)
  72.     {
  73.         if ((*psz < '0') || (*psz > '9'))
  74.             return FALSE;
  75.     }
  76.     BOOL bDollarsOnly = (*psz == 0);
  77.     *psz = 0;
  78.  
  79.     if (_tcslen(pszDollars) > 8)
  80.         return FALSE;
  81.     if (_tcslen(pszDollars) == 0)
  82.     {
  83.         dwDollars = 0L;
  84.     }
  85.     else
  86.     {
  87.         dwDollars = _ttol(pszDollars);
  88.         if (dwDollars > ((DWORD)0xffffffff)/100)
  89.             return FALSE;
  90.     }
  91.  
  92.     if (bDollarsOnly)
  93.     {
  94.         nCents = 0;
  95.     }
  96.     else  // decimal point was found
  97.     {
  98.         // parse cents
  99.         for (pszCents = ++psz; (*psz != 0) && (*psz != ' '); psz++)
  100.         {
  101.             if ((*psz < '0') || (*psz > '9'))
  102.                 return FALSE;
  103.         }
  104.         if (*psz == ' ')
  105.         {
  106.             for (psz++ ; *psz != 0; psz++)
  107.             {
  108.                 if (*psz != ' ')
  109.                     return FALSE;
  110.             }
  111.         }
  112.  
  113.         int nCentsStrLen = _tcslen(pszCents);
  114.         switch (nCentsStrLen)
  115.         {
  116.             case 0:
  117.                 nCents = 0;
  118.                 break;
  119.             case 1:
  120.                 nCents = _ttoi(pszCents) * 10;
  121.                 break;
  122.             case 2:
  123.                 nCents = _ttoi(pszCents);
  124.                 break;
  125.             default:
  126.                 return FALSE;
  127.         }
  128.     }
  129.  
  130.     dwCents = dwDollars * 100 + nCents;
  131.     return TRUE;
  132. }
  133.  
  134.  
  135. void SetDollarsCents(CWnd* pWnd, DWORD dwCents)
  136. {
  137.     ASSERT(pWnd != NULL);
  138.     SetDollarsCents(pWnd->m_hWnd, dwCents);
  139. }
  140.  
  141. void SetDollarsCents(HWND hWnd, DWORD dwCents)
  142. {
  143.     // Convert the DWORD dollars/cents value to a string and
  144.     // display it in the dollars/cents control.
  145.  
  146.     // If the dollar cent field has been previously determined by
  147.     // DDX_DollarsCents() to be invalid, then don't update it.
  148.     // Leave the invalid data in the field so the user can correct
  149.     // it, rather than replace it with the literal translation
  150.     // of the INVALID_DOLLARS_CENTS #define value.
  151.  
  152.     if (dwCents == INVALID_DOLLARS_CENTS)
  153.         return;
  154.  
  155.     CString str = GetDollarsCentsFormatted(dwCents);
  156.     ::SetWindowText(hWnd, str.GetBufferSetLength(20));
  157. }
  158.  
  159.  
  160. CString GetDollarsCentsFormatted(DWORD dwCents)
  161. {
  162.     if (dwCents == INVALID_DOLLARS_CENTS)
  163.     {
  164.         CString str;
  165.         str.LoadString(IDS_UNKNOWN);
  166.         return str;
  167.     }
  168.  
  169.     DWORD dwDollars = dwCents / 100;
  170.     WORD wCents = (WORD)(dwCents - 100 * dwDollars);
  171.  
  172.     CString str;
  173.     str.Format(_T("%lu.%02u"), dwDollars, wCents);
  174.     return str;
  175. }
  176.  
  177.  
  178. CString GetDollarsCentsText(DWORD dwCents)
  179. {
  180.     CString str, strTemp;
  181.     if (dwCents == INVALID_DOLLARS_CENTS)
  182.     {
  183.         str.LoadString(IDS_UNKNOWN);
  184.         return str;
  185.     }
  186.  
  187.     DWORD dwDollars = dwCents / 100;
  188.     WORD wCents = (WORD)(dwCents - (dwDollars * 100));
  189.     if (dwDollars == 0L)
  190.     {
  191.         str.LoadString(IDS_ONES_0);   // "Zero"
  192.         str += ' ';
  193.     }
  194.     else
  195.     {
  196.         if (dwDollars >= 1000000)
  197.         {
  198.             DWORD dwMillions = dwDollars / 1000000;
  199.             CString strMillions = HundredsTensOnes(dwMillions);
  200.             strTemp.LoadString(IDS_MILLION);    // "Million"
  201.             str = strMillions;
  202.             str += ' ';
  203.             str += strTemp;
  204.             str += ' ';
  205.             dwDollars -= (dwMillions * 1000000);
  206.         }
  207.         if (dwDollars >= 1000)
  208.         {
  209.             DWORD dwThousands = dwDollars / 1000;
  210.             CString strThousands = HundredsTensOnes(dwThousands);
  211.             strTemp.LoadString(IDS_THOUSAND);   // "Thousand"
  212.             str += strThousands;
  213.             str += ' ';
  214.             str += strTemp;
  215.             str += ' ';
  216.             dwDollars -= (dwThousands * 1000);
  217.         }
  218.         if (dwDollars > 0)
  219.         {
  220.             CString strHundredsTensOnes = HundredsTensOnes(dwDollars);
  221.             str += strHundredsTensOnes;
  222.             str += ' ';
  223.         }
  224.     }
  225.     TCHAR szCents[10];
  226.     CString strCents(_itot(wCents, szCents, 10));
  227.     strTemp.LoadString(IDS_AND);    // "and"
  228.     str += strTemp;
  229.     str += ' ';
  230.     str += strCents;
  231.     strTemp.LoadString(IDS_HUNDRETHS_DOLLARS);  // "/100ths Dollars"
  232.     str += strTemp;
  233.     return str;
  234. }
  235.  
  236. /////////////////////////////////////////////////////////////////////////////
  237. // Implementation
  238.  
  239. CString HundredsTensOnes(DWORD dwHundredsTensOnes)
  240. {
  241.     CString str, strTemp;
  242.     if (dwHundredsTensOnes >= 100)
  243.     {
  244.         DWORD dwHundreds = dwHundredsTensOnes / 100;
  245.         CString strHundreds;
  246.         strHundreds.LoadString(IDS_ONES_0 + dwHundreds);
  247.         strTemp.LoadString(IDS_HUNDRED);
  248.         str = strHundreds;
  249.         str += ' ';
  250.         str += strTemp;
  251.         str += ' ';
  252.         dwHundredsTensOnes -= (dwHundreds * 100);
  253.     }
  254.     if (dwHundredsTensOnes > 0)
  255.     {
  256.         CString strTensOnes = TensOnes(dwHundredsTensOnes);
  257.         str += strTensOnes;
  258.     }
  259.     return str;
  260. }
  261.  
  262.  
  263. CString TensOnes(DWORD dwTensOnes)
  264. {
  265.     CString str, strTemp;
  266.     if (dwTensOnes > 19)
  267.     {
  268.         DWORD dwTens = dwTensOnes / 10;
  269.         strTemp.LoadString(IDS_TENS_0 + dwTens);
  270.         str += strTemp;
  271.         dwTensOnes -= (dwTens * 10);
  272.         if (dwTensOnes > 0)
  273.         {
  274.             CString strOnes;
  275.             strOnes.LoadString(IDS_ONES_0 + dwTensOnes);
  276.             str += '-';
  277.             str += strOnes;
  278.         }
  279.     }
  280.     else
  281.     if (dwTensOnes >= 10)
  282.     {
  283.         CString strTeens;
  284.         strTeens.LoadString(IDS_TEENS_10 + dwTensOnes - 10);
  285.         str += strTeens;
  286.     }
  287.     else
  288.     {
  289.         CString strOnes;
  290.         strOnes.LoadString(IDS_ONES_0 + dwTensOnes);
  291.         str += strOnes;
  292.     }
  293.     return str;
  294. }
  295.