全角 半角 转换器的 制作 C/C++

#ifndef _DS_H_
#define _DS_H_
#include
#include
#include
#include
using namespace std;
class DBData
{
public:
DBData();
~DBData();
public:
string ToDBS(string str);
string ToSBC(string input);
void LoadData(char* fileName);
string GetStringFromChar(char* pStr);
std::vector& GetM_vs();
const std::string& GetContent();


private:


vector m_vs;
std::string m_strContent;


};


inline const std::string& DBData::GetContent()
{
return m_strContent;
}




#endif

#include "DS.h"




DBData::DBData()
{


}


DBData::~DBData()
{


}


std::string DBData::ToDBS( string str )
{
string result = "";   
unsigned char tmp; 
unsigned char tmp1;   
for (unsigned int i = 0; i < str.length(); i++)
{   
tmp = str[i];   
tmp1 = str[i + 1];   
 
if (tmp == 163) 
{ ///第一个字节是163,标志着是全角字符   
result += (unsigned char) str[i + 1] - 128;   
i++;   
continue;   
}
else if (tmp > 163) 
{ //汉字   
result += str.substr(i, 2);   
i++;   
continue;   
}
else if (tmp == 161 && tmp1 == 161) 
{ ///处理全角空格   
result += "";   
i++;   

else
{   
result += str.substr(i, 1);
}

return result;   
}


void DBData::LoadData( char* fileName )
{
FILE* fp = fopen(fileName,"rb");
if(!fp) return;


char arrCh[10000];


fseek( fp, 0, SEEK_END );
int fileSize = ftell( fp ); 
fseek( fp, 0, SEEK_SET );


fread( arrCh, sizeof(char), fileSize, fp);
//while(!feof(fp))
//{
// fscanf(fp,"%s\n",arrCh);
// string s = GetStringFromChar(arrCh);


// m_vs.push_back(s);


m_strContent = GetStringFromChar( arrCh );
//} 
fclose(fp);

}


std::string DBData::GetStringFromChar( char* pStr )
{
std::string str = pStr;
return str;
}


std::vector& DBData::GetM_vs()
{
return m_vs;


}
// B2SDlg.cpp : 实现文件
//


#include "stdafx.h"
#include "B2S.h"
#include "B2SDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif




// 用于应用程序“关于”菜单项的 CAboutDlg 对话框


class CAboutDlg : public CDialog
{
public:
CAboutDlg();


// 对话框数据
enum { IDD = IDD_ABOUTBOX };


protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持


// 实现
protected:
DECLARE_MESSAGE_MAP()
};


CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}


void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()




// CB2SDlg 对话框








CB2SDlg::CB2SDlg(CWnd* pParent /*=NULL*/)
: CDialog(CB2SDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}


void CB2SDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_EDIT1, m_TestEdit);
DDX_Control(pDX, IDC_EDIT2, m_Test2Edit);
DDX_Control(pDX, IDC_BUTTON2, m_TestButton);
}


BEGIN_MESSAGE_MAP(CB2SDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CB2SDlg::OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, &CB2SDlg::OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, &CB2SDlg::OnBnClickedButton3)
END_MESSAGE_MAP()




// CB2SDlg 消息处理程序


BOOL CB2SDlg::OnInitDialog()
{
CDialog::OnInitDialog();


// 将“关于...”菜单项添加到系统菜单中。


// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);


CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}


// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//  执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标


// TODO: 在此添加额外的初始化代码


return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}


void CB2SDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}


// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。


void CB2SDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文


SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0);


// 使图标在工作矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;


// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}


//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CB2SDlg::OnQueryDragIcon()
{
return static_cast(m_hIcon);
}




void CB2SDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码


CString StrTest;
m_TestEdit.GetWindowText(StrTest);
std::string str = StrTest.GetBuffer(StrTest.GetLength());


std::string Test =  mDBData.ToDBS(str);
CString B2S(Test.c_str());
m_Test2Edit.SetWindowText(B2S);
}


void CB2SDlg::OnBnClickedButton2()
{  
CString FilePathName;
CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
if(dlg.DoModal()==IDOK)
FilePathName=dlg.GetPathName();
std::string str = FilePathName.GetBuffer(FilePathName.GetLength());
char* fileName = (char*)str.c_str();
mDBData.LoadData(fileName);
std::vector  Test = mDBData.GetM_vs();


const std::string& str1 = mDBData.GetContent();
CString strContent(str1.c_str());
//for (int i = 0; i < Test.size(); i++)
//{
// std::string str = Test[i];
//
// strContent += CString(str.c_str()) + "\r\n";
//}
m_TestEdit.SetWindowText(strContent);



}


void CB2SDlg::OnBnClickedButton3()
{
TODO: 在此添加控件通知处理程序代码


CString FilePathName;
CFileDialog dlg(FALSE);
if(dlg.DoModal()==IDOK)
FilePathName=dlg.GetPathName();
std::string str = FilePathName.GetBuffer(FilePathName.GetLength());
char* fileName = (char*)str.c_str();


CString strContent;




m_Test2Edit.GetWindowText( strContent );




FILE* fp = fopen( fileName, "wb" );
if( fp )
{
char* pData = strContent.GetBuffer( 0 );
fwrite( pData, sizeof( char ), strContent.GetLength(), fp );
fclose( fp );
}



}



你可能感兴趣的:(全角 半角 转换器的 制作 C/C++)