readprice.cpp
资源名称:停车场管理系统.zip [点击查看]
上传用户:trilite
上传日期:2007-04-24
资源大小:261k
文件大小:7k
源码类别:
酒店行业
开发平台:
Visual C++
- // ReadPrice.cpp: implementation of the CReadPrice class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "ReadPrice.h"
- #include "Resource.h"
- #include "mmsystem.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CString CReadPrice::RootDir; //声音文件所在的目录。
- CString CReadPrice::m_strsNumber[10]; //存放0~9个数字的wav文件。
- CString CReadPrice::m_strsHolder[14]; //存放占位信息的wav文件。
- char CReadPrice::m_wavstrWelcome[20]; //存放欢迎声音
- char CReadPrice::m_wavstrByeBye[20]; //存放结束声音
- CReadPrice::CReadPrice()
- {
- m_nStartIndex = -1;
- }
- CReadPrice::~CReadPrice()
- {
- }
- BOOL CReadPrice::Format2Voice(double price)
- {
- if(price > 1E8) //如果价格大于壹亿,那么认为数字太大!
- {
- AfxMessageBox("数字太大!", MB_OK);
- return FALSE;
- }
- memset(&m_Voice, 0, sizeof(m_Voice));
- for(int i = 0; i < sizeof(m_Voice.m_pstrHolder) / sizeof(m_Voice.m_pstrHolder[0]); i++)
- m_Voice.m_pstrHolder[i] = &m_strsHolder[i];
- //将"个"去掉
- m_Voice.m_pstrHolder[0] =
- m_Voice.m_pstrHolder[5] =
- m_Voice.m_pstrHolder[10] = NULL;
- //格式化价格数字
- CString strTemp;
- strTemp.Format("%.2f", price);
- strTemp.MakeReverse();
- TRACE("%sn", strTemp);
- //依照原则来处理数字
- strTemp.SetAt(2, '#');//将小数点去掉
- if(strTemp.GetLength() > 7)//增加万位
- {
- #if(_MSC_VER==1100)
- strTemp = strTemp.Left(7) + '#' + strTemp.Right(strTemp.GetLength() - 7);
- #else
- #if(_MSC_VER>1100)
- strTemp.Insert(7, '#');
- #endif
- #endif
- TRACE("%sn", strTemp);
- }
- if(strTemp.GetLength() > 12)//增加亿位
- {
- #if(_MSC_VER==1100)
- strTemp = strTemp.Left(12) + '#' + strTemp.Right(strTemp.GetLength() - 12);
- #else
- #if(_MSC_VER>1100)
- strTemp.Insert(12, '#');
- #endif
- #endif
- TRACE("%sn", strTemp);
- }
- strTemp.MakeReverse();
- strTemp = CString('#', 14) + strTemp;
- strTemp = strTemp.Right(14);//补满14位
- ASSERT(sizeof(m_Voice.m_bstrPrice) == 14);
- strncpy(m_Voice.m_bstrPrice, LPCTSTR(strTemp), sizeof(m_Voice.m_bstrPrice));
- for( i = 0; i < sizeof(m_Voice.m_bstrPrice); i++)
- {
- if(m_Voice.m_bstrPrice[i] == '#')
- {
- m_Voice.m_bstrPrice[i] = 'x0';
- }
- }
- //格式化数字相应的声音文件。
- //第一次扫描
- for( i = 0; i < sizeof(m_Voice.m_bstrPrice); i++)
- {
- if(m_Voice.m_bstrPrice[i] != 'x0')
- {
- m_Voice.m_pstrNumber[i] =
- &this->m_strsNumber[m_Voice.m_bstrPrice[i] - '0'];
- }
- }
- //第二次扫描
- bool LastCharIs0=false;
- for( i = 0; i < sizeof(m_Voice.m_bstrPrice); i++)
- {
- if(m_Voice.m_bstrPrice[i] == 'x0')
- continue;
- if(m_Voice.m_bstrPrice[i] == '0')
- {
- //占位信息不读
- m_Voice.m_pstrHolder[i] = NULL;
- //如果0在个位,那么数字0不读。
- if(LastCharIs0 == true)
- {
- //如果数字连续为0,除最后一个外都不读(只读最后一个0)。
- m_Voice.m_pstrNumber[i - 1] = NULL;
- }
- if(i == 0 || i == 5 || i == 10)
- {
- m_Voice.m_pstrNumber[i] = NULL;
- }
- LastCharIs0 = true;
- }
- else
- {
- LastCharIs0 = false;
- }
- }
- if(m_Voice.m_bstrPrice[sizeof(m_Voice.m_bstrPrice) - 1] == '0')
- {
- //如果0在"分"位,则不读。
- m_Voice.m_pstrNumber[sizeof(m_Voice.m_bstrPrice) - 1] = NULL;
- }
- return TRUE;
- }
- BOOL CReadPrice::ReadPrice(double price)
- {
- if(Format2Voice(price))
- {
- ASSERT(sizeof(m_Voice.m_bstrPrice) == 14);
- for(int i = 0; i < sizeof(m_Voice.m_bstrPrice); i++)
- {
- //查找第一个开始的数字。
- if(m_Voice.m_bstrPrice[i] != 'x0' && m_Voice.m_bstrPrice[i] != '0')
- {
- break;
- }
- }
- i < sizeof(m_Voice.m_bstrPrice) ? m_nStartIndex = i : -1;
- return (Play() == 0);
- //AfxBeginThread(&CReadPrice::Play, NULL);
- //return TRUE;
- }
- else
- return FALSE;
- }
- void CReadPrice::Init()
- {
- //初始化CReadPrice类中,静态成员变量。
- char *szLabels[14] = {
- "个","亿","千","百","十",
- "个","万","千","百","十",
- "个","元","角","分"};
- HKEY hkResult = 0;
- CString strSub;
- strSub = "Software\ParkSys\Voice";
- int status = ::RegOpenKeyEx(HKEY_CURRENT_USER,
- LPCTSTR(strSub), NULL, KEY_READ , &hkResult);
- if(status != ERROR_SUCCESS)
- {
- TRACE("Open registry fail!n");
- AfxMessageBox("打开注册表失败!", MB_OK | MB_ICONERROR);
- THROW(new CException(TRUE));
- }
- char szTemp[20];
- unsigned char szData[50];
- unsigned long lpcbData;
- TRY
- {
- //read 0~10
- CReadPrice::RootDir.Empty();
- lpcbData = sizeof(szData);
- status = RegQueryValueEx(hkResult, "RootDir", NULL, NULL,
- szData, &lpcbData);
- CReadPrice::RootDir = szData;
- if(!CReadPrice::RootDir.IsEmpty())
- {
- if(CReadPrice::RootDir.Right(1) != '\')
- CReadPrice::RootDir += "\";
- }
- for(int i = 0; i < 10; i++)
- {
- wsprintf(szTemp, "%d", i);
- CReadPrice::m_strsNumber[i].Empty();
- lpcbData = sizeof(szData);
- status = RegQueryValueEx(hkResult, (char*)szTemp, NULL, NULL,
- szData, &lpcbData);
- if(status != ERROR_SUCCESS)
- {
- THROW(new CException(TRUE));
- }
- TRACE("%d=%sn", i, szData);
- CReadPrice::m_strsNumber[i] = szData;
- }
- for(i = 0; i< sizeof(szLabels) / sizeof(szLabels[0]); i++)
- {
- //读取占位信息
- lpcbData = sizeof(szData);
- status = RegQueryValueEx(hkResult, szLabels[i], NULL, NULL,
- szData, &lpcbData);
- if(status != ERROR_SUCCESS)
- {
- THROW(new CException(TRUE));
- }
- TRACE("%s=%sn", szLabels[i], szData);
- CReadPrice::m_strsHolder[i] = szData;
- }
- lpcbData = sizeof(szData);
- status = RegQueryValueEx(hkResult, "Welcome", NULL, NULL,
- szData, &lpcbData);
- if(status != ERROR_SUCCESS)
- {
- THROW(new CException(TRUE));
- }
- wsprintf(m_wavstrWelcome, "%s", szData);
- lpcbData = sizeof(szData);
- status = RegQueryValueEx(hkResult, "ByeBye", NULL, NULL,
- szData, &lpcbData);
- if(status != ERROR_SUCCESS)
- {
- THROW(new CException(TRUE));
- }
- wsprintf(m_wavstrByeBye, "%s", szData);
- }
- CATCH_ALL(e)
- {
- AfxMessageBox("读取注册表发生错误!", MB_OK | MB_ICONINFORMATION);
- THROW_LAST();
- }
- END_CATCH_ALL
- ::RegCloseKey(hkResult);
- }
- UINT CReadPrice::Play(LPVOID pParam)
- {
- if(m_nStartIndex < 0)
- return 0xFF;
- //播放欢迎
- ::PlaySound(this->RootDir + m_wavstrWelcome, NULL, SND_FILENAME | SND_SYNC);
- for( int i = m_nStartIndex; i < sizeof(m_Voice.m_bstrPrice); i++)
- {
- BOOL result;
- if(m_Voice.m_pstrNumber[i] != NULL)
- {
- result = ::PlaySound(this->RootDir + *m_Voice.m_pstrNumber[i], NULL, SND_FILENAME | SND_SYNC);
- if(result == FALSE)
- {
- AfxMessageBox("人工智能报价发生错误!", MB_OK | MB_ICONINFORMATION);
- return 0xFF;//失败
- }
- }
- if(m_Voice.m_pstrHolder[i] != NULL)
- {
- result = ::PlaySound(this->RootDir + *m_Voice.m_pstrHolder[i], NULL, SND_FILENAME | SND_SYNC);
- if(result == FALSE)
- {
- AfxMessageBox("人工智能报价发生错误!", MB_OK | MB_ICONINFORMATION);
- return 0xFF; //失败
- }
- }
- }
- //播放结束语
- ::PlaySound(this->RootDir + m_wavstrByeBye, NULL, SND_FILENAME | SND_SYNC);
- return 0; //成功
- }