博文

unicode编程(2008-08-23 18:47:00)

摘要:最近试图将自己的程序编译成Unicode版本,费了不少力气,相关内容整理如下,适用于VC6,但VC7、VC8应该也差不多的(后者新建项目缺省即按Unicode编译)。 1. 添加 UNICODE 和 _UNICODE 预处理定义 位置:Project Settings -> C/C++ -> Preprocessor definitions 添加了这两个定义后,MFC的一些内置类型如 TCHAR、CString 都将转为支持宽字符类型(wchar_t) 2. 使用宽字符相关类型,如: char -> TCHAR、char * -> LPTSTR、const char * -> LPCTSTR 3. 对字符串常量使用 _T() 宏 4. 替换C库中的中字符串操作函数,如 strlen -> _tcslen、strcmp -> _tcscmp 等 类似的还有C库中字符串与数字的转换函数,如 atoi -> _ttoi、itoa -> _itot 等 5. 将 Project Settings -> Link -> Output -> Entry Point 设为 wWinMainCRTSTartup 否则会有如下错误:
msvcrtd.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16 6. C++标准库中的string,有对应的宽字符版本wstring,两者均为basic_string的特化版本 可在StdAfx.h中: #ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif 然后在代码中使用 tstring 即可,类似的还有 fstream/wfstream、ofstream/wofstream 等 7. 宽字符版本的英文字符仍可直接与整型值进行比较,如: CString s = _T("ABC");
ASSERT(s[0] == 'A'); 8. 对于仍需使用ANSI字符串的地方,如第三方类库的接口,仍可......

阅读全文(1683) | 评论:0

string to wstring?(2008-08-23 18:27:00)

摘要: unix下的汉字处理问题 如何把一个汉字作为一个字符来处理?在以前,似乎比较麻烦,因为一个汉字一向是由2个字符来表示的。比较汉字,往往变成了字符串的比较。
unicode出现之后,情况就好多了,每个汉字都有唯一的编码,从此汉字就可以作为单个字符来对待了。 stl提供了string类来处理字符串,但是针对的是单字节字符串。如果想处理汉字,可以选择wstring。用法和string完全相同,但是处理的是宽字符。
string到wstring之间的转换,似乎stl没有提供好的方法,所以还得用c的库函数来处理。
以下给出一段代码,演示在unix下面,处理汉字的方法 #include <iostream>
#include <string>
#include <list>
#include <stdlib.h>
#include <locale.h> 
namespace std {} using namespace std; int main()
{
  int cnt;
  wchar_t wcs[100], wc;
 
  string myword="列表内容为:";
 
  setlocale(LC_CTYPE, "");  //很重要,没有这一句,转换会失败
 
  mbstowcs(wcs, myword.c_str(), 99);
 
  wstring newword(wcs);
 
  cout<<"string content is:"<<myword.c_str()<< endl; 
  
  cout<<"wstring size is:"  <<newword.size()<<endl;
    return 0;
}......

阅读全文(3021) | 评论:0

线段树的应用PKU2761(2008-08-22 16:41:00)

摘要:题目链接:
http://acm.pku.cn/JudgeOnline/problem?id=2761

Feed the dogs


DescriptionWind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs every day for Wind. Jiajia loves Wind, but not the dogs, so Jiajia use a special way to feed the dogs. At lunchtime, the dogs will stand on one line, numbered from 1 to n, the leftmost one is 1, the second one is 2, and so on. In each feeding, Jiajia choose an inteval[i,j], select the k-th pretty dog to feed. Of course Jiajia has his own way of deciding the pretty value of each dog. It should be noted that Jiajia do not want to feed any position too much, because it may cause some death of dogs. If so, Wind will be angry and the aftereffect will be serious. Hence any feeding inteval will not contain another completely, though the intervals may intersect with each other.

Your task is to help Jiajia calculate which dog ate the food after each feeding.
InputThe first line cont......

阅读全文(3291) | 评论:1

linux字符模式下的分辨率设置(2008-08-21 13:26:00)

摘要:前几天拉了个linux系统,听说是不要钱的,结果不会用。 不同色彩和分辨率所对应的值   depth-----640x480----800x600----1024x768-----1280x1024
  8bit---------769--------771--------773----------775
  15bit--------784--------787--------790----------793
  16bit--------785--------788--------791----------794
  24bit--------786--------789--------792----------795
  编辑/boot/grub/目录下的menu.lst文件(有些系统menu.lst文件是软连接到grub.conf文件)
  在启动项中加入vga=xxx(需要的色彩和分辨率值)
  配置实例(Fedora7 默认grub.conf配置):
  default=0 #默认的启动菜单项从0开始计算
  timeout=5 #默认的启动菜单显示时间
  splashimage=(hd0,9)/grub/splash.xpm.gz #grub背景设置
  hiddenmenu #启动时隐藏菜单选项
  title Fedora (2.6.21-1.3194.fc7) #启动项标题
  root (hd0,9) #启动映像所在的分区
  kernel /vmlinuz-2.6.21-1.3194.fc7 ro vga=791 root=LABEL=/ rhgb
  quiet #红色为分辨率设置
  initrd /initrd-2.6.21-1.3194.fc7.img

