1. 下面循环中的语句多长时间执行一次?
while(false)语句;
答:不执行
2.如果在InvestmentTest程序的main方法中,将RATE设置为0将会出现什么情况?
答:死循环
3.将waitYear方法的for循环写为while循环。
答:while(years<=n){
years++;
....
}
4.下面的for循环执行多少次?
for(i=0;i<=10;i++) System.out.println(i*i);
答:11次
5.如何修改嵌套循环,使其输出一个正方形而不是三角形?
答:for(int i=1;i<=width;i++){
for(int j=1;j<=n;j++){
6.在下列嵌套循环执行后,n的值是多少?
int n=0;
for(int i=1;i<=5;i++)
for(int j=0;j<i;j++)
n=n+j;
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐
- —软件人才免语言低担保 赴美带薪读研!—
源代码下载地址:
http://download.csdn.net/detail/cq20110310/4988409
一.建立示例程序hello world
1.编写asdf 文件hello.asd
(defpackage :hello-system (:use #:asdf #:cl))
(in-package :hello-system)
(defsystem hello
:name "hello world"
:version "0.1"
:author "cq"
:depends-on ()
:components ((:file "package")
(:file "hello" :depends-on ("package"))))
这个是asd即编译和加载lisp的依赖关系
defpackage定义一个系统的包名 hello-sytem, 是从后面三个包集成来的。
in-package 导入包,如果要用一个包,必须用in-package导入这个包。
接下来的 defsystem 宏就定义了整个项目的代码结构,以及一些无用的附加信息。重要的部分是 components,它定义了两个有明确依赖关系的源代码文件 package.lisp, 和 hello.lisp,一般而言,对于稍具规模的正规 lisp 程序,至少需要三个代码文件:一个用来定义 package,一个存放配置信息(这里省掉),一个放实际的业务逻辑代码。如果此项目依赖于其他 asdf 格式的 lisp 软件包,那么写在 depends-on 段里即可。
2.包定义文件package.lisp
(in-package :hello-system) (defpackage hello (:nicknames hello) (:use #:cl) (:export main *default-name*))
定义了一个包,名为:hello,然后用 use 段设置这个包可以访问所有标准 Common Lisp 符号,根据 Lisp 标准他们位于 common-lisp 包里,这个包的昵称是 cl。最后我导出了两个 hello 包里的符号作为外部接口。
3.实现文件hello.lisp
(in-package :hello)
(defvar *default-name* "world")
(defun main (args)
(if (null args)
(format t "hello ~a~%" *default-name*)
(hello args)))
(defun hello(names)
(when names
(format t "hello ~a~%" (car names))
(hello (cdr names))))
定义了两个函数main和hello
上述代码里有两个函数定义,main 函数是整个程序的入口,入口参数是一个列表,如果列表为空的话就产生默认输出然后程序结束,否则就调用另一个函数 hello 来实际产生针对每个列表元素的输出,注意到这个函数我采用了尾递归的写法,这在 lisp 程序里是非常自然的编程风格,完全没有任何性能折损而且相比循环结构节省了显式的循环变量。
二.编译和加载lisp文件
1,每个模块都要有一个描述文件,module-name.asd(本例中就是hello.asd)。该文件声明了模块名,和构成该模块的文件列表。可以描述lisp文件之间的依赖关系,也可以描述模块之间的依赖关系。asd文件,类似于VC的工程文件,类似于make文件。
加载模块的方法1:
(push #p”example/” asdf:*central-registry*) :添加asd文件所在的目录
(asdf:load-system “module-name”) :加载一个模块(本例hello)
加载模块的方法2:
(load “example/module-name.asd”) :读取asd文件的内容,不加载模块()
(asdf:load-system “module-name”) :加载一个模块
三.测试代码
CL-USER: (hello:main nil)
hello world
NIL
CL-USER: (hello:main '("a" "netease" "sa"))
hello a
hello netease
hello sa
NIL
hello是包名,main是函数名
也可以用in-package先导入包,在直接输入函数名。
源代码下载地址:
http://download.csdn.net/detail/cq20110310/4988409
引用文章地址
1.http://tianchunbinghe.blog.163.com/blog/static/7001200692314249376/
2.http://blog.csdn.net/dragonzht/article/details/8299539
IOS提供了对各种文档(PDF, PPT, word, txt, jpg等)的浏览功能,这个非常使用,因为我们难免需要app里浏览各种文档。官方的Sample code DocInteraction展示了如何在IOS浏览各种格式的文档。
本Sample非常简单,在tableview里列出了预定的和制定目录下的文档列表,点击某个文档时,能切换view并预览文档内容:
从这个sample里能学到的关键点是:
- 从文件目录中载入文件列表
- 监控文档目录中文件的变动,并实时反馈在UI上
- 预览各种文档内容
IOS提供了NSFileManager来访问文件系统,从以下代码能很清晰地了解到如何通过NSFileManager查询某个目录路径下所有所有文件:
- (void)directoryDidChange:(DirectoryWatcher *)folderWatcher
{
[self.documentURLs removeAllObjects]; // clear out the old docs and start over
NSString *documentsDirectoryPath = [self applicationDocumentsDirectory];
NSArray *documentsDirectoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectoryPath error:NULL];
for (NSString* curFileName in [documentsDirectoryContents objectEnumerator])
{
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:curFileName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
BOOL isDirectory;
[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
// proceed to add the document URL to our list (ignore the "Inbox" folder)
if (!(isDirectory && [curFileName isEqualToString: @"Inbox"]))
{
[self.documentURLs addObject:fileURL];
}
}
[self.tableView reloadData];
}在UI上展现文件列表就是通过tableView的datasource来完成,在上一节的分享中已分析此部分,此时只需reloadData刷新一下即可。监控文档目录中文件的变动,并实时反馈在UI上
对文件目录的监控本例子是借助系统内核函数来完成,先看代码:
- (BOOL)startMonitoringDirectory:(NSString *)dirPath
{
// Double initializing is not going to work...
if ((dirKQRef == NULL) && (dirFD == -1) && (kq == -1))
{
// Open the directory we're going to watch
dirFD = open([dirPath fileSystemRepresentation], O_EVTONLY);
if (dirFD >= 0)
{
// Create a kqueue for our event messages...
kq = kqueue();
if (kq >= 0)
{
struct kevent eventToAdd;
eventToAdd.ident = dirFD;
eventToAdd.filter = EVFILT_VNODE;
eventToAdd.flags = EV_ADD | EV_CLEAR;
eventToAdd.fflags = NOTE_WRITE;
eventToAdd.data = 0;
eventToAdd.udata = NULL;
int errNum = kevent(kq, &eventToAdd, 1, NULL, 0, NULL);
if (errNum == 0)
{
CFFileDescriptorContext context = { 0, self, NULL, NULL, NULL };
CFRunLoopSourceRef rls;
// Passing true in the third argument so CFFileDescriptorInvalidate will close kq.
dirKQRef = CFFileDescriptorCreate(NULL, kq, true, KQCallback, &context);
if (dirKQRef != NULL)
{
rls = CFFileDescriptorCreateRunLoopSource(NULL, dirKQRef, 0);
if (rls != NULL)
{
CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
CFRelease(rls);
CFFileDescriptorEnableCallBacks(dirKQRef, kCFFileDescriptorReadCallBack);
// If everything worked, return early and bypass shutting things down
return YES;
}
// Couldn't create a runloop source, invalidate and release the CFFileDescriptorRef
CFFileDescriptorInvalidate(dirKQRef);
CFRelease(dirKQRef);
dirKQRef = NULL;
}
}
// kq is active, but something failed, close the handle...
close(kq);
kq = -1;
}
// file handle is open, but something failed, close the handle...
close(dirFD);
dirFD = -1;
}
}
return NO;
}
这段代码基本上都是对内核c函数的使用,虽然有些晦涩,但流程还是很简单的,首先是对系统事件的监控,然后基于IOS的thread框架来callback文件目录变动后的回调,回调的具体内容实现在KQCallback中,此方法也非常简单,实际上就是基于Delegate模式调用到上一段的directoryDidChange方法,继而刷新tableview的内容。具体的c函数的用法这里就不多说,但值得我们学习的是如何在IOS的层面上调用底层BSD系统API。
预览各种文档内容
本例子中最核心的点反而是最简单的,IOS提供了QLPreviewController,通过此controller能直接得到预览各种文档的view,你只需要把view塞到navigationController中即可,代码如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSURL *fileURL;
if (indexPath.section == 0)
{
fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:documents[indexPath.row] ofType:nil]];
}
else
{
fileURL = [self.documentURLs objectAtIndex:indexPath.row];
}
[self setupDocumentControllerWithURL:fileURL];
[self.docInteractionController presentPreviewAnimated:YES];
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.delegate = self;
// start previewing the document at the current section index
previewController.currentPreviewItemIndex = indexPath.row;
[[self navigationController] pushViewController:previewController animated:YES];
[previewController release];
}具体如何设定预览的内容和行为,是在QLPreviewControllerDelegate和QLPreviewControllerDataSource中完成的,所以这里只需要在controller中实现QLPreviewControllerDelegate和QLPreviewControllerDataSource相关方法即可:// Returns the number of items that the preview controller should preview
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)previewController
{
NSInteger numToPreview = 0;
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedIndexPath.section == 0)
numToPreview = NUM_DOCS;
else
numToPreview = self.documentURLs.count;
return numToPreview;
}
// returns the item that the preview controller should preview
- (id)previewController:(QLPreviewController *)previewController previewItemAtIndex:(NSInteger)idx
{
NSURL *fileURL = nil;
NSIndexPath *selectedIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedIndexPath.section == 0)
{
fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:documents[idx] ofType:nil]];
}
else
{
fileURL = [self.documentURLs objectAtIndex:idx];
}
return fileURL;
}第一个方法是设定一组文档浏览的个数,会影响到在文档预览中的上/下一页,第二个方法是指定文档预览的文件路径,由此可见在IOS中预览各种文档是件很简单的事儿,并且可预览的格式非常广,只要在mac的预览能支持的,这里都能支持,简单测试后发现基本能支持所有常规文档。