Sunday, October 31, 2010

[Linux]用gs合并几个pdf文件

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=finished.pdf file1.pdf file2.pdf

Thursday, October 21, 2010

PPT 热键

Press F5 in PowerPoint/Windows or Control-Shift-S in PowerPoint X/Mac to start a slide presentation from the beginning

Press Shift+F5 in PowerPoint 2003 or Control+Shift+B in PowerPoint X/Macintosh to start a presentation from the current slide

Friday, October 15, 2010

手机配置

Apple iPhone 4
  • CPU: Apple A4 - fully compatible with ARM Cortex-A8, running at 1 GHz, 64 L1 Cache, 640 L2 Cache
  • Memory: 512 MB eDRAM
  • Storage: 16 GB or 32 GB flash memory
Nokia N900
  • CPU: ARM Cortex-A8, running at 600 MHz
  • Memory: 256 MB
  • Storage: 32 GB internal; Expandable to up to 48 GB with an external microSD card

Thursday, October 14, 2010

Cocoa编程笔记(update中)

Cocoa是Mac上使用的图形界面framework, 简单说来就是一个库使开发人员可以定制自己的基于GUI的 Application。Cocoa使用的语言是Object-C(参见我的Object-C笔记)。 Cocoa编程的学习曲线比其他GUI编程的要陡,因为有很多概念要理解。我在学习过程中,参照如下的一些tutorial或者文档:

Cocoa Fundamental Guide:这是最核心最基础的文档。介绍了最重要的概念

Cocoa Event Handling:Cocoa中如何处理事件

Introduction to Text System User Interface Layer,介绍了比如如何programatically 创建一个NSTextView,添加至NSScrollView,如何调整Text边距等等.

Introduction to Text Editing Programming Guide for Cocoa, 介绍如何使用Cocoa的Text System来完成编辑工作,比如key binding, 如何delegate message等.

一个简单界面的Cocoa Application:最最简单的一个Application.如果想学习一个最简单的Cocoa Application,从这个文档看起.

Model-View-Control模型

Cocoa中广泛使用了MVC这种的Design Pattern,把一个application功能分解为如下三个部分:

Model objects: 负责逻辑处理,不直接和用户界面打交道
View objects: 用户界面.比如label, textfield等
Controller objects: 连结Model和View, 比如处理界面送来的用户action,应用后台逻辑做出update等


内存管理
alloc, init, dealloc, retain, release, autorelease. 所有从NSObject继承而来的类都有这些方法,这些涉及到内存管理.

alloc: 从内存中分配一块内存,但仅仅是获得一块内存空间,未作任何初始化
init:初始化一个类,类似于C++中的构造函数,一般和alloc配合使用:
id  myObject = [[NSString alloc] init];
dealloc: 销毁一个类,释放内存空间
[myObject dealloc];
retain: 将一个对象的引用计数加1
[myObject retain];
release: 将一个对象的引用计数减1
[myObject release];
autorelease:每当一个对象得到autorelease消息,这个对象就将被添加到autorelease pool当中. 当改eventloop结束时,所有autorelease pool当中的对象将被release.通常一个method要返回一个对象的时候,autorelease这个对象是个好主意.
- (NSString)autoreleasedString
{
  NSString *string = [[NSString alloc] initWithString:
      @"The Sender of this message will never see this string…"];
  [string autorelease]; // we have to do this or we'll get a memory leak
  return string;
}


神秘的IBAction和IBOutlet?

经过预处理以后,IBAction实际上就是void而IBOutlet实际上根本就是一个空的宏(见定义在NSNibDeclarations.h).它们的作用是在于作为一种syntax sugar,提醒Interface Builder,"嗨,这几个被我修饰的变量或者函数是你应该知道的~".



事件处理(Event Handling)
有两种Message: 由外部设备比如键盘或者鼠标发起的Event message,以及由其他对象发起的Action message.

Event message 通常已知,所以NSResponder提供了声明以及缺省的实现.
NSResponder是一个抽象类.而Cocoa中最核心的三个类NSApplication, NSWindow, 以及 NSView都继承了NSResponder.

比如我们要处理mouseDown这个事件,我们可以继承一个NSView类(或者NSView的子类,比如NSTextView,NSTabView),然后重载mouseDown:这个方法。如果一个NSWindow上有多个NSView(或其子类), 则应该重载最上面的View,否则盖在底下的不能接受到该消息

- (void)mouseDown:(NSEvent *)theEvent {
    // determine if I handle theEvent
    // if not...
    [super mouseDown:theEvent];
}


Delegate
Common mistakes with delegation in Cocoa
一个在NSImageView中delegate mouseDown等消息的例子:
http://it.toolbox.com/blogs/macsploitation/adding-delegates-to-nsviews-21138

Text System
NSTextStorage 是NSMutableAttributedString的子类. 一般作为NSTextView 的text repository. Introduction to Text System Storage Layer Overview

NSTextView 和用户打交道的界面
NSTextContainer:Introduction to Text Layout Programming Guide