http://tech.ddvip.com/2007-11/119606296137955.html

方法2:

如果你使用grub:
修改/boot/grub/grub.conf文件
在kernel /boot/kernel 那行的末尾添加 :
vga=0x317 append="vesa......

阅读全文(3335) | 评论:0

sort的第三个参数(2008-08-21 13:24:00)

摘要:sort的第三个参数,一直是用仿函数,没有想到用函数指针也可以! sort的第三个参数可以用函数指针也可以用仿函数。
用函数指针如下:
bool str_cmp(const string& first, const string& second)
{
return first.compare(second) < 0 ? true: false;
}
int main(int argv, char* argc[])
{
vector<string> vString;
char str[100];
int i = 0;
while(cin>>str)
{
  vString.push_back(str);
  i++;
}
sort(vString.begin(), vString.end(), str_cmp);
for(i = 0; i < 100; ++i)
  cout<<vString[i].c_str()<<endl;
CreateTree(vString);
DeleteTree();
return 0;
}
以上将字符串非递减排序......

阅读全文(2028) | 评论:0

精确得到系统时间(2008-08-21 13:22:00)

摘要:系统时间精确得到很不容易呀,函数名够长的! 精确得到系统时间#include <windows.h>
#include <iostream>
int main(int argv, char* argc[])
{
  LARGE_INTEGER t1,t2,feq;
  QueryPerformanceFrequency(&feq);//每秒跳动次数
  QueryPerformanceCounter(&t1);//测前跳动次数
  for(int i = 0;i<10000;i++);
  QueryPerformanceCounter(&t2);//测后跳动次数
  double d=((double)t2.QuadPart-(double)t1.QuadPart)/((double)feq.QuadPart);//时间差秒
  std::cout<<d<<std::endl;
}......

阅读全文(1723) | 评论:0

时间函数解析(2008-08-21 13:20:00)

摘要:对于c的时间函数,又能知道多少呢? 时间函数解析
ftime()         获取当前时间,并将其保存在结构体timeb中
localtime()     将time_t转换成结构体tm

#include <stdio.h>
#include <time.h>
#include <sys/timeb.h>

int main()
{
    struct timeb tp;
    struct tm    *tm;

    ftime(&tp);
    tm = localtime(&( tp.time ));

    printf("%02d:%02d:%02d:%03d\n",
            (tm->tm_hour),
            (tm->tm_min),
            (tm->tm_sec),
            (tp.millitm ));

timeb
--------------------------------------------......

阅读全文(2721) | 评论:0

对控制台的操作(2008-08-21 13:19:00)

摘要:怎么样对控制台光标进行控制呢? SetConsoleCursorInfo 设置控制台光标大小 否 是 是
SetConsoleCursorPosition 设置控制台光标位置 否 是 是
SetConsoleCtrlHandler 设置控制台进程的单个句柄 否 是 是
SetConsoleWindowInfo 设置控制台窗口大小 否 是 是
SetConsoleTitle 设置控制台窗口标题字符串 否 是 是
SetConsoleCP 设置控制台输入代码页 否 是 是
SetConsoleMode 设置控制台输入输出模式 否 是 是
SetConsoleOutputCP 设置控制台输出代码页 否 是 是


示例如下:
int main(int argv, char* argc[])
{
HANDLE hCout;
COORD point;
char ch;
hCout = GetStdHandle(STD_OUTPUT_HANDLE);
while(cin>>ch>>point.X>>point.Y)
{
  SetConsoleCursorPosition(hCout, point);
  cout<<ch<<endl;
}
return 0;
} 本文标签:......

阅读全文(2193) | 评论:0

构造键树并打印显示(2008-08-21 13:17:00)

摘要:前些日子做了一个输入法,用到键树研究了一下,此树又叫字典树。
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<windows.h>
#include<fstream>
using namespace std;
//数据结构:
//以键树为数据结构,采用孩子兄弟链表存储
typedef struct Node
{
    char ch;                 //存拼音字母
    int Count;                 //存以ch为前缀的拼音数
    int Index;                 //存第一个以ch为前缀的索引
    struct Node* pChild;          //子结点
    struct Node* pBrother;          //兄弟结点
}PinYin, *pPinYin;

typedef PinYin const* cpPinYin;

//拼音森林,每个开头的字母为一棵树
PinYin PinYinTree[26];

//初始化每棵树的根
void Init()......

阅读全文(2452) | 评论:0

substr(取子串)(2008-08-21 13:14:00)

摘要:c里面居然没有取子串的函数,这不是给老师出题的机会吗?
char * substr(char *string, int start , int end){
int len = strlen(string);
    if(end > len) {
  end = len;
}

if(start < 0) {
  start = len + start;
  if(start < 0) {
   start = 0;
  }
}

if(end <= 0) {
  end = len + end;
  if(end < start) {
   end = len;
  }
}
if(start > end || start < 0) {
  start = 0;
}
char * substring = (char *) malloc(strlen(string)+1);
strncpy(substring, &string[start], end);
return substring;
}......

阅读全文(3077) | 评论:0