home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SAMPLES / CHKBOOK / DOLLCENT.CP_ / DOLLCENT.CP
Encoding:
Text File  |  1993-02-08  |  5.8 KB  |  304 lines

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