NSScrollView: Introduction to Scroll View Programming Guide for Cocoa
NSClipView:


NSParagraphStyle:Introduction to Rulers and Paragraph Styles

一个超级简单的Cocoa Text System Application
HelloWorld.h:
#import 

@interface HelloWorld : NSObject  {
    NSWindow *window;
 
 NSTextStorage *textStorage;
 NSLayoutManager *layoutManager;
 NSTextContainer *textContainer;
 NSTextView *textView;
}

@property (assign) IBOutlet NSWindow *window;

@end
HelloWorld.m
#import "HelloWorld.h"

@implementation HelloWorld

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
 // Insert code here to initialize your application 
 
 //setup textStorge
 textStorage = [[NSTextStorage alloc] initWithString:@"123abcde\n456"];
 
 //setup layoutManager
 layoutManager = [[NSLayoutManager alloc] init]; 
 [textStorage addLayoutManager:layoutManager]; 
 [layoutManager release];
 
 //setup textContainer
 NSRect cFrame = [[window contentView] frame];  
 textContainer = [[NSTextContainer alloc] initWithContainerSize:cFrame.size];
 [layoutManager addTextContainer:textContainer]; 
 [textContainer release];
 
 //setup textView
 textView = [[NSTextView alloc] initWithFrame:cFrame textContainer:textContainer]; 
 [window setContentView:textView]; 
 [window makeKeyAndOrderFront:nil]; 
 [textView release];
}

@end


常用数据结构:
NSArray: 类似list, 不能添加元素
Objective-C Tutorial: NSArray读取index位置的元素
[myArray objectAtIndex:index]

NSMutableArray:一个类似python中list的存在.可以通过addObject:来添加元素.

NSDictionary
NSMutableDictionary:一个类似于python中dict的存在,提供setObject:forKey:和removeObjectForKey:


Do things Programatically

获取中文encoding
http://www.cocoadev.com/index.pl?CharacterEncoding
NSStringEncoding gb18030 = CFStringConvertEncodingToNSStringEncoding 
    (kCFStringEncodingGB_18030_2000);
用中文encoding读文件
[options setObject:absoluteURL
    forKey:NSBaseURLDocumentOption];
[options setObject:[NSNumber numberWithUnsignedInteger:gb18030] 
    forKey:NSCharacterEncodingDocumentOption];
[options setObject:NSPlainTextDocumentType
    forKey:NSDocumentTypeDocumentOption];
 
NSAttributedString *fileContents = [[NSAttributedString alloc]
      initWithURL:absoluteURL options:options 
    documentAttributes:NULL error:outError];
NSTextView背景设为透明
[[textView enclosingScrollView] setDrawsBackground:NO];
[textView setDrawsBackground:NO];
NSImageView显示图片
NSImage *image = [NSImage imageNamed:@"yellow_background.bmp"];
[imageView setImage: image];
给NSTextView和NSTextContainer之间添加空白区域
[myNSTextView setTextContainerInset:NSMakeSize (width, height)];
使用NSLog,NSAssert输出信息,帮助调试
NSLog(@"init, %p, %p",backgroundImage, textView);
NSAssert([myWindow delegate] == self, @"You forgot to connect the window delegate.!");

NSLog 格式化字符串


其他
If you want to name your NSDocument subclass something other than MyDocument (the default name), change the name in Interface Builder and wherever it occurs in the Document header (.h) and implementation (.m) files. You must also change the name under the NSDocumentClass key in the Info.plist file.

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/TextArchitecture/Tasks/TextEditor.html#//apple_ref/doc/uid/20001798-CJBHAJJJ

Monday, October 11, 2010

[Math]凑硬币

有6分9分和25分的硬币,不能用这3种硬币凑出来的面值最大是多少

Sol: General problem 叫 Coin Problem

对于这题, 考察面值N分
(1) N = 3k, 当k>1的时候都可以用6和9凑出来
(2) N = 3k + 1, 当k>=10的时候, N-25是3的倍数且大于等于6,可以用(1)得出
(3) N = 3k + 2, 当k>=18的时候, N-50是3的倍数且大于等于6,可以用(1)得出

所以最大不能表出的面值为53

Sunday, October 03, 2010

[Math]扔骰子

Q: 能否重新设计两个六面骰子,每个骰子上是正整数并且两个骰子之和的分布与两个正常骰子之和相同(Is it possible to change the numbers on two six sided dice to other positive numbers so that the probability distribution of their sum remains unchanged?)

Sol: 考虑两个普通骰子之和的分布的generating function:
(x/6+x^2/6+x^3/6+x^4/6+x^5/6+x^6/6)*(x/6+x^2/6+x^3/6+x^4/6+x^5/6+x^6/6)
= (x/6+2*x^2/6+2*x^3/6+x^4/6)*(x/6+x^3/6+x^4/6+x^5/6+x^6/6+x^8/6)

也就是说可以设计第一个骰子的六面为(1,2,2,3,3,4),第二个骰子的六面为(1,3,4,5,6,8)
这样可以满足新的骰子的和的分布和从前相同