Feb 20

写的单词背诵/题目背诵软件基本完成了⋯⋯

这个界面是主界面,没有复杂和冗余

这是一个添加单词的界面,相当于一些软件的“加入生词本”

单词也可以通过一些词库来添加,点击添加按钮就可以添加,添加初记按钮是添加之后立即进行初记

这是单词背诵界面,有默写功能,选择功能共三种,中间的绿色格子和红色格子是显示输入单词长度和正确单词是否匹配的

包含词典功能,记录其他任务功能,单词背诵使用艾宾浩斯记忆点定期提醒背诵,可以使用名为“WyabdcRealPeopleTTS”的真人语音库,wav格式的

整体采用wxwidgets,sqlite3,c++ 11编写,mba lion环境

Feb 19

python 批量修改文件名的代码

本代码最原始的部分,来自于署名为 肖泽云 先生的博客(http://blog.csdn.net/xwebsite/article/details/4861084)
特此说明

不过本代码经过我修改,增加功能,代码行数扩展两倍多,添加了替换文件名中字符串的功能。

例如把批量的文件名“今天aa.txt 今天bb.txt”替换为“明天aa.txt 明天bb.txt”

然后修改代码,增加usage,添加命令行部分的识别,基本完成我想要的功能了⋯⋯

 

import os
import sys

def serial(arg, dirname, names):
fileNum=len(names)
eNum=1
tempfileNum=fileNum+arg["start"]
while tempfileNum/10>=1:
eNum+=1
tempfileNum=tempfileNum/10.0
print "File counts:",fileNum
i=arg["start"]
for file in names:
if file.startswith(".") == True:
continue
fileIndex=str(i)
fileIndex=fileIndex.zfill(eNum)

filesuffix = ""
dotindex = file.rfind(".")
if ((len(file)-dotindex) < 6) and (dotindex != -1):
filesuffix = file[dotindex:]

print "Edit:"+file+"-->"+arg["prefix"] + fileIndex + filesuffix
file=dirname+"/"+file
newName=dirname+"/"+arg["prefix"] + fileIndex + filesuffix
os.rename(file,newName)
print "Finish!"
i+=1

def replace(oldnew, dirname, names):
fileNum=len(names)
print "File counts:",fileNum
for file in names:
if file.startswith(".") == True:
continue
if file.find(oldnew["old"]) == -1:
continue
newfile = file.replace(oldnew["old"],oldnew["new"])
print "Edit: "+file+"-->" + newfile
file = dirname+"/"+file
newfile = dirname+"/"+newfile
os.rename(file,newfile)
print "Finish!"

def serialModel(path):
print "serial model"
arg = {}
arg["prefix"] = raw_input("Please input a prefix (eg. newfile):\n")
arg["start"]=int(raw_input("Please input start number (eg. 0):"))
print "New files form :"+arg["prefix"]+str(arg["start"])
doOrNot=raw_input("Are you sure(Y/N) :")
if doOrNot=="Y" or doOrNot=="y":
os.path.walk(path, serial, arg )

def replaceModel(path):
print "replace model"
oldnew = {}
oldnew["old"] = raw_input("Please input a old string:\n")
if oldnew["old"] == "" :
print "Old string cannot be empty\n"
os._exit(1)
oldnew["new"] = raw_input("Please input a new string:\n")
doOrNot=raw_input("Are you sure(Y/N) :")
if doOrNot=="Y" or doOrNot=="y":
os.path.walk(path, replace, oldnew)

def PrintUsage():
print "usage: python " + sys.argv[0] + " [ -r | -s | -h ] target_directory"

def PrintHelp():
PrintUsage()
print "-r : into replace model.replace file name use new string instead old string"
print "\t eg. old string is 'aa',new string is 'bb',file 'aabcd' will be 'bbbcd'"
print "-s : into serial model.replace file name use serial"
print "\t eg. 'aa.txt bb.txt' to '1.txt 2.txt'"
print "-h : show this help information"

def main():

path = ""
mod = "-r"
if len(sys.argv) < 2 or len(sys.argv) > 3:
PrintUsage()
return
elif len(sys.argv) == 2:
path = sys.argv[1]
elif len(sys.argv) == 3:
mod = sys.argv[1]
path = sys.argv[2]

if path == "-h":
PrintHelp()
os._exit(0)

if os.path.exists(path)==False:
print "The directory is not exist!\n"
os._exit(1)

if mod == "-r":
replaceModel(path)
elif mod == "-s":
serialModel(path)
elif mod == "-h":
PrintHelp()
else:
PrintUsage()

if __name__ == "__main__":
main();

					
Feb 12

抄一份代码,怎么给wxWidgets任意window添加background image

代码的作者说这份代码很优美,但是其实我总觉得这份代码从某些意义上来说也不能算优美⋯⋯有点侵入性的感觉,无论如何,还是很有用的,感谢作者

class wxBackgroundBitmap : public wxEvtHandler {
    typedef wxEvtHandler Inherited;
public:
    wxBackgroundBitmap(const wxBitmap &B) : wxEvtHandler(), Bitmap(B)  { }
    virtual bool        ProcessEvent(wxEvent &Event);
protected:
    wxBitmap            Bitmap;
};

bool wxBackgroundBitmap::ProcessEvent(wxEvent &Event)
{
    if (Event.GetEventType() == wxEVT_ERASE_BACKGROUND) {
        wxEraseEvent &EraseEvent = dynamic_cast(Event);
        wxDC *DC = EraseEvent.GetDC();
        DC->DrawBitmap(Bitmap, 0, 0, false);
        return true;
    } else return Inherited::ProcessEvent(Event);
}

	wxBitmap tbm;
	tbm.LoadFile("1.bmp"_w,wxBITMAP_TYPE_BMP);
	wxBackgroundBitmap* ToolBarBackground = new wxBackgroundBitmap(tbm);
	this->PushEventHandler(ToolBarBackground);
	//this->RemoveEventHandler(ToolBarBackground);
Feb 09

关于c++头文件后缀名和main的返回值再谈

#include < iostream >

#include < iostream.h >
using namespace std;
void main()
int main()

在c++98标准以前(就是国际标准化组织1998年制定的标准),c++的头文件不够规范,采用的是.h的有后缀名形式。不过在这个标准推出以后,标准规定了头文件统一改为无后缀名形式,并且头文件内容包含在一个名为std的命名空间里。因为这是新的标准所规定的,自然也是建议采用的形式。(说是新标准其实也十几年了)

国内作者的书经常发现各种例子中使用了两种格式或者只使用了老版本的形式,说明这个书的作者是混蛋,这书肯定不是一个人写的,一个人不可能写得出两种代码风格,而且也应该使用新版头文件的标准。这一点在看一些国外作者的书就能看出来,例如《c++ primer》都是很规范、统一的,国内作者的书嘛⋯⋯尽量少看国内作者写的书,误人子弟的货。

至于main的返回值的问题,这又是一群国内混蛋问题,实际上包括最经典的那本出版于70年代的《c programming language》,也就是C语言的创始人的那本书,就没有main的返回值是void的形式。那个时候c语言喜欢把main写成无返回值,因为c语言规定一个函数如果无返回值类型,那么返回值类型默认为int。而C++后来规范了C语言,不允许一个函数无返回值(除了构造函数和析构函数),如此之后有些家伙就非要写一个void的返回值类型,实际上这是极其不标准的。

在很多编译器上,例如gcc,intel c,如果你把main写成void的返回值,会直接报错拒绝编译。为什么这也是有典故的,因为从unix系统开始,程序就规定应该有一个返回值。这个返回值可以代表程序是否运行成功,假设返回0就代表程序运行成功无错,返回其他值代表程序运行出现问题,而返回的数值是问题的错误代码。这是一个古老的习俗,在unix/linux下可以用echo $?命令查看上一个运行的程序的返回值。在Windows下甚至有一个名字为EXIT_SUCESS的宏,值为0,你在部分程序可以看到main函数的最后是return EXIT_SUCESS;代表程序运行成功。所以为了这个习俗,也应该避免void返回值的main函数的出现。