-
Notifications
You must be signed in to change notification settings - Fork 4
/
search.xml
277 lines (133 loc) · 223 KB
/
search.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
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>编译原理课设</title>
<link href="/2018/08/22/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86%E8%AF%BE%E8%AE%BE/"/>
<url>/2018/08/22/%E7%BC%96%E8%AF%91%E5%8E%9F%E7%90%86%E8%AF%BE%E8%AE%BE/</url>
<content type="html"><![CDATA[<h2 id="1-课程设计简介"><a href="#1-课程设计简介" class="headerlink" title="1. 课程设计简介"></a>1. 课程设计简介</h2><blockquote><p>任务: 扩展PL/0编译程序功能</p></blockquote><p>目的:扩充PL/0编译程序功能,<br>要求:</p><ol><li>阅读、研究PL/0编译程序源文件。</li><li>在上述工作基础上,可有选择地补充、完善其中词法分析、语法分析、语义分析、目标代码生成、<br>目标代码解释执行等部分的功能。如以语法分析部分为例,则可以增加处 理更多语法成分的功能,<br>如可处理一维数组、++、–、+=、-=、*=、/=、%(取余)、!(取反)、repeat、for、else、<br>开方、处理注释、错误提示、标示符或变量中可以有下划线等。</li><li>设计编制典型的运行实例,以便能反应出自己所作的改进。</li></ol><h2 id="2-PL-0编译程序解读"><a href="#2-PL-0编译程序解读" class="headerlink" title="2. PL/0编译程序解读"></a>2. PL/0编译程序解读</h2><blockquote><p>程序总体实现</p></blockquote><p><img src="/images/project/compiling/1.png"></p><blockquote><p>主要功能简介</p></blockquote><ol><li>词法分析<pre><code>词法分析子程序名为getsym,功能是从源程序中读出一一个单词符号(token),把它的信息放入全局变量sym、id和num中,语法分析器需要单词时,直接从这三个变量中获得。</code></pre></li><li><p>语法分析</p><pre><code> 语法分析使用自上向下的递归子程序法语法分析主要由分程序分析过程block,常量定义分析过程constdeclaration, 变量定义分析过程vardeclaration,语句处理过程statement,表达式处理过程expressio,项处理过程term, 因子处理过程factor,条件处理过程condition构成,还有出错报告过程error,代码生成过程gen, 测试代码合法性及错误恢复过程test,查询名字表函数position,输出目标代码清单listcode。</code></pre></li><li>目标代码结构和代码生成<pre><code> PL/0编译程序所产生的目标代码是一种栈式机的语言,此类栈式机没有累加器和通用寄存器, 有一个栈式存储器,有四个控制寄存器(指令寄存器 I,指令地址寄存器 P,栈顶寄存器 T 和基址寄存器 B),算术逻辑运算都在栈顶进行。</code></pre></li><li>错误处理的原理和技术<pre><code> 首先,错误处理的原则一是尽可能准确指出错误位置和错误属性,二是尽可能进行校正。 PL/0编译程序根据这两种原则,对语法错误的处理采用了两种办法: (1)对于丢失标点类的等容易矫正的错误,就指出出错位置,并加以矫正。 (2) 对于难以矫正的错误,为了使该错误不影响到整个程序后续的编译, 把错误尽量的局限在一个局部的语法单位中。</code></pre></li></ol><h2 id="3-PL-0功能扩充"><a href="#3-PL-0功能扩充" class="headerlink" title="3. PL/0功能扩充"></a>3. PL/0功能扩充</h2><h3 id="3-1-增加语句for-lt-语句-gt-;-lt-条件-gt-;-lt-语句-gt-;"><a href="#3-1-增加语句for-lt-语句-gt-;-lt-条件-gt-;-lt-语句-gt-;" class="headerlink" title="3.1 增加语句for(<语句>;<条件>;<语句>);"></a>3.1 增加语句for(<语句>;<条件>;<语句>);</h3><p>Step1: 在pl0.h中修改</p><pre><code> pl0.h中在enum symbol { } 符号枚举集中加入 forsym, 并更改符号的个数 symnum</code></pre><p>Step2: 增加保留字名字并增加对应的保留字符号</p><pre><code> 名字: strcpy(&(word[6][0]),"for"); 符号: wsym[6]=forsym; (注: 顺序按26个字母的顺序重新排列)</code></pre><p>Step3: 增加语句开始符号集</p><pre><code> statbegsys[forsym]=true;</code></pre><p>Step4: 语句处理函数内添加关于for语句的处理</p><ul><li>int i , cx1 , cx2 , cx3 , cx4 , cx5; (cx3,cx4,cx5为新增加的地址)</li></ul><pre class=" language-C++"><code class="language-C++">if(sym == forsym){ getsymdo; if(sym != lparen) error(34);//没有左括号出错 else { getsymdo; statementdo(nxtlev, ptx, lev); //S1代码 //语句缺少分号出错 if(sym != semicolon) error(10); else { /*cx是当前指令的地址 保存判断条件操作的 位置 */ cx1=cx; getsymdo; conditiondo(nxtlev, ptx, lev); //E代码 if(sym != semicolon) error(10); else { cx2=cx; gendo(jpc,0,0); cx3=cx; gendo(jmp,0,0); getsymdo; cx4=cx; //S2代码 statementdo(nxtlev, ptx, lev); if(sym != rparen) error(22); //缺少右括号出错 else { gendo(jmp,0,cx1); // 回头重新判断条件 getsymdo; cx5=cx; statementdo(nxtlev, ptx, lev); //S3代码 code[cx3].a=cx5; gendo(jmp,0,cx4); code[cx2].a=cx; // 反填跳出循环的地址,与if类似 } } } }}</code></pre><h3 id="3-2-扩充语句if-lt-条件-gt-then-lt-语句-gt-else-lt-语句-gt-;"><a href="#3-2-扩充语句if-lt-条件-gt-then-lt-语句-gt-else-lt-语句-gt-;" class="headerlink" title="3.2 扩充语句if <条件> then <语句> else <语句>;"></a>3.2 扩充语句if <条件> then <语句> else <语句>;</h3><p>Step1: 在pl0.h中修改</p><pre><code> pl0.h中在enum symbol { } 符号枚举集中加入 elsesym, 并更改符号的个数 symnum</code></pre><p>Step2: 增加保留字名字并增加对应的保留字符号</p><pre><code> 名字: strcpy(&(word[4][0]),"else"); 符号: wsym[4]=elsesym; (注: 顺序按26个字母的顺序重新排列)</code></pre><p>Step3: 语句处理函数内添加关于if语句的处理</p><pre class=" language-c++"><code class="language-c++">if(sym==ifsym) /*准备按照if语句处理*/{ getsymdo; memcpy(nxtlev,fsys,sizeof(bool)*symnum); nxtlev[thensym]=true; nxtlev[dosym]=true; /*后跟符号为then或do*/ conditiondo(nxtlev,ptx,lev); /*调用条件处理(逻辑运算)函数*/ if(sym==thensym) { getsymdo; } else { error(16); /*缺少then*/ } cx1=cx; /*保存当前指令地址*/ gendo(jpc,0,0); /*生成条件跳转指令,跳转地址暂写0*/ statementdo(fsys,ptx,lev); /*处理then后的语句*/ if(sym==semicolon) { getsymdo; if(sym==elsesym) /*then语句后出现else*/ { getsymdo; cx2=cx; /*cx为当前的指令地址,cx+1即为then语句执行后的else语句的位置,回填地址*/ code[cx1].a=cx+1; gendo(jmp,0,0); statementdo(fsys,ptx,lev); /*经statement处理后,cx为else后语句执行 完的位置,它正是前面未定的跳转地址,回填地址*/ code[cx2].a=cx; } else { /*经statement处理后,cx为then后语句执行完的位置,它正是前面未定的跳转地址*/ code[cx1].a=cx; } } else { error(5); }}</code></pre><h3 id="3-3-增加语句repeat-lt-语句-gt-until-lt-条件-gt-;"><a href="#3-3-增加语句repeat-lt-语句-gt-until-lt-条件-gt-;" class="headerlink" title="3.3 增加语句repeat <语句> until <条件>;"></a>3.3 增加语句repeat <语句> until <条件>;</h3><p>Step1: 在pl0.h中修改</p><pre><code> pl0.h中在enum symbol { } 符号枚举集中加入 repeatsym, untilsym, 并更改符号的个数 symnum</code></pre><p>Step2: 增加保留字名字并增加对应的保留字符号</p><pre><code> 名字: strcpy(&(word[11][0]),"repeat"); strcpy(&(word[13][0]),"until"); 符号: wsym[11]=repeatsym; wsym[13]=untilsym; (注: 顺序按26个字母的顺序重新排列)</code></pre><p>Step3: 增加语句开始符号集</p><pre><code> statbegsys[repeatsym]=true;</code></pre><p>Step4: 语句处理函数内添加关于repeat语句的处理</p><pre class=" language-c++"><code class="language-c++">else if(sym==repeatsym){ cx1=cx; /*保存当前指令地址*/ getsymdo; memcpy(nxtlev,fsys,sizeof(bool)*symnum); nxtlev[untilsym]=true; statementdo(fsys,ptx,lev); if(sym==semicolon) { getsymdo; if(sym==untilsym) { getsymdo; conditiondo(fsys,ptx,lev); /*经condition处理后,cx1为repeat后循环语句的位置,条件为假时一直循环*/ gendo(jpc,0,cx1); } } else { error(5); }}</code></pre><h3 id="3-4-增加自增自减运算"><a href="#3-4-增加自增自减运算" class="headerlink" title="3..4. 增加自增自减运算"></a>3..4. 增加自增自减运算</h3><p>对于++和–的扩充,分为两种情况:</p><ol><li>作为语句的情况<pre><code> 例如: ++i,--i,i++,i--;</code></pre></li><li><p>作为表达式的因子的情况</p><pre><code> 例如: b:=a++;b:=a--; b:=++a;b:=--a;</code></pre><p> 第一种作为语句情况的扩充:<br>Step1: 在pl0.h中修改</p><pre><code> pl0.h中在enum symbol { } 符号枚举集中加入 addadd, subsub, 并更改符号的个数 symnum</code></pre><p> Step2: 语句处理函数内添加关于++ 和–的处理</p><pre class=" language-c++"><code class="language-c++"> else if(sym==addadd) /*检测到后置++符号*/ { getsymdo; // 如果++后面跟的量可以在名字表中找到 if(i!=0) { gendo(lod,lev-table[i].level,table[i].adr); gendo(lit,0,1); gendo(opr,0,2); gendo(sto,lev-table[i].level,table[i].adr); } } else if(sym==subsub) /*检测到后置--符号*/ { getsymdo; // 如果 -- 后面跟的量可以在名字表中找到 if(i!=0) { gendo(lod,lev-table[i].level,table[i].adr); gendo(lit,0,1); gendo(opr,0,3); gendo(sto,lev-table[i].level,table[i].adr); } } if(sym==addadd) /*检测到前置++符号*/ { getsymdo; if(sym==ident) /*后面跟的是变量*/ { i=position(id,*ptx); if(i==0) { error(11); } else { if(table[i].kind!=variable) { /*++后没跟变量,出错*/ error(12); i=0; } else { /*++后跟变量,处理生成中间代码*/ getsymdo; /*先取 值到栈顶*/ gendo(lod,lev-table[i].level,table[i].adr); gendo(lit,0,1); /*将1放到栈顶*/ gendo(opr,0,2); /*加法,即+1,栈顶加次栈顶*/ /*出栈取值到内存*/ gendo(sto,lev-table[i].level,table[i].adr); } } } } else if(sym==subsub) /*检测到前置--符号*/ { getsymdo; if(sym==ident) /*后面跟的是变量*/ { i=position(id,*ptx); if(i==0) { error(11); } else { /*--后没跟变量,出错*/ if(table[i].kind!=variable) { error(12); i=0; } else /*--后跟变量,处理生成中间代码*/ { if(table[i].kind==variable) /*后跟变量*/ { getsymdo; /*先取值到栈顶*/ gendo(lod,lev-table[i].level,table[i].adr); gendo(lit,0,1); /*将1放到栈顶*/ gendo(opr,0,3); /*减法,即-1,栈顶减次栈顶*/ /*出栈取值到内存*/ gendo(sto,lev-table[i].level,table[i].adr); } } } } }</code></pre></li></ol><p>第二种作为语句情况的扩充:</p><p>Step1: 添加因子开始符号集</p><pre><code> facbegsys[addadd]=true; /*前置++*/ facbegsys[subsub]=true; /*前置--*/</code></pre><p>Step2: 在因子处理函数factor中添加 ++和–相关处理</p><pre class=" language-c++"><code class="language-c++">if(sym==addadd) /*因子出2现b:=a++类型*/{ gendo(lit,lev-table[i].level,1); /*将值入栈*/ /*加法,即+1,栈顶加次栈顶*/ gendo(opr,lev-table[i].level,2); /*出栈取值到内存*/ gendo(sto,lev-table[i].level,table[i].adr); /*取值到栈顶*/ gendo(lod,lev-table[i].level,table[i].adr); gendo(lit,0,1); gendo(opr,0,3); /*栈顶值减*/ getsymdo;}else if(sym==subsub) /*因子出现b:=a--类型*/{ gendo(lit,lev-table[i].level,1); /*将值入栈*/ /*减法,即-1,栈顶减次栈顶*/ gendo(opr,lev-table[i].level,3); /*出栈取值到内存*/ gendo(sto,lev-table[i].level,table[i].adr); gendo(lod,lev-table[i].level,table[i].adr); gendo(lit,0,1); gendo(opr,0,2); /*栈顶值加*/ getsymdo;}else if(sym==addadd) /*因子出现b:=++a类型*/{ getsymdo; if(sym==ident) { getsymdo; i=position(id,*ptx); if(i==0) { error(11); } else { if(table[i].kind==variable) /*变量*/ { /*先加后再用a*/ /*先取值到栈顶*/ gendo(lod,lev-table[i].level,table[i].adr); gendo(lit,0,1);/*将值入栈*/ gendo(opr,0,2);/*加法,即+1,栈顶加次栈顶*/ /*出栈取值到内存*/ gendo(sto,lev-table[i].level,table[i].adr); /*取值到栈顶*/ gendo(lod,lev-table[i].level,table[i].adr); } } }}else if(sym==subsub) /*因子出现b:=--a类型*/{ getsymdo; if(sym==ident) { getsymdo; i=position(id,*ptx); if(i==0) { error(11); } else { if(table[i].kind==variable) /*变量*/ { /*先减后再用a*/ /*先取值到栈顶*/ gendo(lod,lev-table[i].level,table[i].adr); gendo(lit,0,1); /*将值入栈*/ gendo(opr,0,3); /*减法,即-1,栈顶减次栈顶*/ /*出栈取值到内存*/ gendo(sto,lev-table[i].level,table[i].adr); /*取值到栈顶*/ gendo(lod,lev-table[i].level,table[i].adr); } } }}</code></pre><h3 id="3-5-增加-,-,-,-运算;"><a href="#3-5-增加-,-,-,-运算;" class="headerlink" title="3.5. 增加+=,-=,*=,/=运算;"></a>3.5. 增加+=,-=,*=,/=运算;</h3><p>Step1: 在pl0.h中修改</p><pre><code> pl0.h中在enum symbol { } 符号枚举集中加入 addequal,subequal, timeseql, slasheql,, 并更 改符号的个数 symnum</code></pre><p>Step2: 在getSym()函数中处理:</p><pre class=" language-c++"><code class="language-c++">if(ch=='+'){ getchdo; if(ch=='=') { sym=addequal; getchdo; } else{ sym=plus; }}else{ if(ch=='-') { getchdo; if(ch=='=') { sym=subequal; getchdo; }else{ sym=minus; } } else { if(ch=='*') { getchdo; if(ch=='=') { sym=timeseql; getchdo; } else { sym=times; } } else { if(ch=='/') { getchdo; if(ch=='=') { sym=slasheql; getchdo; } getchdo; } else{ sym=slash; } }else{ /*当符号不满足上述条件时,全部按照单字符号处理*/ sym=ssym[ch]; if(sym!=period) { getchdo; } } }}</code></pre><p>Step3: 在语句处理函数中添加处理</p><pre class=" language-c++"><code class="language-c++">else if(sym==addequal) /*检测到+=符号*/{ getsymdo; /*找到变量地址并将其值入栈*/ gendo(lod,lev-table[i].level,table[i].adr); if(sym==semicolon) { getsymdo; printf("+=后面直接跟了分号"); } memcpy(nxtlev,fsys,sizeof(bool)* symnum); expressiondo(nxtlev,ptx,lev); gendo(opr,0,2); if(i!=0) { gendo(sto,lev-table[i].level,table[i].adr); }}else if(sym==subequal) /*检测到-=符号*/{ getsymdo; /*找到变量地址并将其值入栈*/ gendo(lod,lev-table[i].level,table[i].adr); if(sym==semicolon) { getsymdo; } memcpy(nxtlev,fsys,sizeof(bool)* symnum); expressiondo(nxtlev,ptx,lev); gendo(opr,0,3); if(i!=0) { gendo(sto,lev-table[i].level,table[i].adr); }}else if(sym==timeseql) /*检测到*=符号*/{ getsymdo; /*找到变量地址并将其值入栈*/ gendo(lod,lev-table[i].level,table[i].adr); if(sym==semicolon) { getsymdo; } memcpy(nxtlev,fsys,sizeof(bool)* symnum); expressiondo(nxtlev,ptx,lev); gendo(opr,0,4); if(i!=0) { gendo(sto,lev-table[i].level,table[i].adr); }}else if(sym==slasheql) /*检测到/=符号*/{ getsymdo; /*找到变量地址并将其值入栈*/ gendo(lod,lev-table[i].level,table[i].adr); if(sym==semicolon) { getsymdo; } memcpy(nxtlev,fsys,sizeof(bool)* symnum); expressiondo(nxtlev,ptx,lev); gendo(opr,0,5); if(i!=0) { gendo(sto,lev-table[i].level,table[i].adr); }}</code></pre><h3 id="3-6-增加一维数组的处理-例如-a-1-2"><a href="#3-6-增加一维数组的处理-例如-a-1-2" class="headerlink" title="3.6 增加一维数组的处理 (例如: a[1:2] )"></a>3.6 增加一维数组的处理 (例如: a[1:2] )</h3><pre><code> 在添加数组处理时, 将数组看做变量的一种,由var声明函数调用array声明函数完成数组声明, 这样就处加入文件输出的相关语句 外,可以完全保留block函数和enter函数;通过改写 factor函数使数组因子包括了后缀的索引号,这样就可以调用通用的表达式函数赋值数组了。 为了方便完成数组相关功能,扩充了虚拟机处理代码。</code></pre><p>Step1: 在pl0.h中修改</p><pre class=" language-c++"><code class="language-c++">/* 定义两个全局变量,用来保存数组定义的下界和容量 */static int g_arrBase = 0;static int g_arrSize = 0;/* 虚拟机代码 */增加lda,sta专门由于数组的处理enum fct {...lda, sta }这两个虚拟机指令lda,sta,分别用来从数组中取数和存到数组中数组元素的访问和存储,是将()后的当成表达式,先处理,得到元素的索引,放在栈顶最后根据数组的首地址,得到某个元素的地址/* 扩充名字表结构,增加一个data域保存数组的下界 */ struct tablestruct{...int data; /* 其他数据,对arrays来说是下界*/}/* 名字表中的类型*/enum object {...arrays //添加数组类型}/* 数组声明处理, 下界和上界允许已经定义过的常量标识符 */int arraydeclaration(int* ptx, int lev, int* pdx);/* 数组元素索引计算与“虚拟机”生成 */int arraycoef(bool *fsys,int *ptx,int lev);</code></pre><p>Step2:<br>pl0.cpp中添加相关arraydeclaration,arraycoef数组处理函数</p><pre class=" language-c"><code class="language-c"><span class="token keyword">int</span> <span class="token function">arraydeclaration</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token operator">*</span> ptx<span class="token punctuation">,</span> <span class="token keyword">int</span> lev<span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">*</span> pdx<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/* 暂存数组标识名,避免被覆盖 */</span> <span class="token keyword">char</span> arrId<span class="token punctuation">[</span>al<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> cstId<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* 常量标识符的位置 */</span> <span class="token keyword">int</span> arrBase<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> arrTop<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* 数组下界、上界的数值 */</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>lbrack<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">/* 标识符之后是'[',则识别为数组 */</span> <span class="token punctuation">{</span> <span class="token function">strcpy</span><span class="token punctuation">(</span>arrId<span class="token punctuation">,</span> id<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* 检查下界 */</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>ident<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>cstId<span class="token operator">=</span><span class="token function">position</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token operator">*</span>ptx<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">!=</span><span class="token number">0</span><span class="token punctuation">)</span> arrBase<span class="token operator">=</span><span class="token punctuation">(</span>constant<span class="token operator">==</span>table<span class="token punctuation">[</span>cstId<span class="token punctuation">]</span><span class="token punctuation">.</span>kind<span class="token punctuation">)</span><span class="token operator">?</span>Table<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> arrBase<span class="token operator">=</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>number<span class="token punctuation">)</span><span class="token operator">?</span>num<span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token operator">==</span>arrBase<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* 检查冒号 */</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">!=</span>colon<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* 检查上界 */</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>ident<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token punctuation">(</span>cstId<span class="token operator">=</span><span class="token function">position</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token operator">*</span>ptx<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">!=</span><span class="token number">0</span><span class="token punctuation">)</span> arrTop<span class="token operator">=</span><span class="token punctuation">(</span>constant<span class="token operator">==</span>table<span class="token punctuation">[</span>cstId<span class="token punctuation">]</span><span class="token punctuation">.</span>kind<span class="token punctuation">)</span><span class="token operator">?</span>table<span class="token punctuation">[</span>cstId<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> arrTop<span class="token operator">=</span><span class="token punctuation">(</span>number<span class="token operator">==</span>sym<span class="token punctuation">)</span><span class="token operator">?</span>num<span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span>arrTop<span class="token operator">==</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* 检查']' */</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">!=</span>rbrack<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* 上下界是否符合条件检查 */</span> g_arrSize<span class="token operator">=</span>arrTop<span class="token operator">-</span>arrBase<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span> g_arrBase<span class="token operator">=</span>arrBase<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>g_arrSize<span class="token operator"><=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment" spellcheck="true">/* 恢复数组的标识符 */</span> <span class="token function">strcpy</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span> arrId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token comment" spellcheck="true">/** 数组元素索引计算与“虚拟机”生成*/</span><span class="token keyword">int</span> <span class="token function">arraycoef</span><span class="token punctuation">(</span>bool <span class="token operator">*</span>fsys<span class="token punctuation">,</span><span class="token keyword">int</span> <span class="token operator">*</span>ptx<span class="token punctuation">,</span><span class="token keyword">int</span> lev<span class="token punctuation">)</span><span class="token punctuation">{</span> bool nxtlev<span class="token punctuation">[</span>symnum<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token function">position</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span><span class="token operator">*</span>ptx<span class="token punctuation">)</span><span class="token punctuation">;</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>sym<span class="token operator">==</span>lbrack<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">/* 索引是括号内的表达式 */</span> <span class="token punctuation">{</span> getsymdo<span class="token punctuation">;</span> <span class="token function">memcpy</span><span class="token punctuation">(</span>nxtlev<span class="token punctuation">,</span>fsys<span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>bool<span class="token punctuation">)</span><span class="token operator">*</span>symnum<span class="token punctuation">)</span><span class="token punctuation">;</span> nxtlev<span class="token punctuation">[</span>rbrack<span class="token punctuation">]</span><span class="token operator">=</span>true<span class="token punctuation">;</span> <span class="token function">expressiondo</span><span class="token punctuation">(</span>nxtlev<span class="token punctuation">,</span>ptx<span class="token punctuation">,</span>lev<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>sym<span class="token operator">==</span>rbrack<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">gendo</span><span class="token punctuation">(</span>lit<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>opr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* 系数修正,减去下界的值 */</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">22</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* 缺少右括号 */</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">51</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">/* 数组访问错误 */</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>Step3: 修改函数enter,block,vardeclaration,factor及statement,使其具备处理数组的功能:</p><pre class=" language-c++"><code class="language-c++">/将数组变量登陆名字表void enter(enum object k, int * ptx, int lev, int * pdx){ ... case arrays: /* 数组名,进行记录下界等*/ table[(*ptx)].level = lev; table[(*ptx)].adr = (*pdx); table[(*ptx)].data = g_arrBase; table[(*ptx)].size = g_arrSize; *pdx = (*pdx)+g_arrSize; break;...}//输出数组名字表到控制台和文件fas.tmpint block(int lev, int tx, bool * fsys){ ... case arrays: printf("%d array %s ", i, table[i].name); printf("lev=%d addr=%d size=%d\n", table[i].level, table[i].adr, table[i].size); fprintf(fas, "%d array %s ", i, table[i].name); fprintf(fas, "lev=%d addr=%d size=%d\n", table[i].level, table[i].adr, table[i].size);//加入数组声明int vardeclaration(int * ptx, int lev, int * pdx){ int arrayRet=-1; if (sym==ident){ /* 先判断数组*/ arrayRet=arraydeclaration(ptx,lev,pdx); switch(arrayRet){ case 1: enter(arrays,ptx,lev,pdx); // 填写数组名 getsymdo; break; case 0: enter(variable,ptx,lev,pdx); // 填写名字表 break; default: return -1; /* 数组定义解析出错*/ } } else error(4); /* var后应是标识*/ return 0;}/*当因子是数组型变量时,调用arraycodefdo将数组的索引入栈顶, 之后按vatiabler变量操作*/int factor(bool * fsys, int * ptx, int lev){ ... switch (table[i].kind) { ... case arrays: /* 名字为数组名*/ arraycoefdo(fsys,ptx,lev); /* 找到变量地址并将其值入栈*/ gendo(lda,lev-table[i].level,table[i].adr); ... } ... }int statement(bool * fsys, int * ptx, int lev){ ... if (sym == ident){ ... if ((table[i].kind != variable)&&(table[i].kind != arrays)) { error(12); i = 0; } else{ enum fct fct1 = sto; switch(table[i].kind) { case arrays: arraycoefdo(fsys, ptx, lev); fct1 = sta; /* 数组保存,要多读一个栈*/ case variable:{...} } } }//增加的两个虚拟机代码的处理:lda,stavoid interpret(){ ... case lda: /* 数组元素访问,当前栈顶为元素索引,执行后,栈顶变成元素的值*/ s[t-1] = s[base(i.l,s,b) + i.a + s[t-1]]; break; case sta: /* 栈顶的值存到数组中,索引为次栈顶*/ t-=2; s[base(i.l,s,b) + i.a + s[t]] = s[t+1]; break; ...}</code></pre><h3 id="3-7-增加取反功能-算术取反和逻辑取反"><a href="#3-7-增加取反功能-算术取反和逻辑取反" class="headerlink" title="3.7 增加取反功能(算术取反和逻辑取反)"></a>3.7 增加取反功能(算术取反和逻辑取反)</h3><pre><code> 对于取反的扩充大体上分为两种方式,一种是算术取反, 一种是逻辑取反,两种取反分别用 ! 和 @ 来实现 对于每种取反的实现,也分为两种情况: (1) 作为语句的情况 例如: !a 和 @a (2)作为表达式的因子的情况 例如: b:=!a; b:= @a;</code></pre><p>作为语句情况的扩充:<br>Step1: 在pl0.h中修改</p><pre><code> pl0.h中在enum symbol { } 符号枚举集中加入 not, logic 并更改符号的个数 symnum ( not代表! , logic代表@ )</code></pre><p>Step2: 初始化</p><pre><code> ssym['!'] = not; ssym['@'] = logic;</code></pre><p>Step3: 在getSym( )中添加</p><pre><code> if(ch=='@') { sym=logic; } if(ch=='!') { sym=not; }</code></pre><p>Step4: 在statement中添加</p><pre class=" language-c"><code class="language-c"><span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>logic<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">/*检测逻辑取反符号@*/</span><span class="token punctuation">{</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>ident<span class="token punctuation">)</span> <span class="token punctuation">{</span> i<span class="token operator">=</span><span class="token function">position</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span><span class="token operator">*</span>ptx<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>kind<span class="token operator">!=</span>variable<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/*++后没跟变量,出错*/</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/*++后跟变量,处理生成中间代码*/</span> getsymdo<span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>lod<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>lit<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>opr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>sto<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>not<span class="token punctuation">)</span> <span class="token punctuation">{</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>ident<span class="token punctuation">)</span><span class="token punctuation">{</span> i<span class="token operator">=</span><span class="token function">position</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span><span class="token operator">*</span>ptx<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>kind<span class="token operator">!=</span>variable<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/*++后没跟变量,出错*/</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/*++后跟变量,处理生成中间代码*/</span> getsymdo<span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>lod<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>opr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>sto<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><p>作为表达式的因子情况的扩充:<br>Step1: 增加因子开始符号集</p><pre><code> facbegsys[not]=true; facbegsys[logic]=true;</code></pre><p>Step2: 在因子处理函数factor()中添加</p><pre class=" language-c"><code class="language-c"><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>logic<span class="token punctuation">)</span> <span class="token punctuation">{</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>ident<span class="token punctuation">)</span> <span class="token punctuation">{</span> i<span class="token operator">=</span><span class="token function">position</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span><span class="token operator">*</span>ptx<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/*先取值到栈顶*/</span> <span class="token function">gendo</span><span class="token punctuation">(</span>lod<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>lit<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>opr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">/*栈顶和次栈顶相比较*/</span> <span class="token comment" spellcheck="true">/*出栈取值到内存*/</span> <span class="token function">gendo</span><span class="token punctuation">(</span>sto<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>not<span class="token punctuation">)</span> <span class="token punctuation">{</span> getsymdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>ident<span class="token punctuation">)</span> <span class="token punctuation">{</span> i<span class="token operator">=</span><span class="token function">position</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span><span class="token operator">*</span>ptx<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">error</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/*先取值到栈顶*/</span> <span class="token function">gendo</span><span class="token punctuation">(</span>lod<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>opr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">/*取反*/</span> <span class="token comment" spellcheck="true">/*出栈取值到内存*/</span> <span class="token function">gendo</span><span class="token punctuation">(</span>sto<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h3 id="3-8-增加错误提示"><a href="#3-8-增加错误提示" class="headerlink" title="3.8 增加错误提示"></a>3.8 增加错误提示</h3><p>Step1:<br> 在出错处理error()中加入switch语句,打印错误信息:</p><pre class=" language-c"><code class="language-c"><span class="token keyword">switch</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">case</span> <span class="token number">1</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****常数说明中的“=”写成“:=”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">2</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****常数说明中的“=”后应是数字。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">3</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****常数说明中的标识符后应是“=”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">4</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****const,var,procedure后应为标识符。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">5</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****漏掉了“,”或“;“。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">6</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****过程说明后的符号不正确(应是语句开始符,或过程定义符)\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">7</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****应是语句开始符。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">8</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****程序体内语句部分的后跟符不正确。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">9</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****程序结尾丢了句号“.”\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">10</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****语句之间漏了“;”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">11</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****标识符未说明。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">12</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****赋值语句中,赋值号左部标识符属性应是变量。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">13</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****赋值语句左部标识符后应是赋值号“:=”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">14</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****call后应为标识符。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">15</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****call后标识符属性应为过程。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">16</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****条件语句中丢了“then”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">17</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****丢了“end”或“;”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">18</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****while型循环语句中丢了“do”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">19</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****语句后的符号不正确。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">20</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****应为关系运算符。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">21</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****表达式内标识符属性不能是过程。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">22</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****表达式中漏掉右括号“)”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">23</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****因子后的非法符号。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">24</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****表达式的开始符不能是此符号。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">30</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****常数越界。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">31</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****表达式内常数越界。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">32</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****嵌套深度超过允许值。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">33</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****read或write或for语句中缺“)”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">34</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****read或write或for语句中缺“(”。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token keyword">case</span> <span class="token number">35</span><span class="token punctuation">:</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"****read语句括号中的标识符不是变量。\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><h3 id="3-9-增加注释处理功能"><a href="#3-9-增加注释处理功能" class="headerlink" title="3.9 增加注释处理功能"></a>3.9 增加注释处理功能</h3><p>Step1: 在getSym()函数中添加</p><pre class=" language-c"><code class="language-c"><span class="token keyword">if</span><span class="token punctuation">(</span>ch<span class="token operator">==</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">{</span> getchdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ch<span class="token operator">==</span><span class="token string">'='</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> sym<span class="token operator">=</span>slasheql<span class="token punctuation">;</span> getchdo<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ch<span class="token operator">==</span><span class="token string">'*'</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">/*添加注释*/</span> <span class="token punctuation">{</span> getchdo<span class="token punctuation">;</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"注释/*%c"</span><span class="token punctuation">,</span>ch<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> getchdo<span class="token punctuation">;</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%c"</span><span class="token punctuation">,</span>ch<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ch<span class="token operator">==</span><span class="token string">'*'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> getchdo<span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>ch<span class="token operator">==</span><span class="token string">'/'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%c注释\n"</span><span class="token punctuation">,</span>ch<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">break</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">continue</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> getchdo<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span> sym<span class="token operator">=</span>slash<span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><h3 id="3-10-增加取余-运算"><a href="#3-10-增加取余-运算" class="headerlink" title="3.10 增加取余(%)运算"></a>3.10 增加取余(%)运算</h3><pre><code> 在提供的指令中没有直接可以实现取余的指令,但是取余运算可以 通过 - , / 和 * 的组合来实现 a % b = a - (a/b)*b ; 例如: 3 % 5 = 3 - (3/5)*5 = 3-0*5 = 3 14 % 5 = 14 - (14/5)*5 = 14 - 2*5 = 4</code></pre><p>Step1: 在pl0.h中修改</p><pre><code> pl0.h中在enum symbol { } 符号枚举集中加入 mod, 并更改符号的个数 symnum</code></pre><p>Step2: 初始化取余%符号</p><pre><code> ssym['%'] = mod;</code></pre><p>Step3: 在getSym()函数中添加关于%的识别处理</p><pre><code> if(ch=='%') { sym=mod; }</code></pre><p>Step4: 在语句处理函数中添加关于取余运算的处理</p><pre class=" language-c"><code class="language-c"><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>mod<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">/*检测到%符号*/</span><span class="token punctuation">{</span> getsymdo<span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//例如a%b = a - (a/b)*b </span> <span class="token comment" spellcheck="true">//将a的值入栈</span> <span class="token comment" spellcheck="true">/*找到变量地址并将其值入栈*/</span> <span class="token function">gendo</span><span class="token punctuation">(</span>lod<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>sym<span class="token operator">==</span>semicolon<span class="token punctuation">)</span> <span class="token punctuation">{</span> getsymdo<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token function">memcpy</span><span class="token punctuation">(</span>nxtlev<span class="token punctuation">,</span>fsys<span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>bool<span class="token punctuation">)</span><span class="token operator">*</span> symnum<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">expressiondo</span><span class="token punctuation">(</span>nxtlev<span class="token punctuation">,</span>ptx<span class="token punctuation">,</span>lev<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//将a和b的值相除</span> <span class="token function">gendo</span><span class="token punctuation">(</span>opr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">/</span>再取b的值到栈顶 <span class="token function">gendo</span><span class="token punctuation">(</span>lod<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>opr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>lod<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>opr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">gendo</span><span class="token punctuation">(</span>opr<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator">!=</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">gendo</span><span class="token punctuation">(</span>fct1<span class="token punctuation">,</span>lev<span class="token operator">-</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>level<span class="token punctuation">,</span>table<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>adr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></code></pre><h2 id="4-扩充功能的测试和运行结果"><a href="#4-扩充功能的测试和运行结果" class="headerlink" title="4. 扩充功能的测试和运行结果"></a>4. 扩充功能的测试和运行结果</h2><h3 id="4-1-测试for循环"><a href="#4-1-测试for循环" class="headerlink" title="4.1 测试for循环"></a>4.1 测试for循环</h3><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> a<span class="token punctuation">,</span>i<span class="token punctuation">;</span><span class="token keyword">begin</span> a<span class="token operator">:=</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span>i<span class="token operator">:=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator"><=</span><span class="token number">3</span><span class="token punctuation">;</span>i<span class="token operator">:=</span>i<span class="token number">+2</span><span class="token punctuation">)</span> <span class="token keyword">begin</span> a<span class="token operator">:=</span>a<span class="token operator">+</span>i<span class="token punctuation">;</span> <span class="token keyword">end</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span><span class="token punctuation">.</span> </code></pre><p>第一次循环,i=1, 1<3, a=0+1 = 1<br>第二次循环,i=3, 3=3, a=1+3 = 4<br>第三次,i=5 >3 跳出循环<br>故a的正确值应为 4</p><p>(2) 运行结果</p><p><img src="/images/project/compiling/rs1.png" width="230" style="margin-left: 25px;"></p><h3 id="4-2-测试if-then-else循环"><a href="#4-2-测试if-then-else循环" class="headerlink" title="4.2 测试if-then-else循环"></a>4.2 测试if-then-else循环</h3><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> a<span class="token punctuation">,</span>b<span class="token punctuation">;</span><span class="token keyword">begin</span> a<span class="token operator">:=</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">read</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> b<span class="token operator"><=</span><span class="token number">3</span> <span class="token keyword">then</span> <span class="token keyword">write</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span><span class="token punctuation">.</span></code></pre><p>当输入的b的值小于等于3时,输出b的值,<br>否则输出a的值,即0</p><p>(2) 运行结果</p><blockquote><p>当输入的值小于等于3时</p></blockquote><p><img src="/images/project/compiling/rs2.png" width="230" style="margin-left: 25px;"></p><blockquote><p>当输入的值大于3时</p></blockquote><p><img src="/images/project/compiling/rs3.png" width="230" style="margin-left: 25px;"></p><h3 id="4-3-测试repeat-until循环"><a href="#4-3-测试repeat-until循环" class="headerlink" title="4.3 测试repeat-until循环"></a>4.3 测试repeat-until循环</h3><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> x<span class="token punctuation">;</span><span class="token keyword">begin</span> x<span class="token operator">:=</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">repeat</span> x<span class="token operator">:=</span>x<span class="token number">+1</span><span class="token punctuation">;</span> <span class="token keyword">until</span> x<span class="token operator">></span><span class="token number">5</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span><span class="token punctuation">.</span></code></pre><p> 当x的值大于5时才输出, 应输出6</p><p>(2) 运行结果</p><p><img src="/images/project/compiling/rs4.png" width="230" style="margin-left: 25px;"></p><h3 id="4-4-测试-,–功能"><a href="#4-4-测试-,–功能" class="headerlink" title="4.4 测试++,–功能"></a>4.4 测试++,–功能</h3><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> i<span class="token punctuation">,</span>a<span class="token punctuation">;</span><span class="token keyword">begin</span> i<span class="token operator">:=</span><span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">+</span><span class="token operator">+</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">+</span><span class="token operator">+</span>i<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">-</span><span class="token operator">-</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">-</span><span class="token operator">-</span>i<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> a<span class="token operator">:=</span>i<span class="token operator">+</span><span class="token operator">+</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> a<span class="token operator">:=</span><span class="token operator">+</span><span class="token operator">+</span>i<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> a<span class="token operator">:=</span>i<span class="token operator">-</span><span class="token operator">-</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> a<span class="token operator">:=</span><span class="token operator">-</span><span class="token operator">-</span>i<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span></code></pre><pre><code>i初始值为1, 执行i++后, i的值变为2执行++i后, i的值又变为3执行i - - 后, i的值变为 2执行 - - i后, i的值变为1执行a:=i++ 后,a的值变为1, i的值变为2执行a:=++i 后,a的值变为3, i的值变为3执行a:=i-- 后,a的值变为3, i的值变为2执行a:= --i后,a的值变为1, i的值变为1</code></pre><p>(2) 运行结果</p><p><img src="/images/project/compiling/rs5.png" width="230" style="margin-left: 25px;display: block;"></p><h3 id="4-5-测试-、-、-、-功能"><a href="#4-5-测试-、-、-、-功能" class="headerlink" title="4.5 测试+=、-=、*=、/=功能"></a>4.5 测试+=、-=、*=、/=功能</h3><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">;</span><span class="token keyword">begin</span> a<span class="token operator">:=</span><span class="token number">1</span><span class="token punctuation">;</span> b<span class="token operator">:=</span><span class="token number">2</span><span class="token punctuation">;</span> c<span class="token operator">:=</span><span class="token number">3</span><span class="token punctuation">;</span> b<span class="token operator">+=</span>a<span class="token punctuation">;</span> c<span class="token operator">-=</span>b<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> a<span class="token operator">+=</span><span class="token number">1</span><span class="token punctuation">;</span> b<span class="token operator">*=</span>a<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> c<span class="token operator">+=</span>b<span class="token punctuation">;</span> c<span class="token operator">/=</span>a<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span><span class="token punctuation">.</span></code></pre><pre><code> 初始值a为1, b为2, c为3 执行b+=a后, b的值变为3 执行c - = b后, c的值变为0 执行a+=1后, a的值变为2 执行b*= a后, b的值变为6 执行c+=b后, c的值变为6 执行c/=a后, c的值变为3</code></pre><p>(2) 运行结果</p><p><img src="/images/project/compiling/rs6.png" width="230" style="margin-left: 25px;display: block;"></p><h3 id="4-6-测试一维数组处理功能"><a href="#4-6-测试一维数组处理功能" class="headerlink" title="4.6 测试一维数组处理功能"></a>4.6 测试一维数组处理功能</h3><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">:</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">begin</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">:=</span><span class="token number">1</span><span class="token punctuation">;</span> a<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token operator">:=</span><span class="token number">2</span><span class="token punctuation">;</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">:=</span>a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">*</span><span class="token number">3</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span><span class="token punctuation">.</span></code></pre><pre><code> 初始化数组a[1:2], a[1]初始值为1, a[2]初始值为2, 执行a[1]:=a[1]*3 后a[1]的值变为3</code></pre><p> (2) 运行结果</p><p><img src="/images/project/compiling/rs7.png" width="230" style="margin-left: 25px;display: block;"></p><h3 id="4-7-测试取反功能-算术取反和逻辑取反"><a href="#4-7-测试取反功能-算术取反和逻辑取反" class="headerlink" title="4.7 测试取反功能(算术取反和逻辑取反)"></a>4.7 测试取反功能(算术取反和逻辑取反)</h3><blockquote><p>算术取反:</p></blockquote><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">;</span><span class="token keyword">begin</span> a<span class="token operator">:=</span> <span class="token number">-5</span><span class="token punctuation">;</span> c<span class="token operator">:=</span><span class="token number">3</span><span class="token punctuation">;</span> b<span class="token operator">:=</span>!a<span class="token punctuation">;</span> !c<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span><span class="token punctuation">.</span></code></pre><pre><code> a的初始值为 -5, c的初始值为3, 执行b=!a 后, b的值变为5, 执行 !c 后, c的值变为 - 3</code></pre><p>(2) 运行结果</p><p><img src="/images/project/compiling/rs8.png" width="230" style="margin-left: 25px;display: block;"></p><blockquote><p>逻辑取反:</p></blockquote><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">;</span><span class="token keyword">begin</span> a<span class="token operator">:=</span><span class="token number">0</span><span class="token punctuation">;</span> b<span class="token operator">:=</span> <span class="token number">-3</span><span class="token punctuation">;</span> c<span class="token operator">:=</span><span class="token operator">@</span>b<span class="token punctuation">;</span> <span class="token operator">@</span>a<span class="token punctuation">;</span> <span class="token operator">@</span>b<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span><span class="token punctuation">.</span></code></pre><pre><code> a的初始值为0 , b的初始值为-3, 执行c:=@b 后, b的值变为0 , c的值也变为0 , 执行@a 后, a的值 变为1, 执行@b 后, b的值变为1, 故最后应输出 1, 1 , 0</code></pre><p>(2) 运行结果<br><img src="/images/project/compiling/rs9.png" width="230" style="margin-left: 25px;display: block;"></p><h3 id="4-8-测试错误提示功能"><a href="#4-8-测试错误提示功能" class="headerlink" title="4.8 测试错误提示功能"></a>4.8 测试错误提示功能</h3><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> a<span class="token punctuation">,</span>b<span class="token keyword">begin</span> a<span class="token operator">=</span><span class="token number">-5</span><span class="token punctuation">;</span> b<span class="token operator">:=</span><span class="token number">3</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span><span class="token punctuation">.</span></code></pre><p>第一句定义变量的语句缺少分号,<br>给变量a赋值的语句 := 写成了= </p><p>(2) 运行结果<br><img src="/images/project/compiling/rs10.png" width="230" style="margin-left: 25px;display: block;"></p><h3 id="4-9-测试注释处理功能"><a href="#4-9-测试注释处理功能" class="headerlink" title="4.9 测试注释处理功能"></a>4.9 测试注释处理功能</h3><p>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> x<span class="token punctuation">;</span><span class="token keyword">begin</span> x<span class="token operator">:=</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">repeat</span> x<span class="token operator">:=</span>x<span class="token number">+1</span><span class="token punctuation">;</span> <span class="token keyword">until</span> x<span class="token operator">></span><span class="token number">5</span><span class="token punctuation">;</span> <span class="token operator">/</span><span class="token operator">*</span> 这是注释 <span class="token operator">*</span><span class="token operator">/</span> <span class="token keyword">write</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">end</span><span class="token punctuation">.</span></code></pre><p>(2) 运行结果</p><p><img src="/images/project/compiling/rs11.png" width="230" style="margin-left: 25px;display: block;"></p><p>5.10 测试取余(%)运算<br>(1) 测试文件</p><pre class=" language-pascal"><code class="language-pascal"><span class="token keyword">var</span> a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">,</span><span class="token punctuation">;</span><span class="token keyword">begin</span> a<span class="token operator">:=</span><span class="token number">16</span><span class="token punctuation">;</span> b<span class="token operator">:=</span><span class="token number">3</span><span class="token punctuation">;</span> c<span class="token operator">:=</span> <span class="token number">-2</span><span class="token punctuation">;</span> a%b<span class="token punctuation">;</span> c%b<span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">write</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">end</span><span class="token punctuation">.</span></code></pre><pre><code> a的初始值为16 , b的初始值为3, c的初始值为 -2 , 执行 a%b后, a的值变为1,执行c%b后, c的值为- 2,所以应该输 出1, -2</code></pre><p>(2) 运行结果</p><p><img src="/images/project/compiling/rs12.png" width="230" style="margin-left: 25px;display: block;"></p><h2 id="5-项目源代码"><a href="#5-项目源代码" class="headerlink" title="5. 项目源代码"></a>5. 项目源代码</h2><blockquote><p>百度网盘下载 ( 包含项目报告 )</p><ul><li>百度网盘链接: 链接:<a href="https://pan.baidu.com/s/1-1Ir02jDdBmVo6NUiU4wsg" target="_blank" rel="noopener">https://pan.baidu.com/s/1-1Ir02jDdBmVo6NUiU4wsg</a></li><li>提取码:vkwm</li></ul></blockquote><blockquote><p>github下载 ( 只含源代码 )</p><ul><li>github下载地址: <a href="https://github.com/fyf2016/PL-0.git" target="_blank" rel="noopener">https://github.com/fyf2016/PL-0.git</a></li></ul></blockquote><blockquote><p>注: 两个地址都可以下载</p></blockquote>]]></content>
<categories>
<category> 课程设计 </category>
</categories>
<tags>
<tag> 课程设计 </tag>
<tag> PL/0 </tag>
</tags>
</entry>
<entry>
<title>数据库三级项目(电影院管理系统)</title>
<link href="/2018/08/12/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%89%E7%BA%A7%E9%A1%B9%E7%9B%AE/"/>
<url>/2018/08/12/%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%89%E7%BA%A7%E9%A1%B9%E7%9B%AE/</url>
<content type="html"><![CDATA[<h2 id="1-项目要求"><a href="#1-项目要求" class="headerlink" title="1.项目要求"></a>1.项目要求</h2><pre><code>以一定的应用环境为背景,运用《数据库原理及应用》课程所学的关系数据库设计理论,设计一个小型数据库应用系统,自选一种数据库管理系统,如MySQL、SQL Server 等,并选用VB、VC++、JAVA、.NET、JSP、ASP 等语言中任意一种进行实现。</code></pre><h2 id="2-项目的简介"><a href="#2-项目的简介" class="headerlink" title="2.项目的简介"></a>2.项目的简介</h2><pre><code>项目采用Java语言和MyEclipse、MySQL工具进行编写,界面模仿猫眼电影网站界面,实现了一个在线电影管理系统,具有用户注册和登录,在线选座,影片管理,票务管理等功能。</code></pre><h2 id="3-系统功能概述"><a href="#3-系统功能概述" class="headerlink" title="3.系统功能概述"></a>3.系统功能概述</h2><h3 id="3-1-系统功能模块图"><a href="#3-1-系统功能模块图" class="headerlink" title="3.1 系统功能模块图"></a>3.1 系统功能模块图</h3><p><img src="/images/project/DBProject/1.png" alt=""></p><hr><h3 id="3-2-系统流程结构图"><a href="#3-2-系统流程结构图" class="headerlink" title="3.2 系统流程结构图"></a>3.2 系统流程结构图</h3><p><img src="/images/project/DBProject/2.png" alt=""></p><hr><h2 id="4-数据库设计"><a href="#4-数据库设计" class="headerlink" title="4.数据库设计"></a>4.数据库设计</h2><h3 id="4-1-概念模型设计"><a href="#4-1-概念模型设计" class="headerlink" title="4.1 概念模型设计"></a>4.1 概念模型设计</h3><blockquote><p>全局ER图</p></blockquote><p><img src="/images/project/DBProject/3.png" alt=""></p><hr><h3 id="4-2-逻辑模型设计"><a href="#4-2-逻辑模型设计" class="headerlink" title="4.2 逻辑模型设计"></a>4.2 逻辑模型设计</h3><blockquote><p>关系模式集合</p></blockquote><ul><li>用户(用户ID,用户名,密码,邮箱,角色)</li><li>注:角色属性有两个取值:1表示管理员,0表示用户</li><li>电影(电影ID,电影主演,电影详情,电影时长,电影类型,评分,海报,累计票房,参评人数,上映时间,票房单位,电影的外国名,上映地点)</li><li>影院(影院ID,影院名称,影院地址)</li><li>评价(评价ID,用户ID,评价内容,评论对应的电影ID)</li><li>放映厅(放映厅ID,放映厅名字,影院ID,放映厅容量)</li><li>场次(场次ID, 放映厅ID,电影ID,放映日期,价格,剩余座位数)</li><li>电影票(电影票ID,所属用户ID,场次ID,座位号)</li></ul><h3 id="4-3-数据库表结构设计"><a href="#4-3-数据库表结构设计" class="headerlink" title="4.3 数据库表结构设计"></a>4.3 数据库表结构设计</h3><blockquote><p>数据库表总览</p></blockquote><p><img src="/images/project/DBProject/table.png" alt=""></p><hr><h4 id="4-3-1-用户管理"><a href="#4-3-1-用户管理" class="headerlink" title="4.3.1 用户管理"></a>4.3.1 用户管理</h4><blockquote><p>user表</p></blockquote><table><thead><tr><th>属性名</th><th style="text-align:center">类型</th><th style="text-align:center">长度</th><th style="text-align:center">允许空值</th><th style="text-align:center">备注</th></tr></thead><tbody><tr><td>user_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center">主键</td></tr><tr><td>name</td><td style="text-align:center">varchar</td><td style="text-align:center">30</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>password</td><td style="text-align:center">varchar</td><td style="text-align:center">30</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>email</td><td style="text-align:center">varchar</td><td style="text-align:center">30</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>role</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>headImg</td><td style="text-align:center">varchar</td><td style="text-align:center">30</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr></tbody></table><hr><h4 id="4-3-2-电影管理"><a href="#4-3-2-电影管理" class="headerlink" title="4.3.2 电影管理"></a>4.3.2 电影管理</h4><blockquote><p>movie表</p></blockquote><table><thead><tr><th>属性名</th><th style="text-align:center">类型</th><th style="text-align:center">长度</th><th style="text-align:center">允许空值</th><th style="text-align:center">备注</th></tr></thead><tbody><tr><td>movie_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center">主键</td></tr><tr><td>name</td><td style="text-align:center">varchar</td><td style="text-align:center">30</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>straing</td><td style="text-align:center">varchar</td><td style="text-align:center">30</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>detail</td><td style="text-align:center">varchar</td><td style="text-align:center">350</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>duration</td><td style="text-align:center">varchar</td><td style="text-align:center">20</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>type</td><td style="text-align:center">varchar</td><td style="text-align:center">20</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>score</td><td style="text-align:center">varchar</td><td style="text-align:center">20</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>picture</td><td style="text-align:center">varchar</td><td style="text-align:center">35</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>boxOffice</td><td style="text-align:center">double</td><td style="text-align:center">30</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>commentsCount</td><td style="text-align:center">varchar</td><td style="text-align:center">30</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>releaseDate</td><td style="text-align:center">date</td><td style="text-align:center">0</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>boxOfficeUnit</td><td style="text-align:center">int</td><td style="text-align:center">20</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>foreignName</td><td style="text-align:center">varchar</td><td style="text-align:center">50</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>releasePoint</td><td style="text-align:center">varchar</td><td style="text-align:center">30</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>commentsUnit</td><td style="text-align:center">int</td><td style="text-align:center">20</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr></tbody></table><hr><h4 id="4-3-3-场次管理"><a href="#4-3-3-场次管理" class="headerlink" title="4.3.3 场次管理"></a>4.3.3 场次管理</h4><blockquote><p>cinema表</p></blockquote><table><thead><tr><th>属性名</th><th style="text-align:center">类型</th><th style="text-align:center">长度</th><th style="text-align:center">允许空值</th><th style="text-align:center">备注</th></tr></thead><tbody><tr><td>cinema_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center">主键</td></tr><tr><td>name</td><td style="text-align:center">varchar</td><td style="text-align:center">50</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>adress</td><td style="text-align:center">varchar</td><td style="text-align:center">120</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr></tbody></table><hr><blockquote><p>hall表</p></blockquote><table><thead><tr><th>属性名</th><th style="text-align:center">类型</th><th style="text-align:center">长度</th><th style="text-align:center">允许空值</th><th style="text-align:center">备注</th></tr></thead><tbody><tr><td>hall_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center">主键</td></tr><tr><td>name</td><td style="text-align:center">varchar</td><td style="text-align:center">20</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>cinema_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>capacity</td><td style="text-align:center">int</td><td style="text-align:center">50</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr></tbody></table><hr><blockquote><p>session表</p></blockquote><table><thead><tr><th>属性名</th><th style="text-align:center">类型</th><th style="text-align:center">长度</th><th style="text-align:center">允许空值</th><th style="text-align:center">备注</th></tr></thead><tbody><tr><td>session_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center">主键</td></tr><tr><td>hall_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>cinema_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>movie_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>startTime</td><td style="text-align:center">time</td><td style="text-align:center">0</td><td style="text-align:center">是</td><td style="text-align:center"></td></tr><tr><td>price</td><td style="text-align:center">double</td><td style="text-align:center">50</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>remain</td><td style="text-align:center">int</td><td style="text-align:center">50</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr></tbody></table><hr><h4 id="4-3-4-票务管理"><a href="#4-3-4-票务管理" class="headerlink" title="4.3.4 票务管理"></a>4.3.4 票务管理</h4><blockquote><p>ticket表</p></blockquote><table><thead><tr><th>属性名</th><th style="text-align:center">类型</th><th style="text-align:center">长度</th><th style="text-align:center">允许空值</th><th style="text-align:center">备注</th></tr></thead><tbody><tr><td>ticket_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center">主键</td></tr><tr><td>user_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>session_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>hall_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>seat</td><td style="text-align:center">varchar</td><td style="text-align:center">50</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr></tbody></table><hr><h4 id="4-3-5-评价管理"><a href="#4-3-5-评价管理" class="headerlink" title="4.3.5 评价管理"></a>4.3.5 评价管理</h4><blockquote><p>comments表</p></blockquote><table><thead><tr><th>属性名</th><th style="text-align:center">类型</th><th style="text-align:center">长度</th><th style="text-align:center">允许空值</th><th style="text-align:center">备注</th></tr></thead><tbody><tr><td>comments_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center">主键</td></tr><tr><td>user_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>comments</td><td style="text-align:center">varchar</td><td style="text-align:center">300</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr><tr><td>movie_id</td><td style="text-align:center">int</td><td style="text-align:center">10</td><td style="text-align:center">否</td><td style="text-align:center"></td></tr></tbody></table><hr><h2 id="5-项目展示"><a href="#5-项目展示" class="headerlink" title="5.项目展示"></a>5.项目展示</h2><h3 id="5-1-主要界面展示"><a href="#5-1-主要界面展示" class="headerlink" title="5.1 主要界面展示"></a>5.1 主要界面展示</h3><blockquote><p>主页</p></blockquote><p><img src="/images/project/DBProject/movie1.png" alt=""></p><hr><blockquote><p>购票界面</p></blockquote><p><img src="/images/project/DBProject/movie2.png" alt=""></p><hr><blockquote><p>电影列表界面</p></blockquote><p><img src="/images/project/DBProject/movie3.png" alt=""></p><hr><blockquote><p>电影详情界面</p></blockquote><p><img src="/images/project/DBProject/movie4.png" alt=""></p><hr><blockquote><p>管理界面</p></blockquote><p><img src="/images/project/DBProject/movie5.png" alt=""></p><hr><blockquote><p>选座界面</p></blockquote><p><img src="/images/project/DBProject/movie6.png" alt=""></p><hr><h3 id="5-2-演示视频"><a href="#5-2-演示视频" class="headerlink" title="5.2 演示视频"></a>5.2 演示视频</h3><blockquote><p>演示视频地址:<br><a href="https://video-1254265973.cos.ap-beijing.myqcloud.com/movieManager.mp4" target="_blank" rel="noopener">https://video-1254265973.cos.ap-beijing.myqcloud.com/movieManager.mp4</a></p></blockquote><h2 id="6-项目源码下载"><a href="#6-项目源码下载" class="headerlink" title="6.项目源码下载"></a>6.项目源码下载</h2><blockquote><p>百度网盘下载 ( 包含项目报告 )</p><ul><li>百度网盘链接: 链接:<a href="https://pan.baidu.com/s/16nTrGs_ym_aEnBovWW85GQ" target="_blank" rel="noopener">https://pan.baidu.com/s/16nTrGs_ym_aEnBovWW85GQ</a></li><li>提取码:2opg </li></ul></blockquote><blockquote><p>github下载 ( 只含源代码 )</p><ul><li>github下载地址: <a href="https://github.com/fyf2016/MovieManager.git" target="_blank" rel="noopener">https://github.com/fyf2016/MovieManager.git</a></li></ul></blockquote><blockquote><p>注: 两个地址都可以下载</p></blockquote>]]></content>
<categories>
<category> 三级项目 </category>
</categories>
<tags>
<tag> 三级项目 </tag>
<tag> javaWeb </tag>
<tag> 数据库 </tag>
</tags>
</entry>
<entry>
<title>计算机组成原理课程设计</title>
<link href="/2018/07/29/%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86%E8%AF%BE%E8%AE%BE/"/>
<url>/2018/07/29/%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86%E8%AF%BE%E8%AE%BE/</url>
<content type="html"><![CDATA[<h2 id="1-课程设计简介"><a href="#1-课程设计简介" class="headerlink" title="1. 课程设计简介"></a>1. 课程设计简介</h2><p>综合运用所学的计算机组成原理知识,掌握计算机各条指令运行的过程,<br>设计并实现较为完整的计算机运算过程。设计并实现任意两个八位二进制数的原码一位乘法,实现带有符号位的运算。</p><h2 id="2-实验设备"><a href="#2-实验设备" class="headerlink" title="2. 实验设备"></a>2. 实验设备</h2><p>TDN—CM+ 计算机组成原理实验教学系统一台, 排线若干。</p><h2 id="3-总体思路"><a href="#3-总体思路" class="headerlink" title="3. 总体思路"></a>3. 总体思路</h2><pre><code>(1) 完成接线, 实现基本的复杂模型机(2) 设计实现八位二进制数的原码一位乘法的汇编指令(3) 运行并调试程序</code></pre><h2 id="4-基本概念及原理"><a href="#4-基本概念及原理" class="headerlink" title="4. 基本概念及原理"></a>4. 基本概念及原理</h2><h3 id="4-1-数据格式"><a href="#4-1-数据格式" class="headerlink" title="4.1 数据格式"></a>4.1 数据格式</h3><pre><code>模型机规定采用定点补码表示法表示数据,且字长为8位,其格式如下:</code></pre><p><img src="/images/project/PcOrganization/1.png" alt=""></p><h3 id="4-2-指令格式"><a href="#4-2-指令格式" class="headerlink" title="4.2 指令格式"></a>4.2 指令格式</h3><pre><code>模型机设计四大类指令共十六条,其中包括算术逻辑指令、I/O指令、访问及转移指令和停机指令。 (1) 算数逻辑指令 设计9条算术逻辑指令并用单字节表示,寻址方式采用寄存器直接寻址,其格式如下:</code></pre><p><img src="/images/project/PcOrganization/2.png" alt=""></p><pre><code> 其中,OP-CODE为操作码,rs为源寄存器,rd为目的寄存器,并规定:</code></pre><p><img src="/images/project/PcOrganization/3.png" alt=""></p><pre><code>(2)访存指令及转移指令 模型机设计2条访存指令――存数(STA)和取数(LDA),2条转移指令指,即无条件转移指令(JMP) 和结果为零或有进位转移指令(BZC),指令格式为:</code></pre><p><img src="/images/project/PcOrganization/4.png" alt=""></p><pre><code> 其中,OP-CODE为操作码,rd为目的寄存器地址(LDA、STA指令使用)。 D为偏移量(正负均可),M为寻址模式,其定义如下:</code></pre><p><img src="/images/project/PcOrganization/5.png" alt=""></p><pre><code> 本模型机规定变址寄存器RI指定为寄存器R2(3)I/O指令 输入(IN)和输出(OUT)指令采用单字节指令,其格式如下:</code></pre><p><img src="/images/project/PcOrganization/6.png" alt=""></p><pre><code> 其中,addr=01时,选中“INPUT DEVICE”中的开关组作为输入设备, addr=10时,选中“OUTPUT DEVICE”中的数码块作为输出设备。(4)停机指令 指令格式如下:</code></pre><p><img src="/images/project/PcOrganization/7.png" alt=""></p><pre><code> HALT指令用于实现停机操作.</code></pre><h3 id="4-3-指令系统"><a href="#4-3-指令系统" class="headerlink" title="4.3 指令系统"></a>4.3 指令系统</h3><pre><code>本模型机共有16条基本指令,其中算术逻辑指令7条,访存指令和程序控制指令4条,输入输出指令2条,其它指令1条,表列出了各条指令的格式、汇编符号、指令功能。</code></pre><p><img src="/images/project/PcOrganization/8.png" alt=""></p><h3 id="4-4-接线图"><a href="#4-4-接线图" class="headerlink" title="4.4 接线图"></a>4.4 接线图</h3><hr><h2 id=""><a href="#" class="headerlink" title=" "></a><img src="/images/project/PcOrganization/9.png" alt=""> </h2><h2 id="5-总体流程"><a href="#5-总体流程" class="headerlink" title="5. 总体流程"></a>5. 总体流程</h2><h3 id="5-1-流程图"><a href="#5-1-流程图" class="headerlink" title="5.1 流程图"></a>5.1 流程图</h3><p><img src="/images/project/PcOrganization/10.png" alt=""></p><pre><code>实现过程:(1)向寄存器R0中存入10000000,向寄存器R1中存入被乘数X,清零R2,将R0与R1中存入的X值进行与操作, 求得X的符号位,将X的符号位存入到F0H,将R1中存入的先左移再右移获得|X|(存入到F1H).(2)同上面的操作取得Y的符号位(存入到F2H)和|Y|(存入到F3H)。(3)将X和Y的符号位取异或操作,得到最终的符号位存入到F6H中(4)输入移位循环次数到R0寄存器中,将R2清零,初始化部分积,保存到F7H中.(5)清零cy,将乘数|Y|取出存到R1中,右移,判断最低位是0还是1.如果是0,则执行(6),如果是1,则执行(7).(6)从F7H中取出部分积到R1中,从F3H中取出部分积到R2,然后右移部分积,右移|y|, 将结果有保存到相应的地址中.执行跳转指令,执行(9)(7)取部分积到R1,取|X|到R2然后求部分积加|X|,结果保存到F7H中,然后将|y|右移并保存,清零R1,清零cy.(8)将计数次数右移,判断是否循环结束,若结束则向下执行,若未结束则跳转到(5).(9)将部分积与最终符号位相加.(10)输出高八位R1,第八位R2,停机结束。</code></pre><h3 id="5-2-实现原码一位乘的微指令"><a href="#5-2-实现原码一位乘的微指令" class="headerlink" title="5.2 实现原码一位乘的微指令"></a>5.2 实现原码一位乘的微指令</h3><pre><code>汇编指令 助记符 注释$ P0044 IN 01,R0 输入10000000$ P0145 IN 01,R1 输入x$ P0272 CLR00,R2 清零R2 $ P03F5 RLC R1,R1 左移X,移出符号位$ P04EA RRC R2,R2 右移R2,获得8位符号位$ P0506 STA R2 将x符号位存到F0H$ P06F0 $ P07EA RRC R2,R2 清零cy$ P08E5 RRC R1,R1 右移x获得|x|$ P0905 STA R1 存|x|到F1H$ P0AF1 F1H $ P0B45 IN 01,R1 输入y$ P0CF5 RLC R1,R1 左移Y移出符号位$ P0DEA RRC R2,R2 右移R2获得8位符号位$ P0E06 STA R2 将Y符号位存到F2H$ P0FF2 F2H $ P10EA RRC R2,R2 清零cy$ P11E5 RRC R1,R1 右移y获得|y|$ P1205 STA R1 存|y|到F3H$ P13F3 F3H $ P1401 LDA R1 取x的符号位到R1$ P15F0 F0H $ P1602 LDA R2 取y的符号位到R2$ P17F2 F2H $ P18D4 COM R1,R0 求x的反$ P19C8 AND R2,R0 求(x的反)和y的与$ P1A04 STA R0 存(x的反)和y的与的结果到F4$ P1BF4 F4H $ P1CD8 COM R2,RO 求Y的反$ P1DC4 AND R1,R0 求(Y的反)和X的与$ P1E04 STA R0 存(Y的反)和X的与的结果到F5H$ P1FF5 F5H $ P2001 LDA R1 取(x的反)和y的与的结果到R1$ P21F4 F4H $ P2202 LDA R2 取(y的反)和x的与的结果到R2$ P23F5 F5H $ P2470 CLR R0 清零R0$ P25E0 RRC R0,RO 清零cy$ P2696 ADC R1,R2 获得最终的符号位$ P2706 STA R2 存最终符号位到F6H$ P28F6 F6H $ P2944 IN 01,R0 输入移位次数$ P2A72 CLR R2 清零R2$ P2B06 STA R2 保存部分积到F7H$ P2CF7 F7H $ P2DEA RRC R2,R2 清零cy$ P2E01 LDA R1 取|y|到R1$ P2FF3 F3H $ P30E5 RRC R1,R1 |Y|右移一位$ P310C BZC 00,00 判断最低位是0还是1$ P323F 3FH 跳转地址3FH$ P3301 LDA R1 取部分积到R1$ P34F7 F7H $ P3502 LDA R2 取|y|到R2$ P36F3 F3H $ P37E5 RRC R1,R1 右移部分积$ P38EA RRC R2,R2 右移|y|$ P3905 STA R1 保存右移后的部分积到F7H$ P3AF7 F7H $ P3B06 STA R2 保存右移后的|y|到F3H$ P3CF3 F3H $ P3D08 JMP 00,00 跳转指令$ P3E4E 4EH 跳转地址4EH$ P3F01 LDA R1 取部分积到R1$ P40F7 F7H $ P4172 CLR R2 清零R2$ P42EA RRC R2,R2 清零cy$ P4302 LDA R2 取|x|到R2$ P44F1 F1H $ P4596 ADC R1,R2 求部分积加|x|$ P46EA RRC R2,R2 右移相加后的部分积$ P4706 STA R2 保存部分积到F7H$ P48F7 F7H $ P4902 LDA R2 取|y|到R2$ P4AF3 F3H $ P4BEA RRC R2,R2 右移|y|$ P4C06 STA R2 保存右移后的|y|到F3H$ P4DF3 F3H $ P4E71 CLR R1 清零R1$ P4FE5 RRC R1,R1 清零cy$ P50E0 RRC R0,R0 右移计数次数$ P510C BZC 00,00 判断循环是否结束$ P5257 57H $ P5371 CLR R1 清零R1$ P54E5 RRC R1,R1 清零cy$ P5508 JMP 00,00 跳转指令$ P562F 2FH $ P5701 LDA R1 取部分积到R1$ P58F7 F7H $ P5902 LDA R2 取最终符号到R2$ P5AF6 F6H $ P5B70 CLR R0 清零R0$ P5CE0 RRC R0,R0 清零cy$ P5D96 ADC R1,R2 求符号位加数值位$ P5E5A OUT R2 输出高8位$ P5F01 LDA R1 取出低八位到R1$ P60F3 F3H $ P6159 OUT R1 输出低八位$ P6260 HALT 待机</code></pre><h2 id="6-课设手稿"><a href="#6-课设手稿" class="headerlink" title="6. 课设手稿"></a>6. 课设手稿</h2><p><img src="/images/project/PcOrganization/ks1.jpg" alt=""><br><img src="/images/project/PcOrganization/ks2.jpg" alt=""><br><img src="/images/project/PcOrganization/ks3.jpg" alt=""><br><img src="/images/project/PcOrganization/ks4.jpg" alt=""></p>]]></content>
<categories>
<category> 课程设计 </category>
</categories>
<tags>
<tag> 课程设计 </tag>
<tag> 汇编指令 </tag>
</tags>
</entry>
<entry>
<title>操作系统课设(多道程序缓冲区协调操作)</title>
<link href="/2018/07/29/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E8%AF%BE%E8%AE%BE/"/>
<url>/2018/07/29/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E8%AF%BE%E8%AE%BE/</url>
<content type="html"><![CDATA[<h2 id="项目开发大致流程-多道程序缓冲区协调操作"><a href="#项目开发大致流程-多道程序缓冲区协调操作" class="headerlink" title="项目开发大致流程 ( 多道程序缓冲区协调操作 )"></a><span style="color:rgba(255,165,0,0.8);text-shadow:5px 5px 5px #ccc">项目开发大致流程 ( 多道程序缓冲区协调操作 )</span></h2><p><span style="color:purple;text-shadow:5px 5px 5px #ccc ">1. 项目概述</span></p><p><span style="color:orange;text-shadow:5px 5px 5px #ccc ">2. 项目要求</span></p><p><span style="color:green;text-shadow:5px 5px 5px #ccc ">3. 总体设计</span></p><p><span style="color:red;text-shadow:5px 5px 5px #ccc ">4. 结果演示</span></p><p><span style="color:#459df5;text-shadow:5px 5px 5px #ccc ">5. 项目配置</span></p><p><span style="color:black;text-shadow:5px 5px 5px #ccc ">6. 项目源代码</span></p><h2 id="1-emsp-项目概述"><a href="#1-emsp-项目概述" class="headerlink" title="1. 项目概述"></a><span style="color:purple;text-shadow:5px 5px 5px #ccc ">1. 项目概述</span></h2><blockquote><p>文字描述</p></blockquote><ul><li><p>有多个 PUT 操作要不断循环地向 Buffer1 送字符数据,有 Move1 操作不断地将 Buffer1 的数据取到 Buffer2 。</p></li><li><p>Move2 操作不断地将Buffer1的数据取到 Buffer3 ,有多个 GET 操作要不断地从 Buffer2 和 Buffer3 中取数据。</p></li><li>PUT、 MOVE、 GET每次操作一个数据,为了在操作的过程中要保证数据不丢失, 每个 Buffer 每次只能接受一个 PUT 或一个 Move 或一个 Get 。</li><li>运用进程同步和互斥机制设计一个多道程序完成上述操作。</li></ul><blockquote><p>操作图示</p></blockquote><p><img src="/images/project/OsProject/1.png" alt="" style=" "></p><h2 id="2-emsp-项目要求"><a href="#2-emsp-项目要求" class="headerlink" title="2. 项目要求"></a><span style="color:orange;text-shadow:5px 5px 5px #ccc ">2. 项目要求</span></h2><ol><li>可以随机产生字符数据,由put操作将数据放入Buff,buffer中容量单位是字符;</li><li>提供良好图形界面,显示Buffer的操作过程,可以暂停和继续系统的执行;</li><li>可以设定各Buffer的容量、PUT、GET、Move操作的个数;</li><li>可以设定PUT、GET、Move操作的速度;</li><li>实时显示每个Buffer中已放入的数据和数据个数,当前Buffer中数据的个数和数据的内容,空闲Buffer的空间的个数;</li><li>实时显示线程、进程所处于等待(阻塞)状态的个数;</li><li>程序运行结束,显示汇总数据:</li><li>总的运行时间;Buffer中数据的个数;已放入BUFFER的数据个数;</li><li>已取出的数据个数;平均每个buffer中的数据个数。</li><li>能够将每次的实验输入和实验结果存储起来,下次运行时或以后可查询。 </li><li>可以根据题目的基本要求进行丰富的创意和想象。</li></ol><h2 id="3-emsp-总体设计"><a href="#3-emsp-总体设计" class="headerlink" title="3. 总体设计"></a><span style="color:green;text-shadow:5px 5px 5px #ccc ">3. 总体设计</span></h2><h3 id="3-1-课题分析"><a href="#3-1-课题分析" class="headerlink" title="3.1 课题分析"></a>3.1 课题分析</h3><blockquote><p>课题实质探究</p></blockquote><ul><li>由项目的描述可知该课题为 “多生产者 – 多消费者” 问题, 因此可以分析出其同步和互斥关系。 <ul><li>同步关系:<br>多个生产者和多个消费者之间要按一定顺序访问缓冲区, 共同协作来保障进程顺利执行。</li><li>互斥关系:<br>对缓冲区(临界资源)的访问是互斥的,缓冲区域每次只能接受一个PUT或一个Move或一个Get(互斥访问)。</li></ul></li></ul><h3 id="3-2-解读课题"><a href="#3-2-解读课题" class="headerlink" title="3.2 解读课题"></a>3.2 解读课题</h3><blockquote><p>以书本所学信号量方式为例 , 来实现同步和互斥</p></blockquote><h4 id="3-2-1-信号量的设计"><a href="#3-2-1-信号量的设计" class="headerlink" title="3.2.1 信号量的设计"></a>3.2.1 信号量的设计</h4><ul><li>信号量<ul><li>empty1: 缓冲区Buffer1的空位数</li><li>empty2: 缓冲区Buffer2的空位数</li><li>empty3: 缓冲区Buffer3的空位数</li><li>full1: 缓冲区1内的数据个数</li><li>full2: 缓冲区2内的数据个数</li><li>full2: 缓冲区3内的数据个数</li></ul></li></ul><h4 id="3-2-2-以信号量来实现同步和互斥"><a href="#3-2-2-以信号量来实现同步和互斥" class="headerlink" title="3.2.2 以信号量来实现同步和互斥"></a>3.2.2 以信号量来实现同步和互斥</h4><p><img src="/images/project/OsProject/2.png" alt="" style=" "></p><hr><h3 id="3-3-主要功能设计"><a href="#3-3-主要功能设计" class="headerlink" title="3.3 主要功能设计"></a>3.3 主要功能设计</h3><h4 id="3-3-1-产生随机字符数据"><a href="#3-3-1-产生随机字符数据" class="headerlink" title="3.3.1 产生随机字符数据"></a>3.3.1 产生随机字符数据</h4><ul><li>要求: <ul><li>产生的数据是字符类型,且是随机产生的</li></ul></li><li>实现: <ul><li style="list-style: none"><input type="checkbox" checked> Step1:<br> 定义一个String字符串,包含1-9所有数字和a-z的小写字符以及A-Z的大写字符</li><li style="list-style: none"><input type="checkbox" checked> Step2:<br> 创建Random类,产生一个从0到上述String字符串长度-1的随机整型量,<br> 调用String类的charAt(int i)方法获取字符</li><li style="list-style: none"><input type="checkbox" checked> Step3:<br> 封装成静态类的方法(RandomChar),返回值为获取的字符, 方便调用</li></ul></li></ul><h4 id="3-3-2-实时显示操作内容-暂停和继续"><a href="#3-3-2-实时显示操作内容-暂停和继续" class="headerlink" title="3.3.2 实时显示操作内容,暂停和继续"></a>3.3.2 实时显示操作内容,暂停和继续</h4><ul><li>要求: <ul><li>图形界面,显示Buffer的操作过程,可以暂停和继续系统的执行</li></ul></li><li>实现: <ul><li>界面部分: html+css+js+jquery+ajax (后面会详细介绍)</li><li>显示Buffer操作过程: <ul><li style="list-style: none"><input type="checkbox" checked> Step1:<br> 页面部分创建三个文本域, 分别对应Buffer1,Buffer2,Buffer3的操作内容</li><li style="list-style: none"><input type="checkbox" checked> Step2:<br> 每次对Buffer操作的内容(放入或拿出字符)存放到数据库表中</li><li style="list-style: none"><input type="checkbox" checked> Step3:<br> 页面使用ajax,访问后台,后台查询数据库,将查到的数据转<br> 为json格式返回给前台, 前台将获取的数据送入对应的文本域</li><li style="list-style: none"><input type="checkbox" checked> 暂停的实现:<br> 分别在put线程,move线程,get线程的run方法末尾中加入While(boolean pause)循环,pause为true则一直睡眠,pause为false 则跳出循环, 用暂停对应的按钮来控制pause</li><li style="list-style: none"><input type="checkbox" checked> 继续的实现: 将pause的值改为false, 则可以跳出循环</li></ul></li></ul></li></ul><h4 id="3-3-3-设定操作个数-容量-速度"><a href="#3-3-3-设定操作个数-容量-速度" class="headerlink" title="3.3.3 设定操作个数 容量 速度"></a>3.3.3 设定操作个数 容量 速度</h4><ul><li>要求: <ul><li>可以设定各Buffer的容量、PUT、GET、Move操作的个数</li></ul></li><li>实现: <ul><li>界面部分相关实现: 创建多个input输入框用来存放要设置的参数</li><li>后台实现: <ul><li style="list-style: none"><input type="checkbox" checked> Step1:<br> 创建一个类(Common类), 设置对应的Static静态属性,对应各操作的个数,容量,速度,<br> 并设置默认值,以便在不输入参数时能保证系统正常运行</li><li style="list-style: none"><input type="checkbox" checked> Step2:<br> Common类写一个setParams方法, 用来将从数据库获取到的数据赋值<br> 给Common的静态属性,来实现参数的设置</li></ul></li><li>前后交互的实现:<ul><li style="list-style: none"><input type="checkbox" checked> 涉及技术: ajax异步通信<br> 将前台传入的参数值通过 ajax传入到后台, 后台获取到参数,存入数据库,<br> 然后后台使用的数据可以从数据库中获取</li></ul></li></ul></li></ul><h4 id="3-3-4-设定PUT、GET、Move操作的速度"><a href="#3-3-4-设定PUT、GET、Move操作的速度" class="headerlink" title="3.3.4 设定PUT、GET、Move操作的速度"></a>3.3.4 设定PUT、GET、Move操作的速度</h4><ul><li>要求: <ul><li>可以设定PUT、GET、Move操作的速度</li></ul></li><li>实现: <ul><li>界面部分实现:<br> 创建对应个数的input的框来存放将要输入的各个线程对应的执行速度</li><li>后台实现实现:<br> 用线程的sleep()延迟函数来延迟线程的执行,控制线程的执行速度,每个Put,Move<br> ,Get线程内部都调用了sleep()函数,延迟时间为(0-100)的随机值乘以(50-速度值),<br> 速度值越大, 延迟时间越短,执行速度越快</li></ul></li></ul><h4 id="3-3-5-实时显示Buffer中的内容"><a href="#3-3-5-实时显示Buffer中的内容" class="headerlink" title="3.3.5 实时显示Buffer中的内容"></a>3.3.5 实时显示Buffer中的内容</h4><ul><li>要求: <ul><li>实时显示Buffer中已放入的数据和数据个数,以及空闲的空间数</li></ul></li><li><p>实现: </p><ul><li><p>前台部分的实现: </p><p> 用table表格存放要显示的数据。数据通过设置定时器,每间隔<br> 一段时间就访问后台获取数据, 获取到数据后将数据放入对应<br> table表格项</p></li><li><p>后台部分的实现: </p><p> 将线程产生的数据存入数据库, 每次ajax访问后台的Servlet时从数据库查询数据, 返回给前台</p></li><li><p>获取Buffer已放入的数据和数据个数: </p><p> 在Common中定义static类型的ArrayList,来作为课题中的Buffer 缓冲区, Buffer的size()属性<br> 即为放入的数据个数, 内容即为放入的字符</p></li><li><p>获取buffer的空闲空间数:</p><p> Common中设定static类型的BufferSize来代表每个Buffer的容量, BufferSize和Buffer的<br> size()值之差即为空闲空间数</p></li></ul></li></ul><h4 id="3-3-6-实时显示线程阻塞个数"><a href="#3-3-6-实时显示线程阻塞个数" class="headerlink" title="3.3.6 实时显示线程阻塞个数"></a>3.3.6 实时显示线程阻塞个数</h4><ul><li><p>要求: </p><ul><li>实时显示线程、进程所处于等待(阻塞)状态的个数</li></ul></li><li><p>分析:<br> 引入线程概念后, 线程成了调度的独立单位, 而进程则是资源分配的独立单位,<br> 因此进程的阻塞即可用线程的阻塞个数代表</p></li><li><p>实现: </p><ul><li><p>前台实现:<br> table表格显示,ajax获取数据。</p></li><li><p>后台实现:<br> 在Common中定义static类型的整型变量,用来表示被阻塞的线程数目,当执行到线程的<br> wait()(线程的阻塞方法)时执行++操作,当执行到线程的notify()(线程的唤醒方法)<br> 时执行 – 操作</p></li></ul></li></ul><h4 id="3-3-7-结果汇总显示"><a href="#3-3-7-结果汇总显示" class="headerlink" title="3.3.7 结果汇总显示"></a>3.3.7 结果汇总显示</h4><ul><li>要求: <ul><li>程序运行结束,显示汇总数据</li></ul></li><li>实现:<ul><li>总的运行时间:<br> 线程开始前调用System.currentTimeMillis()方法获取当前时间,当线程终止时 (点击结束按钮)时再调用System.currentTimeMillis()方法获取结束时间,两时间之差即为总运行时间</li><li>Buffer中数据的个数:<br> Common中各Buffer的size()值之和</li><li>已放入BUFFER的数据个数:<br> Common中创建static变量, 每次取出时变量值加1</li><li>已取出的数据个数:<br> Common中创建static变量, 每次取出时变量值加1</li><li>平均每个buffer中的数据个数:<br> Common中各Buffer的size()值之和比上Buffer的数量(3个)</li></ul></li></ul><h4 id="3-1-8-保存实验输入和实验结果"><a href="#3-1-8-保存实验输入和实验结果" class="headerlink" title="3.1.8 保存实验输入和实验结果"></a>3.1.8 保存实验输入和实验结果</h4><ul><li>要求: <ul><li>能够将每次的实验输入和实验结果存储起来,下次运行时或以后可查询。</li></ul></li><li>实现: <ul><li>执行产生的数据都保存在数据库中, 可以通过转储数据库文件来保存,<br> 查询的话直接查询数据库表中的记录即可</li></ul></li></ul><h2 id="4-emsp-结果演示"><a href="#4-emsp-结果演示" class="headerlink" title="4. 结果演示"></a><span style="color:red;text-shadow:5px 5px 5px #ccc ">4. 结果演示</span></h2><h3 id="4-1-项目主要界面展示"><a href="#4-1-项目主要界面展示" class="headerlink" title="4.1 项目主要界面展示"></a>4.1 项目主要界面展示</h3><blockquote><p>主页</p></blockquote><p><img src="/images/project/OsProject/3.png" alt="" style=" "></p><blockquote><p>参数设置界面</p></blockquote><p><img src="/images/project/OsProject/4.png" alt="" style=" "></p><h3 id="4-2-项目运行演示"><a href="#4-2-项目运行演示" class="headerlink" title="4.2 项目运行演示"></a>4.2 项目运行演示</h3><p>腾讯云对象存储COS视频地址:<br> <a href="https://video-1254265973.cos.ap-beijing.myqcloud.com/MyOsProject.mp4" target="_blank" rel="noopener">https://video-1254265973.cos.ap-beijing.myqcloud.com/MyOsProject.mp4</a></p><h2 id="5-emsp-项目配置"><a href="#5-emsp-项目配置" class="headerlink" title="5. 项目配置"></a><span style="color:#459df5;text-shadow:5px 5px 5px #ccc ">5. 项目配置</span></h2><blockquote><p>5.1 数据库配置</p></blockquote><pre><code> (1) 数据库连接信息可在项目下的 src/jdbc.properties 文件中修改, 下图标红线的位置对应的是 自己数据库的名字(类型为Mysql数据库)。</code></pre><hr><p> <img src="/images/project/arithmetic/jdbc.png" alt="" style=" "> </p><hr><pre><code> (2) 将项目中的 osmessage.sql 导入自己电脑的本地数据库 演示Navicat导入的方式: 右键连接-->选择运行SQL文件-->选中osmessage.sql-->运行完毕点击关闭-->右键刷新。 完成以上步骤后, osmessage数据库便成功导入到了你的本地数据库中。</code></pre><hr><p> <img src="/images/project/arithmetic/database.png" alt="" style=" "> </p><hr><h2 id="6-emsp-项目源代码"><a href="#6-emsp-项目源代码" class="headerlink" title="6. 项目源代码"></a><span style="color:black;text-shadow:5px 5px 5px #ccc ">6. 项目源代码</span></h2><blockquote><p>百度网盘下载 ( 包含项目报告 )</p><ul><li>百度网盘链接: 链接:<a href="https://pan.baidu.com/s/1vj8FrbuPTJnummNpnxcVwA" target="_blank" rel="noopener">https://pan.baidu.com/s/1vj8FrbuPTJnummNpnxcVwA</a> </li><li>提取码:pk5z </li></ul></blockquote><blockquote><p>github下载 ( 只含源代码 )</p><ul><li>github下载地址: <a href="https://github.com/fyf2016/MyOsProject.git" target="_blank" rel="noopener">https://github.com/fyf2016/MyOsProject.git</a></li></ul></blockquote><blockquote><p>注: 两个地址都可以下载</p></blockquote>]]></content>
<categories>
<category> 课程设计 </category>
</categories>
<tags>
<tag> 课程设计 </tag>
<tag> javaWeb </tag>
<tag> 操作系统 </tag>
</tags>
</entry>
<entry>
<title>算法三级项目(管道铺设系统)</title>
<link href="/2018/07/22/%E7%AE%97%E6%B3%95%E4%B8%89%E7%BA%A7%E9%A1%B9%E7%9B%AE/"/>
<url>/2018/07/22/%E7%AE%97%E6%B3%95%E4%B8%89%E7%BA%A7%E9%A1%B9%E7%9B%AE/</url>
<content type="html"><![CDATA[<h2 id="项目开发大致流程"><a href="#项目开发大致流程" class="headerlink" title="项目开发大致流程"></a><span style="color:rgba(255,165,0,0.8);text-shadow:5px 5px 5px #ccc">项目开发大致流程</span></h2><p><span style="color:purple;text-shadow:5px 5px 5px #ccc ">1. 摘要</span></p><p><span style="color:orange;text-shadow:5px 5px 5px #ccc ">2. 项目要求</span></p><p><span style="color:green;text-shadow:5px 5px 5px #ccc ">3. 总体设计</span></p><p><span style="color:red;text-shadow:5px 5px 5px #ccc ">4. 结果演示</span></p><p><span style="color:#459df5;text-shadow:5px 5px 5px #ccc ">5. 项目配置</span></p><p><span style="color:black;text-shadow:5px 5px 5px #ccc ">6. 项目源代码</span></p><h2 id="1-emsp-摘要"><a href="#1-emsp-摘要" class="headerlink" title="1. 摘要"></a><span style="color:purple;text-shadow:5px 5px 5px #ccc ">1. 摘要</span></h2><p><br> 本次课程的三级项目是要求设计实现一个管线铺设辅助系统,设计算法并实现使铺设的输水管道距离最短。<br>本项目主要应用了最小生成树算法:<br> prime算法和Kruskal算法两种算法,具备从文本读取数据、显示最佳铺设方案,以及绘制最佳方案的简单示意图等功能,以东校区各建筑物为例,实现了获得任意两个建筑物之间铺设管道的最佳方案。<br><br> 关键词:【最小生成树】 【prime算法】 【kruskal算法】<br></p><h2 id="2-emsp-项目要求"><a href="#2-emsp-项目要求" class="headerlink" title="2. 项目要求"></a><span style="color:orange;text-shadow:5px 5px 5px #ccc ">2. 项目要求</span></h2><p><br> 用最小生成树算法实现东区各建筑之间铺设的输水管道距离最短,需要满足如下要求 :<br><br> <span style="display: block;float: left;width: 25px;height: 77px;">(1)</span>将管线经过的建筑物以及建筑物之间的距离,抽象成无向图,并以矩阵的形式表示,并保存在文本中,<br> 系统通过读取文本的方式,获取该矩阵;<br><br> <span style="display: block;float: left;width: 25px;height: 77px;">(2)</span>从Prim算法和Kruskal算法中至少选择一种实现管线铺设的最优方案,系统可以最优方案<br> 的生成过程,并且可以文本的形式输出;<br><br> (3)在系统上可以生成最优方案的简易图。<br><br><br></p><h2 id="3-emsp-总体设计"><a href="#3-emsp-总体设计" class="headerlink" title="3. 总体设计"></a><span style="color:green;text-shadow:5px 5px 5px #ccc ">3. 总体设计</span></h2><h3 id="3-1-大致划分"><a href="#3-1-大致划分" class="headerlink" title="3.1 大致划分"></a>3.1 大致划分</h3><p><br> 总体上将项目划分为前端和后端两个层面:<br>  |— 前端: 主要负责展示良好的界面, 增强交互性<br>  |— 后端: 主要负责实现项目的功能<br> (1) 项目类型:<br>   javaWeb项目 (Servlet+Jsp)<br> (2) 开发工具:<br>   MyEclipse + MySQL + Navicat<br> (3) 前端界面的实现:<br>   通过 html + css + css3 + js + jquery 来实现<br>   1. 项目结果页面展现<br>   <img src="/images/project/arithmetic/view1.png" style=" margin-left: 50px;"><br> <span style="display: block;float: left;width: 25px;height: 54px;margin-left: 32px;">web.xml:</span><br> 用于配置View层中Servlet服务器的信息和路径<br> <span style="display: block;float: left;width: 25px;height: 50px;margin-left: 32px;">index.jsp:</span><br> 用于显示项目主页的页面<br> <span style="display: block;float: left;width: 25px;height: 50px;margin-left: 32px;">kruskalPage.jsp:</span><br> 用于显示Kruskal算法处理结果的页面<br> <span style="display: block;float: left;width: 25px;height: 50px;margin-left: 32px;">pirmePage.jsp:</span><br> 用于显示Kruskal算法处理结果的页面<br> <span style="display: block;float: left;width: 25px;height: 50px;margin-left: 32px;">showScenery.jsp:</span><br> 用于显示周边建筑物信息的页面<br> <span style="display: block;float: left;width: 25px;height: 60px;margin-left: 32px;">map.jsp:</span><br> 实现了简易图的绘制(引入百度地图api, 从后台传入经算法处理后的数据生成)<br><br> (4) 绘制简易图功能的实现:<br>   引入百度地图api, 从后台传入经算法处理后的数据,然后生成对应最短路线<br></p><h3 id="3-2-算法的实现"><a href="#3-2-算法的实现" class="headerlink" title="3.2 算法的实现:"></a>3.2 算法的实现:</h3><p style="margin-left: 50px;"><br> 1. 算法所用数据结构选型<br><br> <span style="display: block;margin-left: 15px;">所用矩阵: n*2矩阵</span><br> <span style="display: block;margin-left: 15px;">选择原因:方便hashMap的存取。</span><br> <span style="display: block;margin-left: 15px;">存放矩阵内信息所用数据结构:hashMap</span><br> <span style="display: block;margin-left: 15px;">选择原因:</span><br> <span style="display: block;margin-left: 15px;">letCode中有一道返回两数之和的算法题,通过条件遍历查询数组即可获得完成,但是官方推荐的是用hashMap存取并遍历,经过letCode上的大量数据测试,得出在这种情况下hashMap的执行效率更高,所用时间更少,通过分析两算法,得知都要按条件查询矩阵内容,所以对于这种规模的数据和相似的情况,我们采用hashMap来存取数据。</span><br> <span style="display: block;margin-left: 15px;">存放无向图结点的数据结构: Set集合</span><br> <span style="display: block;margin-left: 15px;">选择原因:</span><br> <span style="display: block;margin-left: 15px;">集合具有元素无序不重复的特性,所以用来存放结点可以防止其重复出现。</span><br> <span style="display: block;margin-left: 15px;">路径返回结果所用数据结构: ArrayList</span><br> <span style="display: block;margin-left: 15px;">选择原因:</span><br> <span style="display: block;margin-left: 15px;">ArrayList具有有序便于插入删除的特点,方便对结果进行后续的整理和修改。</span><br></p><br><p><br> 2. 算法的具体实现<br><br> <img src="/images/project/arithmetic/back1.png" alt="" style=" margin-left: 20px;"><br> <span style="display: block;margin-left: 15px;">Prime算法部分主要由PrimeServiceImpl实现</span><br> <span style="display: block;margin-left: 15px;">kruskal算法部分主要由kruskalServiceImpl实现</span><br> <span style="display: block;margin-left: 15px;">CaculateUtil.java :提取并封装了PrimeServiceImpl 和 KruskalServiceImpl算法用到的方法</span><br> <span style="display: block;margin-left: 15px;">2.1 实现Prime算法的基本思想:</span><br></p><blockquote><ol><li>清空生成树,任取一个顶点加入生成树</li></ol></blockquote><blockquote><ol start="2"><li>在那些一个端点在生成树里,另一个端点不在生成树里的边中,选取一条权最小的边,将它和另一个端点加进生成树</li></ol></blockquote><blockquote><ol start="3"><li>重复步骤2,直到所有的顶点都进入了生成树为止,此时的生成树就是最小生成树</li></ol></blockquote><p><br> <span style="display: block;margin-left: 15px;">2.2 Prime算法适用性和时间复杂度:</span><br> <span style="display: block;margin-left: 15px;">Prime算法求最小生成树时候,和边数无关,只和定点的数量相关,所以适合求稠密网的最小生成树,时间复杂度为O(n×n)</span><br></p><br><p><br> <span style="display: block;margin-left: 15px;">2.3 实现Kruskal算法的基本思想:</span><br></p><blockquote><ol><li>首先将G的n个顶点看成n个孤立的连通分支(n个孤立点)并将所有的边按权从小大排序。</li></ol></blockquote><blockquote><ol start="2"><li>按照边权值递增顺序,如果加入边后存在环则这条边不加,直到形成连通图</li></ol></blockquote><p><br> <span style="display: block;margin-left: 15px;">2.4 Kruskal算法适用性和时间复杂度:</span><br> <span style="display: block;margin-left: 15px;">Kruskal算法是对图的边进行访问,所以Kruskal算法的时间复杂度只和边又关系,其时间复杂度为O(eloge)</span><br></p><h2 id="4-emsp-结果演示"><a href="#4-emsp-结果演示" class="headerlink" title="4. 结果演示"></a><span style="color:red;text-shadow:5px 5px 5px #ccc ">4. 结果演示</span></h2><h3 id="4-1-项目主要界面展示"><a href="#4-1-项目主要界面展示" class="headerlink" title="4.1 项目主要界面展示"></a>4.1 项目主要界面展示</h3><blockquote><p>首页</p></blockquote><p><img src="/images/project/arithmetic/index.png" alt="" style=" "></p><blockquote><p>节点信息展示</p></blockquote><p><img src="/images/project/arithmetic/nodeMsg.png" alt="" style=" "></p><blockquote><p>Prime算法界面</p></blockquote><p><img src="/images/project/arithmetic/prime.png" alt="" style=" "></p><blockquote><p>Kruskal算法界面</p></blockquote><p><img src="/images/project/arithmetic/kruskal.png" alt="" style=" "></p><blockquote><p>最终生成的路线图</p></blockquote><p><img src="/images/project/arithmetic/map.png" alt="" style=" "></p><hr><h3 id="4-2-项目运行演示"><a href="#4-2-项目运行演示" class="headerlink" title="4.2 项目运行演示"></a>4.2 项目运行演示</h3><blockquote><p>以下两个地址都可</p></blockquote><p>腾讯云对象存储COS视频地址:<br><a href="https://video-1254265973.cos.ap-beijing.myqcloud.com/arithmetic.mp4" target="_blank" rel="noopener">https://video-1254265973.cos.ap-beijing.myqcloud.com/arithmetic.mp4</a><br>哔哔哩哔哩视频地址:<br><a href="https://www.bilibili.com/video/av40697433/" target="_blank" rel="noopener">https://www.bilibili.com/video/av40697433/</a></p><h2 id="5-emsp-项目配置"><a href="#5-emsp-项目配置" class="headerlink" title="5. 项目配置"></a><span style="color:#459df5;text-shadow:5px 5px 5px #ccc ">5. 项目配置</span></h2><blockquote><p>5.1 矩阵文件格式</p></blockquote><p> 矩阵文件的格式应为N*2矩阵, 形式如 AB 150 。 A, B 为结点的编号, 150为两结点结点间的距离。<br> 详细配置可参照项目中的ReadDatas.txt和ReadDatas3.txt。</p><blockquote><p>5.2 读入文件路径的设置</p></blockquote><p> 读入文件的路径设置在 PipeLineLaying/src/com/dao/impl/ReadDataDAOImpl.java 里面修改,<br> 修改位置为下图标红线的地方:</p><hr><p> <img src="/images/project/arithmetic/readPath.png" alt="" style=" "></p><hr><blockquote><p>5.3 输出文件路径的设置</p></blockquote><p> 输出最终结果的路径设置在 PipeLineLaying/src/com/dao/impl/OutputDataDAOImpl.java 里面修改,<br> 修改位置为下图标红线的地方:</p><p> <img src="/images/project/arithmetic/outputPath.png" alt="" style=" "> </p><blockquote><p>5.4 数据库配置</p></blockquote><p> (1) 数据库连接信息可在项目下的 src/jdbc.properties 文件中修改,<br>   下图标红线的位置对应的是 自己数据库的名字(类型为Mysql数据库)。</p><hr><p> <img src="/images/project/arithmetic/jdbc.png" alt="" style=" "> </p><hr><p> (2) 将项目中的 pipelying.sql 导入自己电脑的本地数据库<br>   演示Navicat导入的方式:<br>   右键连接–>选择运行SQL文件–>选中pipelying.sql–>运行完毕点击关闭–>右键刷新。<br>   完成以上步骤后, pipelying数据库便成功导入到了你的本地数据库中。</p><hr><p> <img src="/images/project/arithmetic/database.png" alt="" style=" "> </p><hr><h2 id="6-emsp-项目源代码"><a href="#6-emsp-项目源代码" class="headerlink" title="6. 项目源代码"></a><span style="color:black;text-shadow:5px 5px 5px #ccc ">6. 项目源代码</span></h2><blockquote><p>百度网盘下载 ( 包含项目报告 )</p><ul><li>百度网盘链接: 链接:<a href="https://pan.baidu.com/s/1FQU3OHlm9E7IU9_0DsJphQ" target="_blank" rel="noopener">https://pan.baidu.com/s/1FQU3OHlm9E7IU9_0DsJphQ</a> </li><li>提取码:qi9m </li></ul></blockquote><blockquote><p>github下载 ( 只含源代码 )</p><ul><li>github下载地址: <a href="https://github.com/fyf2016/PipelineLaying.git" target="_blank" rel="noopener">https://github.com/fyf2016/PipelineLaying.git</a></li></ul></blockquote><blockquote><p>注: 两个地址都可以下载</p></blockquote>]]></content>
<categories>
<category> 三级项目 </category>
</categories>
<tags>
<tag> 三级项目 </tag>
<tag> javaWeb </tag>
<tag> 算法 </tag>
</tags>
</entry>
<entry>
<title>JavaScript中的与数值相关的全局方法。</title>
<link href="/2018/03/31/JavaScript%E4%B8%AD%E7%9A%84%E4%B8%8E%E6%95%B0%E5%80%BC%E7%9B%B8%E5%85%B3%E7%9A%84%E5%85%A8%E5%B1%80%E6%96%B9%E6%B3%95/"/>
<url>/2018/03/31/JavaScript%E4%B8%AD%E7%9A%84%E4%B8%8E%E6%95%B0%E5%80%BC%E7%9B%B8%E5%85%B3%E7%9A%84%E5%85%A8%E5%B1%80%E6%96%B9%E6%B3%95/</url>
<content type="html"><![CDATA[<h2 id="皮卡丘洗脑"><a href="#皮卡丘洗脑" class="headerlink" title="皮卡丘洗脑"></a>皮卡丘洗脑</h2><a id="more"></a><p><img src="/images/emoji/滑稽.png">先来洗脑一下吧 </p><div><br> <video src="/vedio/hc - 去吧皮卡丘,给我撸一发最带感的PikaPikaPi.mp4" controls="controls"><br> </video><br></div><h2 id="全局方法概括"><a href="#全局方法概括" class="headerlink" title="全局方法概括"></a><span style="color:rgba(255,165,0,0.8);text-shadow:5px 5px 5px #ccc">全局方法概括</span></h2><p><span style="color:purple;text-shadow:5px 5px 5px #ccc ">1. parseInt ( )</span></p><p><span style="color:orange;text-shadow:5px 5px 5px #ccc ">2. parseFloat ( )</span></p><p><span style="color:green;text-shadow:5px 5px 5px #ccc ">3. isNaN ( )</span></p><p><span style="color:red;text-shadow:5px 5px 5px #ccc ">4. isFinate ( )</span></p><h2 id="具体内容"><a href="#具体内容" class="headerlink" title="具体内容"></a><span style="color:rgba(144,238,144,1);text-shadow:5px 5px 10px #ccc ">具体内容</span></h2><h2 id="1-emsp-parseInt"><a href="#1-emsp-parseInt" class="headerlink" title="1. parseInt ( )"></a><span style="color:purple;text-shadow:5px 5px 5px #ccc ">1. parseInt ( )</span></h2><p>(1) 基本用法<br> </p><p>parseInt方法用于将字符串转为整数。</p><p></p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'123'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 123</span></code></pre><hr><p>如果字符串头部有空格,空格会被自动去除。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">' 81'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 81</span></code></pre><hr><p>如果parseInt的参数不是字符串,则会先转为字符串再转换。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">1.23</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 1</span> <span class="token comment" spellcheck="true">// 等同于</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'1.23'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 1 </span></code></pre><p>(2)进制转换</p><p>parseInt方法还可以接受第二个参数(2到36之间),表示被解析的值的进制,返回该值对应的十进制<br><br>数。默认情况下,parseInt的第二个参数为10,即默认是十进制转十进制。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'1000'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 1000</span> <span class="token comment" spellcheck="true">// 等同于</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'1000'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 1000</span></code></pre><hr><p>下面是转换指定进制的数的例子。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'1000'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 8</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'1000'</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 216</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'1000'</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 512</span> 上面代码中,二进制、六进制、八进制的<span class="token number">1000</span>,分别等于十进制的<span class="token number">8</span>、<span class="token number">216</span>和<span class="token number">512</span>。 这意味着,可以用parseInt方法进行进制的转换。</code></pre><hr><p>如果第二个参数不是数值,会被自动转为一个整数。这个整数只有在2到36之间,才能得到有意义的结果,超出这个范围,则返回NaN。如果第二个参数是0、undefined和null,则直接忽略。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'10'</span><span class="token punctuation">,</span> <span class="token number">37</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// NaN</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'10'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// NaN</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'10'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 10</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'10'</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 10</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'10'</span><span class="token punctuation">,</span> undefined<span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 10</span></code></pre><hr><p>前面说过,如果parseInt的第一个参数不是字符串,会被先转为字符串。这会导致一些令人意外的结果。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">0x11</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 43</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token number">0x11</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 1</span> <span class="token comment" spellcheck="true">// 等同于</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token function">String</span><span class="token punctuation">(</span><span class="token number">0x11</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">)</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token function">String</span><span class="token punctuation">(</span><span class="token number">0x11</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 等同于</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'17'</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">)</span> <span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">'17'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span></code></pre><pre><code>上面代码中,十六进制的0x11会被先转为十进制的17,再转为字符串。然后,再用36进制或二进制解读字符串17,最后返回结果43和1。</code></pre><p>这种处理方式,对于八进制的前缀0,尤其需要注意。</p><h2 id="2-emsp-parseFloat"><a href="#2-emsp-parseFloat" class="headerlink" title="2. parseFloat ( )"></a><span style="color:orange;text-shadow:5px 5px 5px #ccc ">2. parseFloat ( )</span></h2><p>parseFloat方法用于将一个字符串转为浮点数。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">'3.14'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 3.14</span></code></pre><p>如果字符串符合科学计数法,则会进行相应的转换。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">'314e-2'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 3.14</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">'0.0314E+2'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 3.14</span></code></pre><p>如果字符串包含不能转为浮点数的字符,则不再进行往后转换,返回已经转好的部分。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">'3.14more non-digit characters'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 3.14</span> parseFloat方法会自动过滤字符串前导的空格。 <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">'\t\v\r12.34\n '</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 12.34</span></code></pre><p>如果参数不是字符串,或者字符串的第一个字符不能转化为浮点数,则返回NaN。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// NaN</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">'FF2'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// NaN</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// NaN</span></code></pre><p>上面代码中,尤其值得注意,parseFloat会将空字符串转为NaN。</p><p>这些特点使得parseFloat的转换结果不同于Number函数。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// NaN</span> <span class="token function">Number</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 1</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// NaN</span> <span class="token function">Number</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 0</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// NaN</span> <span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 0</span> <span class="token function">parseFloat</span><span class="token punctuation">(</span><span class="token string">'123.45#'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// 123.45</span> <span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">'123.45#'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// NaN</span></code></pre><h2 id="3-emsp-isNaN"><a href="#3-emsp-isNaN" class="headerlink" title="3. isNaN ( )"></a><span style="color:green;text-shadow:5px 5px 5px #ccc ">3. isNaN ( )</span></h2><p>isNaN方法可以用来判断一个值是否为NaN。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// true</span> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token number">123</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// false</span></code></pre><p>但是,isNaN只对数值有效,如果传入其他值,会被先转成数值。比如,传入字符串<br>的时候,字符串会被先转成NaN,所以最后返回true,这一点要特别引起注意。也就是说,<br>isNaN为true的值,有可能不是NaN,而是一个字符串。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token string">'Hello'</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// true</span> <span class="token comment" spellcheck="true">// 相当于</span> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token function">Number</span><span class="token punctuation">(</span><span class="token string">'Hello'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// true</span></code></pre><p>出于同样的原因,对于对象和数组,isNaN也返回true。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// true</span> <span class="token comment" spellcheck="true">// 等同于</span> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token function">Number</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// true</span> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'xzy'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// true</span> <span class="token comment" spellcheck="true">// 等同于</span> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token function">Number</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'xzy'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// true</span></code></pre><p>但是,对于空数组和只有一个数值成员的数组,isNaN返回false。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// false</span> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">123</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// false</span> <span class="token function">isNaN</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'123'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// false</span></code></pre><p>上面代码之所以返回false,原因是这些数组能被Number函数转成数值,请参见《数据类型转换》一章。</p><p>因此,使用isNaN之前,最好判断一下数据类型。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token keyword">function</span> <span class="token function">myIsNaN</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token keyword">typeof</span> value <span class="token operator">===</span> <span class="token string">'number'</span> <span class="token operator">&&</span> <span class="token function">isNaN</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><p>判断NaN更可靠的方法是,利用NaN为唯一不等于自身的值的这个特点,进行判断。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token keyword">function</span> <span class="token function">myIsNaN</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> value <span class="token operator">!==</span> value<span class="token punctuation">;</span> <span class="token punctuation">}</span></code></pre><h2 id="4-emsp-isFinate"><a href="#4-emsp-isFinate" class="headerlink" title="4. isFinate ( )"></a><span style="color:red;text-shadow:5px 5px 5px #ccc ">4. isFinate ( )</span></h2><p>isFinite方法返回一个布尔值,表示某个值是否为正常的数值。</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token function">isFinite</span><span class="token punctuation">(</span><span class="token number">Infinity</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// false</span> <span class="token function">isFinite</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">Infinity</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// false</span> <span class="token function">isFinite</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// false</span> <span class="token function">isFinite</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">// true</span></code></pre><p>除了Infinity、-Infinity和NaN这三个值会返回false,isFinite对于其他的数值都会返回true。</p>]]></content>
<categories>
<category> 技术 </category>
</categories>
<tags>
<tag> 技术 </tag>
<tag> JavaScript </tag>
</tags>
</entry>
<entry>
<title>Node.js中的MD5加密</title>
<link href="/2018/03/22/Node.js%E4%B8%AD%E7%9A%84MD5%E5%8A%A0%E5%AF%86/"/>
<url>/2018/03/22/Node.js%E4%B8%AD%E7%9A%84MD5%E5%8A%A0%E5%AF%86/</url>
<content type="html"><![CDATA[<h2 id="音乐"><a href="#音乐" class="headerlink" title="音乐"></a>音乐</h2><p>先来一首陈势安的 《 天后 》来缓解一下疲惫的心情吧</p><div><br> <video src="/music/天后-陈势安.mp3" controls="controls" style="background: url('/images/music/music1.jpg');background-size:100% 100%;"><br> </video><br></div><h2 id="MD5简介"><a href="#MD5简介" class="headerlink" title="MD5简介"></a>MD5简介</h2><p><br> <span style="font-size: 17px;color: purple">1.定义</span><br><br> MD5即Message-Digest Algorithm 5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用<br><br> 的一种散列函数,用以提供消息的完整性保护。<br></p><p><br> <span style="font-size: 17px;color: #00e079">2.作用</span><br><br> MD5的作用是让大容量信息在用数字签名软件签署私人密钥前被”压缩”成一种保密的格式(就是把一<br><br>个任意长度的字节串变换成一定长的十六进制数字串)。除了MD5以外,其中比较有名的还有<br><br> <span style="color: skyblue;">sha-1</span>、<span style="color: orange;">RIPEMD</span>以及<span style="color: green;">Haval</span>等。<br></p><p><br> <span style="font-size: 17px;color: #a78e44">3.MD5算法特点</span><br><br> MD5算法具有以下特点:<br><br> (1) 压缩性:任意长度的数据,算出的MD5值长度都是固定的。<br><br> (2) 容易计算:从原数据计算出MD5值很容易。<br><br> (3) 抗修改性:对原数据进行任何改动,哪怕只修改1个字节,所得到的MD5值都有很大区别。<br><br> (4) 强抗碰撞:已知原数据和其MD5值,想找到一个具有相同MD5值的数据(即伪造数据)是非常困<br><br>  难的。<br></p><p><br></p><h2 id="Node-js使用MD5"><a href="#Node-js使用MD5" class="headerlink" title="Node.js使用MD5"></a>Node.js使用MD5</h2><pre class=" language-javascript"><code class="language-javascript">注<span class="token punctuation">:</span> MD5是一种常用的哈希算法,用于给任意数据一个“签名”。这个签名通常用一个十六进制的字符串表示: <span class="token keyword">const</span> crypto <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'crypto'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//引入crypto加密模块</span> <span class="token keyword">const</span> hash <span class="token operator">=</span> crypto<span class="token punctuation">.</span><span class="token function">createHash</span><span class="token punctuation">(</span><span class="token string">'md5'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//规定使用哈希算法中的MD5算法</span> <span class="token comment" spellcheck="true">// 可任意多次调用update(),效果相当于多个字符串相加</span> hash<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token string">'123a'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> hash<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token string">'456'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//最终加密的字符串为'123a456',hash.digest('hex')表示输出的格式为16进制</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>hash<span class="token punctuation">.</span><span class="token function">digest</span><span class="token punctuation">(</span><span class="token string">'hex'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 57d15a6ee827933b25dab42111b6803b</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span>方法默认字符串编码为UTF<span class="token number">-8</span>,也可以传入Buffer。 注<span class="token punctuation">:</span> 如果要计算SHA1,只需要把<span class="token string">'md5'</span>改成<span class="token string">'sha1'</span>,就可以得到SHA1的结果 3be8b1b4c1b081f65522c4dd69b1d5c5713b038b。当然还可以使用更安全的sha256和sha512。</code></pre><p>同样,可以将MD5算法封装成一个函数 , 方便重复调用</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token keyword">const</span> crypto <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'crypto'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">function</span> md5 <span class="token punctuation">(</span>text<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> crypto<span class="token punctuation">.</span><span class="token function">createHash</span><span class="token punctuation">(</span><span class="token string">'md5'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">digest</span><span class="token punctuation">(</span><span class="token string">'hex'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre><p>目前单次的MD5加密并不安全,很容易被暴力破解,因此推荐多次加密和使用秘钥加密</p><pre class=" language-javascript"><code class="language-javascript"> <span class="token number">1</span><span class="token punctuation">.</span>多次MD5加密 例如要加密 <span class="token string">'123'</span> 这个字符串 <span class="token keyword">const</span> crypto <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'crypto'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">function</span> md5 <span class="token punctuation">(</span>text<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> crypto<span class="token punctuation">.</span><span class="token function">createHash</span><span class="token punctuation">(</span><span class="token string">'md5'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">digest</span><span class="token punctuation">(</span><span class="token string">'hex'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> 多次加密<span class="token punctuation">:</span> <span class="token keyword">var</span> str<span class="token operator">=</span><span class="token string">'123'</span><span class="token punctuation">;</span> <span class="token keyword">var</span> pass<span class="token operator">=</span><span class="token function">md5</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">var</span> FinalPass<span class="token operator">=</span><span class="token function">md5</span><span class="token punctuation">(</span>pass<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre><code>多次MD5加密不容易被破解,但是嵌套起来也比较麻烦, 因此我再介绍另外一种方法</code></pre><pre class=" language-javascript"><code class="language-javascript"> <span class="token number">2</span><span class="token punctuation">.</span>MD5结合秘钥加密 例如要加密 <span class="token string">'123'</span> 这个字符串 <span class="token keyword">const</span> crypto <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'crypto'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">function</span> md5 <span class="token punctuation">(</span>text<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> crypto<span class="token punctuation">.</span><span class="token function">createHash</span><span class="token punctuation">(</span><span class="token string">'md5'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">digest</span><span class="token punctuation">(</span><span class="token string">'hex'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> 结合秘钥加密<span class="token punctuation">:</span> <span class="token keyword">var</span> str<span class="token operator">=</span><span class="token string">'123'</span><span class="token punctuation">;</span> <span class="token keyword">var</span> md5_suffix<span class="token operator">=</span><span class="token string">'asd1wdad1d哈do捱三顶五#&8qw'</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//md5_suffix作为秘钥,越复杂越好</span> <span class="token keyword">var</span> FinalPass<span class="token operator">=</span><span class="token function">md5</span><span class="token punctuation">(</span>str<span class="token operator">+</span>md5_suffix<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><pre><code>结合秘钥使用MD5加密更难被破解,但是也有一定的缺陷,就是如果秘钥一旦丢失就无法进行验证</code></pre><h2 id="MD5加密语句的详细讲解"><a href="#MD5加密语句的详细讲解" class="headerlink" title="MD5加密语句的详细讲解"></a>MD5加密语句的详细讲解</h2><pre class=" language-javascript"><code class="language-javascript"> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> crypto<span class="token punctuation">.</span><span class="token function">createHash</span><span class="token punctuation">(</span>algorithm<span class="token punctuation">)</span> 创建并返回一个hash对象,它是一个指定算法的加密hash,用于生成hash摘要。 参数algorithm可选择系统上安装的OpenSSL版本所支持的算法。例如:<span class="token string">'sha1'</span><span class="token punctuation">,</span> <span class="token string">'md5'</span><span class="token punctuation">,</span> <span class="token string">'sha256'</span><span class="token punctuation">,</span> <span class="token string">'sha512'</span>等。在近期发行的版本中,openssl list<span class="token operator">-</span>message<span class="token operator">-</span>digest<span class="token operator">-</span>algorithms会显示这些可用的摘要算法 <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span> hash<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span> 更新hash的内容为指定的data。当使用流数据时可能会多次调用该方法。 <span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> hash<span class="token punctuation">.</span><span class="token function">digest</span><span class="token punctuation">(</span>encoding<span class="token operator">=</span><span class="token string">'binary'</span><span class="token punctuation">)</span> 计算所有传入数据的hash摘要。参数encoding(编码方式)可以为<span class="token string">'hex'</span><span class="token punctuation">,</span> <span class="token string">'binary'</span> 或者<span class="token string">'base64'</span>。 </code></pre>]]></content>
<categories>
<category> 技术 </category>
</categories>
<tags>
<tag> 技术 </tag>
<tag> Node.js </tag>
</tags>
</entry>
<entry>
<title>Hello World</title>
<link href="/2017/03/27/hello-world/"/>
<url>/2017/03/27/hello-world/</url>
<content type="html"><![CDATA[<p>Welcome to <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/" target="_blank" rel="noopener">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html" target="_blank" rel="noopener">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues" target="_blank" rel="noopener">GitHub</a>.</p><h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><pre class=" language-bash"><code class="language-bash">$ hexo new <span class="token string">"My New Post"</span></code></pre><p>More info: <a href="https://hexo.io/docs/writing.html" target="_blank" rel="noopener">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><pre class=" language-bash"><code class="language-bash">$ hexo server</code></pre><p>More info: <a href="https://hexo.io/docs/server.html" target="_blank" rel="noopener">Server</a></p><h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><pre class=" language-bash"><code class="language-bash">$ hexo generate</code></pre><p>More info: <a href="https://hexo.io/docs/generating.html" target="_blank" rel="noopener">Generating</a></p><h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><pre class=" language-bash"><code class="language-bash">$ hexo deploy</code></pre><p>More info: <a href="https://hexo.io/docs/deployment.html" target="_blank" rel="noopener">Deployment</a></p>]]></content>
<categories>
<category> helloWorld </category>
</categories>
</entry>
<entry>
<title>js异步和网络Api发展史</title>
<link href="/2017/03/27/js%E5%BC%82%E6%AD%A5%E5%92%8C%E7%BD%91%E7%BB%9Capi%E5%8F%91%E5%B1%95%E5%8F%B2/"/>
<url>/2017/03/27/js%E5%BC%82%E6%AD%A5%E5%92%8C%E7%BD%91%E7%BB%9Capi%E5%8F%91%E5%B1%95%E5%8F%B2/</url>
<content type="html"><![CDATA[<blockquote><p>从ajax到fetch,从回调到async,滑稽带你领略js异步和网络Api发展史。<br>仅仅是速览不会深入</p></blockquote><a id="more"></a><p>js是事件驱动的语言,单进程单线程。js主线程中有一个<code>Event Loop</code>(事件循环)。</p><h2 id="js运行顺序"><a href="#js运行顺序" class="headerlink" title="js运行顺序"></a>js运行顺序</h2><p>js先执行一遍所有js文件,然后进入<code>Event Loop</code>,等到有事件进入,就执行事件处理程序,执行完后再次回到<code>Event Loop</code>。</p><p>举个例子:</p><pre class=" language-js"><code class="language-js"><span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//死循环</span><span class="token keyword">while</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token punctuation">}</span></code></pre><blockquote><p>如果你在浏览器中运行,页面会卡死(在一些浏览器中仍可以滚动),因为js不执行完,浏览器不会做任何响应。</p></blockquote><p>上面的代码永远不会输出hello,因为<code>setTimeout(...,0)</code>,虽然是延时0ms,但还是会在下一个事件循环中被调用。</p><p>而后面的<code>while</code>循环是死循环,js引擎永远执行不完,所以不会进入事件循环的,也就永远执行不到<code>timeout</code>里的代码</p><p>更详细的介绍可在<a href="http://www.ruanyifeng.com/blog/2014/10/event-loop.html" target="_blank" rel="noopener">阮老师的博客</a>中看见。</p><h2 id="异步"><a href="#异步" class="headerlink" title="异步"></a>异步</h2><p>所以啥是异步呢?</p><p>我们先讲异步的反义词同步。</p><blockquote><p>同步就是在发出一个功能调用时,在没有得到结果之前,该调用就不返回。</p></blockquote><p>比如你发出一个网络请求请求一个数据,程序一直等待数据返回,不返回就不继续执行,这就是同步。</p><p>异步要智能得多。在网络请求没有返回之前程序还会继续执行。</p><p>延迟一秒在不同语言中同步、异步的不同写法:</p><p>JS 异步</p><pre class=" language-js"><code class="language-js">console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//输出 1 3 2</span></code></pre><p>Java 同步</p><pre class=" language-java"><code class="language-java">System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//延迟1000ms</span>Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//输出 1 2 3</span></code></pre><p>显然<code>Java</code>的同步跟符合人类的思想,顺序执行非常简单。</p><p>但<code>JS</code>的优势也特别明显,在延迟时程序不会卡死,而是继续执行下去。</p><h2 id="回调(callback)"><a href="#回调(callback)" class="headerlink" title="回调(callback)"></a>回调(callback)</h2><p>回调是JS异步编程最常见的方式。</p><p><a href="https://www.zhihu.com/question/19801131" target="_blank" rel="noopener">回调函数(callback)是什么?(逼乎)</a></p><pre class=" language-js"><code class="language-js"><span class="token keyword">function</span> <span class="token function">callback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"hello"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token function">setTimeout</span><span class="token punctuation">(</span>callback<span class="token punctuation">,</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>上面的<code>callback()</code>就作为回调函数,在1000ms后会被调用。</p><h2 id="Ajax"><a href="#Ajax" class="headerlink" title="Ajax"></a>Ajax</h2><p>回到我们的主题,运用最广泛的网络api <code>ajax</code>就是以回调方式使用的。</p><pre class=" language-js"><code class="language-js"><span class="token keyword">var</span> xmlhttp<span class="token punctuation">;</span>xmlhttp<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">XMLHttpRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//定义回调函数</span>xmlhttp<span class="token punctuation">.</span>onreadystatechange<span class="token operator">=</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//如果请求完成 且 返回状态码为200</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>xmlhttp<span class="token punctuation">.</span>readyState<span class="token operator">==</span><span class="token number">4</span> <span class="token operator">&&</span> xmlhttp<span class="token punctuation">.</span>status<span class="token operator">==</span><span class="token number">200</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//输出内容</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>xmlhttp<span class="token punctuation">.</span>responseText<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>xmlhttp<span class="token punctuation">.</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">"GET"</span><span class="token punctuation">,</span><span class="token string">"https://about.huaji8.top/links.json"</span><span class="token punctuation">,</span><span class="token boolean">true</span><span class="token comment" spellcheck="true">/*异步运行*/</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment" spellcheck="true">//发送</span>xmlhttp<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote><p>复制到浏览器<code>console</code>可以运行。</p></blockquote><blockquote><p><code>jquery</code>封装的ajax写法要比原版简单的多,对这是原版。</p></blockquote><blockquote><p>ajax也可以同步运行,只要将<code>XMLHttpRequest.open()</code>最后一个参数改成<code>false</code>,就是同步运行,执行<code>send()</code>后浏览器会卡住,直到请求返回。</p></blockquote><h2 id="Promise-(承诺)"><a href="#Promise-(承诺)" class="headerlink" title="Promise (承诺)"></a>Promise (承诺)</h2><p>Promise是一种更高级的回调接口。</p><blockquote><p>详细的介绍在这<a href="http://es6.ruanyifeng.com/#docs/promise" target="_blank" rel="noopener">ECMAScript 6 入门</a>,<code>Promise</code>的好处千千万,建议大家全部使用<code>Promise</code>,<code>jquery</code>的所有异步api也全部支持<code>Promise</code></p></blockquote><blockquote><p>ES6 原生支持 Promise , 在不支持的浏览器上可以使用垫片库 <a href="https://github.com/stefanpenner/es6-promise" target="_blank" rel="noopener">es6-promise</a></p></blockquote><p>下面是一个将<code>setTimeout()</code>改造成<code>Promise</code>的例子。</p><pre class=" language-js"><code class="language-js"><span class="token comment" spellcheck="true">//返回一个Promise 就可以无限then()啦 还可以用catch()捕获异常</span><span class="token keyword">function</span> <span class="token function">setTimeoutP</span><span class="token punctuation">(</span>ms<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//创建Promise 如果成功就调用resolve,失败就调用reject</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span>resolve<span class="token punctuation">,</span>reject<span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span>resolve<span class="token punctuation">,</span>ms<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token function">setTimeoutP</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Hello'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 返回Promise 可以在then()中继续执行</span> <span class="token keyword">return</span> <span class="token function">setTimeoutP</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'world'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// 返回数据 可以在then()中获取</span> <span class="token keyword">return</span> <span class="token number">123</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>r<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//一秒后输出Hello</span></code></pre><h2 id="fetch"><a href="#fetch" class="headerlink" title="fetch"></a>fetch</h2><p>fetch 是新的网络api 基于 Promise 设计。在旧的浏览器上可以使用 <a href="https://github.com/matthew-andrews/isomorphic-fetch" target="_blank" rel="noopener">isomorphic-fetch</a>,这个api是 node 和 浏览器环境通用的,实现同构应用必备的库。</p><blockquote><p>注:同构(isomorphic/universal)就是使前后端运行同一套代码的意思,后端一般是指 NodeJS 环境。</p></blockquote><p>范例</p><pre class=" language-js"><code class="language-js"><span class="token function">fetch</span><span class="token punctuation">(</span><span class="token string">"https://about.huaji8.top/links.json"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span><span class="token keyword">return</span> e<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span>console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote><p>尝试在浏览器<code>console</code>中运行</p></blockquote><h2 id="Generator-函数"><a href="#Generator-函数" class="headerlink" title="Generator 函数"></a>Generator 函数</h2><p><code>Generator 函数</code> 是 <code>ES6</code> 提供的语法,不过很快就被 <code>ES2017</code> 的 <code>async 函数</code> 代替,所以不多说了,感兴趣的可以看<a href="http://es6.ruanyifeng.com/?search=fetch&x=0&y=0#docs/generator" target="_blank" rel="noopener">ECMAScript 6 入门</a></p><blockquote><p>第一次看到 <code>Generator 函数</code> 是在 <code>unity</code> 中的协程,现在js也有这个语法了。</p></blockquote><h2 id="async-函数"><a href="#async-函数" class="headerlink" title="async 函数"></a>async 函数</h2><p>重头来了,这是目前最屌的写法,真正用同步的写法写异步程序。</p><blockquote><p>async 实际是 <code>Generator 函数</code> 的语法糖, <code>Generator</code> 才是技术</p></blockquote><p>我们继续拿上面建的 <code>setTimeoutP()</code> 举例。</p><pre class=" language-js"><code class="language-js"><span class="token comment" spellcheck="true">//返回一个Promise</span><span class="token keyword">function</span> <span class="token function">setTimeoutP</span><span class="token punctuation">(</span>ms<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//创建Promise 如果成功就调用resolve,失败就调用reject</span> <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token punctuation">(</span>resolve<span class="token punctuation">,</span>reject<span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span> <span class="token function">setTimeout</span><span class="token punctuation">(</span>resolve<span class="token punctuation">,</span>ms<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">asyncDelay</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">// await可以拿到Promise的返回值</span> <span class="token keyword">let</span> O <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetch</span><span class="token punctuation">(</span><span class="token string">"https://about.huaji8.top/links.json"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span><span class="token keyword">return</span> e<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token keyword">await</span> <span class="token function">setTimeoutP</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>O<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">//一秒后输出 https://about.huaji8.top/links.json 内容</span><span class="token punctuation">}</span><span class="token function">asyncDelay</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote><p>尝试在浏览器<code>console</code>中运行</p></blockquote><h2 id="async-异常处理"><a href="#async-异常处理" class="headerlink" title="async 异常处理"></a>async 异常处理</h2><p><code>async</code> 可以以同步的方式编写异常处理。</p><pre class=" language-js"><code class="language-js"><span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">asyncDelay</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">try</span><span class="token punctuation">{</span> <span class="token keyword">let</span> O <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token function">fetch</span><span class="token punctuation">(</span><span class="token string">"https://about.huaji8.top/links.json"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">></span><span class="token punctuation">{</span><span class="token keyword">return</span> e<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token keyword">catch</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment" spellcheck="true">//输出异常</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">...</span><span class="token punctuation">.</span> <span class="token punctuation">}</span> <span class="token keyword">await</span> <span class="token function">setTimeoutP</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>O<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span></code></pre><p>是不是超级方便呢。</p><p>关于<code>async</code>的更多信息,可以查看<a href="http://es6.ruanyifeng.com/#docs/async" target="_blank" rel="noopener">ECMAScript 6 入门</a>或<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction" target="_blank" rel="noopener">MDN</a></p>]]></content>
<categories>
<category> 技术 </category>
</categories>
<tags>
<tag> 技术 </tag>
<tag> js </tag>
<tag> web </tag>
</tags>
</entry>
<entry>
<title>性格职业测试app</title>
<link href="/2017/03/12/%E6%80%A7%E6%A0%BC%E8%81%8C%E4%B8%9A%E6%B5%8B%E8%AF%95App/"/>
<url>/2017/03/12/%E6%80%A7%E6%A0%BC%E8%81%8C%E4%B8%9A%E6%B5%8B%E8%AF%95App/</url>
<content type="html"><![CDATA[<h2 id="项目介绍"><a href="#项目介绍" class="headerlink" title="项目介绍"></a><span style="color:rgba(255,165,0,0.8);text-shadow:5px 5px 5px #ccc">项目介绍</span></h2><p><span style="color:purple;text-shadow:5px 5px 5px #ccc ">1. 项目背景</span></p><p><span style="color:orange;text-shadow:5px 5px 5px #ccc ">2. 项目意义</span></p><p><span style="color:green;text-shadow:5px 5px 5px #ccc ">3. 总体设计</span></p><p><span style="color:black;text-shadow:5px 5px 5px #ccc ">4. 项目源代码</span></p><h2 id="1-emsp-项目背景"><a href="#1-emsp-项目背景" class="headerlink" title="1. 项目背景"></a><span style="color:purple;text-shadow:5px 5px 5px #ccc ">1. 项目背景</span></h2><pre><code>在当今这个信息大爆炸的时代 , 各个行业都迅猛发展 ,传达给人们的信息量越来越大, 形形色色的行业的出现和发展往往会给人们在选择职业时带来困扰,而对自身的不了解更让人头痛,因此在这个大的背景下,了解自身性格并选择合适的职业尤为重要。</code></pre><h2 id="2-emsp-项目意义"><a href="#2-emsp-项目意义" class="headerlink" title="2. 项目意义"></a><span style="color:orange;text-shadow:5px 5px 5px #ccc ">2. 项目意义</span></h2><blockquote><p>找寻自身的兴趣和能力</p></blockquote><ul><li>我们知道找到自己的兴趣和能力是选择职业或教育前的重要一步,‘磨刀不误砍柴功’,<br> 我们最不希望几十年的人生光阴因为选择错误而事倍功半。</li></ul><blockquote><p>我们常常对自己的了解不够深入</p></blockquote><ul><li>而我们自己对自己的了解往往不知道如何入手,这样,我们往往会借助于权威专业的职业性格测试来了解自己。<br> 了解自己的人格类型,适合领域和适合职业。</li></ul><blockquote><p>通过app上的测试题了解自己</p></blockquote><ul><li>无论你现在从事什么职业,你都可以通过这个性格职业测试app,看看自己到底喜欢做什么,适合做什么…</li></ul><h2 id="3-emsp-总体设计"><a href="#3-emsp-总体设计" class="headerlink" title="3. 总体设计"></a><span style="color:green;text-shadow:5px 5px 5px #ccc ">3. 总体设计</span></h2><blockquote><p>项目划分</p></blockquote><ul><li>项目类型<ul><li>WebApp项目</li></ul></li><li><p>项目主要划分</p><ul><li>页面</li><li>霍兰德算法(核心)</li></ul></li><li><p>项目主要开发工具</p><ul><li>Sublime</li><li>Hbulider</li></ul></li><li><p>项目开发步骤</p><ul><li>第一步: 实现霍兰德算法</li><li>第二步: 添加界面</li><li>第三步: 使用Hbuilder, 打包成WebApp</li></ul></li></ul><h2 id="4-emsp-结果演示"><a href="#4-emsp-结果演示" class="headerlink" title="4. 结果演示"></a><span style="color:red;text-shadow:5px 5px 5px #ccc ">4. 结果演示</span></h2><h3 id="4-1-项目主要界面展示"><a href="#4-1-项目主要界面展示" class="headerlink" title="4.1 项目主要界面展示"></a>4.1 项目主要界面展示</h3><blockquote><p>logo(启动图标)</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/1.png" alt="" style=" "></p><hr><blockquote><p>启动画面</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/2.png" alt="" style=" " width="230"></p><hr><blockquote><p>主界面</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/3.png" alt="" style=" " width="230"></p><hr><blockquote><p>测试题开始界面</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/4.png" alt="" style=" " width="230"></p><hr><blockquote><p>部分测试内容</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/5.png" alt="" style=" " width="230"></p><hr><blockquote><p>测试结果界面</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/6.png" alt="" style=" " width="230"></p><hr><blockquote><p>性格分析界面</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/7.png" alt="" style=" " width="230"></p><hr><blockquote><p>性格分析部分内容</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/8.png" alt="" style=" " width="230"></p><hr><blockquote><p>算法介绍界面</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/9.png" alt="" style=" " width="230"></p><hr><blockquote><p>团队介绍界面</p></blockquote><hr><p><img src="/images/project/PersonalTestApp/10.png" alt="" style=" " width="230"></p><hr><h2 id="4-emsp-项目源代码"><a href="#4-emsp-项目源代码" class="headerlink" title="4. 项目源代码"></a><span style="color:black;text-shadow:5px 5px 5px #ccc ">4. 项目源代码</span></h2><blockquote><p>百度网盘下载 ( 包含项目报告 )</p><ul><li>百度网盘链接: 链接:<a href="https://pan.baidu.com/s/1bHNPOIbXBDKdVaTZ0iM4Yg" target="_blank" rel="noopener">https://pan.baidu.com/s/1bHNPOIbXBDKdVaTZ0iM4Yg</a></li><li>提取码:u6vn </li></ul></blockquote><blockquote><p>github下载 ( 只含源代码 )</p><ul><li>github下载地址: <a href="https://github.com/fyf2016/PersonalityProfessionalTestApp.git" target="_blank" rel="noopener">https://github.com/fyf2016/PersonalityProfessionalTestApp.git</a></li></ul></blockquote><blockquote><p>注: 两个地址都可以下载</p></blockquote>]]></content>
<categories>
<category> app </category>
</categories>
<tags>
<tag> webapp </tag>
<tag> holland-test </tag>
</tags>
</entry>
</search>