-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
1492 lines (1121 loc) · 82.7 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title><![CDATA[Alvin]]></title>
<link href="http://freealvin.github.io/atom.xml" rel="self"/>
<link href="http://freealvin.github.io/"/>
<updated>2015-04-13T02:09:06+08:00</updated>
<id>http://freealvin.github.io/</id>
<author>
<name><![CDATA[Alvin]]></name>
</author>
<generator uri="http://octopress.org/">Octopress</generator>
<entry>
<title type="html"><![CDATA[Java参数传递]]></title>
<link href="http://freealvin.github.io/blog/2015/04/13/javacan-shu-chuan-di/"/>
<updated>2015-04-13T01:55:00+08:00</updated>
<id>http://freealvin.github.io/blog/2015/04/13/javacan-shu-chuan-di</id>
<content type="html"><![CDATA[<p>参数传递分为值传递和引用传递,Java是面向对象的编程语言,又是如何传递参数的呢?</p>
<!--more-->
<p>Java的值传递和其他语言的类似,没有什么特别的,这里主要介绍对象作为参数的传递。对象作为参数,传递的是对象的引用吗?其实不是。</p>
<p>先面给出一个例子</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">swap</span><span class="o">(</span><span class="n">Employee</span> <span class="n">x</span><span class="o">,</span> <span class="n">Employee</span> <span class="n">y</span><span class="o">)</span>
</span><span class='line'><span class="o">{</span>
</span><span class='line'> <span class="n">Employee</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">x</span><span class="o">;</span>
</span><span class='line'> <span class="n">x</span> <span class="o">=</span> <span class="n">y</span><span class="o">;</span>
</span><span class='line'> <span class="n">y</span> <span class="o">=</span> <span class="n">temp</span><span class="o">;</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>如果Java对对象采用的是引用调用,那么这个方法就能实现交换数据的效果:</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">Employee</span> <span class="n">a</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Employee</span><span class="o">(</span><span class="s">"alvin"</span><span class="o">);</span>
</span><span class='line'><span class="n">Employee</span> <span class="n">b</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Employee</span><span class="o">(</span><span class="s">"nathan"</span><span class="o">);</span>
</span><span class='line'><span class="n">swap</span><span class="o">(</span><span class="n">a</span><span class="o">,</span><span class="n">b</span><span class="o">);</span>
</span></code></pre></td></tr></table></div></figure>
<p>结果会是a变成nathan, b变成alvin吗?最终的结果并没有变化,a仍为alvin,b也仍为nathan。</p>
<p>这个过程说明,Java采用的不是引用调用。实际上,对象引用进行的是<bold>值传递</bold>。</p>
<p>下面总结一下:</p>
<pre><code>1. 方法不能修改一个基本数据类型的参数
2. 方法可以改变对象参数的状态
3. 方法不能对对象参数引用一个新对象。
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Java 类的final引用成员实例]]></title>
<link href="http://freealvin.github.io/blog/2015/04/11/java-lei-de-finalyin-yong-cheng-yuan-shi-li/"/>
<updated>2015-04-11T08:32:47+08:00</updated>
<id>http://freealvin.github.io/blog/2015/04/11/java-lei-de-finalyin-yong-cheng-yuan-shi-li</id>
<content type="html"><![CDATA[<p>final修饰符大都用于基本类型,或不可变类的域(如果类中的每个方法都不会改变其对象,这种类就是不可变的类。例如,String类就是一个不可变的类)。对于可变的类使用final修饰符可能会对读者造成混乱。例如,private final Date hiredate;</p>
<p>仅仅意味着存储在hiredate变量中的<em>对象引用</em>在对象构造之后不能被改变,而不意味着hiredate对象是一个常量。任何方法都可以对hiredate引用的对象调用setTime更改器。</p>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">package</span> <span class="n">per</span><span class="o">.</span><span class="na">alvin</span><span class="o">.</span><span class="na">demos</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">User</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">private</span> <span class="n">String</span> <span class="n">name</span><span class="o">;</span>
</span><span class='line'> <span class="kd">private</span> <span class="kt">int</span> <span class="n">age</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="nf">User</span><span class="o">(){</span>
</span><span class='line'>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="kd">public</span> <span class="nf">User</span><span class="o">(</span><span class="n">String</span> <span class="n">name</span><span class="o">,</span> <span class="kt">int</span> <span class="n">age</span><span class="o">){</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">;</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">age</span> <span class="o">=</span> <span class="n">age</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="n">String</span> <span class="nf">getName</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="n">name</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setName</span><span class="o">(</span><span class="n">String</span> <span class="n">name</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">int</span> <span class="nf">getAge</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="n">age</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="kd">public</span> <span class="kt">void</span> <span class="nf">setAge</span><span class="o">(</span><span class="kt">int</span> <span class="n">age</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">age</span> <span class="o">=</span> <span class="n">age</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">package</span> <span class="n">per</span><span class="o">.</span><span class="na">alvin</span><span class="o">.</span><span class="na">demos</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Item</span> <span class="o">{</span>
</span><span class='line'> <span class="kd">private</span> <span class="kd">final</span> <span class="n">User</span> <span class="n">user</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="nf">Item</span><span class="o">(</span><span class="n">User</span> <span class="n">user</span><span class="o">){</span>
</span><span class='line'> <span class="k">this</span><span class="o">.</span><span class="na">user</span> <span class="o">=</span> <span class="n">user</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'> <span class="kd">public</span> <span class="n">User</span> <span class="nf">getUser</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'> <span class="k">return</span> <span class="n">user</span><span class="o">;</span>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'>
</span><span class='line'> <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span> <span class="o">[]</span> <span class="n">arags</span><span class="o">){</span>
</span><span class='line'> <span class="n">User</span> <span class="n">alvin</span> <span class="o">=</span> <span class="k">new</span> <span class="n">User</span><span class="o">(</span><span class="s">"alvin"</span><span class="o">,</span> <span class="mi">22</span><span class="o">);</span>
</span><span class='line'> <span class="n">Item</span> <span class="n">item</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Item</span><span class="o">(</span><span class="n">alvin</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'> <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">item</span><span class="o">.</span><span class="na">getUser</span><span class="o">().</span><span class="na">getName</span><span class="o">());</span>
</span><span class='line'> <span class="n">alvin</span><span class="o">.</span><span class="na">setName</span><span class="o">(</span><span class="s">"nathan"</span><span class="o">);</span>
</span><span class='line'> <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">item</span><span class="o">.</span><span class="na">getUser</span><span class="o">().</span><span class="na">getName</span><span class="o">());</span>
</span><span class='line'>
</span><span class='line'> <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>
<p></p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Unix环境高级编程之进程环境]]></title>
<link href="http://freealvin.github.io/blog/2015/04/08/unixhuan-jing-gao-ji-bian-cheng-zhi-jin-cheng-huan-jing/"/>
<updated>2015-04-08T19:30:59+08:00</updated>
<id>http://freealvin.github.io/blog/2015/04/08/unixhuan-jing-gao-ji-bian-cheng-zhi-jin-cheng-huan-jing</id>
<content type="html"><![CDATA[<p>当执行程序时,main函数是如何被调用的;命令行参数如何传送给程序;典型的存储器布局是什么样的;如何分配额外的存储空间;进程如何使用环境变量;不同进程的终止形式。</p>
<!--more--->
<h2>main函数</h2>
<p>当内核执行C程序时,在调用main前先调用一个特殊的启动例程。可执行文件将词启动例程指定微程序的起始位置——这由连接编辑器设置,而连接编辑器由C编译器调用,启动例程从内核获得命令行参数和环境变量。</p>
<h2>进程终止</h2>
<p>八种方式可以终止进程,5种为正常终止,它们是:
1.从main返回
2.调用exit
3.调用<em>exit或</em>EXIT
4.最后一个线程从其启动例程返回
5.最后一个线程调用pthread_exit</p>
<p>异常的终止方式有三种
6.调用abort
7.接到一个信号并终止
8.最后一个线程对取消请求做出相应</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[六个月后]]></title>
<link href="http://freealvin.github.io/blog/2015/04/04/liu-ge-yue-hou/"/>
<updated>2015-04-04T17:08:13+08:00</updated>
<id>http://freealvin.github.io/blog/2015/04/04/liu-ge-yue-hou</id>
<content type="html"><![CDATA[<p>六个月后重新找回Octopress开始写作的我,开始知道自己的未来的目标,精通java,阅读Mysql类的书,开始去真正了解分布式系统设计,向我的同学学习,了解机器学习。</p>
<p>我知道现在白天实习,晚上去抽时间学习比较累,但有目标的人心里总是有底的,我相信我能坚持。加油。</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[C语言的缩进和空白]]></title>
<link href="http://freealvin.github.io/blog/2013/12/02/cyu-yan-de-suo-jin-he-kong-bai/"/>
<updated>2013-12-02T21:31:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/12/02/cyu-yan-de-suo-jin-he-kong-bai</id>
<content type="html"><![CDATA[<p>C语言的缩进和空白使用原则</p>
<!--more-->
<h1>一般编码缩进规则</h1>
<p>1.字if、while、for与其后的控制表达式的(括号之间插入一个空格分隔,但括号内的表达式应紧贴括号
例如
<code> while (1) </code>
2.双目运算符的两侧各插入一个空格分隔,单目运算符和操作数之间不加空格
例如:
<code>i␣=␣i␣+␣1、++i、!(i␣<␣1)、-x、&a[1]等)</code></p>
<p>3.后缀运算符和操作数之间也不加空格
例如:
取结构体成员<code>s.a</code>、函数调用<code>foo(arg1)</code>、取数组成员<code>a[i]</code></p>
<ol>
<li><p>,号和;号之后要加空格,这是英文的书写习惯
例如:
<code>for␣(i␣=␣1;␣i␣<␣10;␣i++)、foo(arg1,␣arg2)</code></p></li>
<li><p>有时候为了突出优先级也可以写得更紧凑一些
例如:
<code>for␣(i=1;␣i<10;␣i++)</code>、<code>distance␣=␣sqrt(x<em>x␣+␣y</em>y)</code>等。但是省略的空格一定不要误导了读代码的人,例如<code>a||b␣&&␣c</code>很容易让人理解成错误的优先级.</p></li>
<li><p>接近或大于80个字符的较长语句要折行写
由于UNIX系统标准的字符终端是24行80列的,接近或大于80个字符的较长语句要折行写,折行后用空格和上面的表达式或参数对齐,例如:</p></li>
</ol>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>if␣(sqrt(x*x␣+␣y*y)␣>␣5.0
</span><span class='line'> &&␣x␣<␣0.0
</span><span class='line'> &&␣y␣>␣0.0)</span></code></pre></td></tr></table></div></figure>
<p>再比如:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>foo(sqrt(x*x␣+␣y*y),
</span><span class='line'> a[i-1]␣+␣b[i-1]␣+␣c[i-1])</span></code></pre></td></tr></table></div></figure>
<ol>
<li>较长的字符串可以断成多个字符串然后分行书写
例如:</li>
</ol>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>printf("This is such a long sentence that "
</span><span class='line'> "it cannot be held within a line\n");</span></code></pre></td></tr></table></div></figure>
<p>C编译器会自动把相邻的多个字符串接在一起,以上两个字符串相当于一个字符串”This is such a long sentence that it cannot be held within a line\n”。</p>
<ol>
<li>在变量定义语句中用Tab字符,使变量名对齐
有的人喜欢在变量定义语句中用Tab字符,使变量名对齐,这样看起来很美观。</li>
</ol>
<h1>内核编码缩进规则</h1>
<p>1.要用缩进体现出语句块的层次关系
要用缩进体现出语句块的层次关系,使用Tab字符缩进,不能用空格代替Tab。在标准的字符终端上一个Tab看起来是8个空格的宽度,如果你的文本编辑器可以设置Tab的显示宽度是几个空格,建议也设成8,这样大的缩进使代码看起来非常清晰。如果有的行用空格做缩进,有的行用Tab做缩进,甚至空格和Tab混用,那么一旦改变了文本编辑器的Tab显示宽度就会看起来非常混乱,所以内核代码风格规定只能用Tab做缩进,不能用空格代替Tab。</p>
<ol>
<li>语句块的{或}应该和关键字写在同一行,用空格隔开,而不是单独占一行
if/else、while、do/while、for、switch这些可以带语句块的语句,语句块的{或}应该和关键字写在同一行,用空格隔开,而不是单独占一行。例如应该这样写:</li>
</ol>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>if␣(...)␣{
</span><span class='line'> →语句列表
</span><span class='line'>}␣else␣if␣(...)␣{
</span><span class='line'> →语句列表
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
<p>但很多人习惯这样写:</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>if␣(...)
</span><span class='line'>{
</span><span class='line'> →语句列表
</span><span class='line'>}
</span><span class='line'>else␣if␣(...)
</span><span class='line'>{
</span><span class='line'> →语句列表
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
<p>内核的写法和[K&R]一致,好处是不必占太多行,使得一屏能显示更多代码。这两种写法用得都很广泛,只要在同一个项目中能保持统一就可以了。</p>
<ol>
<li>函数定义的{和}单独占一行,这一点和语句块的规定不同</li>
</ol>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>int␣foo(int␣a,␣int␣b)
</span><span class='line'>{
</span><span class='line'> →语句列表
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
<ol>
<li>switch和语句块里的case、default对齐写
switch和语句块里的case、default对齐写,也就是说语句块里的case、default标号相对于switch不往里缩进,但标号下的语句要往里缩进。例如:</li>
</ol>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>→switch␣(c)␣{
</span><span class='line'>→case 'A':
</span><span class='line'>→ →语句列表
</span><span class='line'>→case 'B':
</span><span class='line'>→ →语句列表
</span><span class='line'>→default:
</span><span class='line'>→ →语句列表
</span><span class='line'>→}</span></code></pre></td></tr></table></div></figure>
<p>用于goto语句的自定义标号应该顶头写不缩进,而不管标号下的语句缩进到第几层。</p>
<ol>
<li>代码中每个逻辑段落之间应该用一个空行分隔开
例如每个函数定义之间应该插入一个空行,头文件、全局变量定义和函数定义之间也应该插入空行,例如:</li>
</ol>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>#include <stdio.h>
</span><span class='line'>#include <stdlib.h>
</span><span class='line'>
</span><span class='line'>int g;
</span><span class='line'>double h;
</span><span class='line'>
</span><span class='line'>int foo(void)
</span><span class='line'>{
</span><span class='line'> →语句列表
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>int bar(int a)
</span><span class='line'>{
</span><span class='line'> →语句列表
</span><span class='line'>}
</span><span class='line'>
</span><span class='line'>int main(void)
</span><span class='line'>{
</span><span class='line'> →语句列表
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
<ol>
<li>一个函数的语句列表如果很长,也可以根据相关性分成若干组,用空行分隔。
一个函数的语句列表如果很长,也可以根据相关性分成若干组,用空行分隔。这条规定不是严格要求,通常把变量定义组成一组,后面加空行,return语句之前加空行,例如:</li>
</ol>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>int main(void)
</span><span class='line'>{
</span><span class='line'> →int →a, b;
</span><span class='line'> →double →c;
</span><span class='line'>
</span><span class='line'> →语句组1
</span><span class='line'>
</span><span class='line'> →语句组2
</span><span class='line'>
</span><span class='line'> →return 0;
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[AT&T汇编语法2]]></title>
<link href="http://freealvin.github.io/blog/2013/12/02/at-and-thui-bian-yu-fa-2/"/>
<updated>2013-12-02T08:45:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/12/02/at-and-thui-bian-yu-fa-2</id>
<content type="html"><![CDATA[<ol>
<li>内联汇编</li>
</ol>
<!--more-->
<p>(一)内联汇编
参考<a href="http://www.ibm.com/developerworks/cn/linux/sdk/assemble/inline/">Linux中x86的内联汇编</a></p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>asm (assembler template
</span><span class='line'>
</span><span class='line'> : output operands (optional)
</span><span class='line'> : input operands (optional)
</span><span class='line'> : list of clobbered registers
</span><span class='line'> (optional)
</span><span class='line'>);</span></code></pre></td></tr></table></div></figure>
<p> 汇编程序模板由汇编指令组成。输入操作数是充当指令输入操作数使用的 C 表达式。输出操作数是将对其执行汇编指令输出的 C 表达式。</p>
<h2>内敛汇编的基本要素</h2>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>{
</span><span class='line'> int a=10, b;
</span><span class='line'> asm ("movl %1, %%eax;
</span><span class='line'> movl %%eax, %0;"
</span><span class='line'> :"=r"(b) /* output */
</span><span class='line'> :"r"(a) /* input */
</span><span class='line'> :"%eax"); /* clobbered register */
</span><span class='line'>}</span></code></pre></td></tr></table></div></figure>
<p>·”b”是输出操作数,由%0引用,“a”是输入操作数,由%1引用<br/>
·”r”是操作数的约束,它指定将变量”a”和”b”存储在寄存器中。输出操作数约束应该带有一个约束修饰符”=“,表示它是输出操作数<br/>
·要在”asm”内使用寄存器%eax,%eax的前面应该再加一个%,换句话说是%%eax,因为”asm”使用%0、%1等来表示变量。任何带有一个%的数都看做是输入/输出操作数,而不认为是寄存器<br/>
·第三个冒号后的修饰寄存器%eax告诉将在asm中修改GCC%eax的值,这样GCC就不适用该寄存器存储任何其它的值<br/>
·movl %1, %%eax将”a”的值移到%eax中, movl %%eax, %0将%eax的内容移到”b”中<br/>
·因为”b”被指定成输出操作数,因此当”asm”的执行完成后,它将反映出更新的值。换句话说,对”asm”内”b”所做的更改将在”asm”外反映出来</p>
<h2>汇编程序模板</h2>
<p>汇编程序模板是一组插入到 C 程序中的汇编指令(可以是单个指令,也可以是一组指令)。每条指令都应该由双引号括起,或者整组指令应该由双引号括起。每条指令还应该用一个定界符结尾。有效的定界符为新行 (\n) 和分号 (;)。 ‘\n’ 后可以跟一个 tab(\t) 作为格式化符号,增加 GCC 在汇编文件中生成的指令的可读性。 指令通过数 %0、%1 等来引用 C 表达式(指定为操作数)。</p>
<p>如果希望确保编译器不会在 “asm” 内部优化指令,可以在 “asm” 后使用关键字 “volatile”。如果程序必须与 ANSI C 兼容,则应该使用 <strong>asm</strong> 和 <strong>volatile</strong>,而不是 asm 和 volatile</p>
<h2>操作数</h2>
<p>C 表达式用作 “asm” 内的汇编指令操作数。在汇编指令通过对 C 程序的 C 表达式进行操作来执行有意义的作业的情况下,操作数是内联汇编的主要特性。</p>
<p>每个操作数都由操作数约束字符串指定,后面跟用括弧括起的 C 表达式,例如:”constraint” (C expression)。操作数约束的主要功能是确定操作数的寻址方式。</p>
<p>可以在输入和输出部分中同时使用多个操作数。每个操作数由逗号分隔开。</p>
<p>在汇编程序模板内部,操作数由数字引用。如果总共有 n 个操作数(包括输入和输出),那么第一个输出操作数的编号为 0,逐项递增,最后那个输入操作数的编号为 n -1。总操作数的数目限制在 10,如果机器描述中任何指令模式中的最大操作数数目大于 10,则使用后者作为限制。</p>
<h2>修饰寄存器列表</h2>
<p>如果 “asm” 中的指令指的是硬件寄存器,可以告诉 GCC 我们将自己使用和修改它们。这样,GCC 就不会假设它装入到这些寄存器中的值是有效值。通常不需要将输入和输出寄存器列为 clobbered,因为 GCC 知道 “asm” 使用它们(因为它们被明确指定为约束)。不过,如果指令使用任何其它的寄存器,无论是明确的还是隐含的(寄存器不在输入约束列表中出现,也不在输出约束列表中出现),寄存器都必须被指定为修饰列表。修饰寄存器列在第三个冒号之后,其名称被指定为字符串。</p>
<p>至于关键字,如果指令以某些不可预知且不明确的方式修改了内存,则可能将 “memory” 关键字添加到修饰寄存器列表中。这样就告诉 GCC 不要在不同指令之间将内存值高速缓存在寄存器中。</p>
<h2>操作数约束</h2>
<p>前面提到过,”asm” 中的每个操作数都应该由操作数约束字符串描述,后面跟用括弧括起的 C 表达式。操作数约束主要是确定指令中操作数的寻址方式。约束也可以指定:
1. 是否允许操作数位于寄存器中,以及它可以包括在哪些种类的寄存器中
2. 操作数是否可以是内存引用,以及在这种情况下使用哪些种类的地址
3. 操作数是否可以是立即数
约束还要求两个操作数匹配。</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[AT&T汇编语言学习1]]></title>
<link href="http://freealvin.github.io/blog/2013/11/27/at-and-thui-bian-yu-yan-xue-xi-1/"/>
<updated>2013-11-27T09:14:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/11/27/at-and-thui-bian-yu-yan-xue-xi-1</id>
<content type="html"><![CDATA[<!--more-->
<h1>一. AT&T汇编语法格式</h1>
<p>(1) 寄存器引用</p>
<pre><code>引用寄存器要在寄存器号前加%,如move %eax, %ebx
</code></pre>
<p>(2) 操作数顺序</p>
<pre><code>操作数排列是从源(左)到目的(右), 如
</code></pre>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>move %eax, %ebx
</span><span class='line'># %eax为源, %ebx为目的地址</span></code></pre></td></tr></table></div></figure>
<p>(3) 常数/立即数的格式<br/></p>
<pre><code>使用立即数,要在数前加$,如mov $4, %ebx
符号常数直接引用 如mov value, %ebx
引用符号地址在符号前加$,如mov $value, %ebx
</code></pre>
<p>(4) 操作数的长度<br/></p>
<pre><code>操作数的长度用加在指令后的符号表示
b(byte)
w(word)
l(long)
如:mov w%ax, %bx
</code></pre>
<h1>二. 转移和调用</h1>
<p>
(1) 在AT&T汇编格式中,绝对转移和调用指令(jmp/call)的操作数前 要加上’*‘作为前缀<br/>
(2) 远转移指令和远调用指令的操作码,在AT&T汇编格式中为”ljump”和”lcall”,在Itel汇编格式中则为”jmp far”和”call far”<br/></p>
<p>AT&T格式</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>ljump $section, $offset
</span><span class='line'>lcall $section, $offset</span></code></pre></td></tr></table></div></figure>
<p>Intel格式</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>jump far section:offset
</span><span class='line'>call far section:offset</span></code></pre></td></tr></table></div></figure>
<p>(3) 远程返回指令<br/></p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>lret $stack_adjust
</span><span class='line'>ret far stack_adjust</span></code></pre></td></tr></table></div></figure>
<p>(4) 寻址方式<br/></p>
<pre><code>用section:disp(base, index, scale)表示,计算方法是:
base + index*scale + disp
section:[base+index*scale+disp] #Intel
</code></pre>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>movl -4(%ebp), %eax mov eax, [ebp-4]
</span><span class='line'>movl array(, %eax, 4), %eax mov eax, [eax*4+array]
</span><span class='line'>movw array(%ebx, %eax, 4), %cx mov cx, [ebx+4*eax+array]
</span><span class='line'>movb $4, %fs:(%eax) mov fs:eax, 4</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Linux中的正则表达式]]></title>
<link href="http://freealvin.github.io/blog/2013/11/26/linuxzhong-de-zheng-ze-biao-da-shi/"/>
<updated>2013-11-26T20:09:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/11/26/linuxzhong-de-zheng-ze-biao-da-shi</id>
<content type="html"><![CDATA[<!--more about 正则表达式-->
<h1>正则表达式</h1>
<br />
<pre><code>正则表达式完成字符串的匹配搜索,在Unix系统中,sed、grep、vi等工具都是用regexp规则。
(1) 正则表达式的基本语法
| 符号 | 含义 | 举例 | 匹配 |
| --------------- | ---------------------------------------- | -------------------- | -------------------------- |
| . | 任何字符 | a.. | a后面有两个字符的字符串 |
| ? | 修饰匹配次数,0次或1次 | x? | 0个或1个x |
| ^ | 匹配行首 | ^word | 位于行首的word |
| + | 重复一次或多次 | y+ | 一个--或多个连续的y |
| $ | 行尾 | x$ | 以x结尾的行 |
| * | 重复0次或多次 | w.*s | 以w开头、s结尾的任何字符串 |
| [字符表] | 字符表中的任一字符 | [tT] | 小写字母t或大写字母T |
| [!字符表] | 匹配不属于范围指定内的字符 | [!a-z] | 匹配不是小写字母的字符 |
| [^字符表] | 任一不在字符表中的字符 | [^a-z A-Z] | 任何非字母 |
| \ | 转义字符 | \\n | 匹配换行符 |
| \{min,max\} | 前导正则表达式最少重复min次,最多重复max次 | x\{1,5\} | 最少1个最多5个x |
| \(...\) | 将圆括号中匹配的字符串存储到下一个寄存器中 | \(more\)and\1 | 匹配more and more |
| 表达式|1表达式2 | 匹配表达式1或表达式2 | hello| happy | 匹配hello或happy |
</code></pre>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Ruby on Rails With Mongodb]]></title>
<link href="http://freealvin.github.io/blog/2013/11/18/ruby-on-rails-with-mongodb/"/>
<updated>2013-11-18T00:35:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/11/18/ruby-on-rails-with-mongodb</id>
<content type="html"><![CDATA[
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[把cs50.h添加到库中]]></title>
<link href="http://freealvin.github.io/blog/2013/10/11/ba-cs50-dot-htian-jia-dao-ku-zhong/"/>
<updated>2013-10-11T23:25:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/10/11/ba-cs50-dot-htian-jia-dao-ku-zhong</id>
<content type="html"><![CDATA[<p> 我最近在看网易公开课CS50,自我感觉非常喜欢David J. Malan的教学方法,非常羡慕和感慨哈佛学生的大学学习环境。也感谢网易为广大中国学生带来的福利。</p>
<p> 以下是自己网上搜素总结的如何模仿搭建CS50中Malan使用的开发环境。</p>
<p> <!--more-->
1. 参考<a href="https://manual.cs50.net/library/"> CS50 Manual</a>,选择适合自己的安装方法
我的是Centos,所以执行以下命令,安装cs50库</p>
<pre><code><div class='bogus-wrapper'><notextile><figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</code></pre>
<p><span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>yum -y install gcc
</span><span class='line'> wget <a href="http://mirror.cs50.net/library50/c/library50-c-5.zip">http://mirror.cs50.net/library50/c/library50-c-5.zip</a>
</span><span class='line'> unzip library50-c-5.zip
</span><span class='line'> rm -f library50-c-5.zip
</span><span class='line'> cd library50-c-5
</span><span class='line'> gcc -c -ggdb -std=c99 cs50.c -o cs50.o
</span><span class='line'> ar rcs libcs50.a cs50.o
</span><span class='line'> chmod 0644 cs50.h libcs50.a
</span><span class='line'> mkdir -p /usr/local/include
</span><span class='line'> chmod 0755 /usr/local/include
</span><span class='line'> mv -f cs50.h /usr/local/include
</span><span class='line'> mkdir -p /usr/local/lib
</span><span class='line'> chmod 0755 /usr/local/lib
</span><span class='line'> mv -f libcs50.a /usr/local/lib
</span><span class='line'> rm -rf library50-c-5</span></code></pre></td></tr></table></div></figure></notextile></div>
2. 安装完cs50库后, 修改make选项</p>
<pre><code>修改~/.bashrc文件
在文件末尾添加
</code></pre>
<p> <figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class=''><span class='line'># configure gcc
</span><span class='line'> export CC=gcc
</span><span class='line'> export CFLAGS=“-ggdb -std=c99 -Wall -Werror -Wformat=0”
</span><span class='line'> export LANG=C
</span><span class='line'> export LDLIBS=“-lcs50 -lm”
</span><span class='line'> alias gcc=“gcc $CFLAGS”</span></code></pre></td></tr></table></div></figure>
3. 执行source ~/.bashrc使更改后的bashrc文件生效</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[递归与分治策略之整数划分]]></title>
<link href="http://freealvin.github.io/blog/2013/09/18/di-gui-yu-fen-zhi-ce-lue-zhi-zheng-shu-hua-fen/"/>
<updated>2013-09-18T10:49:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/09/18/di-gui-yu-fen-zhi-ce-lue-zhi-zheng-shu-hua-fen</id>
<content type="html"><![CDATA[<p> 将正整数划分为一些列正整数之和。在正整数n的所有不同的划分中,将最大加数n1不大于m的划分个数记为q(n, m).可以建立q(n, m)的递归关系。</p>
<!--more-->
<ol>
<li> q(n,1) = 1, n>=1</li>
<li> q(n,m) = q(n,n) m>=n</li>
<li> q(n,n) = 1+q(n,n-1)</li>
<li> q(n,m) = q(n, n-1)+q(n-m, m), n>m>1:正整数n的最大加数不大于m的划分可以分成最大加数为m的划分和最大加数小于等于m-1的划分。</li>
</ol>
<p>有递归关系可以写出递归函数。</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='cpp'><span class='line'><span class="kt">int</span> <span class="nf">q</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">,</span> <span class="kt">int</span> <span class="n">m</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'> <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o"><</span><span class="mi">1</span><span class="o">||</span><span class="n">m</span><span class="o"><</span><span class="mi">1</span><span class="p">)</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'> <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="mi">1</span><span class="o">||</span><span class="n">m</span><span class="o">==</span><span class="mi">1</span><span class="p">)</span> <span class="k">return</span> <span class="mi">1</span><span class="p">;</span>
</span><span class='line'> <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o"><</span><span class="n">m</span><span class="p">)</span> <span class="k">return</span> <span class="n">q</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">);</span>
</span><span class='line'> <span class="k">if</span><span class="p">(</span><span class="n">n</span><span class="o">==</span><span class="n">m</span><span class="p">)</span> <span class="k">return</span> <span class="mi">1</span><span class="o">+</span><span class="n">q</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="n">q</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">m</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">+</span><span class="n">q</span><span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="n">m</span><span class="p">,</span> <span class="n">m</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[如何从零开始学会 Ruby on Rails]]></title>
<link href="http://freealvin.github.io/blog/2013/08/28/ru-he-cong-ling-kai-shi-xue-hui-ruby-on-rails/"/>
<updated>2013-08-28T15:53:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/08/28/ru-he-cong-ling-kai-shi-xue-hui-ruby-on-rails</id>
<content type="html"><![CDATA[<!--more-->
<p>来自<a href="http://huacnlee.com/blog/how-to-start-learning-ruby-on-rails/">http://huacnlee.com/blog/how-to-start-learning-ruby-on-rails/</a>
以下步骤请一步一步来,切勿心急跳过,最好连我上面的链接都不要提前点击!
1.安装 Linux 或 Mac, 开发工具使用 Windows & Linux –> Sublime Text 2, Mac –> TextMate 2 当然你也可以用 Vim 或 Sublime text 2;</p>
<p>2.安装 Ruby 和 Rails 开发环境可以按照这个流程:《如何快速正确的安装 Ruby, Rails》;</p>
<p>3.看完 <a href="http://guides.rubyonrails.org">http://guides.rubyonrails.org</a> 这里有 繁体版本;</p>
<p>4.通过学习 Rails Guides 实现一个博客系统,包涵完整的功能(包括 UI),预计两周;</p>
<p>5.从头到尾一字不漏的看完 《应用 Rails 进行敏捷 Web 开发》;</p>
<p>6.回头审视之前的博客设计和《应用 Rails 进行敏捷 Web 开发》的区别,可以再重新搞个博客;</p>
<p>7.看完 《Getting Real》,预计两个小时;</p>
<p>8.搞明白 Gem, RVM, Bundler 是什么;</p>
<p>9.看完 《Rework》;</p>
<p>10.学习 Git, 上 <a href="http://github.com">http://github.com</a> 并长期使用;</p>
<p>11.看 <a href="http://railscasts.com">http://railscasts.com</a> 的视频教程,从 第一页 开始看,可以学到很多技巧,以免走弯路。</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Windows下Python安装模块]]></title>
<link href="http://freealvin.github.io/blog/2013/08/27/windowsxia-pythonan-zhuang-mo-kuai/"/>
<updated>2013-08-27T17:39:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/08/27/windowsxia-pythonan-zhuang-mo-kuai</id>
<content type="html"><![CDATA[<!--more-->
<p>以安装Beautifulsoup4为例:</p>
<p>1.到网站上下载:<a href="http://www.crummy.com/software/BeautifulSoup/bs4/download/">http://www.crummy.com/software/BeautifulSoup/bs4/download/</a></p>
<p>2.解压文件到C:\Python27</p>
<p>3.cmd运行C:\Python27\BeautifulSoup>python setup.py install</p>
<p>4.注意库名:creating build\lib\bs4</p>
<p>5.测试一下是否能导入:</p>
<blockquote><blockquote><blockquote><p>import bs4</p></blockquote></blockquote></blockquote>
<p>如果不能导入,会提示:ImportError: No module named bs4</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[重载运算符++]]></title>
<link href="http://freealvin.github.io/blog/2013/08/23/zhong-zai-yun-suan-fu-plus-plus/"/>
<updated>2013-08-23T13:19:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/08/23/zhong-zai-yun-suan-fu-plus-plus</id>
<content type="html"><![CDATA[<p>自定义重载运算符++时,为了区分前++和后++,在重载后++时添加哑参数。</p>
<!--more-->
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='c++'><span class='line'> <span class="n">F</span><span class="o">&</span> <span class="k">operator</span><span class="o">++</span><span class="p">(){</span>
</span><span class='line'> <span class="k">return</span> <span class="o">*</span><span class="k">this</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span><span class='line'> <span class="n">F</span> <span class="k">operator</span><span class="o">++</span><span class="p">(</span><span class="kt">int</span><span class="p">){</span><span class="c1">//此处使用到哑元</span>
</span><span class='line'> <span class="n">F</span> <span class="n">old</span><span class="p">(</span><span class="o">*</span><span class="k">this</span><span class="p">);</span>
</span><span class='line'> <span class="k">return</span> <span class="n">old</span><span class="p">;</span>
</span><span class='line'> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>由上可知,自定义重载的++运算符,前++往往比后++效率高,所以一般选择使用前++</p>
]]></content>
</entry>
<entry>
<title type="html"><![CDATA[Python Dict]]></title>
<link href="http://freealvin.github.io/blog/2013/08/22/python-dict/"/>
<updated>2013-08-22T13:07:00+08:00</updated>
<id>http://freealvin.github.io/blog/2013/08/22/python-dict</id>
<content type="html"><![CDATA[<h1>dict</h1>
<p>字典的用法,dict的查找特别快捷。如果我们的项目经常用到搜索某些数据,最好用dict类型。</p>
<!--more-->
<h2>创建dict</h2>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">dict1</span> <span class="o">=</span> <span class="p">{}</span>
</span></code></pre></td></tr></table></div></figure>
<p>或者</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="o">>>></span> <span class="nb">dict</span><span class="p">(</span><span class="n">sape</span><span class="o">=</span><span class="mi">4139</span><span class="p">,</span> <span class="n">guido</span><span class="o">=</span><span class="mi">4127</span><span class="p">,</span> <span class="n">jack</span><span class="o">=</span><span class="mi">4098</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span><span class="s">'sape'</span><span class="p">:</span> <span class="mi">4139</span><span class="p">,</span> <span class="s">'jack'</span><span class="p">:</span> <span class="mi">4098</span><span class="p">,</span> <span class="s">'guido'</span><span class="p">:</span> <span class="mi">4127</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<p>或者用dict构造函数创建dict变量</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">dict2</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="n">key1</span><span class="p">,</span> <span class="n">value1</span><span class="p">),(</span><span class="n">key2</span><span class="p">,</span><span class="n">value2</span><span class="p">),</span><span class="o">...</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>
<p>表达式创建</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="o">>>></span> <span class="n">dict3</span> <span class="o">=</span> <span class="p">{</span><span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">)}</span>
</span><span class='line'><span class="p">{</span><span class="mi">2</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">:</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">6</span><span class="p">:</span> <span class="mi">36</span><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>
<h2>添加元素</h2>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">tel</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'><span class="c">#dict1[key] = value</span>
</span><span class='line'><span class="n">tel</span><span class="p">[</span><span class="s">'guido'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4127</span>
</span><span class='line'><span class="n">tel</span><span class="p">[</span><span class="s">'jack'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4098</span>
</span><span class='line'><span class="n">tel</span><span class="p">[</span><span class="s">'sape'</span><span class="p">]</span> <span class="o">=</span> <span class="mi">4193</span>
</span></code></pre></td></tr></table></div></figure>
<h2>获取元素值</h2>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="n">num1</span> <span class="o">=</span> <span class="n">tel</span><span class="p">[</span><span class="s">'sape'</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>
<p>如果键不存在,tel[key]操作会出错,所以在获取元素之前,需要判断元素键是否存在
判断方法?</p>
<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'> <span class="k">if</span> <span class="n">tel</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"snape"</span><span class="p">):</span>
</span><span class='line'> <span class="n">num1</span> <span class="o">=</span> <span class="n">tel</span><span class="p">[</span><span class="s">"snape"</span><span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>
<p>或者</p>