提倡:将你认为有用的代码放进来,大家共同分享!!!
昨天应一位朋友的请求,写了一个在指定目录下搜索文件,返回其路径的函数,先放这里,大家都来参与吧
[code=C/C++][/code]
#include "Unit1.h"
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ShowMessage( BrowDir("c:\\","services.isc"));
}
//---------------------------------------
String __fastcall TForm1::BrowDir(String path,String file)
{
TSearchRec sr;
static String dpath; //关键是这里,必须定义为静态,否则下一次递归将丢掉所得到的值
if (FindFirst(path+"*.*", faAnyFile, sr) == 0) //列举所有的目录
{
do
{
if((sr.Attr & faDirectory) && sr.Name!="." && sr.Name!="..") //判断是否是目录,并排除目录“.”和“..”
BrowDir(path+sr.Name+"\\",file); //调用函数本身,进入子目录
else
if( sr.Name.UpperCase()==file.UpperCase())//找到所需文件
return dpath=path;
} while (FindNext(sr) == 0);
FindClose(sr);
}
return dpath;
}
//---------------------------------------
[解决办法]
好主意.
#include <Forms.hpp>#include <boost/scoped_ptr.hpp>template <typename _TFrom>void __fastcall FormShowModal(TComponent *Owner){ boost::scoped_ptr<TForm> frm(new _TFrom(Owner)); frm->ShowModal();}
[解决办法]
CString strExt; CString strName; CString strPattern; BOOL bRC = TRUE; HANDLE hFind = NULL; WIN32_FIND_DATA FindFileData; std::vector<CString> VectorImageNames; if ( m_strImageDir[m_strImageDir.GetLength() - 1] == TCHAR('\\') ) strPattern.Format( TEXT("%s*.*"), m_strImageDir ); else strPattern.Format( TEXT("%s\\*.*"), m_strImageDir ); hFind = ::FindFirstFile(strPattern, &FindFileData); // strat search if (hFind == INVALID_HANDLE_VALUE) { LPVOID msg; ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msg, 0, NULL); MessageBox((LPTSTR)msg, CString((LPCSTR)IDS_TITLE), MB_OK|MB_ICONSTOP); ::LocalFree(msg); return FALSE; } // filter off the system files and directories if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)) { // test file extension strName = FindFileData.cFileName; strExt = strName.Right(3); if ( (strExt.CompareNoCase( TEXT("bmp") ) == 0) || (strExt.CompareNoCase( TEXT("jpg") ) == 0) || (strExt.CompareNoCase( TEXT("gif") ) == 0) || (strExt.CompareNoCase( TEXT("tif") ) == 0) || (strExt.CompareNoCase( TEXT("png") ) == 0) ) { // save the image file name VectorImageNames.push_back(strName); } } // loop through to add all of them to our vector while (bRC) { bRC = ::FindNextFile(hFind, &FindFileData); if (bRC) { // filter off the system files and directories if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) && !(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)) { // test file extension strName = FindFileData.cFileName; strExt = strName.Right(3); if ( (strExt.CompareNoCase( TEXT("bmp") ) == 0) || (strExt.CompareNoCase( TEXT("jpg") ) == 0) || (strExt.CompareNoCase( TEXT("gif") ) == 0) || (strExt.CompareNoCase( TEXT("tif") ) == 0) || (strExt.CompareNoCase( TEXT("png") ) == 0) ) { // save the image file name VectorImageNames.push_back(strName); } } } else { DWORD err = ::GetLastError(); if (err != ERROR_NO_MORE_FILES) { LPVOID msg; ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&msg, 0, NULL); MessageBox((LPTSTR)msg, CString((LPCSTR)IDS_TITLE), MB_OK|MB_ICONSTOP); ::LocalFree(msg); ::FindClose(hFind); return FALSE; } } } // end of while loop // close the search handle ::FindClose(hFind); // update the names, if any if ( !VectorImageNames.empty() ) { // reset the image name vector m_VectorImageNames.clear(); m_VectorImageNames = VectorImageNames; return TRUE; } return FALSE;
[解决办法]
最好把代码究竟能达到什么目的写上吧
//重启操作系统
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
{
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
ExitWindowsEx(EWX_REBOOT, 0);
}
[解决办法]
看看,跑跑。
[解决办法]
获取汉字的拼音索引
//---------------------------------------bool __fastcall In(int LowPosition, int HighPosition,int Value){ return ((Value<=HighPosition)&&(Value>=LowPosition));}//---------------------------------------char __fastcall GetPYFirstChar(AnsiString HZ){ WORD High =WORD(HZ[1])<<8; WORD Low =BYTE(HZ[2]); int n=High +Low ; if (In(0xB0A1,0xB0C4,n)) return 'A'; if (In(0XB0C5,0XB2C0,n)) return 'B'; if (In(0xB2C1,0xB4ED,n)) return 'C'; if (In(0xB4EE,0xB6E9,n)) return 'D'; if (In(0xB6EA,0xB7A1,n)) return 'E'; if (In(0xB7A2,0xB8c0,n)) return 'F'; if (In(0xB8C1,0xB9FD,n)) return 'G'; if (In(0xB9FE,0xBBF6,n)) return 'H'; if (In(0xBBF7,0xBFA5,n)) return 'J'; if (In(0xBFA6,0xC0AB,n)) return 'K'; if (In(0xC0AC,0xC2E7,n)) return 'L'; if (In(0xC2E8,0xC4C2,n)) return 'M'; if (In(0xC4C3,0xC5B5,n)) return 'N'; if (In(0xC5B6,0xC5BD,n)) return 'O'; if (In(0xC5BE,0xC6D9,n)) return 'P'; if (In(0xC6DA,0xC8BA,n)) return 'Q'; if (In(0xC8BB,0xC8F5,n)) return 'R'; if (In(0xC8F6,0xCBF0,n)) return 'S'; if (In(0xCBFA,0xCDD9,n)) return 'T'; if (In(0xCDDA,0xCEF3,n)) return 'W'; if (In(0xCEF4,0xD188,n)) return 'X'; if (In(0xD1B9,0xD4D0,n)) return 'Y'; if (In(0xD4D1,0xD7F9,n)) return 'Z'; return char(0);}//---------------------------------------AnsiString __fastcall GetPYString(AnsiString InputHZString){ AnsiString TempString, ReturnString; AnsiString Str =InputHZString.Trim(); for (int i=1; i<=Str.Length(); i++) { if (isprint(Str[i])) { ReturnString = ReturnString + Str[i]; } else { TempString += Str[i]; if (TempString.Length() == 2) { ReturnString = ReturnString + GetPYFirstChar(TempString); TempString = ""; } } } return ReturnString;}//---------------------------------------
[解决办法]
我顶
[解决办法]
函数作用,动态ShowModal窗口,仅限cbuilder
while (path->Count)
{
AnsiString S = path->Strings[0];
path->Delete(0);
if (FindFirst(S+"\\*.*", faAnyFile, sr) == 0)
{
do
{
if (sr.Name!="." && sr.Name!="..") 排除目录“.”和“..”
if(sr.Attr & faDirectory) //判断是否是目录
path->Add(S + "\\" + sr.Name); //调用函数本身,进入子目录
else
file->Add(path + "\\" + sr.Name);
} while (FindNext(sr) == 0);
FindClose(sr);
}
Sleep(7);
}
}
[解决办法]
楼主建议不错,不过最好按照一定的规格提交比较好,而且谁帮忙整理分类一下!
并且由谁放在csdn或者某个地方形成1个文章列表,这样方便其他人阅读和索引!
[解决办法]
这个建议很好,提倡开源.
[解决办法]
http://blog.chinaunix.net/u/22170/showart_479599.html
http://www.encrypter.net/cn/article.htm
两个C++ Builder 技术类网页~~
刚学BCB 哈哈
[解决办法]
C++不熟悉。
[解决办法]
递归查找并不会有什么问题。
我自己写的,也是这样,用来查我的500G硬盘也不见到出一点问题。
还是健壮性的问题。
[解决办法]
僵哥(重要的不是为之付出了多少,误导才是最大的失败)
等级:
可用分等级:乞丐
总技术分:76907
总技术分排名:80
可怜的僵哥,怎会伦落到如此...
[解决办法]
很好。。。。
[解决办法]
懒人居多,具体实现问题比较大
[解决办法]
好啊
[解决办法]
回帖是一种美德!传说每天回帖即可获得 10 分可用分!
[解决办法]
偶没啥能拿得出手的代码,不过发现了几个VCL自带但BCB帮助里没有的函数:
Base64编解码:
#include <encddecd.hpp>extern PACKAGE void __fastcall EncodeStream(Classes::TStream* Input, Classes::TStream* Output); //对流Input进行Base64编码,输出到Output流里extern PACKAGE void __fastcall DecodeStream(Classes::TStream* Input, Classes::TStream* Output); //Base64解码,...extern PACKAGE AnsiString __fastcall EncodeString(const AnsiString Input); //Base64编码,...extern PACKAGE AnsiString __fastcall DecodeString(const AnsiString Input); //Base64解码,...
[解决办法]
jf
[解决办法]
jf
[解决办法]
毛毛,没看到你的TParser类的源码呀?
嘿嘿!
[解决办法]
#ifdef _DEBUG#define MYTRACE(s, ...) DebugString(s, __VA_ARGS__)void DebugString(const TCHAR *format, ...);#pragma warning (push) #pragma warning(disable:4996)const UINT DEBUG_BUFFER_SIZE = 1024;void DebugString(const TCHAR *format, ...) { va_list arglist; wchar_t buffer[DEBUG_BUFFER_SIZE]; va_start(arglist, format); _vsntprintf(buffer, DEBUG_BUFFER_SIZE, format, arglist); va_end(arglist); _tcsncat(buffer, _T("\n"), 1); OutputDebugString(buffer); } #pragma warning(pop)#else#define MYTRACE(s, ...)#endif //_DEBUG
[解决办法]
dig
[解决办法]
嘿嘿,来学习
[解决办法]
水平有限,看不懂。
[解决办法]
都是c/c++的高人啊,佩服佩服
[解决办法]
没试过,可能不行吧,它应该只负责main的解析,WinMain估计不管。
[解决办法]
up
[解决办法]
果然是好地方
[解决办法]
学习
[解决办法]
另外,大学的时候,老师总是告诫:goto语句少用,最好不用,容易使程序混乱。而在这段拆分的代码中,goto语句发挥出了优势.
下面是所写的拆分函数
//---------------------------------------void TForm1::subFun(AnsiString str){ int i=0,j=0; String s_Show,s_String1,s_String2; //加上全角空格是为了解决拆分的字符串全为半角空格串时的不跳转问题 strfull += str + " "; s_Show = "生成的SQL语句为:"; s_String1 = "测试字符串为:"; s_String2 = "拆分空格后合并的字符串为:"; s_String1 += str; Memo1->Lines->Add(s_String1); Memo1->Lines->Add("以下为拆分出来的字符串:"); //从最左侧的全角空格开始拆分 FullShape: //取字符串左侧第一个全角空格位置 posfull = strfull.Pos(" "); if(strfull.Pos(" ") > 0) { //将最左侧全角空格前的字符串取出用于拆分半角空格 //剩余部分继续用作拆分全角空格 strfulluse = strfull.SubString(1,posfull-1); strfullremain = strfull.SubString(posfull+2,strfull.Length()-posfull-1); strfull =strfullremain.Trim(); strhalf = strfulluse.Trim(); goto HalfShape; } else //全角拆分结束 if(strfull.Pos(" ") == 0) { if(strfull != "" && strfull != " " && strfull != " ") { //处理函数 s_Show += " like '%" + strfull; s_Show += "' and "; s_String2 += strfull +"、"; Memo1->Lines->Add(strfull); //strhalf = strfull; goto End; } if(strfull == "" && strhalf == "") goto End; } //从全角空格拆分的最左侧字符串开始拆分半角空格 HalfShape: //取最左侧半角空格位置 poshalf = strhalf.Pos(" "); //没有半角空格 if(strhalf.Pos(" ") == 0 ) { if(strhalf != " " && strhalf != " " && strhalf != "") { //处理函数 s_Show += " like '%" + strhalf; s_Show += "' and "; s_String2 += strhalf +"、"; Memo1->Lines->Add(strhalf); strhalf = ""; } //半角空格拆分结束,返回继续拆分全角空格 goto FullShape; } else //有半角空格 if(strhalf.Pos(" ") > 0) { strhalfuse = strhalf.SubString(1,poshalf-1); strhalfremain =strhalf.SubString(poshalf+1,strhalf.Length()-poshalf); strhalf =strhalfremain.Trim(); //处理函数 if(strhalfuse != " " && strhalfuse != " "&& strhalfuse != "") { s_Show += " like '%" + strhalfuse; s_Show += "%' and "; s_String2 += strhalfuse +"、"; Memo1->Lines->Add(strhalfuse); } goto HalfShape; } End: Memo1->Lines->Add(s_Show); Memo1->Lines->Add(s_String2);}//---------------------------------------
[解决办法]
/比較兩個__closure指針指向的函數地址是否一樣。
//例:EventIsEqual(&this->myEditClick, Edit1->OnClick)
//if(Form1->OnClose == Form2->OnClose) //這比較總是false,原因是OnClose是個closure指針
template <typename T>
inline bool EventIsEqual(T const & EV1,T const & EV2)
{
return *(const void **)&EV1 == *(const void **)&EV2 ;
}
每個closure指針是個TMethod *, 這裡相當於只比較Code部分。這樣強制轉換是為了繞過BCB的語法檢查。
struct TMethod
{
void *Code; //指向函數地址
void *Data; //指向對象實例
};
[解决办法]
study
[解决办法]
初学者谢谢各位了!
[解决办法]
来学习下,发现什么都看不懂..惭愧得掉下头
[解决办法]
有没有 c#的 或 .net 的代码 共享一下
[解决办法]
学习中……
[解决办法]
学习中....晚上好好的研究一下这些代码...
[解决办法]
越来越老了,是需要整理下
[解决办法]
来个简单的,显示m的3次方
wchar_t ws[2];
ws[0] = L'\x00\x6D';
ws[1] = L'\x00\xB3';
lab_Test->Caption = " ";
this->Repaint();
TextOutW(lab_Test->Canvas->Handle, 0, 0, ws, 2);
[解决办法]
在CSDN回答一个问题时写的程序
利用后缀表达式法 计算数学表达式的,比如:((1+2)*3-1)*4
bool __fastcall IsOpertator(char aChar){ switch(aChar) { case '+': case '-': case '*': case '/': case '(': case ')': return true; default: return false; }}int __fastcall OperatorLevel(char Opt){ switch(Opt) { case '*': return 2; case '/': return 2; case '(': return 99; case ')': return 0; case '+': return 1; case '-': return 1; default: return 0; }}string __fastcall InfixToPostFix(string inputString){ vector<char> OperatorStack; string PostFixString; //将表达式改为后缀表达式 string tmpNumber; for(unsigned int i=0;i<inputString.length();i++) { char tmpChar = inputString.c_str()[i]; if(IsOpertator(tmpChar)) // 如果是操作符或括号 { if(tmpNumber.length()>0) // 如果还有正在处理的数字,数字读取完整,入后缀队列。 PostFixString += tmpNumber+" "; tmpNumber = ""; if(tmpChar == ')') // 如果是闭括号,需要一直弹出到开括号 { while(!OperatorStack.empty()) { char thePopOpt = OperatorStack.back(); if(thePopOpt == '(') { OperatorStack.pop_back(); break; } else { PostFixString.push_back(thePopOpt); PostFixString.push_back(' '); OperatorStack.pop_back(); } } continue; // continue for; } // 同操作符堆栈栈顶元素比较,如果优先级高,就入栈,否则直到弹出所有元素 while(!OperatorStack.empty()) { char thePopOpt = OperatorStack.back(); if(OperatorLevel(thePopOpt)>OperatorLevel(tmpChar)) { if(thePopOpt=='(') { OperatorStack.push_back(tmpChar); tmpChar = '\0'; break; } else { OperatorStack.pop_back(); PostFixString.push_back(thePopOpt); PostFixString.push_back(' '); } } else if(OperatorLevel(thePopOpt)==OperatorLevel(tmpChar)) { OperatorStack.pop_back(); PostFixString.push_back(thePopOpt); PostFixString.push_back(' '); OperatorStack.push_back(tmpChar); tmpChar = '\0'; break; } else { OperatorStack.push_back(tmpChar); tmpChar = '\0'; break; // break while } } // end while if(tmpChar!='\0') OperatorStack.push_back(tmpChar); } else // 如果是数字 { tmpNumber.push_back(tmpChar); if(i == inputString.length()-1) { PostFixString += tmpNumber+" "; break; } } } //----end for while(!OperatorStack.empty()) { PostFixString.push_back(OperatorStack.back()); PostFixString.push_back(' '); OperatorStack.pop_back(); } return PostFixString;}double __fastcall Calculate(string inputStr){ vector<double> stack; string tempNumber; string theString = inputStr; while(theString.length()) { string strItem; int nPos = theString.find(" ",0);; if(nPos>0) { strItem = theString.substr(0, nPos); theString.erase(0, nPos+1); } else { strItem = theString; theString = ""; } if(strItem.length() ==1 && IsOpertator(strItem.c_str()[0])) { double d1,d2; d2 = stack.back(); stack.pop_back(); d1 = stack.back(); stack.pop_back(); switch(strItem.c_str()[0]) { case '+': stack.push_back(d1 + d2); break; case '-': stack.push_back(d1 - d2); break; case '*': stack.push_back(d1 * d2); break; case '/': stack.push_back(d1 / d2); break; } } else { stack.push_back(atol(strItem.c_str())); } } return stack[0];}__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner){}//---------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){ string PostFixStr = InfixToPostFix(LabeledEdit1->Text.c_str()); double ret = Calculate(PostFixStr); ShowMessage("后缀表达式为:"+ AnsiString(PostFixStr.c_str()) + "\r运算结果是:" +FormatFloat("0.00", ret));}//---------------------------------------
[解决办法]
reMARK
[解决办法]
//Ansistring 转 char void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "哈哈"; char *chr = Test.c_str(); } //char转Ansistring #include <windef.h> void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString str = "Sample"; char chr[MAX_PATH]; strcpy( chr , str.c_str() ); } //Bool转AnsiString void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test=BoolToStr(CheckBox1->Checked); } //Ansistring转bool void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test="-1" CheckBox1->Checked= StrToBool( Test ); } //int转ansistring void __fastcall TForm1::Button1Click(TObject *Sender) { int i = 123; AnsiString str = IntToStr( i ); } //AnsiString转double void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "123"; long double d = StrToFloat( Test ); } //double转AnsiString void __fastcall TForm1::Button1Click(TObject *Sender) { double d = 123.456; AnsiString str = FloatToStr( d ); } //double转AnsiString并四舍五入 void __fastcall TForm1::Button1Click(TObject *Sender) { long double d = 123.456121212; AnsiString str = FloatToStrF( d , ffFixed ,5 , 4 ); //说明FloatTostrF里5代表从第几个数字的后一位开始四舍五入,4代表取4位小数。 //执行后得到str是123.4600。:roll: } //double转AnsiString使用类似vb的format函数 void __fastcall TForm1::Button1Click(TObject *Sender) { double d = 123.456; AnsiString str = FormatFloat( "000000.00" , d ); } //得到 000123.45,当然你可以使用"# . , ; E+ E- xx"等符号,你自己试试 :wink: //AnsiString转Tclor型 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "0x00FF8080"; TColor Col = StringToColor( Test ); } //Tcolor转AnsiString void __fastcall TForm1::Button1Click(TObject *Sender) { TColor Col = 0x00FF8080; AnsiString str = ColorToString( Col ); } //消除AnsiString 中的一部分字符串 代码: void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCDEF"; int First = 3; // 消除制定开头 int Length = 2; // 消除制定长度 AnsiString Dstr = Test.Delete( First , Length ); }//得到ABEF //在AnsiString 中插入字符串 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCDEF"; AnsiString Ins = "12345"; // 插入串 int Pos = 3; // 在哪插 AnsiString Istr = Test.Insert( Ins , Pos ); //得到AB12345CDEF } //取得ansi某一位字符 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCDEF"; AnsiString Npos = Test[3];//得到C } //取得AnsiString里最后一个字符 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCDEF"; char *Lstr = Test.AnsiLastChar();//得到F } //取出AnsiString字符,这个类似vb的mid函数! void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCDEF"; int First = 3; // 3开始取 int Length = 2; // 取2位 AnsiString Getstr = Test.SubString( First , Length ); //得到CD } //AnsiString的字母比较 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCDEF"; AnsiString Sample = "abcdef"; int Result = Test.AnsiCompare( Sample ); //返回1,不同!分大小写。 } void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCDEF"; AnsiString Sample = "abcdef"; int Result = Test.AnsiCompareIC( Sample ); //返回0,相同!没有分大小写,哈哈 } //在AnsiString中寻找字符 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCDEF"; AnsiString Sample = "E"; int Result = Test.Pos( Sample ); //返回5,如果你写Sample="haha",就返回0,找不到,哈哈 }
------解决方案--------------------
//在AnsiString中找字符串,和上一个类似 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCDEF"; AnsiString Sample = "EF"; int Result = Test.Pos( Sample ); //返回5,即E字符的位置 } //判断字符串长度,类似vb的len void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "拿金币来"; int Len = Test.Length(); //返回8 } //取得字符串,类似vb的left void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "小苹果然看了这篇文章"; AnsiString SLstr = Test.SetLength(6); }//得到"小苹果" //检测双字节字串 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString ChkStr = "你好"; int ChkPos = 1 ; if ( ByteType( ChkStr , ChkPos ) == mbSingleByte ){ Edit1->Text="0"; } else{ Edit1->Text="1"; }//返回1,如果你写ChkStr="fxxk",就返回0 } //检测空字符串 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = ""; bool chk = Test.IsEmpty(); if (chk ) Edit1->Text="1";//返回1 } //全部变小写vs全部变大写 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCdef"; AnsiString Lstr = Test.LowerCase(); } 代码: void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = "ABCdef"; AnsiString Ustr = Test.UpperCase(); } //类似vb中Trim 的去空格函数 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = " ABCDEF "; AnsiString TLstr = Test.TrimLeft(); AnsiString TRstr = Test.TrimRight(); AnsiString Tstr = Test.Trim(); } //但是,这个处理不了全角的空格 代码: AnsiString __fastcall TForm1::TrimStr( AnsiString Tm , AnsiString LR ) { // LR … L:左除去 R:右除去 B:dou除去 int len; // 左除去 if ( LR == "L" || LR == "B" ){ len = Tm.Length(); while ( Tm.SubString(1,1) == " " || Tm.SubString(1,2) == " "){ // 半角除去 if ( Tm.SubString(1,1) == " " ){ Tm = Tm.SubString(2,len); len = Tm.Length(); } // 全角除去 else if ( Tm.SubString(1,2) == " " ){ Tm = Tm.SubString(3,len); len = Tm.Length(); } } } // 右除去 if ( LR == "R" || LR == "B" ){ len = Tm.Length(); while ( Tm.SubString(len,1) == " " || Tm.SubString(len-1,2) == " " ){ // 半角除去 if ( Tm.SubString(len,1) == " " ){ len = Tm.Length(); Tm = Tm.SubString(1,len-1); len = Tm.Length(); } // 全角除去 else if ( Tm.SubString(len-1,2) == " " ){ len = Tm.Length(); Tm = Tm.SubString(1,len-2); len = Tm.Length(); } } } return Tm; } void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Test = " 拳脚 "; AnsiString Ret = TrimStr(Test,"B") } //相同字符重复输入 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Soc = AnsiString::StringOfChar( '*' , 100 ); Edit1->Text=Soc ;//显示100个* } //字符串替换 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Str = "Borland C++ Builder is free"; AnsiString From = "C++ Builder"; AnsiString To = "Delphi"; AnsiString Result; Result = StringReplace( Str, From, To, TReplaceFlags() << rfReplaceAll << rfIgnoreCase ); //<<后是参数,得到Borland Delphi is free } //全角变半角 AnsiString Zen = "1234567890"; int Len = Zen.Length(); char buf[MAX_PATH]; ZeroMemory( buf, sizeof( buf ) ); LCMapString( GetUserDefaultLCID(), LCMAP_HALFWIDTH, Zen.c_str(), Len, buf, sizeof( buf ) ); AnsiString Han = AnsiString( buf ); Edit1->Text=Han; //半角变全角 void __fastcall TForm1::Button1Click(TObject *Sender) { AnsiString Han = "1234567890"; int Len = Han.Length(); char buf[MAX_PATH]; ZeroMemory( buf, sizeof( buf ) ); LCMapString( GetUserDefaultLCID(), LCMAP_FULLWIDTH, Han.c_str(), Len, buf, sizeof( buf ) ); AnsiString Zen = AnsiString( buf ); }
[解决办法]
报着学习的态度,谢谢开源的各位高手兄弟
[解决办法]
/* 人民币小写转大写,这应该是某本书上的一个列子,忘记是哪本书上的了 整理了一下,可以对小数位数,等控制*/AnsiString __fastcall TransferNumberToChinese(double fNumber,bool DispLayZero,int SpaceLength,int Language){ //18个单位 const String StrScale[18] = {"分","角","元","拾","佰","仟","万","拾","佰","仟","亿","拾","佰","仟","兆","拾","佰","仟"}; //10个数字 const String StrNumber[10] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; TVarRec arg[3]; arg[0]=8; arg[1]=2; arg[2] = fNumber ; AnsiString StrTemp = Trim(Format("%*.*f",arg,2)) ; StrTemp.Delete(StrTemp.Pos("."),1); AnsiString StrRusult = " "; for( int i = StrTemp.Length() ; i > 0 ; --i ) { if( !DispLayZero && i > 2 && StrTemp[StrTemp.Length() - i + 1] - 48 == 0 ) continue ; StrRusult += StrNumber[StrTemp[StrTemp.Length() - i + 1] - 48]; StrRusult += StrScale[i-1]; StrRusult += AnsiString::StringOfChar(' ',SpaceLength); } return StrRusult ;}
[解决办法]
这个要顶。。。。。。。。。。
[解决办法]
都是高手,学习了
[解决办法]
非常好的想法,以后将把自己的代码发上去
[解决办法]
我顶
[解决办法]
好贴,收藏!
[解决办法]
/* 汗,自己没有啥好代码献上了,先顶下。 最好版主们有空花时间把这些东东整理下,搞个归类,索引啥的, 以后遇到类似的问题了,就可以直接到那里去寻求帮助了。*/
[解决办法]
一部分有用一点的代码, 起码都要有过千行
比如说一个类似数据库形式的 文件流数据库( 读取 和 写入都是读写文件的, 并非是读取数据库, 也不选择注册表或INI, INI存不了那么多东西, 开Access等于让人看机密)
或者是一些报表的生成算法 : 交叉报表, 分组/分级统计( 采用内存表的形式而非数据库语句实现 )等等
一些组件的实现 : 比如会自动生成整条查询SQL, 提供给用户进行各种查询 ( 包括Lookup字段的查找, 字段跟字段的查找, 主从表的查找 )
用组件来创建某些对象池 ( TCollection, TCollectionItem )
线程模型的处理 : 怎样按排线程, 线程要怎样进行操作等等, 线程怎样排队等等
某些窗体重绘的算法 : 比如在窗体要画些什么箭头啊, 图形啊之类
基本没得发
[解决办法]
还有一个忘记了说:
用递归来解决目录浏览, 是足够的.
递归是省下不少代码的, 我相信没有一个人的电脑里面, 会有深到多于100个目录的文件, 点都点死你!