-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
1121 lines (538 loc) · 582 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>分布式和集群</title>
<link href="/2024/04/17/fen-bu-shi-he-ji-qun/"/>
<url>/2024/04/17/fen-bu-shi-he-ji-qun/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="分布式与集群"><a href="#分布式与集群" class="headerlink" title="分布式与集群"></a>分布式与集群</h2><h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p><strong>分布式</strong>是指将一个任务或应用程序分解成多个子任务,并在多台计算机上同时执行。分布式可以提高系统的并发处理能力和资源利用率。</p><p><strong>集群</strong>是指将多台计算机连接起来,作为一个整体对外提供服务。集群中的计算机通常具有相同的硬件和软件配置,它们之间通过网络进行通信。集群可以用于提高计算能力、存储容量、可靠性和可用性。</p><h3 id="作用"><a href="#作用" class="headerlink" title="作用"></a>作用</h3><p><strong>分布式</strong></p><ul><li>提高并发处理能力:多个子任务可以并行执行,从而提高系统的并发处理能力。</li><li>提高资源利用率:可以利用多台计算机的资源,提高资源利用率。</li><li>扩展性好:可以根据需要添加或删除计算机,方便扩展。</li></ul><p><strong>集群</strong></p><ul><li>提高计算能力:多台计算机可以协同工作,提高计算能力。</li><li>提高存储容量:多台计算机的存储空间可以累加,提高存储容量。</li><li>提高可靠性:一台计算机出现故障,其他计算机可以继续提供服务,提高可靠性。</li><li>提高可用性:可以将服务部署在多个数据中心,提高可用性。</li></ul><h3 id="例子"><a href="#例子" class="headerlink" title="例子"></a>例子</h3><p><strong>分布式</strong></p><ul><li>大型数据库:将数据库的数据分散到多台服务器上,提高数据库的查询性能和存储容量。</li><li>搜索引擎:将搜索任务分散到多台服务器上,提高搜索速度。</li><li>云计算:将计算资源分散到多台服务器上,为用户提供弹性可扩展的计算服务。</li></ul><p><strong>集群</strong></p><ul><li>高性能计算:用于科学计算、基因组学、金融分析等领域。</li><li>负载均衡:将负载分散到多台服务器上,避免单台服务器成为瓶颈。</li><li>高可用性:保证服务 7x24 小时可用。</li></ul><h3 id="现实中的实际例子"><a href="#现实中的实际例子" class="headerlink" title="现实中的实际例子"></a>现实中的实际例子</h3><p><strong>分布式</strong></p><ul><li>谷歌搜索引擎:使用分布式技术将搜索任务分散到全球数百万台服务器上,为用户提供快速准确的搜索结果。</li><li>亚马逊云计算平台:使用分布式技术为用户提供弹性可扩展的计算、存储、网络等服务。</li><li>Facebook:使用分布式技术存储和处理数十亿用户的社交数据。</li></ul><p><strong>集群</strong></p><ul><li>天网:中国国家超级计算中心,使用集群技术实现千万亿次浮点运算的计算能力。</li><li>CERN 大型强子对撞机:使用集群技术收集和分析实验数据。</li><li>高盛交易系统:使用集群技术处理高并发交易请求。</li></ul><h3 id="区别"><a href="#区别" class="headerlink" title="区别"></a>区别</h3><p>分布式和集群是两个密切相关的概念,但它们之间也存在一些区别:</p><ul><li><strong>目的</strong>:分布式的目的是提高并发处理能力和资源利用率,而集群的目的是提高计算能力、存储容量、可靠性和可用性。</li><li><strong>实现方式</strong>:分布式通过将任务分解成多个子任务并行执行,而集群通过将多台计算机连接起来作为一个整体对外提供服务。</li><li><strong>应用场景</strong>:分布式通常应用于大数据处理、云计算、物联网等领域,而集群通常应用于高性能计算、高可用性、负载均衡等领域。</li></ul><h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><p>分布式和集群都是计算机领域的重要技术,它们可以有效地提高系统的性能、可靠性、并发处理能力和资源利用率。</p><p><strong>参考资料</strong></p><ul><li><a href="https://tidb.net/blog/eb3cb609">https://tidb.net/blog/eb3cb609</a></li><li><a href="https://zh.wikipedia.org/zh-tw/%E9%9B%86%E7%BE%A4">https://zh.wikipedia.org/zh-tw/%E9%9B%86%E7%BE%A4</a></li><li><a href="https://baike.baidu.com/item/%E8%B6%85%E5%A4%A7%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93/22087312">https://baike.baidu.com/item/%E8%B6%85%E5%A4%A7%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93/22087312</a></li><li><a href="https://www.google.com/?hl=zh_CN">https://www.google.com/?hl=zh_CN</a></li><li><a href="https://aws.amazon.com/cn/choosing-a-cloud-platform/">https://aws.amazon.com/cn/choosing-a-cloud-platform/</a></li><li><a href="https://www.facebook.com/">https://www.facebook.com/</a></li><li><a href="https://en.wikipedia.org/wiki/Wikt:%E5%A4%A9">https://en.wikipedia.org/wiki/Wikt:%E5%A4%A9</a></li><li><a href="https://zh.wikipedia.org/wiki/Large_Hadron_Collider">https://zh.wikipedia.org/wiki/Large_Hadron_Collider</a></li><li><a href="https://www.goldmansachs.com/worldwide/greater-china/insights/GMD-People-Profile-Ling-Yuan.html">https://www.goldmansachs.com/worldwide/greater-china/insights/GMD-People-Profile-Ling-Yuan.html</a></li></ul>]]></content>
</entry>
<entry>
<title></title>
<link href="/2024/01/04/typora/"/>
<url>/2024/01/04/typora/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Typora"><a href="#Typora" class="headerlink" title="Typora"></a>Typora</h1><h2 id="文档分类"><a href="#文档分类" class="headerlink" title="文档分类"></a>文档分类</h2><h3 id="aaa"><a href="#aaa" class="headerlink" title="aaa"></a>aaa</h3><h3 id="bbb"><a href="#bbb" class="headerlink" title="bbb"></a>bbb</h3>]]></content>
</entry>
<entry>
<title>.editorconfig文件控制代码格式</title>
<link href="/2023/12/06/editorconfig-wen-jian-kong-zhi-dai-ma-ge-shi/"/>
<url>/2023/12/06/editorconfig-wen-jian-kong-zhi-dai-ma-ge-shi/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>文件根目录新建<code>.editorconfig文件</code>,键入以下内容:</p><pre class="line-numbers language-none"><code class="language-none">root = true[*]charset = utf-8end_of_line = lfindent_size = 4indent_style = spaceinsert_final_newline = falsemax_line_length = 140tab_width = 4[*.less]indent_size = 4[*.sass]indent_size = 4[*.scss]indent_size = 4[*.vue]indent_size = 4tab_width = 4[{*.bash,*.sh,*.zsh}]indent_size = 4tab_width = 4[{*.har,*.jsb4,*.jsb3,*.json,.babelrc,.eslintrc,.prettierrc,.stylelintrc,bowerrc,jest.config}]indent_size = 4[{*.http,*.rest}]indent_size = 4[{*.yaml,*.yml}]indent_size = 4<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<tags>
<tag> 代码格式控制 </tag>
</tags>
</entry>
<entry>
<title>Hello World</title>
<link href="/2023/12/05/hello-world/"/>
<url>/2023/12/05/hello-world/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">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">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">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="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo new <span class="token string">"My New Post"</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo server<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/server.html">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="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo generate<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/generating.html">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="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo deploy<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>]]></content>
<tags>
<tag> init blog </tag>
</tags>
</entry>
<entry>
<title>图连通性判断</title>
<link href="/2023/11/30/tu-lian-tong-xing-pan-duan/"/>
<url>/2023/11/30/tu-lian-tong-xing-pan-duan/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="使用并查集判断图连通性"><a href="#使用并查集判断图连通性" class="headerlink" title="使用并查集判断图连通性"></a>使用并查集判断图连通性</h1><h2 id="并查集-英文名称Disjoint-set-data-structure"><a href="#并查集-英文名称Disjoint-set-data-structure" class="headerlink" title="并查集,英文名称Disjoint-set data structure"></a><a href="https://zh.wikipedia.org/wiki/%E5%B9%B6%E6%9F%A5%E9%9B%86">并查集,英文名称Disjoint-set data structure</a></h2><p>在計算機科學中,併查集(英文:Disjoint-set data structure,直譯為不交集數據結構)是一種數據結構,用於處理一些不交集(Disjoint sets,一系列沒有重複元素的集合)的合併及查詢問題。併查集支持如下操作:</p><h2 id="判断依据"><a href="#判断依据" class="headerlink" title="判断依据"></a>判断依据</h2><p>图联通,则所有节点具有相同的根节点</p><h2 id="题目"><a href="#题目" class="headerlink" title="题目"></a>题目</h2><p>使用并查集判断图连通性</p><h2 id="输入"><a href="#输入" class="headerlink" title="输入"></a>输入</h2><p>本问题有多组测试数据,每组测试数据有两部分,第一部分只有一行,是两个正整数,分别表示图的节点数N(节点编号从1到N,1<=N<=100)和图的边数E;第二部分共有E行,每行也是两个整数N1,N2(1<=N1,N2<=N),分别表示N1和N2之间有一条边。</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">6 101 21 31 41 51 62 32 43 43 53 64 31 21 32 3<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h2><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">YesNo<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h2 id="数据结构"><a href="#数据结构" class="headerlink" title="数据结构"></a>数据结构</h2><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">class</span> <span class="token class-name">UnionFind</span> <span class="token punctuation">{</span><span class="token keyword">private</span><span class="token operator">:</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> parent<span class="token punctuation">;</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> rank<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h3 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h3><p>让所有节点的根节点都为她自己</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token function">UnionFind</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span> parent<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> rank<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</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">// 使用 std::iota 初始化 parent 数组</span> <span class="token function">iota</span><span class="token punctuation">(</span>parent<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> parent<span class="token punctuation">.</span><span class="token function">end</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 punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="并查集find操作"><a href="#并查集find操作" class="headerlink" title="并查集find操作"></a>并查集find操作</h3><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>parent<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">!=</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> parent<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>parent<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> parent<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>这里用到了<a href="https://oi-wiki.org/ds/dsu/">路径压缩</a>,将所有根节点下的节点变成兄弟节点,树呈两层</p><h3 id="并查集的合并操作"><a href="#并查集的合并操作" class="headerlink" title="并查集的合并操作"></a>并查集的合并操作</h3><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">void</span> <span class="token function">unite</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> rootX <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> rootY <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 这里用到了按秩合并,优化查询的时间开销</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>rootX <span class="token operator">!=</span> rootY<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>rank<span class="token punctuation">[</span>rootX<span class="token punctuation">]</span> <span class="token operator"><</span> rank<span class="token punctuation">[</span>rootY<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> parent<span class="token punctuation">[</span>rootX<span class="token punctuation">]</span> <span class="token operator">=</span> rootY<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>rank<span class="token punctuation">[</span>rootX<span class="token punctuation">]</span> <span class="token operator">></span> rank<span class="token punctuation">[</span>rootY<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> parent<span class="token punctuation">[</span>rootY<span class="token punctuation">]</span> <span class="token operator">=</span> rootX<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> parent<span class="token punctuation">[</span>rootY<span class="token punctuation">]</span> <span class="token operator">=</span> rootX<span class="token punctuation">;</span> rank<span class="token punctuation">[</span>rootX<span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="判断是否联通"><a href="#判断是否联通" class="headerlink" title="判断是否联通"></a>判断是否联通</h3><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">bool</span> <span class="token function">isConnected</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">const</span> vector<span class="token operator"><</span>pair<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">>></span><span class="token operator">&</span>edges<span class="token punctuation">)</span> <span class="token punctuation">{</span> UnionFind <span class="token function">uf</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> edges<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <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 punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> a <span class="token operator">=</span> edges<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>first<span class="token punctuation">;</span> <span class="token keyword">int</span> b <span class="token operator">=</span> edges<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>second<span class="token punctuation">;</span> uf<span class="token punctuation">.</span><span class="token function">unite</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> root <span class="token operator">=</span> uf<span class="token punctuation">.</span><span class="token function">find</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">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>uf<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">!=</span> root<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="完整代码-better-c-14-and-than"><a href="#完整代码-better-c-14-and-than" class="headerlink" title="完整代码(better c++ 14 and than)"></a>完整代码(better c++ 14 and than)</h2><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">STDHPP</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">STDHPP</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">compiler_directive</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">GCC <span class="token function">optimize</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">GCC <span class="token function">optimize</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">GCC <span class="token function">optimize</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> </span><span class="token string">"ofast"</span><span class="token expression"><span class="token punctuation">,</span> </span><span class="token string">"inline"</span><span class="token expression"><span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">headfile</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><algorithm></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><bitset></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cassert></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cmath></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstdio></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstdlib></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstring></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><ctime></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iomanip></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><limits></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><map></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><queue></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><set></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stack></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><vector></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><unordered_set></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><regex></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">macro_definition</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">_SILENCE_CXX20_CISO646_REMOED_WARNING</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">fspr</span><span class="token expression"><span class="token punctuation">(</span>n<span class="token punctuation">)</span> fixed <span class="token operator"><<</span> <span class="token function">setprecision</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">spr</span><span class="token expression"><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token function">setprecision</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">sci</span> <span class="token expression"><span class="token function">setiosflags</span><span class="token punctuation">(</span>ios<span class="token double-colon punctuation">::</span>scientific<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">siosf</span> <span class="token expression">setiosflags</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">fspr</span><span class="token expression"><span class="token punctuation">(</span>n<span class="token punctuation">)</span> fixed <span class="token operator"><<</span> <span class="token function">setprecision</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">spr</span><span class="token expression"><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token function">setprecision</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">sci</span> <span class="token expression"><span class="token function">setiosflags</span><span class="token punctuation">(</span>ios<span class="token double-colon punctuation">::</span>scientific<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">siosf</span> <span class="token expression">setiosflags</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">ifor</span><span class="token expression"><span class="token punctuation">(</span>i<span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">;</span> i <span class="token operator"><=</span> r<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">rfor</span><span class="token expression"><span class="token punctuation">(</span>i<span class="token punctuation">,</span> r<span class="token punctuation">,</span> l<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> r<span class="token punctuation">;</span> i <span class="token operator">>=</span> l<span class="token punctuation">;</span> <span class="token operator">--</span>i<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">npos</span> <span class="token expression">string<span class="token double-colon punctuation">::</span>npos</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">fir</span> <span class="token expression">first</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">sec</span> <span class="token expression">second</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">pb</span> <span class="token expression">push_back</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">pf</span> <span class="token expression">push_front</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">inlast</span><span class="token expression"><span class="token punctuation">(</span>a<span class="token punctuation">)</span> a<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">itlast</span><span class="token expression"><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token function">prev</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">all</span><span class="token expression"><span class="token punctuation">(</span>v<span class="token punctuation">)</span> v<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> v<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">usenamespace</span></span><span class="token comment">// using namespace std;</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">type_alias</span></span><span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">long</span> i64<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">double</span> f80<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">unsigned</span> <span class="token keyword">int</span> u32<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">unsigned</span> <span class="token keyword">long</span> <span class="token keyword">long</span> u64<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">unsigned</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ull<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">double</span> doubleL<span class="token punctuation">;</span><span class="token keyword">template</span><span class="token operator"><</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token punctuation">,</span> <span class="token keyword">class</span> <span class="token class-name">Container</span> <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span>vector<span class="token operator"><</span>T<span class="token operator">></span><span class="token punctuation">,</span> <span class="token keyword">class</span> <span class="token class-name">Compare</span> <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span>less<span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">Container</span><span class="token double-colon punctuation">::</span>value_type<span class="token operator">>></span><span class="token keyword">using</span> priqueue <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span>priority_queue<span class="token operator"><</span>T<span class="token punctuation">,</span> Container<span class="token punctuation">,</span> Compare<span class="token operator">></span><span class="token punctuation">;</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">customized_namespace</span></span><span class="token keyword">namespace</span> cus <span class="token punctuation">{</span> <span class="token comment">// standard output</span> <span class="token keyword">template</span><span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">container</span><span class="token operator">></span> <span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span>container<span class="token operator">&</span>a2<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> i <span class="token operator">=</span> a2<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> a2<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token operator">*</span>i <span class="token operator"><<</span> <span class="token string">" \n"</span><span class="token punctuation">[</span>i <span class="token operator">==</span> <span class="token function">prev</span><span class="token punctuation">(</span>a2<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</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 punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">template</span><span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">T1</span><span class="token operator">></span> <span class="token keyword">auto</span> <span class="token function">print</span><span class="token punctuation">(</span>T1 a<span class="token punctuation">,</span> i64 l<span class="token punctuation">,</span> i64 r<span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token keyword">void</span> <span class="token punctuation">{</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">long</span> i <span class="token operator">=</span> l<span class="token punctuation">;</span> i <span class="token operator"><=</span> r<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator"><<</span> <span class="token string">" /n"</span><span class="token punctuation">[</span>i <span class="token operator">==</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">//__int128 write</span> <span class="token keyword">template</span><span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">></span> <span class="token keyword">void</span> <span class="token function">read</span><span class="token punctuation">(</span>T<span class="token operator">&</span>w<span class="token punctuation">)</span> <span class="token punctuation">{</span> w <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> T f <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token keyword">char</span> ch <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>ch <span class="token operator"><</span> <span class="token char">'0'</span> <span class="token operator">||</span> ch <span class="token operator">></span> <span class="token char">'9'</span><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 char">'-'</span><span class="token punctuation">)</span> f <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span> ch <span class="token operator">=</span> <span class="token function">getchar</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">while</span> <span class="token punctuation">(</span>ch <span class="token operator"><=</span> <span class="token char">'9'</span> <span class="token operator">&&</span> ch <span class="token operator">>=</span> <span class="token char">'0'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> w <span class="token operator">=</span> w <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> ch <span class="token operator">-</span> <span class="token char">'0'</span><span class="token punctuation">;</span> ch <span class="token operator">=</span> <span class="token function">getchar</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">template</span><span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">></span> <span class="token keyword">void</span> <span class="token function">print128</span><span class="token punctuation">(</span>T x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <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">putchar</span><span class="token punctuation">(</span><span class="token char">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> x <span class="token operator">=</span> <span class="token operator">-</span>x<span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">></span> <span class="token number">9</span><span class="token punctuation">)</span> <span class="token function">print128</span><span class="token punctuation">(</span>x <span class="token operator">/</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">putchar</span><span class="token punctuation">(</span>x <span class="token operator">%</span> <span class="token number">10</span> <span class="token operator">+</span> <span class="token char">'0'</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">// namespace cus</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">constant</span></span><span class="token keyword">constexpr</span> <span class="token keyword">int</span> I_INF <span class="token operator">=</span> <span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span><span class="token keyword">constexpr</span> <span class="token keyword">long</span> L_INF <span class="token operator">=</span> <span class="token number">0x3f3f3f3f3f3f3f3f</span><span class="token punctuation">;</span><span class="token keyword">constexpr</span> <span class="token keyword">double</span> EPS <span class="token operator">=</span> <span class="token number">1.0e-9</span><span class="token punctuation">;</span><span class="token keyword">constexpr</span> <span class="token keyword">long</span> MOD <span class="token operator">=</span> <span class="token number">1e9</span> <span class="token operator">+</span> <span class="token number">7</span><span class="token punctuation">;</span><span class="token keyword">constexpr</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">1e2</span> <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">;</span><span class="token keyword">constexpr</span> <span class="token keyword">double</span> d_INF <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span><span class="token class-name">numeric_limits</span><span class="token operator"><</span><span class="token keyword">double</span><span class="token operator">></span><span class="token double-colon punctuation">::</span><span class="token function">infinity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">constexpr</span> <span class="token keyword">long</span> <span class="token keyword">long</span> LL_INF <span class="token operator">=</span> std<span class="token double-colon punctuation">::</span><span class="token class-name">numeric_limits</span><span class="token operator"><</span>i64<span class="token operator">></span><span class="token double-colon punctuation">::</span><span class="token function">infinity</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">global_variable</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">class</span> <span class="token class-name">UnionFind</span> <span class="token punctuation">{</span><span class="token keyword">private</span><span class="token operator">:</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> parent<span class="token punctuation">;</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> rank<span class="token punctuation">;</span><span class="token keyword">public</span><span class="token operator">:</span> <span class="token function">UnionFind</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token punctuation">{</span> parent<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> rank<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span>n <span class="token operator">+</span> <span class="token number">1</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 keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> parent<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> i<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 路径压缩,将根节点下的所有节点都变成兄弟节点两层</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>parent<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">!=</span> x<span class="token punctuation">)</span> <span class="token punctuation">{</span> parent<span class="token punctuation">[</span>x<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>parent<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> parent<span class="token punctuation">[</span>x<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">void</span> <span class="token function">unite</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> rootX <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>x<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> rootY <span class="token operator">=</span> <span class="token function">find</span><span class="token punctuation">(</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>rootX <span class="token operator">!=</span> rootY<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>rank<span class="token punctuation">[</span>rootX<span class="token punctuation">]</span> <span class="token operator"><</span> rank<span class="token punctuation">[</span>rootY<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> parent<span class="token punctuation">[</span>rootX<span class="token punctuation">]</span> <span class="token operator">=</span> rootY<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>rank<span class="token punctuation">[</span>rootX<span class="token punctuation">]</span> <span class="token operator">></span> rank<span class="token punctuation">[</span>rootY<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> parent<span class="token punctuation">[</span>rootY<span class="token punctuation">]</span> <span class="token operator">=</span> rootX<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> parent<span class="token punctuation">[</span>rootY<span class="token punctuation">]</span> <span class="token operator">=</span> rootX<span class="token punctuation">;</span> rank<span class="token punctuation">[</span>rootX<span class="token punctuation">]</span><span class="token operator">++</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 punctuation">;</span><span class="token keyword">bool</span> <span class="token function">isConnected</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">const</span> vector<span class="token operator"><</span>pair<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">>></span><span class="token operator">&</span>edges<span class="token punctuation">)</span> <span class="token punctuation">{</span> UnionFind <span class="token function">uf</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> edges<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <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 punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> a <span class="token operator">=</span> edges<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>first<span class="token punctuation">;</span> <span class="token keyword">int</span> b <span class="token operator">=</span> edges<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>second<span class="token punctuation">;</span> uf<span class="token punctuation">.</span><span class="token function">unite</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> root <span class="token operator">=</span> uf<span class="token punctuation">.</span><span class="token function">find</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">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>uf<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span> <span class="token operator">!=</span> root<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span><span class="token punctuation">}</span>vector<span class="token operator"><</span>pair<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">>></span> edges<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> n<span class="token punctuation">,</span> m<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>cin <span class="token operator">>></span> n <span class="token operator">>></span> m<span class="token punctuation">)</span> <span class="token punctuation">{</span> edges<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span>m <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">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator"><=</span> m<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> cin <span class="token operator">>></span> edges<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>first <span class="token operator">>></span> edges<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>second<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">bool</span> connected <span class="token operator">=</span> <span class="token function">isConnected</span><span class="token punctuation">(</span>n<span class="token punctuation">,</span> edges<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>connected<span class="token punctuation">)</span> <span class="token punctuation">{</span> cout <span class="token operator"><<</span> <span class="token string">"Yes"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> cout <span class="token operator"><<</span> <span class="token string">"No"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token punctuation">}</span> edges<span class="token punctuation">.</span><span class="token function">clear</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">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> OJ </category>
</categories>
<tags>
<tag> 图论 </tag>
<tag> 连通性 </tag>
<tag> 并查集 </tag>
</tags>
</entry>
<entry>
<title>frp实现内网穿透</title>
<link href="/2023/11/29/frp-shi-xian-nei-wang-chuan-tou/"/>
<url>/2023/11/29/frp-shi-xian-nei-wang-chuan-tou/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="frq实现内网穿透"><a href="#frq实现内网穿透" class="headerlink" title="frq实现内网穿透"></a>frq实现内网穿透</h1><h2 id="frq简介"><a href="#frq简介" class="headerlink" title="frq简介"></a>frq简介</h2><blockquote><p>frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议,且支持 P2P 通信。可以将内网服务以安全、便捷的方式通过具有公网 IP节点的中转暴露到公网。后端采用go,管理面板使用vue.</p><p>Github官网:<a href="https://github.com/fatedier/frp">https://github.com/fatedier/frp</a></p></blockquote><h2 id="实现原理图"><a href="#实现原理图" class="headerlink" title="实现原理图"></a>实现原理图</h2><p><img src="/2023/11/29/frp-shi-xian-nei-wang-chuan-tou/image.png" alt="Alt text"></p><h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><p>需要一台具有公共IP的服务器,以及客户端(需要穿透的电脑)</p><h2 id="服务器端-frps"><a href="#服务器端-frps" class="headerlink" title="服务器端(frps)"></a>服务器端(frps)</h2><ol><li>下载指定的发行版压缩包解压</li><li>修改<code>frps.toml</code>文件<pre class="line-numbers language-toml" data-language="toml"><code class="language-toml"><span class="token comment"># frps.toml</span><span class="token comment"># 内网映射到服务器端的端口</span><span class="token key property">bindPort</span> <span class="token punctuation">=</span> <span class="token number">7000</span> <span class="token comment">#访问客户端web服务自定义的端口号</span><span class="token key property">vhostHTTPPort</span> <span class="token punctuation">=</span> <span class="token number">5173</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li>运行<code>frp</code>服务<pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">./frps -c ./frps.toml<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li></ol><h2 id="客户端-frpc"><a href="#客户端-frpc" class="headerlink" title="客户端(frpc)"></a>客户端(frpc)</h2><ol><li>下载指定的发行版压缩包解压</li><li>修改<code>frpc.toml</code>文件<pre class="line-numbers language-toml" data-language="toml"><code class="language-toml"><span class="token key property">serverAddr</span> <span class="token punctuation">=</span> <span class="token string">"47.97.170.198"</span><span class="token key property">serverPort</span> <span class="token punctuation">=</span> <span class="token number">7000</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token table class-name">proxies</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token key property">name</span> <span class="token punctuation">=</span> <span class="token string">"HoGo Music"</span><span class="token key property">type</span> <span class="token punctuation">=</span> <span class="token string">"http"</span><span class="token comment"># localIP = "127.0.0.1"</span><span class="token comment"># 绑定的域名,需要云服务器配置过域名解析</span><span class="token key property">customDomains</span> <span class="token punctuation">=</span> <span class="token punctuation">[</span><span class="token string">"cscoder.top"</span><span class="token punctuation">]</span><span class="token comment"># 本地web应用端口</span><span class="token key property">localPort</span> <span class="token punctuation">=</span> <span class="token number">5173</span><span class="token comment"># remotePort = 6000</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li>启动<code>frpc</code>服务<pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">./frpc -c ./frpc.toml<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li></ol>]]></content>
<categories>
<category> 内网穿透 </category>
</categories>
<tags>
<tag> frp </tag>
</tags>
</entry>
<entry>
<title>scanf高级用法</title>
<link href="/2023/11/22/scanf-gao-ji-yong-fa/"/>
<url>/2023/11/22/scanf-gao-ji-yong-fa/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="题目详情-7-4-查找书籍-pintia-cn"><a href="#题目详情-7-4-查找书籍-pintia-cn" class="headerlink" title="题目详情 - 7-4 查找书籍 (pintia.cn)"></a><a href="https://pintia.cn/problem-sets/1602920136007483392/exam/problems/1602920196917161989">题目详情 - 7-4 查找书籍 (pintia.cn)</a></h1><p> 给定n本书的名称和定价,本题要求编写程序,查找并输出其中定价最高和最低的书的名称和定价。</p><h1 id="输入格式"><a href="#输入格式" class="headerlink" title="输入格式"></a>输入格式</h1><p> 输入第一行给出正整数n(<10),随后给出n本书的信息。每本书在一行中给出书名,即长度不超过30的字符串,随后一行中给出正实数价格。题目保证没有同样价格的书。</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token number">3</span>Programming in C<span class="token number">21.5</span>Programming in VB<span class="token number">18.5</span>Programming in Delphi<span class="token number">25.0</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="输出格式"><a href="#输出格式" class="headerlink" title="输出格式"></a>输出格式</h3><pre class="line-numbers language-javascipt" data-language="javascipt"><code class="language-javascipt">25.00, Programming in Delphi18.50, Programming in VB<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><blockquote><p><strong>核心问题</strong></p></blockquote><p> 如何读取含有空格的字符串</p><blockquote><p> <strong>解决方法</strong></p></blockquote><p> 使用scanf函数的高级用法</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%[^\n]"</span><span class="token punctuation">,</span>s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%lf"</span><span class="token punctuation">,</span><span class="token operator">&</span>jia<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//需要注意的是一定要清除缓冲区的换行字符啊</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h1 id="AC代码"><a href="#AC代码" class="headerlink" title="AC代码"></a>AC代码</h1><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stdio.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><math.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stdlib.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><string.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><math.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><ctype.h></span></span><span class="token comment">// #pragma GCC optimize(1)</span><span class="token comment">// #pragma GCC optimize(2)</span><span class="token comment">// #pragma GCC optimize(3,"ofast","inline")</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">ifor</span><span class="token expression"><span class="token punctuation">(</span>i<span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">;</span> i <span class="token operator"><=</span> r<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">rfor</span><span class="token expression"><span class="token punctuation">(</span>i<span class="token punctuation">,</span> r<span class="token punctuation">,</span> l<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> r<span class="token punctuation">;</span> i <span class="token operator">>=</span> l<span class="token punctuation">;</span> <span class="token operator">--</span>i<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">_SILENCE_CXX20_CISO646_REMOED_WARNING</span></span><span class="token keyword">typedef</span> <span class="token keyword">unsigned</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ull<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">double</span> doubleL<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ll<span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">long</span> <span class="token keyword">long</span> N <span class="token operator">=</span> <span class="token number">2e+5</span><span class="token punctuation">;</span><span class="token keyword">char</span> a<span class="token punctuation">[</span><span class="token number">1000</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">10000</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token class-name">stu</span><span class="token punctuation">{</span> <span class="token keyword">char</span> a<span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">char</span> b<span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> grade<span class="token punctuation">;</span><span class="token punctuation">}</span> stu<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> args<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">int</span> n<span class="token punctuation">;</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%d"</span><span class="token punctuation">,</span> <span class="token operator">&</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//一定要注意清除换行符号,不然打死这个题都要错</span> <span class="token keyword">char</span> s<span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">double</span> jia<span class="token punctuation">,</span>xiao<span class="token operator">=</span><span class="token number">35125</span><span class="token punctuation">,</span>da<span class="token operator">=</span><span class="token operator">-</span><span class="token number">413</span><span class="token punctuation">;</span> <span class="token keyword">int</span> p1<span class="token punctuation">,</span>p2<span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>i<span class="token operator"><=</span>n<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%[^\n]"</span><span class="token punctuation">,</span>s<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">"%lf"</span><span class="token punctuation">,</span><span class="token operator">&</span>jia<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">getchar</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>n<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">printf</span><span class="token punctuation">(</span><span class="token string">"%.2lf, %s\n"</span><span class="token punctuation">,</span>jia<span class="token punctuation">,</span>s<span class="token punctuation">[</span>i<span class="token punctuation">]</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">"%.2lf, %s"</span><span class="token punctuation">,</span>jia<span class="token punctuation">,</span>s<span class="token punctuation">[</span>i<span class="token punctuation">]</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 keyword">if</span><span class="token punctuation">(</span>jia<span class="token operator"><</span>xiao<span class="token punctuation">)</span> <span class="token punctuation">{</span> xiao<span class="token operator">=</span>jia<span class="token punctuation">;</span> p1<span class="token operator">=</span>i<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span>jia<span class="token operator">></span>da<span class="token punctuation">)</span> <span class="token punctuation">{</span> da<span class="token operator">=</span>jia<span class="token punctuation">;</span> p2<span class="token operator">=</span>i<span class="token punctuation">;</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">"%.2f, %s\n"</span><span class="token punctuation">,</span>da<span class="token punctuation">,</span>s<span class="token punctuation">[</span>p2<span class="token punctuation">]</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">"%.2f, %s"</span><span class="token punctuation">,</span>xiao<span class="token punctuation">,</span>s<span class="token punctuation">[</span>p1<span class="token punctuation">]</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 aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> C </category>
</categories>
<tags>
<tag> OJ </tag>
<tag> C </tag>
</tags>
</entry>
<entry>
<title>Typescirpt基础类型</title>
<link href="/2023/11/22/typescirpt-ji-chu-lei-xing/"/>
<url>/2023/11/22/typescirpt-ji-chu-lei-xing/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Typescript类型总结"><a href="#Typescript类型总结" class="headerlink" title="Typescript类型总结"></a>Typescript类型总结</h1><h2 id="ts文件导入vue组件报错"><a href="#ts文件导入vue组件报错" class="headerlink" title="ts文件导入vue组件报错"></a>ts文件导入vue组件报错</h2><blockquote><p>报错信息:<code>Cannot find module './App.vue' or its corresponding type declarations.ts(2307),</code></p></blockquote><p>在TypeScript文件中导入Vue组件时,需要额外的配置才能让TypeScript识别Vue文件。这是因为TypeScript默认不知道如何处理Vue文件。以下是一些可能会帮助解决此问题的步骤:</p><ol><li><p>在项目根目录中创建一个名为 <code>shims-vue.d.ts</code> 的新文件。该文件应包含以下代码:</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">declare</span> <span class="token keyword">module</span> <span class="token string">"*.vue"</span> <span class="token punctuation">{</span> <span class="token keyword">import</span> <span class="token punctuation">{</span> defineComponent <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">"vue"</span><span class="token punctuation">;</span> <span class="token keyword">const</span> component<span class="token operator">:</span> ReturnType<span class="token operator"><</span><span class="token keyword">typeof</span> defineComponent<span class="token operator">></span><span class="token punctuation">;</span> <span class="token keyword">export</span> <span class="token keyword">default</span> component<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>在 <code>tsconfig.json</code> 文件中添加以下选项:</p><pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">{</span> <span class="token property">"compilerOptions"</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token property">"module"</span><span class="token operator">:</span> <span class="token string">"ESNext"</span><span class="token punctuation">,</span> <span class="token property">"target"</span><span class="token operator">:</span> <span class="token string">"ESNext"</span><span class="token punctuation">,</span> <span class="token property">"moduleResolution"</span><span class="token operator">:</span> <span class="token string">"Node"</span><span class="token punctuation">,</span> <span class="token property">"esModuleInterop"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token property">"resolveJsonModule"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token property">"noImplicitAny"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token property">"strict"</span><span class="token operator">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token property">"skipLibCheck"</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token property">"forceConsistentCasingInFileNames"</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token property">"include"</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"src/**/*"</span><span class="token punctuation">,</span> <span class="token string">"shims-vue.d.ts"</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>确保您的 <code>vue</code> 和 <code>@vue/compiler-sfc</code> 包已正确安装,并且已在 <code>package.json</code> 中添加了相应的依赖项。</p></li></ol><p>执行以上步骤后,TypeScript将能够正确地识别导入的Vue组件。</p><h2 id="TypeScript-支持以下数据类型:"><a href="#TypeScript-支持以下数据类型:" class="headerlink" title="TypeScript 支持以下数据类型:"></a>TypeScript 支持以下数据类型:</h2><h2 id="布尔类型"><a href="#布尔类型" class="headerlink" title="布尔类型"></a>布尔类型</h2><p>布尔类型表示 true 或 false 值。</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> isDone<span class="token operator">:</span> <span class="token builtin">boolean</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h2 id="数字类型"><a href="#数字类型" class="headerlink" title="数字类型"></a>数字类型</h2><p>数字类型可以是整数或浮点数。</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> decimal<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">6</span><span class="token punctuation">;</span><span class="token keyword">let</span> hex<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">0xf00d</span><span class="token punctuation">;</span><span class="token keyword">let</span> binary<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">0b1010</span><span class="token punctuation">;</span><span class="token keyword">let</span> octal<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token number">0o744</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h2 id="字符串类型"><a href="#字符串类型" class="headerlink" title="字符串类型"></a>字符串类型</h2><p>字符串类型用于表示文本数据,可以使用单引号或双引号来定义字符串。</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> color<span class="token operator">:</span> <span class="token builtin">string</span> <span class="token operator">=</span> <span class="token string">"blue"</span><span class="token punctuation">;</span>color <span class="token operator">=</span> <span class="token string">'red'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h2 id="数组类型"><a href="#数组类型" class="headerlink" title="数组类型"></a>数组类型</h2><p>数组类型用于表示同一类型的元素的集合。可以使用[]或Array<type>来定义数组。</type></p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> list<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <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 number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token keyword">let</span> list<span class="token operator">:</span> <span class="token builtin">Array</span><span class="token operator"><</span><span class="token builtin">number</span><span class="token operator">></span> <span class="token operator">=</span> <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 number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h2 id="元组类型"><a href="#元组类型" class="headerlink" title="元组类型"></a>元组类型</h2><p>元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同。</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> x<span class="token operator">:</span> <span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token builtin">number</span><span class="token punctuation">]</span><span class="token punctuation">;</span>x <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">"hello"</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h2 id="枚举类型"><a href="#枚举类型" class="headerlink" title="枚举类型"></a>枚举类型</h2><p>枚举类型用于定义有命名的常量集合。</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">enum</span> Color <span class="token punctuation">{</span>Red<span class="token punctuation">,</span> Green<span class="token punctuation">,</span> Blue<span class="token punctuation">}</span><span class="token keyword">let</span> c<span class="token operator">:</span> Color <span class="token operator">=</span> Color<span class="token punctuation">.</span>Green<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h2 id="Any-类型"><a href="#Any-类型" class="headerlink" title="Any 类型"></a>Any 类型</h2><p>Any 类型用于表示不确定的类型,允许在编译时可选择地包括或排除类型检查。</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> notSure<span class="token operator">:</span> <span class="token builtin">any</span> <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span>notSure <span class="token operator">=</span> <span class="token string">"maybe a string instead"</span><span class="token punctuation">;</span>notSure <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h2 id="Void-类型"><a href="#Void-类型" class="headerlink" title="Void 类型"></a>Void 类型</h2><p>Void 类型表示没有类型,通常用于函数没有返回值的情况。</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">function</span> <span class="token function">warnUser</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">{</span> <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"This is my warning message"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h2 id="Null-和-Undefined-类型"><a href="#Null-和-Undefined-类型" class="headerlink" title="Null 和 Undefined 类型"></a>Null 和 Undefined 类型</h2><p>Null 和 Undefined 类型分别具有 null 和 undefined 值。</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> u<span class="token operator">:</span> <span class="token keyword">undefined</span> <span class="token operator">=</span> <span class="token keyword">undefined</span><span class="token punctuation">;</span><span class="token keyword">let</span> n<span class="token operator">:</span> <span class="token keyword">null</span> <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h2 id="Never-类型"><a href="#Never-类型" class="headerlink" title="Never 类型"></a>Never 类型</h2><p>Never 类型表示不会出现的值的类型。例如,永远不会返回的函数的返回类型可以声明为 never。</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">function</span> <span class="token function">error</span><span class="token punctuation">(</span>message<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token builtin">never</span> <span class="token punctuation">{</span> <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h2 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h2><h3 id="函数对象"><a href="#函数对象" class="headerlink" title="函数对象"></a>函数对象</h3><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span>a<span class="token operator">:</span><span class="token builtin">number</span><span class="token punctuation">,</span>b<span class="token operator">:</span><span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">:</span><span class="token builtin">number</span> <span class="token punctuation">{</span><span class="token keyword">return</span> <span class="token number">7</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h3 id="匿名函数"><a href="#匿名函数" class="headerlink" title="匿名函数"></a>匿名函数</h3><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">const</span> <span class="token function-variable function">add</span><span class="token operator">:</span><span class="token punctuation">(</span>a<span class="token operator">:</span><span class="token builtin">number</span><span class="token punctuation">,</span>b<span class="token operator">:</span><span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token function-variable function">number</span><span class="token operator">=</span><span class="token punctuation">(</span>num1<span class="token punctuation">,</span>num2<span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">3</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h3 id="不定参函数"><a href="#不定参函数" class="headerlink" title="不定参函数"></a>不定参函数</h3><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">const</span> <span class="token function-variable function">add</span><span class="token operator">:</span><span class="token punctuation">(</span>a<span class="token operator">?</span><span class="token operator">:</span><span class="token builtin">number</span><span class="token punctuation">,</span>b<span class="token operator">?</span><span class="token operator">:</span><span class="token builtin">number</span><span class="token punctuation">)</span><span class="token operator">=></span><span class="token function-variable function">number</span><span class="token operator">=</span><span class="token punctuation">(</span>num1<span class="token punctuation">,</span>num2<span class="token punctuation">)</span><span class="token operator">=></span><span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token number">3</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h2 id="对象"><a href="#对象" class="headerlink" title="对象"></a>对象</h2><h3 id="包含函数的对象"><a href="#包含函数的对象" class="headerlink" title="包含函数的对象"></a>包含函数的对象</h3><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> person<span class="token operator">:</span> <span class="token punctuation">{</span> age<span class="token operator">:</span> Number<span class="token punctuation">;</span> name<span class="token operator">:</span> String<span class="token punctuation">;</span> <span class="token function">add</span><span class="token punctuation">(</span>age<span class="token operator">:</span> Number<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token punctuation">{</span> age<span class="token operator">:</span> <span class="token number">34</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">'fas'</span><span class="token punctuation">,</span> <span class="token function">add</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>或者</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> person<span class="token operator">:</span> <span class="token punctuation">{</span> age<span class="token operator">:</span> Number<span class="token punctuation">;</span> name<span class="token operator">:</span> String<span class="token punctuation">;</span> <span class="token function">add</span><span class="token punctuation">(</span>age<span class="token operator">:</span> Number<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token keyword">void</span> <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token punctuation">{</span> age<span class="token operator">:</span> <span class="token number">34</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">'fas'</span><span class="token punctuation">,</span> <span class="token function">add</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span><span class="token punctuation">}</span> <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="对象的可选属性"><a href="#对象的可选属性" class="headerlink" title="对象的可选属性"></a>对象的可选属性</h3><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> person<span class="token operator">:</span> <span class="token punctuation">{</span> age<span class="token operator">:</span> Number<span class="token punctuation">;</span> name<span class="token operator">:</span> String<span class="token operator">|</span><span class="token keyword">undefined</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token punctuation">{</span> age<span class="token operator">:</span> <span class="token number">34</span><span class="token punctuation">,</span> name<span class="token operator">:</span> <span class="token string">'fas'</span><span class="token punctuation">}</span><span class="token keyword">function</span> <span class="token function">shuchu</span><span class="token punctuation">(</span>zhi<span class="token operator">:</span><span class="token punctuation">{</span>age<span class="token operator">:</span>Number<span class="token punctuation">,</span>name1<span class="token operator">?</span><span class="token operator">:</span>String<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>zhi<span class="token punctuation">.</span>name1<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token function">shuchu</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>对象作为函数参数,需要<code>对象属性名一样</code></p></blockquote><h2 id="type类型别名"><a href="#type类型别名" class="headerlink" title="type类型别名"></a>type类型别名</h2><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">type</span> <span class="token class-name">name</span><span class="token operator">=</span><span class="token punctuation">{</span> age<span class="token operator">:</span><span class="token builtin">number</span><span class="token punctuation">,</span> a<span class="token operator">:</span><span class="token builtin">string</span><span class="token punctuation">}</span><span class="token keyword">let</span> a<span class="token operator">:</span>name<span class="token operator">=</span><span class="token punctuation">{</span> age<span class="token operator">:</span><span class="token number">34</span><span class="token punctuation">,</span> a<span class="token operator">:</span><span class="token string">'wer'</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="接口-继承"><a href="#接口-继承" class="headerlink" title="接口[继承]"></a>接口[继承]</h2><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">interface</span> <span class="token class-name">name</span><span class="token punctuation">{</span> age<span class="token operator">:</span><span class="token builtin">number</span><span class="token punctuation">,</span> a<span class="token operator">:</span><span class="token builtin">string</span><span class="token punctuation">}</span><span class="token keyword">interface</span> <span class="token class-name">name2</span> <span class="token keyword">extends</span> <span class="token class-name">name</span><span class="token punctuation">{</span> z<span class="token operator">:</span><span class="token builtin">string</span><span class="token punctuation">}</span><span class="token keyword">let</span> a<span class="token operator">:</span>name2<span class="token operator">=</span><span class="token punctuation">{</span> age<span class="token operator">:</span><span class="token number">34</span><span class="token punctuation">,</span> a<span class="token operator">:</span><span class="token string">'wer'</span><span class="token punctuation">,</span> z<span class="token operator">:</span><span class="token string">'fas'</span><span class="token punctuation">}</span><span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h2><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> a<span class="token operator">:</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">,</span><span class="token builtin">number</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'fasd'</span><span class="token punctuation">,</span><span class="token number">324</span><span class="token punctuation">]</span><span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h2 id="类型断言"><a href="#类型断言" class="headerlink" title="类型断言"></a>类型断言</h2><h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p>TypeScript类型断言可以让开发者在编写代码时手动指定一个值的类型。在需要使用类型断言的地方,可以使用尖括号(<>)语法或as语法来进行声明。以下是两种类型断言的示例:</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token comment">// 尖括号语法</span><span class="token keyword">let</span> myVariable<span class="token operator">:</span> <span class="token builtin">any</span> <span class="token operator">=</span> <span class="token string">"this is a string"</span><span class="token punctuation">;</span><span class="token keyword">let</span> strLength<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token operator"><</span><span class="token builtin">string</span><span class="token operator">></span>myVariable<span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span><span class="token comment">// as 语法</span><span class="token keyword">let</span> myVariable<span class="token operator">:</span> <span class="token builtin">any</span> <span class="token operator">=</span> <span class="token string">"this is a string"</span><span class="token punctuation">;</span><span class="token keyword">let</span> strLength<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token operator">=</span> <span class="token punctuation">(</span>myVariable <span class="token keyword">as</span> <span class="token builtin">string</span><span class="token punctuation">)</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>在这里,我们将任意类型<code>myVariable</code>转换为一个字符串类型,并且使用它的长度来声明另一个变量<code>strLength</code>,这需要使用类型断言。</p><p>尖括号和as语法都可以做到这一点。当然,在某些情况下,TypeScript也会自动推断类型,因此不必使用类型断言。</p><h3 id="html标签类型"><a href="#html标签类型" class="headerlink" title="html标签类型"></a>html标签类型</h3><p>HTML标签对应的TypeScript类型是<code>HTMLElement</code>或者它的子接口,具体取决于使用哪个HTML元素。如果是input元素,则对应类型为<code>HTMLInputElement</code>,如果是button元素,则对应类型为<code>HTMLButtonElement</code>。此外,还可以使用通用类型<code>Element</code>表示任何HTML元素。</p><h3 id="vue组件类型"><a href="#vue组件类型" class="headerlink" title="vue组件类型"></a>vue组件类型</h3><p>在Vue开发中,可以使用 <code>Vue.extend</code> 函数来创建组件,并且为了让TypeScript能够识别到组件的类型,需要定义一个接口来描述组件的选项对象。通常情况下,这个类型接口的名称与组件的名称相同。</p><p>例如,定义一个名为 <code>MyComponent</code> 的组件,它的类型接口应该像这样:</p><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token keyword">interface</span> <span class="token class-name">MyComponent</span> <span class="token keyword">extends</span> <span class="token class-name">Vue</span> <span class="token punctuation">{</span> <span class="token comment">// 组件选项</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>这里的 <code>MyComponent</code> 类型继承自 <code>Vue</code> 类型,表示这是一个Vue组件,并且还可以包含组件的选项,如 <code>props</code>、<code>data</code>、<code>methods</code> 等等。这样定义后,就可以将 <code>MyComponent</code> 作为一个泛型参数传递给 <code>Vue.extend</code> 方法,以便创建组件实例时进行类型检查。</p><h3 id="xx-d-ts-解决ts无法识别vue组件"><a href="#xx-d-ts-解决ts无法识别vue组件" class="headerlink" title="xx.d.ts:解决ts无法识别vue组件"></a>xx.d.ts:解决ts无法识别vue组件</h3><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">declare</span> <span class="token keyword">module</span> <span class="token string">"*.vue"</span> <span class="token punctuation">{</span> <span class="token keyword">import</span> <span class="token punctuation">{</span> defineComponent <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">"vue"</span><span class="token punctuation">;</span> <span class="token keyword">const</span> component<span class="token operator">:</span> ReturnType<span class="token operator"><</span><span class="token keyword">typeof</span> defineComponent<span class="token operator">></span><span class="token punctuation">;</span> <span class="token keyword">export</span> <span class="token keyword">default</span> component<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>这段代码是一个TypeScript语法的声明文件,它告诉编译器如何处理<code>.vue</code>文件,在这里我们使用了模块扩展声明。</p><p>具体来说:</p><ol><li><code>declare module "*.vue"</code> 声明了我们将要扩展的模块类型,也就是指定了所有以<code>.vue</code>结尾的文件都可以通过这个模块名来引用。</li><li><code>import { defineComponent } from "vue";</code> 引入所需的依赖,这里我们需要Vue.js的<code>defineComponent</code>函数。</li><li><code>const component: ReturnType<typeof defineComponent>;</code> 创建一个组件对象的常量,类型为<code>defineComponent</code>返回值的类型。这里的<code>ReturnType</code>工具类型可以获取<code>defineComponent</code>函数的返回类型,也就是组件选项对象的类型。</li><li><code>export default component;</code> 将组件对象作为默认导出,使得其他地方可以通过这个模块名来引用组件。</li></ol><p>这个声明文件的主要作用是为了让TypeScript可以正确地处理<code>.vue</code>文件,并且提供了类型提示和检查,有助于代码的可维护性和可读性。</p><h2 id="字面量类型"><a href="#字面量类型" class="headerlink" title="字面量类型"></a>字面量类型</h2><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token keyword">let</span> a<span class="token operator">:</span><span class="token punctuation">(</span><span class="token string">'f'</span><span class="token operator">|</span><span class="token string">'a'</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token string">'f'</span><span class="token punctuation">;</span><span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>f<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>字符只能是’f,’a’</p>]]></content>
<categories>
<category> Typescirpt </category>
</categories>
<tags>
<tag> Typescript </tag>
</tags>
</entry>
<entry>
<title>C语言有名管道实现进程的通信</title>
<link href="/2023/11/22/c-yu-yan-you-ming-guan-dao-shi-xian-jin-cheng-de-tong-xin/"/>
<url>/2023/11/22/c-yu-yan-you-ming-guan-dao-shi-xian-jin-cheng-de-tong-xin/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="非亲属进程之间的通信通过有名管道来实现"><a href="#非亲属进程之间的通信通过有名管道来实现" class="headerlink" title="非亲属进程之间的通信通过有名管道来实现"></a>非亲属进程之间的通信通过有名管道来实现</h1><ul><li>接受消息<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><fstream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstring></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><sys/types.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><sys/stat.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><unistd.h></span></span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">// 创建命名管道</span> <span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span> pipeName <span class="token operator">=</span> <span class="token string">"/tmp/pipe1"</span><span class="token punctuation">;</span> std<span class="token double-colon punctuation">::</span>ifstream <span class="token function">pipeIn</span><span class="token punctuation">(</span>pipeName<span class="token punctuation">,</span>std<span class="token double-colon punctuation">::</span>ios<span class="token double-colon punctuation">::</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 接受消息</span> std<span class="token double-colon punctuation">::</span>string mes<span class="token punctuation">;</span> std<span class="token double-colon punctuation">::</span><span class="token function">getline</span><span class="token punctuation">(</span>pipeIn<span class="token punctuation">,</span>mes<span class="token punctuation">)</span><span class="token punctuation">;</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"Received message in Process 2:"</span><span class="token operator"><<</span>mes<span class="token operator"><<</span>std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span> pipeIn<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 删除命名管道 </span> <span class="token function">unlink</span><span class="token punctuation">(</span>pipeName<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 operator">-</span> 发送消息的进程```cpp<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><fstream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><sys/stat.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><unistd.h></span></span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span> pipeName<span class="token operator">=</span><span class="token string">"/pipe1"</span><span class="token punctuation">;</span> <span class="token function">mkfifo</span><span class="token punctuation">(</span>pipeName<span class="token punctuation">,</span><span class="token number">0666</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 命名管道路径,后面代表用户权限</span> std<span class="token double-colon punctuation">::</span>ofstream <span class="token function">pipeOut</span><span class="token punctuation">(</span>pipeName<span class="token punctuation">,</span>std<span class="token double-colon punctuation">::</span>ios<span class="token double-colon punctuation">::</span>out<span class="token punctuation">)</span><span class="token punctuation">;</span> std<span class="token double-colon punctuation">::</span>string mes<span class="token punctuation">;</span> mes<span class="token operator">=</span><span class="token string">"hello from progreess 1"</span><span class="token punctuation">;</span> pipeOut<span class="token operator"><<</span>mes<span class="token punctuation">;</span> pipeOUt<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 删除命名管道</span> <span class="token function">unlink</span><span class="token punctuation">(</span>pipeName<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 aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>注意:命名管道是Linux下一种基于文件的通信方式,必须要路径存在于文件系统,且当前用户有权限在路径目录下执行写文件操作<br>命名管道是一种阻塞式的通信机制。如果没有数据可读,读取操作会被阻塞,直到有数据写入到管道中。类似地,如果管道已满,写入操作也会被阻塞,直到有空间可用。因此,在实际使用中,可能需要使用多线程或非阻塞 I/O 来处理管道通信,</li></ul>]]></content>
<categories>
<category> 进程通信 </category>
<category> C </category>
</categories>
<tags>
<tag> C </tag>
<tag> 有名管道 </tag>
</tags>
</entry>
<entry>
<title>C语言通过无名管道实现进程通信</title>
<link href="/2023/11/22/c-yu-yan-tong-guo-wu-ming-guan-dao-shi-xian-jin-cheng-tong-xin/"/>
<url>/2023/11/22/c-yu-yan-tong-guo-wu-ming-guan-dao-shi-xian-jin-cheng-tong-xin/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="实验1:编写一个简单的程序实现管道的创建与读写方法"><a href="#实验1:编写一个简单的程序实现管道的创建与读写方法" class="headerlink" title="实验1:编写一个简单的程序实现管道的创建与读写方法"></a>实验1:编写一个简单的程序实现管道的创建与读写方法</h1><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cassert></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><fstream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><string></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><sys/wait.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><unistd.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">BUFFERSIZE</span> <span class="token expression"><span class="token number">257</span></span></span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span>argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> pipefd<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">char</span> buffer<span class="token punctuation">[</span>BUFFERSIZE<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 存子进程写入的消息</span> <span class="token comment">// 父进程创建匿名管道</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">pipe</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">)</span> <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> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"管道创建错误\n"</span><span class="token punctuation">;</span> <span class="token function">exit</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 comment">//创建子进程</span> pid_t p1 <span class="token operator">=</span> <span class="token function">fork</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>p1 <span class="token operator"><</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"子进程创建失败"</span> <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span> <span class="token function">exit</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 punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>p1 <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 子进程负责写,关闭读取管道</span> <span class="token function">close</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">char</span> <span class="token operator">*</span>mes <span class="token operator">=</span> <span class="token string">"hello,friend"</span><span class="token punctuation">;</span> <span class="token function">write</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> mes<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>mes<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 function">close</span><span class="token punctuation">(</span>pipefd<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 punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 父进程负责等待子进程完成</span> <span class="token function">waitpid</span><span class="token punctuation">(</span>p1<span class="token punctuation">,</span> <span class="token keyword">nullptr</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">// 父进程负责读,关闭写管道</span> <span class="token function">close</span><span class="token punctuation">(</span>pipefd<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 function">read</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> buffer<span class="token punctuation">,</span> BUFFERSIZE<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">close</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> buffer <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<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 aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>pipefd[2] 是一个整型数组,它用于存储无名管道(unnamed pipe)的文件描述符。在创建无名管道时,pipe() 函数会将两个相关联的文件描述符分别存储在 pipefd[0] 和 pipefd[1] 中。</p><p>具体作用如下:</p><p>pipefd[0]:该文件描述符用于从管道中读取数据。当调用 read(pipefd[0], buffer, BUFFERSIZE) 时,将从管道中读取数据,并将其存储到 buffer 缓冲区中。<br>pipefd[1]:该文件描述符用于向管道中写入数据。当调用 write(pipefd[1], mes, strlen(mes) + 1) 时,将把 mes 字符串中的数据写入管道中。<br>通过使用这两个文件描述符,父子进程可以通过无名管道进行进程间通信。子进程可以将数据写入管道,而父进程可以从管道中读取数据,实现了简单的数据交换和共享。</p><h1 id="实验二-利用进程间通信的方式实现一个文件的逐行复制到另外一个文件-文件路径由终端参数给出,子进程读源文件-父进程写入到目标文件"><a href="#实验二-利用进程间通信的方式实现一个文件的逐行复制到另外一个文件-文件路径由终端参数给出,子进程读源文件-父进程写入到目标文件" class="headerlink" title="实验二:利用进程间通信的方式实现一个文件的逐行复制到另外一个文件,文件路径由终端参数给出,子进程读源文件,父进程写入到目标文件."></a>实验二:利用进程间通信的方式实现一个文件的逐行复制到另外一个文件,文件路径由终端参数给出,子进程读源文件,父进程写入到目标文件.</h1><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><unistd.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><sys/wait.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><fstream></span></span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span><span class="token operator">*</span> argv<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>argc <span class="token operator"><</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"请输入源文件路径和目标文件路径作为参数。"</span> <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<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">int</span> pipefd<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// 管道文件描述符数组</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">pipe</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">)</span> <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 comment">// 创建管道</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"无法创建管道。"</span> <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<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> pid_t pid <span class="token operator">=</span> <span class="token function">fork</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建子进程</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>pid <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 comment">// fork 失败</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"无法创建子进程。"</span> <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<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">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>pid <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 子进程</span> <span class="token function">close</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 关闭子进程的读取端</span> std<span class="token double-colon punctuation">::</span>ifstream <span class="token function">sourceFile</span><span class="token punctuation">(</span>argv<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 comment">// 打开源文件</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>sourceFile<span class="token punctuation">.</span><span class="token function">is_open</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"无法打开源文件。"</span> <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span> <span class="token function">close</span><span class="token punctuation">(</span>pipefd<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 comment">// 关闭子进程的写入端</span> <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> std<span class="token double-colon punctuation">::</span>string line<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span><span class="token function">getline</span><span class="token punctuation">(</span>sourceFile<span class="token punctuation">,</span> line<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// 逐行读取源文件内容</span> <span class="token function">write</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> line<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> line<span class="token punctuation">.</span><span class="token function">length</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">// 将每行内容写入管道</span> <span class="token function">write</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">"\n"</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">// 写入换行符</span> <span class="token punctuation">}</span> sourceFile<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">close</span><span class="token punctuation">(</span>pipefd<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 comment">// 关闭子进程的写入端</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 comment">// 父进程</span> <span class="token function">close</span><span class="token punctuation">(</span>pipefd<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 comment">// 关闭父进程的写入端</span> std<span class="token double-colon punctuation">::</span>ofstream <span class="token function">targetFile</span><span class="token punctuation">(</span>argv<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 comment">// 打开目标文件</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>targetFile<span class="token punctuation">.</span><span class="token function">is_open</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"无法打开目标文件。"</span> <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span> <span class="token function">close</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 关闭父进程的读取端</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">const</span> <span class="token keyword">int</span> bufferSize <span class="token operator">=</span> <span class="token number">1024</span><span class="token punctuation">;</span> <span class="token keyword">char</span> buffer<span class="token punctuation">[</span>bufferSize<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> bytesRead<span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>bytesRead <span class="token operator">=</span> <span class="token function">read</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> buffer<span class="token punctuation">,</span> bufferSize<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> <span class="token punctuation">{</span> <span class="token comment">// 从管道读取数据</span> targetFile<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>buffer<span class="token punctuation">,</span> bytesRead<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 将数据写入目标文件</span> <span class="token punctuation">}</span> targetFile<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">close</span><span class="token punctuation">(</span>pipefd<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 关闭父进程的读取端</span> <span class="token function">wait</span><span class="token punctuation">(</span><span class="token constant">NULL</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 等待子进程终止</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 punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> C </category>
<category> 进程通信 </category>
</categories>
<tags>
<tag> C </tag>
<tag> 无名管道 </tag>
</tags>
</entry>
<entry>
<title>C语言消息队列实现进程通信</title>
<link href="/2023/11/22/c-yu-yan-xiao-xi-dui-lie-shi-xian-jin-cheng-tong-xin/"/>
<url>/2023/11/22/c-yu-yan-xiao-xi-dui-lie-shi-xian-jin-cheng-tong-xin/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="通过消息队列实现进程的通信"><a href="#通过消息队列实现进程的通信" class="headerlink" title="通过消息队列实现进程的通信"></a>通过消息队列实现进程的通信</h1><h2 id="ftok和msgget创建消息队列的关键函数。"><a href="#ftok和msgget创建消息队列的关键函数。" class="headerlink" title="ftok和msgget创建消息队列的关键函数。"></a><code>ftok</code>和<code>msgget</code>创建消息队列的关键函数。</h2><ol><li><p><code>ftok</code>函数:</p><ul><li><code>ftok</code>函数用于将文件路径和一个整数标识符(通常是一个字符)转换为一个唯一的键值(key)。它的原型如下:<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">key_t <span class="token function">ftok</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>pathname<span class="token punctuation">,</span> <span class="token keyword">int</span> proj_id<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><code>pathname</code>是一个指向文件的路径的字符串。通常,你可以创建一个独立的文件,用于生成唯一的键值。</li><li><code>proj_id</code>是一个用户定义的整数标识符,用于进一步区分不同的消息队列。通常使用一个字符来表示。</li><li><code>ftok</code>函数根据<code>pathname</code>和<code>proj_id</code>生成一个唯一的键值,并返回该键值。</li></ul></li><li><p><code>msgget</code>函数:</p><ul><li><code>msgget</code>函数用于创建或获取消息队列的标识符(msgid)。它的原型如下:<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> <span class="token function">msgget</span><span class="token punctuation">(</span>key_t key<span class="token punctuation">,</span> <span class="token keyword">int</span> msgflg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><code>key</code>是通过<code>ftok</code>函数生成的唯一键值,用于识别特定的消息队列。</li><li><code>msgflg</code>是一个标志参数,用于指定消息队列的创建和访问权限。可以使用位运算符将多个标志组合在一起。常用的标志包括:<ul><li><code>IPC_CREAT</code>:如果消息队列不存在,则创建一个新的消息队列。</li><li><code>IPC_EXCL</code>:与<code>IPC_CREAT</code>一起使用,用于确保创建一个新的消息队列,如果消息队列已经存在则返回错误。</li><li>权限标志:用于指定消息队列的访问权限,例如<code>0666</code>表示所有用户都具有读写权限。</li></ul></li><li><code>msgget</code>函数返回一个用于标识消息队列的整数值(msgid)。你可以使用这个msgid来进行后续的消息发送和接收操作。</li></ul></li></ol><p>通过调用<code>ftok</code>函数生成唯一的键值,并将该键值传递给<code>msgget</code>函数,你可以创建或获取一个特定的消息队列,并获得与之关联的msgid。</p><h2 id="msgsnd、msgrcv发送和接受消息"><a href="#msgsnd、msgrcv发送和接受消息" class="headerlink" title="msgsnd、msgrcv发送和接受消息"></a><code>msgsnd</code>、<code>msgrcv</code>发送和接受消息</h2><p>当使用System V消息队列进行进程间通信时,<code>msgsnd</code>和<code>msgrcv</code>是用于发送和接收消息的关键函数。</p><ol><li><p><code>msgsnd</code>函数:</p><ul><li><code>msgsnd</code>函数用于向消息队列发送消息。它的原型如下:<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">int</span> <span class="token function">msgsnd</span><span class="token punctuation">(</span><span class="token keyword">int</span> msqid<span class="token punctuation">,</span> <span class="token keyword">const</span> <span class="token keyword">void</span> <span class="token operator">*</span>msgp<span class="token punctuation">,</span> size_t msgsz<span class="token punctuation">,</span> <span class="token keyword">int</span> msgflg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><code>msqid</code>是消息队列的标识符,即通过<code>msgget</code>函数获取到的msgid。</li><li><code>msgp</code>是一个指向消息数据的指针,通常是一个自定义的结构体指针。你可以定义一个结构体来存储消息的类型和内容等信息。</li><li><code>msgsz</code>是消息的大小,以字节为单位。你需要确保<code>msgsz</code>与实际发送的消息大小相匹配。</li><li><code>msgflg</code>是一个标志参数,用于指定发送消息的行为。常用的标志包括:<ul><li><code>IPC_NOWAIT</code>:如果消息队列已满,不要等待,立即返回错误。</li><li><code>0</code>:阻塞发送,如果消息队列已满则等待直到有空间可用。</li></ul></li><li><code>msgsnd</code>函数返回一个整数值,表示消息发送的结果。如果成功发送消息,则返回0;如果出现错误,则返回-1。</li></ul></li><li><p><code>msgrcv</code>函数:</p><ul><li><code>msgrcv</code>函数用于从消息队列接收消息。它的原型如下:<pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">ssize_t <span class="token function">msgrcv</span><span class="token punctuation">(</span><span class="token keyword">int</span> msqid<span class="token punctuation">,</span> <span class="token keyword">void</span> <span class="token operator">*</span>msgp<span class="token punctuation">,</span> size_t msgsz<span class="token punctuation">,</span> <span class="token keyword">long</span> msgtyp<span class="token punctuation">,</span> <span class="token keyword">int</span> msgflg<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><code>msqid</code>是消息队列的标识符,即通过<code>msgget</code>函数获取到的msgid。</li><li><code>msgp</code>是一个指向接收消息的缓冲区的指针,通常是一个自定义的结构体指针,用于存储接收到的消息数据。</li><li><code>msgsz</code>是接收缓冲区的大小,以字节为单位。你需要确保<code>msgsz</code>足够大以容纳接收的消息。</li><li><code>msgtyp</code>是消息类型,用于指定接收消息的条件。通常,你可以使用一个特定的值来筛选消息类型,例如,只接收特定类型的消息。</li><li><code>msgflg</code>是一个标志参数,用于指定接收消息的行为。常用的标志包括:<ul><li><code>IPC_NOWAIT</code>:如果消息队列为空,不要等待,立即返回错误。</li><li><code>0</code>:阻塞接收,如果消息队列为空则等待直到有消息可用。</li></ul></li><li><code>msgrcv</code>函数返回一个整数值,表示接收消息的结果。如果成功接收消息,则返回接收到的消息的长度;如果出现错误,则返回-1。</li></ul></li></ol><p>这两个函数提供了发送和接收消息的基本功能,你可以根据需要进行调用和处理接收到的消息。记住,发送和接收消息的进程需要拥有对消息队列的适当权限。</p><h2 id="样例"><a href="#样例" class="headerlink" title="样例"></a>样例</h2><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstring></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><sys/ipc.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><sys/msg.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><sys/types.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><unistd.h></span> <span class="token comment">// fork</span></span><span class="token comment">// 定义消息结构体</span><span class="token keyword">struct</span> <span class="token class-name">Message</span> <span class="token punctuation">{</span> <span class="token keyword">long</span> mtype<span class="token punctuation">;</span> <span class="token keyword">char</span> mtext<span class="token punctuation">[</span><span class="token number">100</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">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> key_t key<span class="token punctuation">;</span> <span class="token keyword">int</span> msgid<span class="token punctuation">;</span> Message msg<span class="token punctuation">;</span> <span class="token comment">// 生成唯一的key</span> key <span class="token operator">=</span> <span class="token function">ftok</span><span class="token punctuation">(</span><span class="token string">"message_queue_example"</span><span class="token punctuation">,</span> <span class="token number">65</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 创建消息队列</span> msgid <span class="token operator">=</span> <span class="token function">msgget</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> <span class="token number">0666</span> <span class="token operator">|</span> IPC_CREAT<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 发送消息的进程</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">fork</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> <span class="token punctuation">{</span> <span class="token comment">// 子进程发送消息</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> msg<span class="token punctuation">.</span>mtype <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> <span class="token function">strcpy</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span>mtext<span class="token punctuation">,</span> <span class="token string">"Hello from child process!"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">msgsnd</span><span class="token punctuation">(</span>msgid<span class="token punctuation">,</span> <span class="token operator">&</span>msg<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span>mtext<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 punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token comment">// 父进程接收消息</span> <span class="token function">msgrcv</span><span class="token punctuation">(</span>msgid<span class="token punctuation">,</span> <span class="token operator">&</span>msg<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span>mtext<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"Received message: "</span> <span class="token operator"><<</span> msg<span class="token punctuation">.</span>mtext <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span> <span class="token comment">// 删除消息队列</span> std<span class="token double-colon punctuation">::</span>cout <span class="token operator"><<</span> <span class="token string">"end"</span> <span class="token operator"><<</span> std<span class="token double-colon punctuation">::</span>endl<span class="token punctuation">;</span> <span class="token function">msgctl</span><span class="token punctuation">(</span>msgid<span class="token punctuation">,</span> IPC_RMID<span class="token punctuation">,</span> <span class="token keyword">nullptr</span><span class="token punctuation">)</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 aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> 进程通信 </category>
</categories>
<tags>
<tag> C </tag>
<tag> 进程通信 </tag>
</tags>
</entry>
<entry>
<title>Css总结</title>
<link href="/2023/11/22/css-zong-jie/"/>
<url>/2023/11/22/css-zong-jie/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="CSS语法总结"><a href="#CSS语法总结" class="headerlink" title="CSS语法总结"></a>CSS语法总结</h1><h2 id="CSS优先级"><a href="#CSS优先级" class="headerlink" title="CSS优先级"></a>CSS优先级</h2><p><code>继承<通配符选择器<标签选择器<类选择器<id选择器<行内样式<!important</code></p><h2 id="设置全局背景的方法"><a href="#设置全局背景的方法" class="headerlink" title="设置全局背景的方法"></a>设置全局背景的方法</h2><pre class="line-numbers language-scss" data-language="scss"><code class="language-scss"><span class="token keyword">@mixin</span> <span class="token selector">fullcolor </span><span class="token punctuation">{</span> <span class="token property">position</span><span class="token punctuation">:</span> absolute<span class="token punctuation">;</span> <span class="token property">top</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> <span class="token property">left</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> <span class="token property">bottom</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> <span class="token property">right</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span><span class="token punctuation">}</span><span class="token selector">.container</span><span class="token punctuation">{</span> <span class="token keyword">@include</span> fullcolor<span class="token punctuation">;</span> <span class="token property">background</span><span class="token punctuation">:</span>red<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="全局清除样式"><a href="#全局清除样式" class="headerlink" title="全局清除样式"></a>全局清除样式</h2><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">*</span><span class="token punctuation">{</span> <span class="token property">margin</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> <span class="token property">padding</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token selector">html, body</span> <span class="token punctuation">{</span> <span class="token property">width</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span> <span class="token property">min-width</span><span class="token punctuation">:</span> 1000px<span class="token punctuation">;</span> <span class="token property">height</span><span class="token punctuation">:</span> 100%<span class="token punctuation">;</span> <span class="token property">min-height</span><span class="token punctuation">:</span> 400px<span class="token punctuation">;</span> <span class="token property">overflow</span><span class="token punctuation">:</span> hidden<span class="token punctuation">;</span> <span class="token property">overflow-y</span><span class="token punctuation">:</span>auto<span class="token punctuation">;</span><span class="token property">overflow-x</span><span class="token punctuation">:</span>auto<span class="token punctuation">;</span> // auto为自动拉伸 <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="标签居中margin-0-auto"><a href="#标签居中margin-0-auto" class="headerlink" title="标签居中margin:0 auto;"></a>标签居中<code>margin:0 auto;</code></h2><ul><li>居中相对于的是父级元素,需要<font color="red">设置宽度</font> </li><li>若采用该样式的元素未设置<code>width</code>大小将没有用处</li></ul><h2 id="css原生选择器"><a href="#css原生选择器" class="headerlink" title="css原生选择器"></a>css原生选择器</h2><h3 id="后代选择器"><a href="#后代选择器" class="headerlink" title="后代选择器"></a>后代选择器</h3><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">div p</span><span class="token punctuation">{</span> <span class="token property">margin</span><span class="token punctuation">:</span>0 auto<span class="token punctuation">;</span> <span class="token property">text-decoration</span><span class="token punctuation">:</span>none<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h3 id="子代选择器"><a href="#子代选择器" class="headerlink" title="子代选择器"></a>子代选择器</h3><ul><li>使用==标签==选择子代</li></ul><pre class="line-numbers language-css" data-language="css"><code class="language-css"><div style=<span class="token string">"margin:0 auto;height: 500px"</span>> <p><a href=<span class="token string">"#"</span>>?fas</a></p></div><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">div p > a</span> <span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><ul><li>使用标签的==id==选择子代<pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">#div1 p > a,span</span> <span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></li></ul><h2 id="伪类-用做背景颜色的设置等等"><a href="#伪类-用做背景颜色的设置等等" class="headerlink" title="伪类:用做背景颜色的设置等等"></a>伪类:用做背景颜色的设置等等</h2><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">.a::after</span><span class="token punctuation">{</span> <span class="token property">content</span><span class="token punctuation">:</span><span class="token string">""</span><span class="token punctuation">;</span> <span class="token property">position</span><span class="token punctuation">:</span>absolute<span class="token punctuation">;</span> <span class="token property">top</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span> <span class="token property">left</span><span class="token punctuation">:</span>0<span class="token punctuation">;</span> <span class="token property">z-index</span><span class="token punctuation">:</span>-1<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="背景图片样式设置"><a href="#背景图片样式设置" class="headerlink" title="背景图片样式设置"></a>背景图片样式设置</h2><h3 id="背景平铺-background-repeat-no-repeat"><a href="#背景平铺-background-repeat-no-repeat" class="headerlink" title="背景平铺 background-repeat:no-repeat"></a>背景平铺 <code>background-repeat:no-repeat</code></h3><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">#div1</span> <span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token property">background-image</span><span class="token punctuation">:</span> <span class="token url"><span class="token function">url</span><span class="token punctuation">(</span><span class="token string url">"1.jpg"</span><span class="token punctuation">)</span></span><span class="token punctuation">;</span> <span class="token property">background-repeat</span><span class="token punctuation">:</span> no-repeat<span class="token punctuation">;</span> <span class="token property">width</span><span class="token punctuation">:</span>30000px<span class="token punctuation">;</span> <span class="token property">height</span><span class="token punctuation">:</span> 1440px<span class="token punctuation">;</span> <span class="token property">ackground-positio</span><span class="token punctuation">:</span>center center <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="背景图片显示位置background-positio-center-center"><a href="#背景图片显示位置background-positio-center-center" class="headerlink" title="背景图片显示位置background-positio:center center"></a>背景图片显示位置<code>background-positio:center center</code></h3><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">#div1</span> <span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token property">background-image</span><span class="token punctuation">:</span> <span class="token url"><span class="token function">url</span><span class="token punctuation">(</span><span class="token string url">"1.jpg"</span><span class="token punctuation">)</span></span><span class="token punctuation">;</span> <span class="token property">background-repeat</span><span class="token punctuation">:</span> no-repeat<span class="token punctuation">;</span> <span class="token property">width</span><span class="token punctuation">:</span>30000px<span class="token punctuation">;</span> <span class="token property">height</span><span class="token punctuation">:</span> 1440px<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="元素显示模式-block-inline-block-inline"><a href="#元素显示模式-block-inline-block-inline" class="headerlink" title="元素显示模式:block,inline-block,inline"></a>元素显示模式:block,inline-block,inline</h2><table><thead><tr><th>属性</th><th>效果</th></tr></thead><tbody><tr><td><code>display:block</code></td><td>转化为块级元素</td></tr><tr><td><code>display:inline-block</code></td><td>转化为行内块级元素</td></tr><tr><td><code>display:inline</code></td><td>转化为行内元素</td></tr></tbody></table><h3 id="inline-无法设置宽度和高度-只能自适应子元素大小"><a href="#inline-无法设置宽度和高度-只能自适应子元素大小" class="headerlink" title="inline:无法设置宽度和高度,只能自适应子元素大小,"></a>inline:无法设置宽度和高度,只能自适应子元素大小,</h3><p>修改高度通过<code>line-height</code></p><p>给元素设置行高不会被执行自动忽略,实际所占大小依照元素内容大小</p><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">#div1</span> <span class="token punctuation">{</span> <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span>//被忽略 <span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span> <span class="token property">background-color</span><span class="token punctuation">:</span> red<span class="token punctuation">;</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li><p>代表标签:<code>a,p,h,span,label</code></p></li><li><p>代表标签 <code>inline、textarea、input、select、reset</code></p></li></ul><h2 id="标签嵌套"><a href="#标签嵌套" class="headerlink" title="标签嵌套"></a>标签嵌套</h2><ul><li><input disabled="" type="checkbox"> <code>p</code>标签不能嵌套其他标签</li><li><input disabled="" type="checkbox"> <code>a标签可以嵌套出了自己(a)以外的其他标签</code></li></ul><h2 id="SS继承性"><a href="#SS继承性" class="headerlink" title="SS继承性"></a>SS继承性</h2><ul><li><input disabled="" type="checkbox"> <code>a</code>标签不会继承父级元素的颜色属性</li><li><input disabled="" type="checkbox"> <code>h</code>标签不会继承父级元素字体大小属性 <code>font-size</code></li></ul><h2 id="两种盒子模型"><a href="#两种盒子模型" class="headerlink" title="两种盒子模型"></a>两种盒子模型</h2><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">div</span> <span class="token punctuation">{</span> <span class="token property">width</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span> <span class="token property">height</span><span class="token punctuation">:</span> 100px<span class="token punctuation">;</span> <span class="token property">background-color</span><span class="token punctuation">:</span> #00b7ee<span class="token punctuation">;</span> <span class="token property">margin</span><span class="token punctuation">:</span> 0 auto<span class="token punctuation">;</span> <span class="token property">border</span><span class="token punctuation">:</span>1px solid #000<span class="token punctuation">;</span> <span class="token property">padding</span><span class="token punctuation">:</span>10px 10px 10px 10px<span class="token punctuation">;</span> <span class="token property">box-sizing</span><span class="token punctuation">:</span>border-box<span class="token punctuation">;</span> //这里不同 /*<span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><code>box-sizing</code> 是一个 CSS 属性,用于设置元素的盒模型大小计算方式。它有两个取值:<code>content-box</code> 和 <code>border-box</code>,前者为标准盒子模型,后者为怪异盒子模型,</p><h3 id="标准盒子模型-少用-为了保持元素和外边框的边距少用这个"><a href="#标准盒子模型-少用-为了保持元素和外边框的边距少用这个" class="headerlink" title="标准盒子模型:(少用,为了保持元素和外边框的边距少用这个)"></a>标准盒子模型:(少用,为了保持元素和外边框的边距少用这个)</h3><p> <code>content-box</code> 是默认值,它表示元素的宽度和高度只包括内容区域(content),不包括边框(border)和内边距(padding)。也就是说,当你设置一个元素的宽度为 100px 时,它的内容区域宽度就是 100px,而整个元素的宽度则是内容宽度加上边框和内边距的宽度。这意味着,如果你设置了边框或内边距,就会导致元素的实际宽度比你设置的宽度更宽。</p><h3 id="怪异模型"><a href="#怪异模型" class="headerlink" title="怪异模型"></a>怪异模型</h3><p><code>border-box</code> 表示元素的宽度和高度包括内容区域、边框和内边距,也就是说,当你设置一个元素的宽度为 100px 时,它的实际宽度就是 100px,包括了内容宽度、边框和内边距的宽度。这样设置可以让开发者更方便地控制元素的尺寸,避免因为边框和内边距的加入而导致元素的尺寸不准确。</p><p>总的来说,<code>box-sizing</code> 的作用是控制元素的盒模型大小计算方式,从而更方便地控制元素的尺寸。</p><p>当你需要设置边框和内边距时,使用 <code>border-box</code> 可以更好地控制元素的实际尺寸,避免出现意外的布局问题.</p><h2 id="修改inline行内标签元素垂直位置"><a href="#修改inline行内标签元素垂直位置" class="headerlink" title="修改inline行内标签元素垂直位置"></a>修改<code>inline</code>行内标签元素垂直位置</h2><pre class="line-numbers language-css" data-language="css"><code class="language-css">//只能通过修改属性line-height来实现,margin-top and bottom 都是无效的,padding也是无效的<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h2 id="flex-布局"><a href="#flex-布局" class="headerlink" title="flex 布局"></a><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox#flex_%E5%85%83%E7%B4%A0%E5%B1%9E%E6%80%A7%EF%BC%9Aflex-grow">flex 布局</a></h2><h3 id="主轴方向-flex-wrap-row-nowrap"><a href="#主轴方向-flex-wrap-row-nowrap" class="headerlink" title="主轴方向 flex-wrap:row nowrap"></a>主轴方向 <code>flex-wrap:row nowrap</code></h3><p> 这是<code>display:flex</code>初始样子,主轴为水平,不换行;如果子元素宽高过长溢出就会溢出,<strong>会压缩子元素的宽度和高度</strong></p><h3 id="flex子元素水平拉伸属性-flex-basis"><a href="#flex子元素水平拉伸属性-flex-basis" class="headerlink" title="flex子元素水平拉伸属性: flex-basis"></a>flex子元素水平拉伸属性: <code>flex-basis</code></h3><p> 写在flex布局的下的<strong>子元素</strong>,以下选择的属性,</p><p> /* 指定<’width’> */</p><ul><li>flex-basis: auto;根据子元素自动扩展</li><li>flex-basis: 10em;</li><li>flex-basis: 3px;</li><li>flex-basis: auto;根据子元素自动扩展</li></ul><p> /* 固有的尺寸关键词 */</p><ul><li>flex-basis: fill;</li><li>flex-basis: max-content;</li><li>flex-basis: min-content;</li><li>flex-basis: fit-content;</li><li>/* 在 flex item 内容上的自动尺寸 */</li><li>flex-basis: content;</li></ul><p> /* 全局数值 */</p><ul><li><p>flex-basis: inherit; 继承最近父元素的 flex-basis 值</p></li><li><p>flex-basis: initial;</p><p>表示将 flex-basis 值重置为其初始值,即自动计算伸缩项的大小。通常情况下,这个值等价于 flex-basis: auto;</p></li><li><p>flex-basis: unset;</p></li><li><p>flex-basis: unset;</p><p>默认情况下,它等价于 flex-basis: auto;,但是在使用 flex 缩写属性时,它可能会等价于 flex-basis: 0</p></li></ul><h3 id="flex子元素伸展属性flex-grow-基于flex-basis"><a href="#flex子元素伸展属性flex-grow-基于flex-basis" class="headerlink" title="flex子元素伸展属性flex-grow:基于flex-basis"></a>flex子元素伸展属性<code>flex-grow</code>:基于<code>flex-basis</code></h3><p> flex-grow 若被赋值为一个<strong>正整数</strong>,flex 元素会以 flex-basis 为基础,沿主轴方向增长尺寸。这会使该元素延展,并占据此方向轴上的可用空间(available space)。如果有其他元素也被允许延展,那么他们会各自占据可用空间的一部分。</p><p>如果我们给上例中的<strong>所有元素设定 flex-grow 值为 1</strong>,容器中的可用空间会被这些元素<strong>平分</strong>。它们会延展以填满容器主轴方向上的空间。</p><h3 id="flex子元素压缩属性-flex-shrink"><a href="#flex子元素压缩属性-flex-shrink" class="headerlink" title="flex子元素压缩属性:flex-shrink"></a>flex子元素压缩属性:<code>flex-shrink</code></h3><p>flex-grow属性是处理 flex 元素在主轴上增加空间的问题,相反flex-shrink属性是处理 flex 元素收缩的问题。如果我们的容器中没有足够排列 flex 元素的空间,那么可以把 flex 元素<strong>flex-shrink属性设置为正整数来缩小它所占空间</strong>到flex-basis以下。与flex-grow属性一样,可以赋予不同的值来控制 flex 元素收缩的程度 —— 给flex-shrink属性赋予更大的数值可以比赋予小数值的同级元素收缩程度更大。</p><p>负值是不被允许的</p><pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token property">flex-shrink</span><span class="token punctuation">:</span> 2<span class="token punctuation">;</span><span class="token property">flex-shrink</span><span class="token punctuation">:</span> 0.6<span class="token punctuation">;</span><span class="token comment">/* Global values */</span><span class="token property">flex-shrink</span><span class="token punctuation">:</span> inherit<span class="token punctuation">;</span><span class="token property">flex-shrink</span><span class="token punctuation">:</span> initial<span class="token punctuation">;</span><span class="token property">flex-shrink</span><span class="token punctuation">:</span> unset<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="flex属性简写"><a href="#flex属性简写" class="headerlink" title="flex属性简写"></a>flex属性简写</h3><p>Flex 简写形式允许你把三个数值按这个顺序书写 — flex-grow,flex-shrink,flex-basis:<br><code>flex:1 0 auto</code></p><h3 id="flex对齐属性"><a href="#flex对齐属性" class="headerlink" title="flex对齐属性"></a>flex对齐属性</h3><p>控制对齐的属性本指南中的属性如下。</p><p>justify-content - 控制主轴(横轴)上所有 flex 布局下的元素的对齐。</p><p>align-items - 控制交叉轴(纵轴)上所有 flex 布局下的元素对齐。</p><p>align-self - 控制交叉轴(纵轴)上的单个 flex 布局下的元素对齐。 //align-self:stretch /* 该项在交叉轴上拉伸以填充可用空间 */</p><p>align-content - 控制“多条主轴”的 flex 布局下的元素在交叉轴的对齐。</p><h3 id="fixed定位-absolute定位"><a href="#fixed定位-absolute定位" class="headerlink" title="fixed定位,absolute定位"></a>fixed定位,absolute定位</h3><p>这些定位作用于flex布局下的任意元素会影响到整体的flex布局方式,调整相对位置用position:relative,或者用margin去调整</p><h2 id="文本排列方式"><a href="#文本排列方式" class="headerlink" title="文本排列方式"></a>文本排列方式</h2><p>这些 CSS 属性都涉及文本方向和排列方式。</p><ul><li><p><code>horizontal-tb</code> 是 CSS 中默认的文本排列方式,即从左到右、从上到下,也就是水平方向从左到右,垂直方向从上到下排列。这是大多数语言的书写方式,如英语、中文等。</p></li><li><p><code>vertical-rl</code> 表示文本的排列方向是从上到下,从右到左。这种排列方式常用于一些从右到左书写的语言,如阿拉伯语等。</p></li><li><p><code>vertical-lr</code> 表示文本的排列方向是从上到下,从左到右。这种排列方式同样适用于一些从右到左书写的语言,如波斯语等。</p></li><li><p><code>sideways-rl</code> 表示文本的排列方式是从上到下,从右到左,但是排列方式是横向的,即文字是横向书写的。这种排列方式常常用于一些特殊排版需求,如横向排列的表格标题等。</p></li><li><p><code>sideways-lr</code> 表示文本的排列方式是从上到下,从左到右,但是排列方式是横向的,即文字是横向书写的。这种排列方式同样适用于一些特殊排版需求,如横向排列的表格标题等。</p> <pre class="line-numbers language-css" data-language="css"><code class="language-css"><span class="token selector">.box</span> <span class="token punctuation">{</span> <span class="token property">display</span><span class="token punctuation">:</span> flex<span class="token punctuation">;</span> <span class="token property">writing-mode</span><span class="token punctuation">:</span> horizontal-tb<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre></li></ul>]]></content>
<categories>
<category> CSS </category>
</categories>
<tags>
<tag> CSS </tag>
</tags>
</entry>
<entry>
<title>MacOS显示隐藏文件</title>
<link href="/2023/11/22/macos-xian-shi-yin-cang-wen-jian/"/>
<url>/2023/11/22/macos-xian-shi-yin-cang-wen-jian/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>在 macOS 上,隐藏文件和文件夹默认情况下是不可见的,但你可以通过以下方法来永久显示所有隐藏文件:</p><ol><li><p>打开“终端”应用程序。你可以在“应用程序”文件夹下的“实用工具”文件夹中找到它。</p></li><li><p>输入以下命令并按下回车键:</p></li></ol><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">defaults <span class="token function">write</span> com.apple.finder AppleShowAllFiles <span class="token parameter variable">-bool</span> YES<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ol start="3"><li>输入以下命令来重新启动 Finder(文件管理器):</li></ol><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">killall</span> Finder<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>Finder 将会重新启动,并且你将能够看到所有的隐藏文件和文件夹。它们会以半透明的方式显示。</p><p>如果你想恢复隐藏文件的默认设置,可以重复上述步骤,并将命令中的 <code>-bool YES</code> 替换为 <code>-bool NO</code>,然后重新启动 Finder。</p><p>请注意,显示所有隐藏文件可能会让你的文件系统看起来杂乱无章,因此在完成任务后,最好将隐藏文件设置为默认不可见的状态。</p>]]></content>
<categories>
<category> Mac </category>
<category> MacOs </category>
</categories>
<tags>
<tag> MasOS </tag>
<tag> Linux </tag>
</tags>
</entry>
<entry>
<title>npm创建typescript项目</title>
<link href="/2023/11/22/npm-chuang-jian-typescript-xiang-mu/"/>
<url>/2023/11/22/npm-chuang-jian-typescript-xiang-mu/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>首先,您需要确保已在计算机上安装了Node.js和NPM。在安装完成后,您可以按照以下步骤初始化一个TypeScript项目:</p><ol><li><p>打开命令行界面(例如终端或命令提示符)并导航到要创建项目的目录。</p></li><li><p>使用npm init 命令初始化npm项目(如果你已经有了npm项目,请跳过此步骤),这将创建一个package.json文件来管理您的项目依赖。</p><pre class="line-numbers language-none"><code class="language-none">npm init<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>现在,您可以使用npm install命令安装TypeScript。请确保使用–save-dev选项来将其添加到您的开发依赖中。</p><pre class="line-numbers language-none"><code class="language-none">npm install typescript --save-dev<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>接下来,在项目根目录下创建一个名为tsconfig.json的TypeScript配置文件。</p><pre class="line-numbers language-none"><code class="language-none">touch tsconfig.json<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>在tsconfig.json文件中添加 TypeScript 编译器选项,以指定编译生成 JavaScript 的输出目标格式和目录结构等内容。</p><pre class="line-numbers language-none"><code class="language-none">{ "compilerOptions": { "target": "es6", "module": "commonjs", "outDir": "dist" }}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>最后,请在package.json文件中添加一个scripts对象来定义使用TypeScript编译器编译您的代码命令。</p><pre class="line-numbers language-none"><code class="language-none">"scripts": { "build": "tsc"}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre></li></ol><p>现在,您应该可以运行以下命令来编译您的TypeScript代码:</p><pre class="line-numbers language-none"><code class="language-none">npm run build<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>此命令将使用tsc编译器将项目中所有的TypeScript文件转换为JavaScript,并将其输出到tsconfig.json文件中指定的目录结构(例如dist文件夹)。</p>]]></content>
<categories>
<category> Node.js </category>
<category> Typescript </category>
</categories>
<tags>
<tag> Node.js </tag>
<tag> typescript </tag>
</tags>
</entry>
<entry>
<title>centos安装mysql8</title>
<link href="/2023/11/22/centos-an-zhuang-mysql/"/>
<url>/2023/11/22/centos-an-zhuang-mysql/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p><a href="https://juejin.cn/post/7056265988673568781">centos8 安装mysql</a></p><blockquote><p>不同的地方</p></blockquote><ol><li><p>拉仓库:<code>wget https://repo.mysql.com/mysql80-community-release-el8-1.noarch.rpm</code></p></li><li><p>yum 安装<code>yum -y install mysql80-community-release-el8-1.noarch.rpm</code></p></li><li><p><code>yum module disable mysql</code></p></li><li><p><code>yum -y install mysql-community-server</code></p></li><li><p>删除rpm文件防止更新 <code>yum -e mysql80-community-release-el8-1.noarch.rpm</code></p></li></ol><p></p><blockquote><p>删除mysql</p></blockquote><p>如果您希望完全删除MySQL数据库,请使用以下命令:</p><ol><li><p>删除MySQL服务器软件包:</p><pre class="line-numbers language-none"><code class="language-none">perlCopy coderpm -qa | grep mysql | xargs rpm -e<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></li><li><p>删除MySQL数据目录(默认情况下是/var/lib/mysql):</p><pre class="line-numbers language-none"><code class="language-none">bashCopy coderm -rf /var/lib/mysql<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></li></ol><p>请注意,在执行这些命令之前,务必确保您真正希望删除MySQL数据库,并且备份重要的数据以防意外。</p>]]></content>
<categories>
<category> Centos </category>
<category> Mysql </category>
</categories>
<tags>
<tag> Centos </tag>
<tag> Mysql </tag>
</tags>
</entry>
<entry>
<title>查看私网和公网IP</title>
<link href="/2023/11/22/cha-kan-si-wang-luo-he-gong-wang-ip/"/>
<url>/2023/11/22/cha-kan-si-wang-luo-he-gong-wang-ip/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="macOS-Linux"><a href="#macOS-Linux" class="headerlink" title="macOS Linux"></a>macOS Linux</h2><p>在 macOS 上,你可以使用以下命令来查看本机的 IP 地址:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">ifconfig | grep "inet "<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这个命令会列出所有网络接口的详细信息,并通过管道 (<code>|</code>) 将输出传递给 <code>grep</code> 命令进行过滤。<code>grep "inet "</code> 将只显示包含 “inet “ 的行,也就是 IP 地址所在的行。</p><p>另外,你还可以使用以下命令来仅显示 IP 地址:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">ipconfig getifaddr en0<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>其中,<code>en0</code> 是主要网络接口的标识符。如果你使用的是其他接口(例如 Wi-Fi 或以太网接口),可能需要将 <code>en0</code> 替换为相应接口的标识符。</p><p>请注意,在不同的 macOS 版本中,网络接口的标识符可能会有所不同。如果上述命令无法正常工作,请尝试使用 <code>ifconfig</code> 命令来查看网络接口列表,并找到适合你系统的接口标识符。</p><p>根据你提供的信息,第二个IP地址(10.89.134.138)是一个内部私有IP地址,而不是公网IP地址。内部私有IP地址通常在局域网内使用,并由路由器或网络设备分配。</p><p>要查找你的公网IP地址,你可以使用以下命令:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">curl</span> ifconfig.me<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>运行该命令后,会返回你的公网IP地址。请确保你的设备能够连接到互联网,否则无法正确获取公网IP地址。</p><h2 id="windows"><a href="#windows" class="headerlink" title="windows"></a>windows</h2>]]></content>
<categories>
<category> IP </category>
<category> Mac </category>
</categories>
<tags>
<tag> Mac </tag>
<tag> IP </tag>
</tags>
</entry>
<entry>
<title>Mac OS常用命令</title>
<link href="/2023/11/22/mac-os-chang-yong-ming-ling/"/>
<url>/2023/11/22/mac-os-chang-yong-ming-ling/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="terminal走代理"><a href="#terminal走代理" class="headerlink" title="terminal走代理"></a><a href="https://www.hangge.com/blog/cache/detail_3138.html">terminal走代理</a></h2><ol><li>修改配置文件。<code>vi ~/.zshrc</code></li><li>add config: <code>alias proxy='export all_proxy=socks5://127.0.0.1:7890'</code> <code>alias unproxy='unset all_proxy'</code></li><li>make config affect <code>source ~/.zshrc</code> </li><li>查询ip地址:<code>curl ipinfo.io</code></li><li>启用代理再查询ip地址:<code>proxy</code>+<code>curl ipinfo.io</code></li></ol><h2 id="添加brew到系统变量"><a href="#添加brew到系统变量" class="headerlink" title="添加brew到系统变量"></a><a href="https://blog.51cto.com/u_15655559/5530796">添加brew到系统变量</a></h2><p>1.<code>echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.bash_profile </code> :将文件输入到.bash_profile文件path变量中<br>2. <code>source ~/.bash_profile</code>:执行修改<br>3. <code>echo 'export PATH="/opt/homebrew/bin/:$PATH"' >>~/.zshrc 4. </code>source ~/.zshrc`</p><h2 id="brew-list-软件名称显示软件的安装路径"><a href="#brew-list-软件名称显示软件的安装路径" class="headerlink" title="brew list 软件名称显示软件的安装路径"></a><code>brew list 软件名称</code>显示软件的安装路径</h2><h2 id="命令前加sudo为管理员执行"><a href="#命令前加sudo为管理员执行" class="headerlink" title="命令前加sudo为管理员执行"></a>命令前加<code>sudo</code>为管理员执行</h2><h2 id="命令走代理"><a href="#命令走代理" class="headerlink" title="命令走代理"></a>命令走代理</h2><p>1.git</p><p>设置端口代理</p><pre class="line-numbers language-none"><code class="language-none">git config --global http.proxy http://127.0.0.1:1088git config --global https.proxy https://127.0.0.1:1088<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>取消代理</p><pre class="line-numbers language-none"><code class="language-none">git config --global --unset http.proxygit config --global --unset https.proxy<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>2.npm</p><p>取消代理</p><pre class="line-numbers language-none"><code class="language-none">npm config delete proxynpm config delete https-proxy设置代理<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>npm config set proxy <a href="http://127.0.0.1:1088/">http://127.0.0.1:1088</a><br>npm config set https-proxy <a href="http://127.0.0.1:1088/">http://127.0.0.1:1088</a><br>npm config set strict-ssl false</p><h2 id="给文件添加读写权限"><a href="#给文件添加读写权限" class="headerlink" title="给文件添加读写权限"></a>给文件添加读写权限</h2><p><code>sudo chmod -R 777 文件夹路径</code></p>]]></content>
<categories>
<category> mac </category>
</categories>
<tags>
<tag> MacOS </tag>
</tags>
</entry>
<entry>
<title>JDBC链接数据库</title>
<link href="/2023/11/22/jdbc-lian-jie-shu-ju-ku/"/>
<url>/2023/11/22/jdbc-lian-jie-shu-ju-ku/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Java使用驱动连接链接数据库"><a href="#Java使用驱动连接链接数据库" class="headerlink" title="Java使用驱动连接链接数据库"></a>Java使用驱动连接链接数据库</h1><h2 id="驱动下载"><a href="#驱动下载" class="headerlink" title="驱动下载"></a><a href="https://mvnrepository.com/artifact/mysql/mysql-connector-java/8.0.27">驱动下载</a></h2><h2 id="配置驱动"><a href="#配置驱动" class="headerlink" title="配置驱动"></a><a href="https://www.cnblogs.com/dadian/p/11936056.html">配置驱动</a></h2><h2 id="链接MySQL"><a href="#链接MySQL" class="headerlink" title="链接MySQL"></a>链接MySQL</h2><blockquote><p><code>try-catch</code>语句,打开链接,执行MySQL语句,接受返回值,关闭连接</p></blockquote><pre class="line-numbers language-Java" data-language="Java"><code class="language-Java">import java.sql.*;public class Main { public void del(String url, String user, String password){ try { Connection con = DriverManager.getConnection(url, user, password); // Executing a query Statement stmt = con.createStatement(); // 执行删除操作 int rowCount = stmt.executeUpdate("DELETE FROM student WHERE SName LIKE '白%'"); if (rowCount > 0) { System.out.println("成功删除 " + rowCount + " 条记录。"); } else { System.out.println("没有找到匹配的记录。"); } // stmt.executeQuery返回表第一个元组的引用 ResultSet rs = stmt.executeQuery("SELECT * FROM student");// while (rs.next()) {// String id = rs.getString("SId");// String name = rs.getString("SName");// System.out.println("ID: " + id + ", Name: " + name);// } rs.close(); stmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } public void ins(String url, String user, String password){ try { Connection con = DriverManager.getConnection(url, user, password); // Executing a query Statement stmt = con.createStatement(); int numRowsAffected = stmt.executeUpdate("INSERT INTO student(GId,SId,SName,SSexy)VALUES(11,'018001','李白','男')"); System.out.println("插入了"+numRowsAffected+"行数据"); stmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } public void update(String url, String user, String password){ try { // Connection con = DriverManager.getConnection(url, user, password); // Executing a query Statement stmt = con.createStatement(); // executeUpdate方法返回表中更新的记录. int numRowsAffected = stmt.executeUpdate("UPDATE student SET STele='660990' WHERE SId='032006007'"); System.out.println(numRowsAffected + "行已更新"); stmt.close(); con.close(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { // Establishing a connection to the database String url = "jdbc:mysql://localhost:3306/learndb"; String user = "root"; String password = "775028"; Main test=new Main(); test.del(url, user, password); test.update(url, user, password); test.ins(url, user, password); }}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="使用mybatis连接数据库"><a href="#使用mybatis连接数据库" class="headerlink" title="使用mybatis连接数据库"></a>使用<code>mybatis</code>连接数据库</h2>]]></content>
<categories>
<category> Java </category>
<category> JDBC </category>
</categories>
<tags>
<tag> JDBC </tag>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>mysql创建表</title>
<link href="/2023/11/22/mysql-chuang-jian-biao/"/>
<url>/2023/11/22/mysql-chuang-jian-biao/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>这段代码可能有以下原因导致错误:</p><ol><li><p>表名和列名使用了单引号,正确的写法应该是使用反引号。</p></li><li><p>‘uersname’ 应该是 ‘username’ 才正确。</p></li><li><p>指定了字符集为 utf8,但是未指定排序规则。</p></li></ol><p>下面是修正后的代码:</p><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">CREATE TABLE IF NOT EXISTS `user` ( `id` INT UNSIGNED AUTO_INCREMENT, `username` VARCHAR(100) NOT NULL, `host` VARCHAR(40) NOT NULL, `password` VARCHAR(40) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>在这个修正后的代码中,表名和列名使用了反引号,<code>username</code> 被修正了拼写,同时字符集被指定为 <code>utf8mb4</code>,并且指定了排序规则。<br><code>ENGINE=InnoDB</code> 表示创建的表使用 InnoDB 引擎,这是 MySQL 中一种流行的事务性存储引擎,具有 ACID(原子性、一致性、隔离性、持久性)特性,支持 MVCC 并发控制。</p><p><code>CHARSET=utf8mb4</code> 指定了字符集为 <code>utf8mb4</code>,它是一种支持更广泛的 Unicode 字符集的 UTF-8 编码方式。与传统的 <code>utf8</code> 相比,<code>utf8mb4</code> 可以存储更多的字符,包括一些 Emoji 等特殊字符。</p><p><code>COLLATE=utf8mb4_unicode_ci</code> 是排序规则,指定了在比较和排序时使用的字符集和排序方式。<code>utf8mb4_unicode_ci</code> 是一种基于 Unicode 的排序规则,支持多语言,比如中文、日语、韩语等,能够确保不同语言之间的字符串比较正确。</p>]]></content>
<categories>
<category> Mysql </category>
</categories>
<tags>
<tag> Mysql </tag>
</tags>
</entry>
<entry>
<title>mysql权限设置</title>
<link href="/2023/11/22/mysql-quan-xian-she-zhi/"/>
<url>/2023/11/22/mysql-quan-xian-she-zhi/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="MySQL权限命令"><a href="#MySQL权限命令" class="headerlink" title="MySQL权限命令"></a>MySQL权限命令</h1><h2 id="创建用户"><a href="#创建用户" class="headerlink" title="创建用户"></a>创建用户</h2><blockquote><p>创建用户并将用户信息储存在``表mysql.user`中.</p></blockquote><p><code>CREATE USER 'new_username'@'localhost' IDENTIFIED BY 'password';</code></p><blockquote><p>查询某个用户信息</p></blockquote><p><code>select * from mysql.user where User = '名字' and Host ='主机名'</code></p><h2 id="赋予用户权限"><a href="#赋予用户权限" class="headerlink" title="赋予用户权限"></a>赋予用户权限</h2><p><code>GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON </code>1<code>.* TO 'local1'@'%';</code></p><p><em><em>注意:EXECUTE ON <code>1</code>.</em> TO ‘local1‘@’%’;`这表示给用户local1 主机号为% 在数据库为1上创建的用户权限</em>*.<strong>数据库的名字要用反引号包裹</strong></p><p>.</p><h3 id="修改外键权限"><a href="#修改外键权限" class="headerlink" title="修改外键权限"></a>修改外键权限</h3><p><code>grant references on </code> <code>1.* to 'local1' and 'host'</code></p><h2 id="查看用户权限"><a href="#查看用户权限" class="headerlink" title="查看用户权限"></a>查看用户权限</h2><h3 id="查看自己权限"><a href="#查看自己权限" class="headerlink" title="查看自己权限"></a>查看自己权限</h3><p><code>show grants</code></p><h3 id="查看特定用户权限"><a href="#查看特定用户权限" class="headerlink" title="查看特定用户权限"></a>查看特定用户权限</h3><p><code>show grants for 'name'@'%'</code></p><h2 id="修改用户密码"><a href="#修改用户密码" class="headerlink" title="修改用户密码"></a>修改用户密码</h2><h2 id="SET-PASSWORD-FOR-alogin-mima"><a href="#SET-PASSWORD-FOR-alogin-mima" class="headerlink" title="SET PASSWORD FOR 'alogin'@'%' = 'mima';"></a><code>SET PASSWORD FOR 'alogin'@'%' = 'mima';</code></h2><h2 id="创建角色"><a href="#创建角色" class="headerlink" title="创建角色"></a>创建角色</h2><p>以下是在MySQL中创建新角色并为其授予权限的步骤:</p><ol><li><p>使用管理员权限登录MySQL,比如root用户。</p></li><li><p>创建名为“arole”的角色,请使用以下命令:</p></li></ol><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">CREATE ROLE arole;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ol start="3"><li>授予角色在表Student上的select、insert、update和delete权限。请使用以下命令:</li></ol><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">GRANT SELECT, INSERT, UPDATE, DELETE ON learndb.Student TO arole;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ol start="4"><li>授予角色在表Grade上的select、insert、update和delete权限。请使用以下命令:</li></ol><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">GRANT SELECT, INSERT, UPDATE, DELETE ON learndb.Grade TO arole;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ol start="5"><li>将arole授予用户,使用户可以继承该角色的权限。例如,如果要将arole授予用户alogin_ldb,请使用以下命令:</li></ol><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">GRANT arole TO 'alogin_ldb'@'localhost';<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ol start="6"><li>刷新MySQL权限表以使更改生效:</li></ol><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">FLUSH PRIVILEGES;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>现在,您可以尝试使用用户alogin_ldb登录到MySQL客户端,并测试是否可以查询、插入、更新和删除learndb数据库中的Student和Grade表。</p><p>例如,您可以使用以下语句查询Student表:</p><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">SELECT * FROM Student;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>或者,您可以使用以下语句向Grade表中添加新行:</p><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">INSERT INTO Grade (sid, cid, score) VALUES ('10001', '1', 85);<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>如果用户具有足够的权限,则应成功执行查询或插入操作。否则,将显示错误消息并拒绝该操作。</p><h2 id="删除除开root以外的所有用户"><a href="#删除除开root以外的所有用户" class="headerlink" title="删除除开root以外的所有用户"></a>删除除开<code>root</code>以外的所有用户</h2><p>在MySQL中,您可以使用以下步骤删除除root以外的所有用户:</p><ol><li><p>使用管理员权限登录MySQL,比如root用户。</p></li><li><p>执行以下查询以查找当前所有用户(除root之外):</p></li></ol><pre class="line-numbers language-none"><code class="language-none">SELECT user FROM mysql.user WHERE user NOT LIKE 'root' AND user NOT LIKE 'mysql.%';<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>此查询将返回不是以“root”或“mysql”开头的所有用户的列表。</p><ol start="3"><li>对于返回的每个用户,执行以下查询来删除该用户和其关联的权限和对象:</li></ol><pre class="line-numbers language-none"><code class="language-none">DROP USER 'username'@'localhost';<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>请将“username”替换为要删除的特定用户的名称。</p><ol start="4"><li>刷新MySQL权限表以使更改生效:</li></ol><pre class="line-numbers language-none"><code class="language-none">FLUSH PRIVILEGES;<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>现在,除了系统用户和root用户之外,所有其他用户均已从MySQL服务器中删除。请注意,在执行此操作之前,请务必备份所有需要保留的数据,并确保不会误删重要的用户账户。</p>]]></content>
<categories>
<category> Mysql </category>
</categories>
<tags>
<tag> mysql </tag>
<tag> 权限、角色 </tag>
</tags>
</entry>
<entry>
<title>正则表达式检验身份证有效性</title>
<link href="/2023/11/22/zheng-ze-biao-da-shi-jian-yan-shen-fen-zheng-you-xiao-xing/"/>
<url>/2023/11/22/zheng-ze-biao-da-shi-jian-yan-shen-fen-zheng-you-xiao-xing/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Java正则表达式实现身份证号码检验"><a href="#Java正则表达式实现身份证号码检验" class="headerlink" title="Java正则表达式实现身份证号码检验"></a>Java正则表达式实现身份证号码检验</h1><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token namespace">rookie<span class="token punctuation">.</span>list</span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>text<span class="token punctuation">.</span></span><span class="token class-name">ParseException</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>text<span class="token punctuation">.</span></span><span class="token class-name">SimpleDateFormat</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Date</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Scanner</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>regex<span class="token punctuation">.</span></span><span class="token class-name">Pattern</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Main</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">Boolean</span> <span class="token function">check</span><span class="token punctuation">(</span><span class="token class-name">String</span> a<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> a<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">StringBuilder</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</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">public</span> <span class="token keyword">static</span> <span class="token keyword">boolean</span> <span class="token function">isIdNumberValid</span><span class="token punctuation">(</span><span class="token class-name">String</span> idNumber<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>idNumber <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> idNumber<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">18</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token class-name">String</span> regex <span class="token operator">=</span> <span class="token string">"^[1-9]\\d{5}(19|20)\\d{2}(0\\d|1[012])(0\\d|[12]\\d|3[01])\\d{3}[\\dxX]$"</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">matches</span><span class="token punctuation">(</span>regex<span class="token punctuation">,</span> idNumber<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> birthday <span class="token operator">=</span> idNumber<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">SimpleDateFormat</span> format <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SimpleDateFormat</span><span class="token punctuation">(</span><span class="token string">"yyyyMMdd"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> format<span class="token punctuation">.</span><span class="token function">setLenient</span><span class="token punctuation">(</span><span class="token boolean">false</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 class-name">Date</span> date <span class="token operator">=</span> format<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>birthday<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"生日:"</span> <span class="token operator">+</span> format<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>date<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><span class="token class-name">ParseException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">int</span> genderIndex <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span>idNumber<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">String</span> gender <span class="token operator">=</span> genderIndex <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">?</span> <span class="token string">"女"</span> <span class="token operator">:</span> <span class="token string">"男"</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"性别:"</span> <span class="token operator">+</span> gender<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> list<span class="token generics"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span> str <span class="token operator">=</span> <span class="token keyword">new</span> list<span class="token generics"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Scanner</span> scanner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">String</span> a <span class="token operator">=</span> scanner<span class="token punctuation">.</span><span class="token function">nextLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> str<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">isIdNumberValid</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"valid id number"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"invalid id number"</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 function">check</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Is Palindrome string"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">else</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"Is not Palindrome string"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="windowd-app"><a href="#windowd-app" class="headerlink" title="windowd app"></a>windowd app</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>swing<span class="token punctuation">.</span></span><span class="token class-name">JOptionPane</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>regex<span class="token punctuation">.</span></span><span class="token class-name">Matcher</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span>regex<span class="token punctuation">.</span></span><span class="token class-name">Pattern</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">IDCardValidator</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Pattern</span> pattern <span class="token operator">=</span> <span class="token class-name">Pattern</span><span class="token punctuation">.</span><span class="token function">compile</span><span class="token punctuation">(</span><span class="token string">"^[1-9]\\d{5}(18|19|20)\\d{2}(0\\d|1[0-2])([0-2]\\d|3[01])\\d{3}[0-9Xx]$"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">boolean</span> isValid <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token class-name">String</span> idCard <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token operator">!</span>isValid<span class="token punctuation">)</span> <span class="token punctuation">{</span> idCard <span class="token operator">=</span> <span class="token class-name">JOptionPane</span><span class="token punctuation">.</span><span class="token function">showInputDialog</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token string">"请输入身份证号码:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Matcher</span> matcher <span class="token operator">=</span> pattern<span class="token punctuation">.</span><span class="token function">matcher</span><span class="token punctuation">(</span>idCard<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>matcher<span class="token punctuation">.</span><span class="token function">matches</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> isValid <span class="token operator">=</span> <span class="token boolean">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 class-name">JOptionPane</span><span class="token punctuation">.</span><span class="token function">showMessageDialog</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token string">"身份证号码不合法,请重新输入。"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token class-name">String</span> year <span class="token operator">=</span> idCard<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">String</span> month <span class="token operator">=</span> idCard<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">String</span> day <span class="token operator">=</span> idCard<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">14</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">String</span> gender <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span>idCard<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">16</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">?</span> <span class="token string">"女"</span> <span class="token operator">:</span> <span class="token string">"男"</span><span class="token punctuation">;</span> <span class="token class-name">JOptionPane</span><span class="token punctuation">.</span><span class="token function">showMessageDialog</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token string">"出生年月日:"</span> <span class="token operator">+</span> year <span class="token operator">+</span> <span class="token string">"年"</span> <span class="token operator">+</span> month <span class="token operator">+</span> <span class="token string">"月"</span> <span class="token operator">+</span> day <span class="token operator">+</span> <span class="token string">"日\n性别:"</span> <span class="token operator">+</span> gender<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="核心代码"><a href="#核心代码" class="headerlink" title="核心代码"></a>核心代码</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">if</span> <span class="token punctuation">(</span>idNumber <span class="token operator">==</span> <span class="token keyword">null</span> <span class="token operator">||</span> idNumber<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token number">18</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token class-name">String</span> regex <span class="token operator">=</span> <span class="token string">"^[1-9]\\d{5}(19|20)\\d{2}(0\\d|1[012])(0\\d|[12]\\d|3[01])\\d{3}[\\dxX]$"</span><span class="token punctuation">;</span><span class="token keyword">return</span> <span class="token class-name">Patter</span><span class="token punctuation">.</span><span class="token function">matches</span><span class="token punctuation">(</span>regex<span class="token punctuation">,</span>idNumber<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> Regex </category>
</categories>
<tags>
<tag> Regex </tag>
<tag> java </tag>
</tags>
</entry>
<entry>
<title>java实现计算器</title>
<link href="/2023/11/22/java-shi-xian-ji-suan-qi/"/>
<url>/2023/11/22/java-shi-xian-ji-suan-qi/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>awt<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>awt<span class="token punctuation">.</span>event<span class="token punctuation">.</span></span><span class="token class-name">ActionEvent</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>awt<span class="token punctuation">.</span>event<span class="token punctuation">.</span></span><span class="token class-name">ActionListener</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Objects</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>swing<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span><span class="token comment">//Calculator类,继承JFrame框架,实现事件监听器接口</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Calculator</span> <span class="token keyword">extends</span> <span class="token class-name">JFrame</span> <span class="token keyword">implements</span> <span class="token class-name">ActionListener</span> <span class="token punctuation">{</span> <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token constant">KEYS</span> <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token string">"7"</span><span class="token punctuation">,</span> <span class="token string">"8"</span><span class="token punctuation">,</span> <span class="token string">"9"</span><span class="token punctuation">,</span> <span class="token string">"AC"</span><span class="token punctuation">,</span> <span class="token string">"4"</span><span class="token punctuation">,</span> <span class="token string">"5"</span><span class="token punctuation">,</span> <span class="token string">"6"</span><span class="token punctuation">,</span> <span class="token string">"-"</span><span class="token punctuation">,</span> <span class="token string">"1"</span><span class="token punctuation">,</span> <span class="token string">"2"</span><span class="token punctuation">,</span> <span class="token string">"3"</span><span class="token punctuation">,</span> <span class="token string">"+"</span><span class="token punctuation">,</span> <span class="token string">"0"</span><span class="token punctuation">,</span> <span class="token string">"e"</span><span class="token punctuation">,</span> <span class="token string">"pi"</span><span class="token punctuation">,</span> <span class="token string">"/"</span><span class="token punctuation">,</span> <span class="token string">"sqrt"</span><span class="token punctuation">,</span> <span class="token string">"%"</span><span class="token punctuation">,</span> <span class="token string">"x*x"</span><span class="token punctuation">,</span> <span class="token string">"*"</span><span class="token punctuation">,</span> <span class="token string">"("</span><span class="token punctuation">,</span> <span class="token string">")"</span><span class="token punctuation">,</span> <span class="token string">"."</span><span class="token punctuation">,</span> <span class="token string">"="</span> <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token class-name">JButton</span> keys<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JButton</span><span class="token punctuation">[</span><span class="token constant">KEYS</span><span class="token punctuation">.</span>length<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token class-name">JTextArea</span> resultText <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JTextArea</span><span class="token punctuation">(</span><span class="token string">"0.0"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 文本域组件TextArea可容纳多行文本;文本框内容初始值设为0.0</span> <span class="token keyword">private</span> <span class="token class-name">JTextArea</span> <span class="token class-name">History</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JTextArea</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 历史记录文本框初始值设为空</span> <span class="token keyword">private</span> <span class="token class-name">JPanel</span> jp2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">JPanel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token class-name">JScrollPane</span> gdt1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">JScrollPane</span><span class="token punctuation">(</span>resultText<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//给输入显示屏文本域新建一个垂直滚动滑条</span> <span class="token keyword">private</span> <span class="token class-name">JScrollPane</span> gdt2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">JScrollPane</span><span class="token punctuation">(</span><span class="token class-name">History</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//给历史记录文本域新建一个垂直滚动滑条</span> <span class="token comment">// private JScrollPane gdt3=new JScrollPane(History);//给历史记录文本域新建一个水平滚动滑条</span> <span class="token keyword">private</span> <span class="token class-name">JLabel</span> label <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JLabel</span><span class="token punctuation">(</span><span class="token string">"历史记录"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">private</span> <span class="token class-name">String</span> b <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span> <span class="token comment">// 构造方法</span> <span class="token keyword">public</span> <span class="token class-name">Calculator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token string">"Caculator"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//“超”关键字,表示调用父类的构造函数,</span> resultText<span class="token punctuation">.</span><span class="token function">setBounds</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">18</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">,</span> <span class="token number">115</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 设置文本框大小</span> resultText<span class="token punctuation">.</span><span class="token function">setAlignmentX</span><span class="token punctuation">(</span><span class="token constant">RIGHT_ALIGNMENT</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 文本框内容右对齐</span> resultText<span class="token punctuation">.</span><span class="token function">setEditable</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 文本框不允许修改结果</span> <span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">setBounds</span><span class="token punctuation">(</span><span class="token number">290</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">,</span> <span class="token number">250</span><span class="token punctuation">,</span><span class="token number">370</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 设置文本框大小</span> <span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">setAlignmentX</span><span class="token punctuation">(</span><span class="token constant">LEFT_ALIGNMENT</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 文本框内容右对齐</span> <span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">setEditable</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 文本框不允许修改结果</span> label<span class="token punctuation">.</span><span class="token function">setBounds</span><span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置标签位置及大小</span> jp2<span class="token punctuation">.</span><span class="token function">setBounds</span><span class="token punctuation">(</span><span class="token number">290</span><span class="token punctuation">,</span><span class="token number">40</span><span class="token punctuation">,</span><span class="token number">250</span><span class="token punctuation">,</span><span class="token number">370</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置面板窗口位置及大小</span> jp2<span class="token punctuation">.</span><span class="token function">setLayout</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">GridLayout</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">JPanel</span> jp1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JPanel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> jp1<span class="token punctuation">.</span><span class="token function">setBounds</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">18</span><span class="token punctuation">,</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">115</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置面板窗口位置及大小</span> jp1<span class="token punctuation">.</span><span class="token function">setLayout</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">GridLayout</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> resultText<span class="token punctuation">.</span><span class="token function">setLineWrap</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 comment">// 激活自动换行功能</span> resultText<span class="token punctuation">.</span><span class="token function">setWrapStyleWord</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 comment">// 激活断行不断字功能</span> resultText<span class="token punctuation">.</span><span class="token function">setSelectedTextColor</span><span class="token punctuation">(</span><span class="token class-name">Color</span><span class="token punctuation">.</span><span class="token constant">RED</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">setLineWrap</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 comment">//自动换行</span> <span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">setWrapStyleWord</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 class-name">History</span><span class="token punctuation">.</span><span class="token function">setSelectedTextColor</span><span class="token punctuation">(</span><span class="token class-name">Color</span><span class="token punctuation">.</span>blue<span class="token punctuation">)</span><span class="token punctuation">;</span> gdt1<span class="token punctuation">.</span><span class="token function">setViewportView</span><span class="token punctuation">(</span>resultText<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//使滚动条显示出来</span> gdt2<span class="token punctuation">.</span><span class="token function">setViewportView</span><span class="token punctuation">(</span><span class="token class-name">History</span><span class="token punctuation">)</span><span class="token punctuation">;</span> gdt1<span class="token punctuation">.</span><span class="token function">setVerticalScrollBarPolicy</span><span class="token punctuation">(</span><span class="token class-name">JScrollPane</span><span class="token punctuation">.</span><span class="token constant">VERTICAL_SCROLLBAR_ALWAYS</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置让垂直滚动条一直显示</span> gdt2<span class="token punctuation">.</span><span class="token function">setVerticalScrollBarPolicy</span><span class="token punctuation">(</span><span class="token class-name">JScrollPane</span><span class="token punctuation">.</span><span class="token constant">VERTICAL_SCROLLBAR_ALWAYS</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置让垂直滚动条一直显示</span> gdt2<span class="token punctuation">.</span><span class="token function">setHorizontalScrollBarPolicy</span><span class="token punctuation">(</span><span class="token class-name">JScrollPane</span><span class="token punctuation">.</span><span class="token constant">HORIZONTAL_SCROLLBAR_ALWAYS</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//设置让水平滚动条一直显示</span> jp1<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>gdt1<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将滚动条添加入面板窗口中</span> jp2<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>gdt2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>jp1<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将面板添加到总窗体中</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>jp2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将面板添加到总窗体中</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setLayout</span><span class="token punctuation">(</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>label<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 新建“历史记录”标签</span> <span class="token comment">//this.add(resultText);// 新建文本框,该语句会添加进去一个新的JTextArea导致带有滚动条的文本无法显示或者发生覆盖</span> <span class="token comment">//this.add(History);// 新建历史记录文本框,该语句会添加进去一个新的JTextArea导致带有滚动条的文本无法显示</span> <span class="token comment">// 放置按钮</span> <span class="token keyword">int</span> x <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">,</span> y <span class="token operator">=</span> <span class="token number">150</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token constant">KEYS</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> keys<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JButton</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> keys<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token constant">KEYS</span><span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> keys<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">setBounds</span><span class="token punctuation">(</span>x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator"><</span> <span class="token number">215</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> x <span class="token operator">+=</span> <span class="token number">65</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> x <span class="token operator">=</span> <span class="token number">20</span><span class="token punctuation">;</span> y <span class="token operator">+=</span> <span class="token number">45</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>keys<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token constant">KEYS</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">// 每个按钮都注册事件监听器</span> <span class="token punctuation">{</span> keys<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">addActionListener</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setResizable</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setBounds</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">567</span><span class="token punctuation">,</span> <span class="token number">480</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setDefaultCloseOperation</span><span class="token punctuation">(</span><span class="token constant">EXIT_ON_CLOSE</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setVisible</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> <span class="token comment">// 事件处理</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">actionPerformed</span><span class="token punctuation">(</span><span class="token class-name">ActionEvent</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//History.setText(b);//使输入的表达式显示在历史记录文本框中</span> <span class="token class-name">String</span> label<span class="token operator">=</span>e<span class="token punctuation">.</span><span class="token function">getActionCommand</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//获得事件源的标签</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token class-name">Objects</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>label<span class="token punctuation">,</span> <span class="token string">"="</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//</span> <span class="token punctuation">{</span> resultText<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span>resultText<span class="token punctuation">.</span><span class="token function">getText</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>label<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"="</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//调用计算方法,得出最终结果</span> <span class="token punctuation">{</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> s <span class="token operator">=</span><span class="token function">houzhui</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">String</span> result<span class="token operator">=</span><span class="token class-name">Result</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">=</span>result<span class="token operator">+</span><span class="token string">""</span><span class="token punctuation">;</span> <span class="token comment">//更新文本框,当前结果在字符串b中,并未删除,下一次输入接着此结果以实现连续运算</span> resultText<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"="</span><span class="token operator">+</span>resultText<span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"\n"</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><span class="token class-name">Objects</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>label<span class="token punctuation">,</span> <span class="token string">"AC"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token comment">//清空按钮,消除显示屏文本框前面所有的输入和结果</span> <span class="token punctuation">{</span> <span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">=</span><span class="token string">""</span><span class="token punctuation">;</span> resultText<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token string">"0"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//更新文本域的显示,显示初始值;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token class-name">Objects</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>label<span class="token punctuation">,</span> <span class="token string">"sqrt"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> n<span class="token operator">=</span><span class="token function">kfys</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> resultText<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token string">"sqrt"</span><span class="token operator">+</span><span class="token string">"("</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">+</span><span class="token string">")"</span><span class="token operator">+</span><span class="token string">"="</span><span class="token operator">+</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//使运算表达式显示在输入界面</span> <span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">"sqrt"</span><span class="token operator">+</span><span class="token string">"("</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">+</span><span class="token string">")"</span><span class="token operator">+</span><span class="token string">"="</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//获取输入界面的运算表达式并使其显示在历史记录文本框</span> <span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">=</span>n<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><span class="token class-name">Objects</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>label<span class="token punctuation">,</span> <span class="token string">"x*x"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> m<span class="token operator">=</span><span class="token function">pfys</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> resultText<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">+</span><span class="token string">"^2"</span><span class="token operator">+</span><span class="token string">"="</span><span class="token operator">+</span>m<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//使运算表达式显示在输入界面</span> <span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token class-name">History</span><span class="token punctuation">.</span><span class="token function">getText</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">+</span><span class="token string">"^2"</span><span class="token operator">+</span><span class="token string">"="</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//获取输入界面的运算表达式并使其显示在历史记录文本框</span> <span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">=</span>m<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><span class="token class-name">Objects</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>label<span class="token punctuation">,</span> <span class="token string">"e"</span><span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token class-name">Objects</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>label<span class="token punctuation">,</span> <span class="token string">"pi"</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>label<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"e"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> m<span class="token operator">=</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token number">2.71828</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将e的值以字符串的形式传给m</span> <span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">=</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">+</span>m<span class="token punctuation">;</span><span class="token comment">//保留显示m之前输入的运算符或数字字符继续下一步运算</span> resultText<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// History.setText(History.getText()+this.b);</span> <span class="token punctuation">}</span> <span class="token keyword">if</span><span class="token punctuation">(</span>label<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">"pi"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> m<span class="token operator">=</span><span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token number">3.14159265</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">=</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">+</span>m<span class="token punctuation">;</span> resultText<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// History.setText(History.getText()+this.b);</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">this</span><span class="token punctuation">.</span>b<span class="token operator">=</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token operator">+</span>label<span class="token punctuation">;</span> resultText<span class="token punctuation">.</span><span class="token function">setText</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// History.setText(History.getText()+this.b);</span> <span class="token punctuation">}</span> <span class="token comment">//History.setText(History.getText()+this.b);//使输入的表达式显示在历史记录文本框中</span> <span class="token punctuation">}</span> <span class="token comment">//将中缀表达式转换为后缀表达式</span> <span class="token keyword">private</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">houzhui</span><span class="token punctuation">(</span><span class="token class-name">String</span> str<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span><span class="token comment">// 用于承接多位数的字符串</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> opStack <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">// 静态栈,对用户输入的操作符进行处理,用于存储运算符</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> postQueue <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">// 后缀表达式字符串数组,为了将多位数存储为独立的字符串</span> <span class="token keyword">int</span> top <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">// 静态指针top,控制变量j</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> str<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token comment">// 遍历中缀表达式</span> <span class="token comment">// indexof函数,返回字串首次出现的位置;charAt函数返回index位置处的字符;</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">"0123456789."</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">charAt</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 number">0</span><span class="token punctuation">)</span> <span class="token comment">// 遇到数字字符的情况直接入队</span> <span class="token punctuation">{</span> s <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span><span class="token comment">// 作为承接字符,每次开始时都要清空</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token punctuation">;</span> i <span class="token operator"><</span> str<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&&</span> <span class="token string">"0123456789."</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">charAt</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 number">0</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> s <span class="token operator">=</span> s <span class="token operator">+</span> str<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//比如,中缀表达式:234+4*2,我们扫描这个字符串的时候,s的作用相当于用来存储长度为3个字符的操作数:234</span> <span class="token punctuation">}</span> i<span class="token operator">--</span><span class="token punctuation">;</span> postQueue<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> s<span class="token punctuation">;</span><span class="token comment">// 数字字符直接加入后缀表达式</span> j<span class="token operator">++</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><span class="token string">"("</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">charAt</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 number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">// 遇到左括号</span> top<span class="token operator">++</span><span class="token punctuation">;</span> opStack<span class="token punctuation">[</span>top<span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 左括号入栈</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">")"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">charAt</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 number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">// 遇到右括号</span> <span class="token keyword">for</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">// 栈顶元素循环出栈,直到遇到左括号为止</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>opStack<span class="token punctuation">[</span>top<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token char">'('</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">// 栈顶元素不是左括号</span> postQueue<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> opStack<span class="token punctuation">[</span>top<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">""</span><span class="token punctuation">;</span><span class="token comment">// 栈顶元素出栈</span> j<span class="token operator">++</span><span class="token punctuation">;</span> top<span class="token operator">--</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">// 找到栈顶元素是左括号</span> top<span class="token operator">--</span><span class="token punctuation">;</span><span class="token comment">// 删除栈顶左括号</span> <span class="token keyword">break</span><span class="token punctuation">;</span><span class="token comment">// 循环结束</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><span class="token string">"*%/+-"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">charAt</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 number">0</span><span class="token punctuation">)</span><span class="token comment">// 遇到运算符</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>top <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 comment">// 若栈为空则直接入栈</span> top<span class="token operator">++</span><span class="token punctuation">;</span> opStack<span class="token punctuation">[</span>top<span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span>i<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><span class="token string">"*%/"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>opStack<span class="token punctuation">[</span>top<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> <span class="token punctuation">{</span><span class="token comment">// 当栈顶元素为高优先级运算符时,让栈顶元素出栈进入后缀表达式后,当前运算符再入栈</span> postQueue<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> opStack<span class="token punctuation">[</span>top<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">""</span><span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">;</span> opStack<span class="token punctuation">[</span>top<span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span>i<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> top<span class="token operator">++</span><span class="token punctuation">;</span> opStack<span class="token punctuation">[</span>top<span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 当前元素入栈</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>top <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 comment">// 遍历结束后将栈中剩余元素依次出栈进入后缀表达式</span> postQueue<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> opStack<span class="token punctuation">[</span>top<span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token string">""</span><span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">;</span> top<span class="token operator">--</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> postQueue<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//开方运算方法</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">kfys</span><span class="token punctuation">(</span><span class="token class-name">String</span> str<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> result <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span> <span class="token keyword">double</span> a <span class="token operator">=</span> <span class="token class-name">Double</span><span class="token punctuation">.</span><span class="token function">parseDouble</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">,</span> b <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 class-name">Math</span><span class="token punctuation">.</span><span class="token function">sqrt</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> result <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将运算结果转换为string类型并赋给string类型的变量result</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//平方运算方法</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">pfys</span><span class="token punctuation">(</span><span class="token class-name">String</span> str<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> result <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">;</span> <span class="token keyword">double</span> a <span class="token operator">=</span> <span class="token class-name">Double</span><span class="token punctuation">.</span><span class="token function">parseDouble</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">,</span> b <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 class-name">Math</span><span class="token punctuation">.</span><span class="token function">pow</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> result <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> result<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// 计算后缀表达式,并返回最终结果</span> <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token class-name">Result</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> str<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token class-name">Result</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">// 顺序存储的栈,数据类型为字符串</span> <span class="token keyword">int</span> <span class="token class-name">Top</span> <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">// 静态指针Top</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> str<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">;</span> i<span class="token operator">++</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 string">"+-*%/"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">[</span>i<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> <span class="token punctuation">{</span> <span class="token comment">//遇到数字,直接入栈</span> <span class="token class-name">Top</span><span class="token operator">++</span><span class="token punctuation">;</span> <span class="token class-name">Result</span><span class="token punctuation">[</span><span class="token class-name">Top</span><span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">[</span>i<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><span class="token string">"+-*%/"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">[</span>i<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><span class="token comment">// 遇到运算符字符,将栈顶两个元素出栈计算并将结果返回栈顶</span> <span class="token punctuation">{</span> <span class="token keyword">double</span> x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> n<span class="token punctuation">;</span> x <span class="token operator">=</span> <span class="token class-name">Double</span><span class="token punctuation">.</span><span class="token function">parseDouble</span><span class="token punctuation">(</span><span class="token class-name">Result</span><span class="token punctuation">[</span><span class="token class-name">Top</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 顺序出栈两个数字字符串,并转换为double类型</span> <span class="token class-name">Top</span><span class="token operator">--</span><span class="token punctuation">;</span> y <span class="token operator">=</span> <span class="token class-name">Double</span><span class="token punctuation">.</span><span class="token function">parseDouble</span><span class="token punctuation">(</span><span class="token class-name">Result</span><span class="token punctuation">[</span><span class="token class-name">Top</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Top</span><span class="token operator">--</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">"*"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">[</span>i<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> <span class="token punctuation">{</span> n <span class="token operator">=</span> y <span class="token operator">*</span> x<span class="token punctuation">;</span> <span class="token class-name">Top</span><span class="token operator">++</span><span class="token punctuation">;</span> <span class="token class-name">Result</span><span class="token punctuation">[</span><span class="token class-name">Top</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 将运算结果重新入栈</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">"/"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">[</span>i<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> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">// 被除数不允许为0</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token string">"error!"</span><span class="token punctuation">;</span> <span class="token keyword">return</span> s<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> n <span class="token operator">=</span> y <span class="token operator">/</span> x<span class="token punctuation">;</span> <span class="token class-name">Top</span><span class="token operator">++</span><span class="token punctuation">;</span> <span class="token class-name">Result</span><span class="token punctuation">[</span><span class="token class-name">Top</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 将运算结果重新入栈</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 string">"%"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">[</span>i<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> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token comment">// 被除数不允许为0</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token string">"error!"</span><span class="token punctuation">;</span> <span class="token keyword">return</span> s<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> n <span class="token operator">=</span> y <span class="token operator">%</span> x<span class="token punctuation">;</span> <span class="token class-name">Top</span><span class="token operator">++</span><span class="token punctuation">;</span> <span class="token class-name">Result</span><span class="token punctuation">[</span><span class="token class-name">Top</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 将运算结果重新入栈</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 string">"-"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">[</span>i<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> <span class="token punctuation">{</span> n <span class="token operator">=</span> y <span class="token operator">-</span> x<span class="token punctuation">;</span> <span class="token class-name">Top</span><span class="token operator">++</span><span class="token punctuation">;</span> <span class="token class-name">Result</span><span class="token punctuation">[</span><span class="token class-name">Top</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 将运算结果重新入栈</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token string">"+"</span><span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>str<span class="token punctuation">[</span>i<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> <span class="token punctuation">{</span> n <span class="token operator">=</span> y <span class="token operator">+</span> x<span class="token punctuation">;</span> <span class="token class-name">Top</span><span class="token operator">++</span><span class="token punctuation">;</span> <span class="token class-name">Result</span><span class="token punctuation">[</span><span class="token class-name">Top</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 将运算结果重新入栈</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> <span class="token class-name">Result</span><span class="token punctuation">[</span><span class="token class-name">Top</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">// 返回最终结果</span> <span class="token punctuation">}</span> <span class="token comment">// 主函数</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Calculator</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Calculator</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 aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> Java </category>
<category> GUI </category>
</categories>
<tags>
<tag> Java </tag>
<tag> GUI </tag>
<tag> Swing </tag>
</tags>
</entry>
<entry>
<title>c++代码模板</title>
<link href="/2023/11/20/cpp-dai-ma-mo-ban/"/>
<url>/2023/11/20/cpp-dai-ma-mo-ban/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><bits/stdc++.h></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">valid_header1</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifndef</span> <span class="token expression">valid_header</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><any></span> <span class="token comment">//(C++17) std::any class</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><bitset></span> <span class="token comment">//std::bitset class template</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><chrono></span> <span class="token comment">//(C++11)C++ time utilites</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><compare></span> <span class="token comment">//(C++20)Three-way comparison operator support</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><csetjmp></span> <span class="token comment">//Macro (and function) that saves (and jumps) to an execution context</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><csignal></span> <span class="token comment">//Functions and macro constants for signal management</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstdarg></span> <span class="token comment">//Handling of variable length argument lists</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstddef></span> <span class="token comment">//Standard macros and typedefs</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstdlib></span> <span class="token comment">//General purpose utilities: program control, dynamic memory allocation, random numbers, sort and search</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><ctime></span> <span class="token comment">//C-style time/date utilites</span></span><span class="token comment">// #include <expected> //c++23 std::expected class template</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><functional></span> <span class="token comment">//Function objects, Function invocations, Bind operations and Reference wrappers</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><initializer_list></span> <span class="token comment">//(C++11)</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><optional></span> <span class="token comment">//(C++17)std::optional class template</span></span><span class="token comment">// #include <source_location> //(C++20) Supplies means to obtain source code location</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><tuple></span> <span class="token comment">//(C++11) std::tuple class template</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><type_traits></span> <span class="token comment">//(C++11) Compile-time type information</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><typeindex></span> <span class="token comment">//(C++11) std::type_index</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><typeinfo></span> <span class="token comment">//Runtime type information utilities</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><utility></span> <span class="token comment">//Various utility components</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><version></span> <span class="token comment">//(C++20) Supplies implementation-dependent library information</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><variant></span> <span class="token comment">//(C++17) std::variant class template></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><memory></span> <span class="token comment">//High-level memory management utilities</span></span><span class="token comment">// #include <memory_resource> //(C++17) Polymorphic allocators and memory resources</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><scoped_allocator></span> <span class="token comment">//(C++11) Nested allocator class</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><scoped_allocator></span> <span class="token comment">//(C++11) Nested allocator class</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cfloat></span> <span class="token comment">//Limits of floating-point types</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cinttypes></span> <span class="token comment">//(C++11) Formatting macros, intmax_t and uintmax_t math and conversions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><climits></span> <span class="token comment">//Limits of integral types</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstdint></span> <span class="token comment">//(C++11) Fixed-width integer types and limits of other types</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><limits></span> <span class="token comment">//Uniform way to query properties of arithmetic types</span></span><span class="token comment">// #include <stdfloat> //(C++23) Optional extended floating-point types</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cassert></span> <span class="token comment">//Conditionally compiled macro that compares its argument to zero</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cerrno></span> <span class="token comment">//Macro containing the last error number</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><exception></span> <span class="token comment">//Exception handling utilities</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stdexcept></span> <span class="token comment">//Standard exception objects</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><system_error></span> <span class="token comment">//(C++11) Defines std::error_code, a platform-dependent error code</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cctype></span> <span class="token comment">//Functions to determine the category of narrow characters</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><charconv></span> <span class="token comment">//(C++17) std::to_chars and std::from_chars</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstring></span> <span class="token comment">//Various narrow character string handling functions</span></span><span class="token comment">// #include <cuchar> //(C++11)C-style Unicode character conversion functions</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cwchar></span> <span class="token comment">//Various wide and multibyte string handling functions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cwctype></span> <span class="token comment">//Functions to determine the catagory of wide characters</span></span><span class="token comment">//#include <format> //(C++20) Formatting library including std::format</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><string></span> <span class="token comment">//std::basic_string class template</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><array></span> <span class="token comment">//(C++11) std::array container</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><deque></span> <span class="token comment">//std::deque container</span></span><span class="token comment">// #include <flat_map> //(C++23) std::flat_map and std::flat_multimap container adaptors</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><forward_list></span> <span class="token comment">//(C++11) std::forward_list container</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><list></span> <span class="token comment">//std::list container</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><map></span> <span class="token comment">//std::map and std::multimap associative containers</span></span><span class="token comment">// #include <mdspan> //(C++23) std::mdspan view</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><queue></span> <span class="token comment">//std::queue and std::priority_queue container adaptors</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><set></span> <span class="token comment">//std::set and std::multiset associative containers</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><span></span> <span class="token comment">//(C++20) std::span view</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stack></span> <span class="token comment">//std::stack container adaptor</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><unordered_map></span> <span class="token comment">//(C++11) std::unordered_map and std::unordered_multimap unordered associative containers</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><unordered_set></span> <span class="token comment">//(C++11) std::unordered_set and std::unordered_multiset unordered associative containers</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><vector></span> <span class="token comment">//std::vector container</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iterator></span> <span class="token comment">//Range iterators</span></span><span class="token comment">// #include <generator> //(C++23) std::generator class template</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><ranges></span> <span class="token comment">//(C++20) Range access, primitives, requirements, utilities and adaptors</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><algorithm></span> <span class="token comment">//Algorithms that operate on ranges</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><execution></span> <span class="token comment">//(C++17) Predefined execution policies for parallel versions of the algorithms</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><bit></span> <span class="token comment">//(C++20) Bit manipulation functions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cfenv></span> <span class="token comment">//(C++11) Floating-point environment access functions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cmath></span> <span class="token comment">//Common mathematics functions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><complex></span> <span class="token comment">//Complex number type</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><numeric></span> <span class="token comment">//Numeric operations on values in ranges</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><random></span> <span class="token comment">//(C++11) Random number generators and distributions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><ratio></span> <span class="token comment">//(C++11) Compile-time rational arithmetic</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><valarray></span> <span class="token comment">//Class for representing and manipulating arrays of values</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><clocale></span> <span class="token comment">//C localization utilities</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><codecvt></span> <span class="token comment">//(C++11) (deprecated in C++17) Unicode conversion facilities</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><locale></span> <span class="token comment">//Localization utilities</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstdio></span> <span class="token comment">//C-style input-output functions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><fstream></span> <span class="token comment">//std::basic_fstream, std::basic_ifstream, std::basic_ofstream class templates and several typedefs</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iomanip></span> <span class="token comment">//Helper functions to control the format of input and output</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><ios></span> <span class="token comment">//std::ios_base class, std::basic_ios class template and several typedefs</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iosfwd></span> <span class="token comment">//Forward declarations of all classes in the input/output library</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span> <span class="token comment">//Several standard stream objects</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><istream></span> <span class="token comment">//std::basic_istream class template and several typedefs</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><ostream></span> <span class="token comment">//std::basic_ostream, std::basic_iostream class templates and several typedefs</span></span><span class="token comment">// #include <print> //(C++23) Formatted output library including std::print</span><span class="token comment">// #include <spanstream> //(C++23) std::basic_spanstream, std::basic_ispanstream, std::basic_ospanstream class templates and typedefs</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><sstream></span> <span class="token comment">//std::basic_stringstream, std::basic_istringstream, std::basic_ostringstream class templates and several typedefs</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><streambuf></span> <span class="token comment">//std::basic_streambuf class template</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><strstream></span> <span class="token comment">//(deprecated in C++98) std::strstream, std::istrstream, std::ostrstream</span></span><span class="token comment">// #include <syncstream> //(C++20) std::basic_osyncstream, std::basic_syncbuf, and typedefs</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><filesystem></span> <span class="token comment">//(C++17) std::path class and supporting functions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><regex></span> <span class="token comment">//(C++11) Classes, algorithms and iterators to support regular expression processing</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><atomic></span> <span class="token comment">//(C++11) Atomic operations library</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><barrier></span> <span class="token comment">//(C++20) Barriers</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><condition_variable></span> <span class="token comment">//(C++11) Thread waiting conditions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><latch></span> <span class="token comment">//(C++20) Latches</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><mutex></span> <span class="token comment">//(C++11) Mutual exclusion primitives</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><semaphore></span> <span class="token comment">//(C++20) Semaphores</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><shared_mutex></span> <span class="token comment">//(C++14)Shared mutual exclusion primitives</span></span><span class="token comment">// #include <stop_token> //(C++20) Stop tokens for std::jthread</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><thread></span> <span class="token comment">//(C++11) std::thread class and supporting functions</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stdbool.h></span> <span class="token comment">//(C++11) Defines one compatibility macro constant</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><stdalign.h></span> <span class="token comment">//(C++11) Defines one compatibility macro constant</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iso646.h></span> <span class="token comment">//Has no effect</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><ciso646></span> <span class="token comment">//(removed in C++20) Empty header. The macros that appear in iso646.h in C</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cstdalign></span> <span class="token comment">//(C++11)(deprecated in C++17)(removed in C++20) Defines one compatibility macro constant</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iso646.h></span> <span class="token comment">//Has no effect</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><numbers></span> <span class="token comment">//</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token double-colon punctuation">::</span>numbers<span class="token punctuation">;</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">GCC <span class="token function">optimize</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">GCC <span class="token function">optimize</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">pragma</span> <span class="token expression">GCC <span class="token function">optimize</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span></span><span class="token string">"ofast"</span><span class="token expression"><span class="token punctuation">,</span></span><span class="token string">"inline"</span><span class="token expression"><span class="token punctuation">)</span></span></span><span class="token keyword">typedef</span> <span class="token keyword">unsigned</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ull<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">double</span> doubleL<span class="token punctuation">;</span><span class="token keyword">typedef</span> <span class="token keyword">long</span> <span class="token keyword">long</span> ll<span class="token punctuation">;</span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">_SILENCE_CXX20_CISO646_REMOED_WARNING</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">fspr</span><span class="token expression"><span class="token punctuation">(</span>n<span class="token punctuation">)</span> fixed <span class="token operator"><<</span> <span class="token function">setprecision</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">spr</span><span class="token expression"><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token function">setprecision</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">sci</span> <span class="token expression"><span class="token function">setiosflags</span><span class="token punctuation">(</span>ios<span class="token double-colon punctuation">::</span>scientific<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">siosf</span> <span class="token expression">setiosflags</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">fspr</span><span class="token expression"><span class="token punctuation">(</span>n<span class="token punctuation">)</span> fixed <span class="token operator"><<</span> <span class="token function">setprecision</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">spr</span><span class="token expression"><span class="token punctuation">(</span>n<span class="token punctuation">)</span> <span class="token function">setprecision</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">sci</span> <span class="token expression"><span class="token function">setiosflags</span><span class="token punctuation">(</span>ios<span class="token double-colon punctuation">::</span>scientific<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">siosf</span> <span class="token expression">setiosflags</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">endl</span> <span class="token char">'\n'</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">ifor</span><span class="token expression"><span class="token punctuation">(</span>i<span class="token punctuation">,</span> l<span class="token punctuation">,</span> r<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> l<span class="token punctuation">;</span> i <span class="token operator"><=</span> r<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">rfor</span><span class="token expression"><span class="token punctuation">(</span>i<span class="token punctuation">,</span> r<span class="token punctuation">,</span> l<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> r<span class="token punctuation">;</span> i <span class="token operator">>=</span> l<span class="token punctuation">;</span> <span class="token operator">--</span>i<span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">npos</span> <span class="token expression">string<span class="token double-colon punctuation">::</span>npos</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">fir</span> <span class="token expression">first</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">sec</span> <span class="token expression">second</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">pb</span> <span class="token expression">push_back</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">pf</span> <span class="token expression">push_front</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">type</span> <span class="token expression"><span class="token keyword">decltype</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">inlast</span><span class="token expression"><span class="token punctuation">(</span>a<span class="token punctuation">)</span> a<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">itlast</span><span class="token expression"><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token function">prev</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">all</span><span class="token expression"><span class="token punctuation">(</span>v<span class="token punctuation">)</span> v<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>v<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span></span><span class="token keyword">template</span> <span class="token operator"><</span><span class="token keyword">class</span> <span class="token class-name">T</span><span class="token punctuation">,</span> <span class="token keyword">class</span> <span class="token class-name">Container</span> <span class="token operator">=</span> vector<span class="token operator"><</span>T<span class="token operator">></span><span class="token punctuation">,</span><span class="token keyword">class</span> <span class="token class-name">Compare</span> <span class="token operator">=</span> less<span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">Container</span><span class="token double-colon punctuation">::</span>value_type<span class="token operator">></span> <span class="token operator">></span><span class="token keyword">using</span> priqueue <span class="token operator">=</span> priority_queue<span class="token operator"><</span>T<span class="token punctuation">,</span> Container<span class="token punctuation">,</span> Compare<span class="token operator">></span><span class="token punctuation">;</span><span class="token keyword">namespace</span> cus <span class="token punctuation">{</span><span class="token comment">//standard output</span><span class="token keyword">template</span><span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">container</span><span class="token operator">></span><span class="token keyword">void</span> <span class="token function">print</span><span class="token punctuation">(</span>container<span class="token operator">&</span> a2<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> i <span class="token operator">=</span> a2<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i <span class="token operator">!=</span> a2<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>cout <span class="token operator"><<</span> <span class="token operator">*</span>i <span class="token operator"><<</span> <span class="token string">" \n"</span><span class="token punctuation">[</span>i <span class="token operator">==</span> <span class="token function">prev</span><span class="token punctuation">(</span>a2<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</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 punctuation">;</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token keyword">template</span><span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">T1</span><span class="token operator">></span><span class="token keyword">auto</span> <span class="token function">print</span><span class="token punctuation">(</span>T1 a<span class="token punctuation">,</span> ll l<span class="token punctuation">,</span> ll r<span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token keyword">void</span> <span class="token punctuation">{</span><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">long</span> i <span class="token operator">=</span> l<span class="token punctuation">;</span> i <span class="token operator"><=</span> r<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>cout <span class="token operator"><<</span> a<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator"><<</span> <span class="token string">" /n"</span><span class="token punctuation">[</span>i <span class="token operator">==</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">//__int128 write</span><span class="token keyword">template</span><span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">></span><span class="token keyword">void</span> <span class="token function">read</span><span class="token punctuation">(</span>T<span class="token operator">&</span> w<span class="token punctuation">)</span> <span class="token punctuation">{</span>w <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>T f <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span><span class="token keyword">char</span> ch <span class="token operator">=</span> <span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">while</span> <span class="token punctuation">(</span>ch <span class="token operator"><</span> <span class="token char">'0'</span> <span class="token operator">||</span> ch <span class="token operator">></span> <span class="token char">'9'</span><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 char">'-'</span><span class="token punctuation">)</span>f <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>ch <span class="token operator">=</span> <span class="token function">getchar</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">while</span> <span class="token punctuation">(</span>ch <span class="token operator"><=</span> <span class="token char">'9'</span> <span class="token operator">&&</span> ch <span class="token operator">>=</span> <span class="token char">'0'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>w <span class="token operator">=</span> w <span class="token operator">*</span> <span class="token number">10</span> <span class="token operator">+</span> ch <span class="token operator">-</span> <span class="token char">'0'</span><span class="token punctuation">;</span>ch <span class="token operator">=</span> <span class="token function">getchar</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">template</span><span class="token operator"><</span><span class="token keyword">typename</span> <span class="token class-name">T</span><span class="token operator">></span><span class="token keyword">void</span> <span class="token function">print128</span><span class="token punctuation">(</span>T x<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">if</span> <span class="token punctuation">(</span>x <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">putchar</span><span class="token punctuation">(</span><span class="token char">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span>x <span class="token operator">=</span> <span class="token operator">-</span>x<span class="token punctuation">;</span><span class="token keyword">if</span> <span class="token punctuation">(</span>x <span class="token operator">></span> <span class="token number">9</span><span class="token punctuation">)</span> <span class="token function">print128</span><span class="token punctuation">(</span>x <span class="token operator">/</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">putchar</span><span class="token punctuation">(</span>x <span class="token operator">%</span> <span class="token number">10</span> <span class="token operator">+</span> <span class="token char">'0'</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">const</span> <span class="token keyword">int</span> IINF <span class="token operator">=</span> <span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">long</span> LINF <span class="token operator">=</span> <span class="token number">0x3f3f3f3f3f3f3f3f</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">double</span> EPS <span class="token operator">=</span> <span class="token number">1.0e-9</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">long</span> MOD <span class="token operator">=</span> <span class="token number">1e9</span> <span class="token operator">+</span> <span class="token number">7</span><span class="token punctuation">;</span><span class="token keyword">const</span> <span class="token keyword">int</span> N <span class="token operator">=</span> <span class="token number">2e5</span> <span class="token operator">+</span> <span class="token number">100</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> args<span class="token punctuation">,</span> <span class="token keyword">char</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token comment">//ios::sync_with_stdio(false);</span><span class="token comment">//cin.tie(nullptr);</span><span class="token comment">//cin.tie(nullptr);</span><span class="token comment">//auto start = std::chrono::steady_clock::now();</span><span class="token comment">//auto end = std::chrono::steady_clock::now();</span><span class="token comment">//std::chrono::duration<double> diff = end - start;</span><span class="token comment">//std::cout << diff.count() << " s\n";</span><span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> c++ </category>
<category> OJ </category>
</categories>
<tags>
<tag> c++ </tag>
<tag> OJ </tag>
</tags>
</entry>
<entry>
<title>git命令大全</title>
<link href="/2023/11/20/git-ming-ling-da-quan/"/>
<url>/2023/11/20/git-ming-ling-da-quan/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="Git指南"><a href="#Git指南" class="headerlink" title="Git指南"></a>Git指南</h1><h2 id="git-checkout-切换分支"><a href="#git-checkout-切换分支" class="headerlink" title="git checkout 切换分支"></a><code>git checkout</code> 切换分支</h2><p>是的,<code>git checkout</code> 命令可以用于切换到不同的提交。您可以使用该命令来查看历史记录中的旧版本,或者在不同的分支和标签之间切换。</p><p>要切换到特定的提交,您可以使用以下命令:</p><pre class="line-numbers language-none"><code class="language-none">git checkout <commit><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这会将您的工作目录和索引设置为提交 <code><commit></code> 所表示的状态。请注意,这将会使您处于“分离 HEAD”状态,因此您不能直接对该提交进行修改。如果您想要在此基础上进行修改并提交更改,您需要创建一个新的分支。</p><p>如果您想要返回到当前分支的最新提交,您可以使用以下命令:</p><pre class="line-numbers language-none"><code class="language-none">git checkout <branch><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这会将您的工作目录和索引设置为分支 <code><branch></code> 的最新提交。</p><p>如果您想要在不同的分支之间切换,您可以使用以下命令:</p><pre class="line-numbers language-none"><code class="language-none">git checkout <branch><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这会将您的工作目录和索引设置为分支 <code><branch></code> 的最新提交,并且将您的 HEAD 指针移动到该分支。</p><p>请注意,<code>git checkout</code> 命令可能会更改您的工作目录和索引,因此请确保您已经保存了任何未提交的更改,并且在执行此类操作之前备份您的项目。</p><h2 id="移除已经tracked的文件"><a href="#移除已经tracked的文件" class="headerlink" title="移除已经tracked的文件"></a>移除已经<code>tracked</code>的文件</h2><p>根据您提供的信息,您已经添加了.gitignore文件来忽略node_modules和unpackage目录。然而,您发现即使在这些目录下的文件内容发生更改后,Git仍然会检测到并提交这些更改。</p><p>这可能是因为这些文件已经被Git跟踪并包含在先前的提交中。一旦Git开始跟踪某个文件,即使将其添加到.gitignore文件中,Git仍然会继续跟踪该文件的更改。</p><p>为了解决这个问题,您需要告诉Git停止跟踪这些文件。您可以使用以下命令将这些文件从Git中移除:</p><pre class="line-numbers language-none"><code class="language-none">git rm -r --cached node_modules/git rm -r --cached unpackage/<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>执行这些命令后,Git将停止跟踪这些文件的更改,并且它们将不再出现在未来的提交中。请确保在执行这些命令之前备份任何重要的文件。</p><p>完成此操作后,您的.gitignore文件应该能够正确地忽略这些文件夹及其内容的更改。</p><h2 id="首次关联远端分支"><a href="#首次关联远端分支" class="headerlink" title="首次关联远端分支"></a>首次关联远端分支</h2><p><code>git push -u <仓库名字> <仓库分支,没有就会新创建一个></code></p><h2 id="查询远端仓库信息"><a href="#查询远端仓库信息" class="headerlink" title="查询远端仓库信息"></a>查询远端仓库信息</h2><p>要查询远程仓库中有哪些分支,可以使用以下命令:</p><pre class="line-numbers language-none"><code class="language-none">git ls-remote --heads <repository-url>|或者是已经关联的远端仓库的名字<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>请将<code><repository-url></code>替换为远程仓库的URL。执行此命令后,Git会输出远程仓库中所有分支的引用,类似于以下示例:</p><pre class="line-numbers language-none"><code class="language-none">b3e5b1c2e9e8a937b7c1faa9e8c8dbb6d8acab13 refs/heads/branch12a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1 refs/heads/branch2a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0 refs/heads/branch3<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>其中每一行都包含一个提交的哈希值(SHA-1),后面紧跟着分支的完整引用名称(包括<code>refs/heads/</code>前缀)和分支的名称。这个命令可以帮助您了解远程仓库中所有的分支,以便您可以进行相应的操作,例如克隆特定的分支或推送本地分支到远程仓库。</p><h2 id="git配置本地用户名和邮箱"><a href="#git配置本地用户名和邮箱" class="headerlink" title="git配置本地用户名和邮箱"></a>git配置本地用户名和邮箱</h2><ol><li>查询用户名和email<br><code>git config user.email</code>和<code>git config user.name</code></li><li>配置用户名和email<pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git config --global user.name "Acaibird"git config --global user.email "13234966+a-caibird@user.noreply.gitee.com"<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></li></ol><h2 id="克隆特定的分支"><a href="#克隆特定的分支" class="headerlink" title="克隆特定的分支"></a>克隆特定的分支</h2><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git clone -b <branch-name> --single-branch <repository-url> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>请将<code><branch-name></code>替换为您想要克隆的分支名称,将<code><repository-url></code>替换为远程仓库的URL。例如,要克隆名为<code>develop</code>的分支,可以执行以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git clone -b develop --single-branch <repository-url><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将只克隆指定的分支,并且只会拉取该分支的历史记录,从而节省时间和带宽。如果您不使用<code>--single-branch</code>选项,则默认情况下将克隆所有分支。</p><h2 id="克隆到指定本地目录"><a href="#克隆到指定本地目录" class="headerlink" title="克隆到指定本地目录"></a>克隆到指定本地目录</h2><p>是的,可以在 <code>git clone</code> 命令后面加上一个目录名称参数,来指定将仓库克隆到的本地目录名称。具体操作方式如下:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git clone <repository> <directory><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>其中,<code><repository></code> 是要克隆的仓库 URL, <code><directory></code> 是要将仓库克隆到的本地目录名称。例如,要将名为 <code>my-project</code> 的仓库克隆到本地的 <code>my-local-project</code> 目录下,可以使用以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git clone https://github.com/username/my-project.git my-local-project<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这样就会将 <code>my-project</code> 仓库中的文件克隆到本地的 <code>my-local-project</code> 目录下。</p><h2 id="合并远端分支到本地分支"><a href="#合并远端分支到本地分支" class="headerlink" title="合并远端分支到本地分支"></a>合并远端分支到本地分支</h2><p>如果您的远程仓库有最新的提交,但执行 <code>git fetch</code> 后没有更新到本地仓库,可能有几个可能的原因:</p><p>如果您确认远程仓库的配置正确,并且最新的提交确实存在于当前分支的关联远程分支上,您可以尝试使用以下命令将远程分支的更新合并到本地分支:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git merge origin/branch-name<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>其中,<code>origin/branch-name</code> 是包含最新提交的远程分支的名称。请将其替换为实际的远程分支名称。</p><p>如果以上解决方案无效,还可以尝试使用 <code>git pull</code> 命令,它会自动执行 <code>git fetch</code> 并尝试合并更新到当前分支:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git pull origin branch-name<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>同样,<code>origin</code> 是远程仓库的名称,<code>branch-name</code> 是包含最新提交的远程分支的名称。</p><p>如果问题仍然存在,建议检查您的网络连接和远程仓库的状态,以确保能够正常连接和获取更新。</p><h2 id="删除远端分支"><a href="#删除远端分支" class="headerlink" title="删除远端分支"></a>删除远端分支</h2><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">acaibird@localhost f2 % git branch --allamasterremotes/shop/aremotes/shop/masteracaibird@localhost f2 %这个分支remotes/shop/master是在我本地吗,能删除吗中文回答我<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><code>remotes/shop/master</code> 是一个远程分支,它表示在服务器上的 <code>shop/master</code> 分支。它并不是在你的本地仓库,而是你本地仓库的一个远程跟踪分支,用于跟踪服务器上的 <code>shop/master</code> 分支。因此,你无法直接删除它。</p><p>如果你要删除服务器上的 <code>shop/master</code> 分支,你可以使用以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git push origin --delete shop/master<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这个命令会从服务器上删除 <code>shop/master</code> 分支。如果你不想删除服务器上的分支,你可以继续保留这个远程跟踪分支。</p><h2 id="删除远端分支最近三次提交"><a href="#删除远端分支最近三次提交" class="headerlink" title="删除远端分支最近三次提交"></a>删除远端分支最近三次提交</h2><h3 id="解决方法1:这是删除最新的三个提交才有效"><a href="#解决方法1:这是删除最新的三个提交才有效" class="headerlink" title="解决方法1:这是删除最新的三个提交才有效"></a>解决方法1:这是删除最新的三个提交才有效</h3><p>要删除远程仓库的<code>changed</code>分支上的三个提交,您可以按照以下步骤进行操作:</p><ol><li><p>首先,使用以下命令从远程仓库拉取最新的分支信息:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git fetch origin changed<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>然后,使用以下命令查看要删除的提交的哈希值:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git log origin/changed<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将列出<code>changed</code>分支的所有提交,您需要找到要删除的三个提交的哈希值。</p></li><li><p>接下来,使用以下命令将<code>changed</code>分支重置到要删除的提交的前一个提交:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git reset --hard <前一个提交的哈希值><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>请确保将<code><前一个提交的哈希值></code>替换为您要删除的第一个提交的哈希值。</p></li><li><p>然后,使用以下命令将更改推送到远程仓库:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git push origin -f changed<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><code>-f</code>标志表示强制推送,这将覆盖远程仓库中的<code>changed</code>分支。请注意,这可能会影响其他人的工作,因此请确保在操作之前与团队进行沟通。</p></li></ol><p>这样,您就可以成功地从远程仓库的<code>changed</code>分支中删除所需的三个提交。</p><h3 id="解决方法2-删除任意三个提交-git-rebase"><a href="#解决方法2-删除任意三个提交-git-rebase" class="headerlink" title="解决方法2 :删除任意三个提交 git rebase "></a>解决方法2 :删除任意三个提交 <code>git rebase </code></h3><p>非常抱歉我误解了您的问题。如果您要删除远程仓库<code>changed</code>分支上的任意三个提交,您可以按照以下步骤进行操作:</p><ol><li><p>使用以下命令获取远程<code>changed</code>分支的所有提交记录:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git fetch origin changed<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>使用以下命令列出<code>changed</code>分支的所有提交记录:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git log origin/changed<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将列出<code>changed</code>分支的所有提交记录,包括每个提交的哈希值和提交信息,以便您可以确定要删除的三个提交的哈希值。</p></li><li><p>使用以下命令创建一个新分支,该分支将从<code>changed</code>分支的最新提交开始:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git checkout -b temp origin/changed# 可以不要这一步<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre></li><li><p>使用以下命令对新分支进行交互式重写历史记录:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git rebase -i HEAD~<提交数目><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>在这里,<code><提交数目></code>应该是从最新的提交开始数的三个提交的数量。例如,如果您要删除最新的三个提交,则应将<code><提交数目></code>设置为3。</p><p>这将打开一个文本编辑器,其中包含新分支的提交历史记录。在编辑器中,将要删除的提交的行修改为<code>squash</code>,保存并关闭文件。要删除的提交最晚的一个提交不需要修改成<code>squash</code></p></li><li><p>使用以下命令将新分支推送到远程仓库:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git push -f origin temp:changed<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将强制将新分支推送到远程<code>changed</code>分支,并覆盖原始的<code>changed</code>分支。请注意,这可能会影响其他开发人员的工作,因此请确保在操作之前与团队进行沟通。</p></li><li><p>最后,删除临时分支:(没有建立临时分支这个也不用要了)</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git branch -D temp<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将删除本地的临时分支。</p></li></ol><p>这样,您就可以成功地从远程仓库的<code>changed</code>分支上删除任意三个提交。请注意,这种方法会重写历史记录,因此请确保在操作之前备份重要的数据。</p><h2 id="查看本地分支和远端分支关联情况"><a href="#查看本地分支和远端分支关联情况" class="headerlink" title="查看本地分支和远端分支关联情况"></a>查看本地分支和远端分支关联情况</h2><p>本地分支与远程分支没有关联:<code>git fetch</code> 只会更新与当前分支关联的远程分支。如果您的本地分支与远程分支没有明确的关联,<code>git fetch</code> 不会自动将更新拉取到本地分支。您可以使用 <code>git branch -vv</code> 命令来查看本地分支与远程分支的关联情况。</p><h2 id="删除本地的部分提交"><a href="#删除本地的部分提交" class="headerlink" title="删除本地的部分提交"></a>删除本地的部分提交</h2><p>要从本地 Git 项目中删除一个提交,可以使用 <code>git reset</code> 命令。请注意,这将会撤销该提交以及此后的所有提交,并且会永久性地删除它们。因此,在执行此操作之前,请确保您确实希望删除这些提交。</p><p>以下是一些可能有用的 <code>git reset</code> 命令选项:</p><ol><li><p><code>git reset --soft <commit></code>:此选项将会撤销指定提交及其之后的所有提交,但是不会修改您的工作目录或暂存区。这意味着您可以重新提交这些更改。</p></li><li><p><code>git reset --mixed <commit></code>:此选项将会撤销指定提交及其之后的所有提交,并且将更改还原到暂存区。这意味着您需要重新提交这些更改。</p></li><li><p><code>git reset --hard <commit></code>:此选项将会撤销指定提交及其之后的所有提交,并且将更改还原到您上次提交时的状态。这意味着您将永久性地删除这些更改,所以请确保您已经备份了这些更改。</p></li></ol><p>请注意,<commit> 参数可以是提交的 SHA-1 标识符、分支名称或标签名称。</commit></p><p>例如,如果您想要删除最新的提交并且撤销它对工作目录和暂存区的影响,您可以运行以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git reset --soft HEAD~1<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>如果您想要彻底删除最新的提交并且永久性地删除它的更改,您可以运行以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git reset --hard HEAD~1<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>请注意,这些命令可能会更改您的 Git 项目历史记录,因此请确保您知道自己在做什么,并且请在执行此类操作之前备份您的项目。</p><h2 id="取消本地分支与远端分支的关联情况"><a href="#取消本地分支与远端分支的关联情况" class="headerlink" title="取消本地分支与远端分支的关联情况"></a>取消本地分支与远端分支的关联情况</h2><p>如果您想要将本地分支 “a” 与远程分支 “a”、”b” 和 “c” 关联,但不再与远程分支 “d” 关联,可以使用以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git branch --unset-upstream d<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将取消本地分支 “d” 与远程分支的关联。如果您想要完全删除本地分支 “d”,可以使用以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git branch -d d<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>请注意,如果 “d” 分支上有未合并的更改,则必须使用 <code>-D</code> 选项删除该分支,即:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git branch -D d<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>如果您要将本地分支 “a” 与另一个远程分支关联,可以使用以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git branch -u <remote_name>/<branch_name> a<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>其中,<code><remote_name></code> 是远程仓库的名称,<code><branch_name></code> 是您要关联的远程分支的名称。</p><h2 id="本地分支关联多个远仓库分支"><a href="#本地分支关联多个远仓库分支" class="headerlink" title="本地分支关联多个远仓库分支"></a>本地分支关联多个远仓库分支</h2><p>是的,一个本地分支可以关联多个远程分支。</p><p>在Git中,一个本地分支可以与多个远程分支进行关联。这通常会在以下情况下使用:</p><ol><li>你想将本地分支推送到多个远程分支,以便在不同的服务器上部署应用程序。</li><li>你想在多个远程分支之间同步代码,以便在不同的开发环境中协作开发应用程序。</li></ol><p>要将本地分支与多个远程分支关联起来,可以使用以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git branch --set-upstream-to=<remote>/<branch> <local-branch><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>其中,<code><remote></code>是远程仓库的名称,<code><branch></code>是远程分支的名称,<code><local-branch></code>是本地分支的名称。</p><p>例如,要将本地分支<code>feature</code>与远程仓库<code>origin</code>下的两个远程分支<code>branch1</code>和<code>branch2</code>关联起来,可以使用以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git branch --set-upstream-to=origin/branch1 featuregit branch --set-upstream-to=origin/branch2 feature<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>这将使得本地分支<code>feature</code>能够与远程分支<code>origin/branch1</code>和<code>origin/branch2</code>进行关联。</p><h2 id="设置-git-仓库默认分支"><a href="#设置-git-仓库默认分支" class="headerlink" title="设置 git 仓库默认分支"></a>设置 git 仓库默认分支</h2><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git config --global init.defaultBranch <name><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h2 id="Windows-下换行符号和-mac不一样,解决方式"><a href="#Windows-下换行符号和-mac不一样,解决方式" class="headerlink" title="Windows 下换行符号和 mac不一样,解决方式"></a>Windows 下换行符号和 mac不一样,解决方式</h2><h3 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h3><p>git 拉取仓库文件的时候,如果是mac os和window系统同时编辑文件,会导致换行符号不同,而出现不必要的change</p><h3 id="window和类unix的换行符号不同"><a href="#window和类unix的换行符号不同" class="headerlink" title="window和类unix的换行符号不同"></a>window和类unix的换行符号不同</h3><p>修改前对这个文件进行处理,将其换行符转换为适合在 Mac 上使用的形式。常用的换行符有两种:Windows 使用回车换行符(CR+LF,\r\n),而 Mac 和 Linux 使用换行符(LF,\n)。</p><p>可以使用 <code>git config</code> 命令来设置 Git 的自动换行符转换功能,以便在从 Windows 环境中拉取文件时自动将换行符转换为适合在 Mac 环境中使用的形式。执行以下命令即可:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git config --global core.autocrlf input<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将设置 Git 在检出文件时自动将回车换行符转换为换行符。如果在提交文件时需要将换行符转换回回车换行符,则可以将 <code>core.autocrlf</code> 设置为 <code>true</code>。</p><p>如果已经将文件拉取到本地,可以使用 <code>dos2unix</code> 命令将回车换行符转换为换行符,或使用 <code>unix2dos</code> 命令将换行符转换为回车换行符。这些命令通常可以在 Mac 上的终端中使用,可以使用 <code>brew</code> 包管理器安装它们。例如,执行以下命令将 <code>example.txt</code> 文件中的回车换行符转换为换行符:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">dos2unix example.txt<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>转换后的文件可以在 Mac 上进行修改,然后可以再次使用 <code>unix2dos</code> 命令将换行符转换回回车换行符,以便在 Windows 环境中使用。</p><h3 id="解决方法"><a href="#解决方法" class="headerlink" title="解决方法"></a>解决方法</h3><h4 id="配置git-config解决"><a href="#配置git-config解决" class="headerlink" title="配置git config解决"></a>配置git config解决</h4><p>在Git中,换行符有两种常见的形式:Unix风格的LF(Line Feed)和Windows风格的CRLF(Carriage Return 和 Line Feed)。Git默认将所有换行符转换为LF格式,这在大多数情况下都是可以工作的。但在某些情况下,你可能需要使用不同的换行符格式。</p><p>要配置Git使用不同的换行符格式,可以使用以下两个配置选项:</p><ol><li><p><code>core.autocrlf</code>:指定Git是否应该自动将CRLF转换为LF(在提交时)或LF转换为CRLF(在检出文件时)。这个选项有三个可能的值:</p><ul><li><code>true</code>:在提交时自动将CRLF转换为LF,在检出文件时自动将LF转换为CRLF。</li><li><code>false</code>:不进行任何自动转换。</li><li><code>input</code>:在提交时自动将CRLF转换为LF,但在检出文件时不进行任何转换。</li></ul><p>例如,要设置Git在提交时自动将CRLF转换为LF,在检出文件时不进行任何转换,可以运行以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git config --global core.autocrlf input<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将在全局范围内设置<code>core.autocrlf</code>选项。</p></li><li><p><code>core.eol</code>:指定Git应该在检出文件时使用的换行符类型。这个选项有两个可能的值:</p><ul><li><code>lf</code>:使用LF换行符。</li><li><code>crlf</code>:使用CRLF换行符。</li></ul><p>例如,要设置Git在检出文件时使用CRLF换行符,可以运行以下命令:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">git config --global core.eol crlf<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将在全局范围内设置<code>core.eol</code>选项。</p></li></ol><p>请注意,如果你在跨平台开发中使用Git,使用正确的换行符格式非常重要。如果你在Windows上编写代码,但在Linux或macOS上进行构建,那么确保你的代码在不同平台上都可以正常构建和运行,可能需要使用不同的换行符格式。</p><h4 id="使用第三方包转换"><a href="#使用第三方包转换" class="headerlink" title="使用第三方包转换"></a>使用第三方包转换</h4><p>在 Mac 上,可以使用 <code>dos2unix</code> 和 <code>unix2dos</code> 工具将文件的换行符进行转换,以便在不同的操作系统之间共享文件时能够正常工作。这些工具通常可以通过命令行安装,例如可以使用 <code>brew</code> 包管理器来安装它们。</p><p>以下是如何使用 <code>dos2unix</code> 和 <code>unix2dos</code> 工具将文件的换行符进行转换的步骤:</p><ol><li><p>使用终端进入到存放需要转换的文件的目录下。</p></li><li><p>执行以下命令安装 <code>dos2unix</code> 工具:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">brew install dos2unix<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>执行以下命令将文件中的回车换行符转换为换行符:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">dos2unix example.txt<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将把 <code>example.txt</code> 文件中的回车换行符转换为换行符,并保存到同名文件中。</p></li><li><p>在 Mac 上使用文本编辑器修改文件。</p></li><li><p>如果需要将文件重新转换为回车换行符的形式,可以执行以下命令安装 <code>unix2dos</code> 工具:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">brew install unix2dos<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>执行以下命令将文件中的换行符转换为回车换行符:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">unix2dos example.txt<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将把 <code>example.txt</code> 文件中的换行符转换为回车换行符,并保存到同名文件中。</p></li><li><p>批量转换<br>执行以下命令批量转换文件夹内所有文件及其子文件夹下的所有文件的换行符:</p><pre class="line-numbers language-zsh" data-language="zsh"><code class="language-zsh">Copyfind . -type f -exec dos2unix {} \;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>这将在当前目录下查找所有文件,并对每个文件执行 dos2unix 命令,从而将文件中的回车换行符转换为换行符。</p></li></ol><p>需要注意的是,这些工具在转换换行符时可能会修改文件的其他部分。因此,在进行换行符转换之前,应该备份文件的原始副本,并在转换后仔细检查文件的内容,以确保没有意外的修改。</p><h2 id="配置GMC自动配置和http凭证"><a href="#配置GMC自动配置和http凭证" class="headerlink" title="配置GMC自动配置和http凭证"></a>配置GMC自动配置和http凭证</h2><p><a href="https://docs.github.com/zh/get-started/getting-started-with-git/caching-your-github-credentials-in-git">方法</a></p>]]></content>
<categories>
<category> Git </category>
</categories>
<tags>
<tag> Git </tag>
</tags>
</entry>
<entry>
<title>mysql 储存过程</title>
<link href="/2023/11/20/mysql-chu-cun-guo-cheng/"/>
<url>/2023/11/20/mysql-chu-cun-guo-cheng/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="mysql储存过程"><a href="#mysql储存过程" class="headerlink" title="mysql储存过程"></a>mysql储存过程</h1><h2 id="成绩-学生信息表定义"><a href="#成绩-学生信息表定义" class="headerlink" title="成绩,学生信息表定义"></a>成绩,学生信息表定义</h2><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">CREATE TABLE exam ( examno VARCHAR(50), stuno VARCHAR(50) PRIMARY KEY, exam1 DOUBLE, exam2 DOUBLE) DEFAULT CHARSET=utf8;CREATE TABLE `stuinfo` ( `stuname` varchar(10) DEFAULT NULL, `stuno` varchar(10) DEFAULT NULL, `stusex` char(2) DEFAULT NULL, `stuage` int DEFAULT NULL, `stuseat` int DEFAULT NULL, `stuaddress` varchar(20) DEFAULT NULL, `lost` char(10) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3-- 查询表的完整定义show create table stuinfo <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="储存过程"><a href="#储存过程" class="headerlink" title="储存过程"></a>储存过程</h2><h3 id="新建一列-给成绩分类"><a href="#新建一列-给成绩分类" class="headerlink" title="新建一列,给成绩分类"></a>新建一列,给成绩分类</h3><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">DELIMITER //CREATE PROCEDURE CalculateGrade()BEGIN ALTER TABLE exam ADD COLUMN grade VARCHAR(10); UPDATE exam SET grade = CASE WHEN (exam1 + exam2) < 60 THEN '不及格' WHEN (exam1 + exam2) >= 60 AND (exam1 + exam2) < 80 THEN '及格' WHEN (exam1 + exam2) >= 80 AND (exam1 + exam2) < 90 THEN '良好' ELSE '优秀' END;END //DELIMITER ;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="新建一个列-表示那些是缺考了-并输出及格率"><a href="#新建一个列-表示那些是缺考了-并输出及格率" class="headerlink" title="新建一个列,表示那些是缺考了,并输出及格率"></a>新建一个列,表示那些是缺考了,并输出及格率</h3><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">drop procedure statistics;delimiter //create procedure statistics()begin DECLARE total_students INT; DECLARE passed_students INT; DECLARE pass_rate DECIMAL(5, 2); SELECT COUNT(*) INTO total_students FROM exam; SELECT COUNT(*) INTO passed_students FROM exam WHERE (exam1 + exam2) >= 60; SET pass_rate = (passed_students / total_students) * 100; SELECT CONCAT('及格率为:', pass_rate, '%') AS pass_rate; alter table stuinfo add column lost char(10); UPDATE stuinfo SET lost = CASE WHEN stuno NOT IN (SELECT stuno FROM exam) THEN '缺考' ELSE 'OK' END;end //delimiter ;call statistics();<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>对应的mysql查询语句</p><pre class="line-numbers language-mysql" data-language="mysql"><code class="language-mysql">-- 获取总学生数SELECT COUNT(*) AS total_students FROM exam;-- 获取及格学生数SELECT COUNT(*) AS passed_students FROM exam WHERE (exam1 + exam2) >= 60;-- 计算及格率SELECT CONCAT('及格率为:', (passed_students / total_students) * 100, '%') AS pass_rate;-- 添加lost列到stuinfo表ALTER TABLE stuinfo ADD COLUMN lost CHAR(10);-- 更新lost列的值UPDATE stuinfoSET lost = CASE WHEN stuno NOT IN (SELECT stuno FROM exam) THEN '缺考' ELSE 'OK'END;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="exam输入及格线-统计及格率"><a href="#exam输入及格线-统计及格率" class="headerlink" title="exam输入及格线,统计及格率"></a>exam输入及格线,统计及格率</h3><pre class="line-numbers language-MySQL" data-language="MySQL"><code class="language-MySQL">show create table stuinfo;DELIMITER //CREATE PROCEDURE maxmin(IN p_stusex CHAR(2), OUT p_max_score DOUBLE, OUT p_min_score DOUBLE, OUT p_avg_score DOUBLE)BEGIN SELECT MAX((exam1 + exam2) / 2) INTO p_max_score FROM exam -- 等值连接查询 INNER JOIN stuinfo ON exam.stuno = stuinfo.stuno WHERE stuinfo.stusex = p_stusex; SELECT MIN((exam1 + exam2) / 2) INTO p_min_score FROM exam INNER JOIN stuinfo ON exam.stuno = stuinfo.stuno WHERE stuinfo.stusex = p_stusex; SELECT AVG((exam1 + exam2) / 2) INTO p_avg_score FROM exam INNER JOIN stuinfo ON exam.stuno = stuinfo.stuno WHERE stuinfo.stusex = p_stusex;END //DELIMITER ;SET @max_score = 0;SET @min_score = 0;SET @avg_score = 0;CALL maxmin('男', @max_score, @min_score, @avg_score);SELECT @max_score AS max_score, @min_score AS min_score, @avg_score AS avg_score;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> mysql </category>
</categories>
<tags>
<tag> mysql </tag>
</tags>
</entry>
<entry>
<title>正则表达式断言</title>
<link href="/2023/11/20/zheng-ze-biao-da-shi-duan-yan/"/>
<url>/2023/11/20/zheng-ze-biao-da-shi-duan-yan/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="断言"><a href="#断言" class="headerlink" title="断言"></a>断言</h1><p>(练习网站)[<a href="https://regexlearn.com/zh-cn/learn/regex101]">https://regexlearn.com/zh-cn/learn/regex101]</a></p><h2 id="正向先行断言"><a href="#正向先行断言" class="headerlink" title="正向先行断言"></a>正向先行断言</h2><p>在正则表达式中,<code>(?=...)</code> 是一个正向肯定预查(positive lookahead)的语法结构。它用于在匹配字符串时,查找某个位置后面是否跟着特定的模式,而不消耗实际的字符。</p><p>具体来说,<code>(?=PM)</code> 匹配一个数字序列(<code>\d+</code>),但该数字序列后面必须紧跟着字符串 “PM”。这种预查会在匹配的时候向前看,并确定某个位置后面是否满足预期的条件。</p><p>举个例子,假设有以下文本:</p><pre class="line-numbers language-none"><code class="language-none">12PM 3PM 6PM<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>如果使用正则表达式 <code>\d+(?=PM)</code> 进行匹配,它将匹配到以下结果:</p><ul><li>“12”:在 “12” 后面紧跟着 “PM”。</li><li>“3”:在 “3” 后面紧跟着 “PM”。</li><li>“6”:在 “6” 后面紧跟着 “PM”。</li></ul><p>需要注意的是,正向肯定预查只是用来判断某个位置后面的模式是否匹配,它并不会包含在最终的匹配结果中。因此,预查部分(即 “PM”)不会包含在匹配的结果中,只有数字序列会被捕获。</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> reres <span class="token operator">=</span> re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">"\d+(?=PM)"</span><span class="token punctuation">,</span> <span class="token string">"23PM 42ji"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token comment"># ['23'] </span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h2 id="负向先行断言"><a href="#负向先行断言" class="headerlink" title="负向先行断言"></a>负向先行断言</h2><p>负向先行断言(negative lookahead)是正则表达式的一种语法结构,用于在匹配字符串时,查找某个位置后面是否不跟着特定的模式。它与负向后行断言类似,但是用于否定条件。</p><p>负向先行断言的语法结构是 <code>(?!...)</code>,其中 <code>...</code> 表示要匹配的模式。</p><p>举个例子,假设我们想匹配一个字符串中不跟着 “xyz” 的数字序列。我们可以使用负向先行断言来实现这个目标。</p><p>正则表达式:<code>\d+(?!xyz)</code></p><p>这个正则表达式会匹配一个或多个数字,但要求它后面不跟着 “xyz”。下面是一些例子来说明匹配情况:</p><ul><li>“123”:匹配成功,因为它后面不是 “xyz”。</li><li>“456xyz”:不匹配,因为 “456” 后面是 “xyz”。</li><li>“789abc”:匹配成功,因为 “789” 后面不是 “xyz”。</li></ul><p>负向先行断言允许我们在匹配过程中限制某个位置后面的内容,以满足特定的条件。</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> reres <span class="token operator">=</span> re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">"\d+(?!PM)"</span><span class="token punctuation">,</span> <span class="token string">"23PM 42ji"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token comment"># ['2', '42']</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h2 id="负向后行断言"><a href="#负向后行断言" class="headerlink" title="负向后行断言"></a>负向后行断言</h2><p>负向后行断言是正则表达式的一种语法结构,用于在匹配字符串时,查找某个位置前面是否不跟着特定的模式。它类似于正向肯定预查,但是用于否定条件。</p><p>假设我们想匹配一个字符串中不紧跟着 “abc” 的 “def”。我们可以使用负向后行断言来实现这个目标。</p><p>正则表达式:<code>(?<!abc)def</code></p><p>这个正则表达式会匹配 “def”,但是要求它前面的三个字符不是 “abc”。下面是一些例子来说明匹配情况:</p><ul><li>“def”:匹配成功,因为它前面不是 “abc”。</li><li>“abcdef”:不匹配,因为 “def” 前面是 “abc”。</li><li>“xyzdef”:匹配成功,因为 “def” 前面不是 “abc”。</li></ul><p>负向后行断言允许我们在匹配过程中限制某个位置前面的内容,以满足特定的条件。</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> reres <span class="token operator">=</span> re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">r'(?<!abc)PM'</span><span class="token punctuation">,</span> <span class="token string">"abcPM 23pm"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token comment"># []</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><h2 id="正向后行断言"><a href="#正向后行断言" class="headerlink" title="正向后行断言"></a>正向后行断言</h2><p>正向后行断言(positive lookbehind)是正则表达式的一种语法结构,用于在匹配字符串时,查找某个位置之前是否跟着特定的模式。它类似于正向肯定预查,但是用于前面的条件。</p><p>正向后行断言的语法结构是 <code>(?<=...)</code>,其中 <code>...</code> 表示要匹配的模式。</p><p>举个例子,假设我们想匹配一个字符串中紧跟着 “abc” 的 “def”。我们可以使用正向后行断言来实现这个目标。</p><p>正则表达式:<code>(?<=abc)def</code></p><p>这个正则表达式会匹配 “def”,但要求它前面的三个字符是 “abc”。下面是一些例子来说明匹配情况:</p><ul><li>“abcdef”:匹配成功,因为 “def” 前面是 “abc”。</li><li>“xyzabcdef”:不匹配,因为 “def” 前面不是 “abc”。</li><li>“abcxyzdef”:不匹配,因为 “def” 前面不是紧跟着 “abc”。</li></ul><p>正向后行断言允许我们在匹配过程中限制某个位置之前的内容,以满足特定的条件。需要注意的是,断言部分(即 “abc”)不会包含在最终的匹配结果中,只有断言后面的部分会被捕获。</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> reres <span class="token operator">=</span> re<span class="token punctuation">.</span>findall<span class="token punctuation">(</span><span class="token string">"(?<=23)\w+"</span><span class="token punctuation">,</span> <span class="token string">"23PM 42ji"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h1 id="匹配模式"><a href="#匹配模式" class="headerlink" title="匹配模式"></a>匹配模式</h1><h2 id="贪婪匹配"><a href="#贪婪匹配" class="headerlink" title="贪婪匹配"></a>贪婪匹配</h2><p>正则默认贪婪匹配,意思就是匹配到第一个符合要求的以后,不会暂停,会继续匹配</p><h2 id="懒惰匹配"><a href="#懒惰匹配" class="headerlink" title="懒惰匹配"></a>懒惰匹配</h2><p>匹配到第一个符合要求的就不匹配了,例如:</p><pre class="line-numbers language-r" data-language="r"><code class="language-r">ber beer beeer beeeer.<span class="token operator">*</span>?r只会匹配到ber<span class="token punctuation">,</span>这第一个单词<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>与贪婪匹配不同,懒惰匹配在第一次匹配时停止。下面的例子中,在 * 之后添加 ?,将查找以 r 结尾且前面带有任意字符的第一个匹配项。这意味着本次匹配将会在第一个字母 r 处停止。</p><h1 id="引用组"><a href="#引用组" class="headerlink" title="引用组"></a>引用组</h1><p>引用组(capturing group)是正则表达式中的一种语法结构,用于将匹配的内容进行分组并在后续操作中引用。引用组使用圆括号 <code>( )</code> 来定义。</p><p>当正则表达式匹配到引用组时,匹配的内容会被捕获并<code>保存</code>在一个<code>特定的索引</code>中,可以在后续的正则表达式操作或替换中使用这些引用。<br>例如:</p><pre class="line-numbers language-none"><code class="language-none">ha-ha,haa-haa(ha)-\1,(haa)-\2<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>\1 保存的是ha,\2保存的是haa</p><p>下面是一个使用引用组的 Python 代码示例:</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> re<span class="token comment"># 定义要匹配的字符串</span>text <span class="token operator">=</span> <span class="token string">"Hello, my name is John Doe."</span><span class="token comment"># 使用引用组进行匹配和替换</span>pattern <span class="token operator">=</span> <span class="token string">r"Hello, my name is ([A-Za-z\s]+)\."</span>replacement <span class="token operator">=</span> <span class="token string">r"Nice to meet you, \1!"</span><span class="token comment"># 替换匹配的内容并输出结果</span>result <span class="token operator">=</span> re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> replacement<span class="token punctuation">,</span> text<span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>运行以上代码,将输出:</p><pre class="line-numbers language-none"><code class="language-none">Nice to meet you, John Doe!<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>下面是 re.sub 函数的完整参数列表和用法:</p><pre class="line-numbers language-python" data-language="python"><code class="language-python">re<span class="token punctuation">.</span>sub<span class="token punctuation">(</span>pattern<span class="token punctuation">,</span> repl<span class="token punctuation">,</span> string<span class="token punctuation">,</span> count<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> flags<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>pattern:要匹配的正则表达式模式。<br>repl:替换字符串,可以是普通字符串或一个替换函数。<br>string:待处理的原始字符串。<br>count(可选):指定替换的最大次数,默认为 0,表示全部替换。<br>flags(可选):用于控制正则表达式的匹配方式,例如忽略大小写、多行匹配等。</p><p>在这个例子中,我们使用引用组 <code>( )</code> 将 <code>([A-Za-z\s]+)</code> 匹配的内容进行了分组。这个引用组匹配一个或多个字母和空格,表示一个人的姓名。然后,我们使用 <code>\1</code> 来引用第一个引用组的匹配结果,将其用于替换字符串中的内容:插入到<code>"replacement"中\1处</code>。</p><p>在替换中,我们将匹配到的内容替换为 “Nice to meet you, “ 加上引用组中捕获的姓名。因此,”Hello, my name is John Doe.” 被替换为 “Nice to meet you, John Doe!”。</p><p>通过引用组,我们可以在正则表达式中捕获并引用特定的匹配结果,从而进行更灵活的操作和替换。</p>]]></content>
<categories>
<category> 正则表达式 </category>
<category> Regex </category>
</categories>
<tags>
<tag> Regex </tag>
<tag> 正则表达式 </tag>
</tags>
</entry>
<entry>
<title>动态拼接本地图片路径</title>
<link href="/2023/11/20/dong-tai-pin-jie-ben-di-tu-pian-lu-jing/"/>
<url>/2023/11/20/dong-tai-pin-jie-ben-di-tu-pian-lu-jing/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="vue图片路径动态拼接报错"><a href="#vue图片路径动态拼接报错" class="headerlink" title="vue图片路径动态拼接报错"></a>vue图片路径动态拼接报错</h1><h2 id="失败加载的原因"><a href="#失败加载的原因" class="headerlink" title="失败加载的原因"></a>失败加载的原因</h2><p>然而这时候,会发现这个时候图片并没有被加载出来,图片没有显示出来,通过查看发现这张图片的地址显示 ../images/demo.png ,也就是说通过v-bind形式绑定的相对路径不会被webpack的file-loader处理,只会做简单的文本替换。</p><h2 id="解决办法"><a href="#解决办法" class="headerlink" title="解决办法"></a>解决办法</h2><ol><li><p>图片转位base64编码</p><p><code><img src="data:image/png;base64,iVBYKIGloxxxxxxxxxxxxxxxxxxx..."> </code></p><p>一般图片比较小的可以这么做,比如图标icon等,大小一般在10KB以内的。</p></li><li><p>图片放置在 public 目录下或通过绝对路径被引用。<strong>推荐</strong></p><p>这类资源将会直接被拷贝,而不会经过 webpack 的处理。</p></li></ol>]]></content>
<categories>
<category> vue </category>
</categories>
<tags>
<tag> vue </tag>
</tags>
</entry>
<entry>
<title>组件之间数据传递</title>
<link href="/2023/11/20/zu-jian-zhi-jian-shu-ju-chuan-di/"/>
<url>/2023/11/20/zu-jian-zhi-jian-shu-ju-chuan-di/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="组件之间的数据传递"><a href="#组件之间的数据传递" class="headerlink" title="组件之间的数据传递"></a>组件之间的数据传递</h2><h3 id="父组件向子组件传递值v-bind-单向数据传递"><a href="#父组件向子组件传递值v-bind-单向数据传递" class="headerlink" title="父组件向子组件传递值v-bind(单向数据传递)"></a>父组件向子组件传递值<code>v-bind(单向数据传递)</code></h3><blockquote><p><strong>使用v-bind定义一个属性传值,到子组件</strong></p></blockquote><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">setup</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> computed<span class="token punctuation">,</span> defineProps<span class="token punctuation">,</span> defineComponent <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token keyword">import</span> wind <span class="token keyword">from</span> <span class="token string">'./components/windCard.vue'</span><span class="token function">defineComponent</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'app'</span><span class="token punctuation">,</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">let</span> va<span class="token operator">=</span><span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> va<span class="token punctuation">.</span>value<span class="token operator">++</span><span class="token punctuation">;</span><span class="token punctuation">}</span></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>div1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-text</span> <span class="token punctuation">></span></span> count: {{ va }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-text</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>add<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>+1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-button</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>hr</span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>wind</span> <span class="token attr-name"><span class="token namespace">v-bind:</span>number</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>va<span class="token punctuation">"</span></span> <span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>wind</span><span class="token punctuation">></span></span> //在这里,重点v-bind命令 <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>子组件通过宏定义``defineProps`创建一个json对象,显式声明接到的子组件信号</p></blockquote><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">setup</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> computed<span class="token punctuation">,</span> defineProps<span class="token punctuation">,</span> defineComponent <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token function">defineComponent</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'MyComponent'</span><span class="token punctuation">,</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">let</span> va <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token keyword">const</span> emit<span class="token operator">=</span><span class="token function">defineEmits</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'inFocus'</span><span class="token punctuation">,</span> <span class="token string">'update:number'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token keyword">const</span> props<span class="token operator">=</span><span class="token function">defineProps</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">number</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">type</span><span class="token operator">:</span> Number<span class="token punctuation">,</span> <span class="token keyword">default</span><span class="token operator">:</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">emit</span><span class="token punctuation">(</span><span class="token string">'update:number'</span><span class="token punctuation">,</span>props<span class="token punctuation">.</span>number<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">}</span></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-text</span> <span class="token punctuation">></span></span> count:{{ props.number}}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-text</span><span class="token punctuation">></span></span> //重点:一定要通过这种方式来访问接收到属性值,json对象 <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>add<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>+1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-button</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="父组件与子组件值双向传递-v-model"><a href="#父组件与子组件值双向传递-v-model" class="headerlink" title="父组件与子组件值双向传递(``v-model`)"></a>父组件与子组件值双向传递(``v-model`)</h3><blockquote><p>父组件<code>v-model</code>传递一个属性给子组件</p></blockquote><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">setup</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> computed<span class="token punctuation">,</span> defineProps<span class="token punctuation">,</span> defineComponent <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token keyword">import</span> wind <span class="token keyword">from</span> <span class="token string">'./components/windCard.vue'</span><span class="token function">defineComponent</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'app'</span><span class="token punctuation">,</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">let</span> va<span class="token operator">=</span><span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> va<span class="token punctuation">.</span>value<span class="token operator">++</span><span class="token punctuation">;</span><span class="token punctuation">}</span></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>div1<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-text</span> <span class="token punctuation">></span></span> count: {{ va }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-text</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>add<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>+1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-button</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>hr</span><span class="token punctuation">/></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>wind</span> <span class="token attr-name"><span class="token namespace">v-model:</span>number</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>va<span class="token punctuation">"</span></span> <span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>wind</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p><code>重点:子组件 接受 父组件中的数据,并通过 (自定义事件,再触发) 返回子组件中 更新后的属性值 给父组件</code></p></blockquote><ol><li><p>先定义:自定义事件,更新父组件中的自定义事件名字模板<code>'update:属性名字'</code></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup">const emit=defineEmits(['inFocus', 'update:number']) //自定义事件<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre></li><li><p>函数中触发事件,<code>emit(事件名称:string,新的属性值)</code></p></li></ol><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">setup</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> computed<span class="token punctuation">,</span> defineProps<span class="token punctuation">,</span> defineComponent <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token function">defineComponent</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">'MyComponent'</span><span class="token punctuation">,</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">let</span> va <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token keyword">const</span> emit<span class="token operator">=</span><span class="token function">defineEmits</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'inFocus'</span><span class="token punctuation">,</span> <span class="token string">'update:number'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">//自定义事件</span><span class="token keyword">const</span> props<span class="token operator">=</span><span class="token function">defineProps</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">number</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">type</span><span class="token operator">:</span> Number<span class="token punctuation">,</span> <span class="token keyword">default</span><span class="token operator">:</span> <span class="token number">0</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token keyword">function</span> <span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token function">emit</span><span class="token punctuation">(</span><span class="token string">'update:number'</span><span class="token punctuation">,</span>props<span class="token punctuation">.</span>number<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">//函数中触发自定义事件</span><span class="token punctuation">}</span></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>注意点:子组件中不可以<code>直接操作</code> 来自父组件中 <code>传递的属性</code>,<code>一定不可以</code></strong></p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-text</span> <span class="token punctuation">></span></span> count:{{ props.number}}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-text</span><span class="token punctuation">></span></span> //json对象的访问 <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>add<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>+1<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-button</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h3><h4 id="组合式api中需要在子组件中-显式指定-组件接收到来自父组件的值-是一个json对象"><a href="#组合式api中需要在子组件中-显式指定-组件接收到来自父组件的值-是一个json对象" class="headerlink" title="- 组合式api中需要在子组件中 显式指定 组件接收到来自父组件的值(是一个json对象))"></a>- 组合式api中需要在子组件中 显式指定 组件接收到来自父组件的值(是一个json对象))</h4><h4 id="子组件中不能-直接更改-父组件中传递的属性"><a href="#子组件中不能-直接更改-父组件中传递的属性" class="headerlink" title="- 子组件中不能 直接更改 父组件中传递的属性"></a>- 子组件中不能 直接更改 父组件中传递的属性</h4><h4 id="更新父组件传递的属性-采用的触发自定义格式相同"><a href="#更新父组件传递的属性-采用的触发自定义格式相同" class="headerlink" title="- 更新父组件传递的属性,采用的触发自定义格式相同"></a>- 更新父组件传递的属性,采用的触发自定义格式相同</h4><p><code>emit('update:number',props.number+1)</code>: <strong>emit(‘update:名字’,新的属性值)</strong></p><h2 id="miit实现兄弟组件传递数值"><a href="#miit实现兄弟组件传递数值" class="headerlink" title="miit实现兄弟组件传递数值"></a>miit实现兄弟组件传递数值</h2><p>综上所述,无论是多个子组件还是单个组件中,都可以使用 <code>mitt</code> 库来实现组件之间的通信。如果在多个子组件之间传递数据,则需要在它们的父组件中创建一个单一的事件总线对象。</p><blockquote><p>数据总线一定要相同,才能监听同一个事件</p></blockquote><h3 id="导出一份mitt实例"><a href="#导出一份mitt实例" class="headerlink" title="导出一份mitt实例"></a>导出一份<code>mitt实例</code></h3><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">import mitt from 'mitt'const bus = mitt()export default bus<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h3 id="全局导入-main-js中"><a href="#全局导入-main-js中" class="headerlink" title="全局导入[main.js中]"></a>全局导入[<code>main.js中</code>]</h3><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">import mitt from 'mitt' const bus = mitt() app.config.globalProperties.$EventBus = bus<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h3 id="在组件中导入"><a href="#在组件中导入" class="headerlink" title="在组件中导入"></a>在组件中导入</h3><h4 id="兄弟组件A监听事件"><a href="#兄弟组件A监听事件" class="headerlink" title="兄弟组件A监听事件"></a>兄弟组件A监听事件</h4><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>{{ receivedMessage }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">setup</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token keyword">import</span> bus <span class="token keyword">from</span> <span class="token string">'../bus'</span><span class="token comment">// 监听事件</span><span class="token keyword">let</span> receivedMessage<span class="token operator">=</span><span class="token function">ref</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span>bus<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'jisho'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">message</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span> receivedMessage<span class="token punctuation">.</span>value <span class="token operator">=</span> message<span class="token punctuation">}</span><span class="token punctuation">)</span></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="兄弟组件B中触发事件"><a href="#兄弟组件B中触发事件" class="headerlink" title="兄弟组件B中触发事件"></a>兄弟组件B中触发事件</h4><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-input</span> <span class="token attr-name">v-model</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>input<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-input</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>fa<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>send<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-button</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">setup</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token keyword">import</span> bus <span class="token keyword">from</span> <span class="token string">'../bus'</span><span class="token punctuation">;</span><span class="token keyword">let</span> input<span class="token operator">=</span><span class="token function">ref</span><span class="token punctuation">(</span><span class="token string">'fasdfas'</span><span class="token punctuation">)</span><span class="token keyword">function</span> <span class="token function">fa</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//触发事件</span> bus<span class="token punctuation">.</span><span class="token function">emit</span><span class="token punctuation">(</span><span class="token string">'jisho'</span><span class="token punctuation">,</span> input<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">}</span></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> vue </category>
</categories>
<tags>
<tag> vue </tag>
</tags>
</entry>
<entry>
<title>虚拟 dom 延时加载</title>
<link href="/2023/11/20/xu-ni-dom-yan-shi-jia-zai/"/>
<url>/2023/11/20/xu-ni-dom-yan-shi-jia-zai/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="解决虚拟DOM未更新【异步】对于问题"><a href="#解决虚拟DOM未更新【异步】对于问题" class="headerlink" title="解决虚拟DOM未更新【异步】对于问题"></a>解决虚拟<code>DOM</code>未更新【异步】对于问题</h2><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">setup</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>ts<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> nextTick <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token keyword">const</span> count <span class="token operator">=</span> <span class="token function">ref</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token keyword">async</span> <span class="token keyword">function</span> <span class="token function">increment</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> count<span class="token punctuation">.</span>value<span class="token operator">++</span> <span class="token keyword">const</span> ele<span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'counter'</span><span class="token punctuation">)</span> <span class="token keyword">await</span> <span class="token function">nextTick</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// DOM 此时已经更新</span> console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ele<span class="token operator">?.</span>textContent<span class="token punctuation">)</span> <span class="token comment">// 1</span><span class="token punctuation">}</span> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>button</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>counter<span class="token punctuation">"</span></span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>increment<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>{{ count }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>button</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>这段代码是使用Vue3的<code><script setup></code>语法进行开发的,主要实现的功能是一个点击按钮后计数器自增1,并将计数器显示在页面上。下面是对这段代码的详细讲解:</p><p>首先引入了Vue3中的<code>ref</code>和<code>nextTick</code>方法,其中<code>ref</code>用来定义响应式数据,而<code>nextTick</code>则是用来在DOM更新后执行某些操作的异步方法。</p><p>接下来使用<code>const count = ref(0)</code>定义了一个初始值为0的响应式数据<code>count</code>。</p><p>然后定义了一个名为<code>increment()</code>的异步函数,该函数实现了计数器自增1以及通过<code>getElementById</code>方法获取到计数器所在的DOM元素,接着使用<code>await nextTick()</code>等待DOM更新完成。由于Vue3使用了异步渲染机制,因此需要等待DOM更新完成后才能获取到最新的DOM内容。</p><p>最后,在模板中将计数器显示在了页面上,并设定了一个<code>@click</code>事件,当按钮被点击时会触发<code>increment()</code>方法自增计数器。</p>]]></content>
<categories>
<category> vue </category>
</categories>
<tags>
<tag> vue </tag>
</tags>
</entry>
<entry>
<title>vue 函数获取原生 dom</title>
<link href="/2023/11/20/vue-han-shu-huo-qu-yuan-sheng-dom/"/>
<url>/2023/11/20/vue-han-shu-huo-qu-yuan-sheng-dom/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><pre class="line-numbers language-typescript" data-language="typescript"><code class="language-typescript"><span class="token operator"><</span>script setup lang<span class="token operator">=</span><span class="token string">"ts"</span><span class="token operator">></span><span class="token keyword">function</span> <span class="token function">handleChange</span><span class="token punctuation">(</span>event<span class="token operator">:</span><span class="token builtin">any</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">// `event` 隐式地标注为 `any` 类型</span> <span class="token builtin">console</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>target<span class="token punctuation">.</span>value<span class="token punctuation">)</span><span class="token punctuation">}</span><span class="token operator"><</span><span class="token operator">/</span>script<span class="token operator">></span><span class="token operator"><</span>template<span class="token operator">></span> <span class="token operator"><</span>input type<span class="token operator">=</span><span class="token string">"text"</span> <span class="token decorator"><span class="token at operator">@</span><span class="token function">change</span></span><span class="token operator">=</span><span class="token string">"handleChange"</span> <span class="token operator">/</span><span class="token operator">></span><span class="token operator"><</span><span class="token operator">/</span>template<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> vue </category>
</categories>
<tags>
<tag> vue </tag>
</tags>
</entry>
<entry>
<title>vue路由</title>
<link href="/2023/11/20/vue-lu-you/"/>
<url>/2023/11/20/vue-lu-you/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="vue中的路由"><a href="#vue中的路由" class="headerlink" title="vue中的路由"></a><code>vue</code>中的路由</h2><h3 id="实现原理"><a href="#实现原理" class="headerlink" title="实现原理"></a>实现原理</h3><h3 id="导入路由模块"><a href="#导入路由模块" class="headerlink" title="导入路由模块"></a>导入路由模块</h3><h4 id="定义vue路由模块"><a href="#定义vue路由模块" class="headerlink" title="定义vue路由模块"></a>定义<code>vue</code>路由模块</h4><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">import ChildB from './components/HelloWorld.vue';import ChildC from './components/comp.vue';import {createRouter, createWebHistory} from 'vue-router';const routers=createRouter({ history:createWebHistory(), routes:[ { path:'/childb', component:ChildB }, { path:'/childc', component:ChildC } ]})export default routers;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="全局导入路由-main-js"><a href="#全局导入路由-main-js" class="headerlink" title="全局导入路由[main.js]"></a>全局导入路由[<code>main.js</code>]</h4><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">import router from './vuerouter'const app=createApp(App)app.use(router)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h3 id="router的基本使用"><a href="#router的基本使用" class="headerlink" title="router的基本使用"></a><code>router</code>的基本使用</h3><h4 id="声明式使用"><a href="#声明式使用" class="headerlink" title="声明式使用"></a>声明式使用</h4><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/childb<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>b<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>router-link</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>/childc<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>c<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>router-link</span><span class="token punctuation">></span></span> //路由占位符,跳转的路由组件的渲染的位置 <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>router-view</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>router-view</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>注明: <code><router-link>其实就是a标签</code></p><h3 id="router高级使用"><a href="#router高级使用" class="headerlink" title="router高级使用"></a>router高级使用</h3><h4 id="路由重定向"><a href="#路由重定向" class="headerlink" title="路由重定向"></a>路由重定向</h4><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">import {createRouter, createWebHistory} from 'vue-router';const routers=createRouter({ history:createWebHistory(), routes:[ { path:'/childb', component:ChildB }, { path:'/childc', component:ChildC }, { path:'/other', redirect:'/childb' } ]})<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="自定义路由高亮类名"><a href="#自定义路由高亮类名" class="headerlink" title="自定义路由高亮类名"></a>自定义路由高亮类名</h4><blockquote><p><code>linkActiveClass:'router-active'</code>:指定<code>触发的路由的css样式</code>,在vue项目的<code>index.css</code>中设置或者<code>'style.css'</code></p></blockquote><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">import {createRouter, createWebHistory} from 'vue-router';const routers=createRouter({ history:createWebHistory(), linkActiveClass:'router-active' routes:[ { path:'/childb', component:ChildB }, { path:'/childc', component:ChildC }, { path:'/other', redirect:'/childb' } ]})// 在index.css或者style.css中.active{ background-color: red; font-weight: bold; color:white;}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="嵌套路由router"><a href="#嵌套路由router" class="headerlink" title="嵌套路由router"></a>嵌套路由<code>router</code></h4><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">import {createRouter, createWebHistory} from 'vue-router';const routers=createRouter({ history:createWebHistory(), routes:[ { path:'/childb', component:ChildB, children:[ { path:'child', component:ChildC } ] }, { path:'/childc', component:ChildC }, { path:'/other', redirect:'/childb' } ]})<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="vue动态匹配router"><a href="#vue动态匹配router" class="headerlink" title="vue动态匹配router"></a>vue动态匹配<code>router</code></h3><blockquote><p>定义router规则时候,需要动态识别的<code>采用:的形式</code></p></blockquote><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">import {createRouter, createWebHistory} from 'vue-router';const routers=createRouter({ history:createWebHistory(), routes:[ { path:'/childc/:id', component:movie, props:true //手动声明是否传参props }, { path:'/childb', component:ChildB }, { path:'/childc', component:ChildC }, { path:'/other', redirect:'/childb' } ]})<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><code> { path:'/childc/:id', component:movie, props:true //手动声明是否传参props },</code></p><h3 id="组件获得路由动态绑定的参数"><a href="#组件获得路由动态绑定的参数" class="headerlink" title="组件获得路由动态绑定的参数"></a>组件获得路由动态绑定的参数</h3><blockquote><p> 方式1 :<code>props获取</code></p></blockquote><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>这是一个电影组件-----{{ props.id }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">setup</span> <span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token keyword">let</span> props<span class="token operator">=</span> <span class="token function">defineProps</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">type</span><span class="token operator">:</span> String<span class="token punctuation">,</span> <span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>方式2: <code>$route.params.id</code></p></blockquote><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>这是一个电影组件-----{{ route.params.id }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">setup</span> <span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"><span class="token keyword">import</span> <span class="token punctuation">{</span> ref<span class="token punctuation">,</span> onMounted <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue'</span><span class="token keyword">import</span> <span class="token punctuation">{</span> useRouter<span class="token punctuation">,</span> useRoute <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue-router'</span><span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token function">useRouter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">const</span> route <span class="token operator">=</span> <span class="token function">useRoute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">let</span> props<span class="token operator">=</span> <span class="token function">defineProps</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">id</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token literal-property property">type</span><span class="token operator">:</span> String<span class="token punctuation">,</span> <span class="token literal-property property">required</span><span class="token operator">:</span> <span class="token boolean">false</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="组合式API编程式使用vue-route"><a href="#组合式API编程式使用vue-route" class="headerlink" title="组合式API编程式使用vue route`"></a><code>组合式API编程式使用</code>vue route`</h3><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript"><template> <div> <p>这是一个电影组件-----{{ $route.params.id }}</p> <el-button @click="goback">后退</el-button> </div></template><script setup >import { ref, onMounted } from 'vue'let props= defineProps({ id: { type: String, required: false }})import { useRouter, useRoute } from 'vue-router'const router = useRouter() const route = useRoute()function goback(){ router.go(-1)}</script><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="命名路由"><a href="#命名路由" class="headerlink" title="命名路由"></a>命名路由</h3><p><strong>路由的名字具有唯一性</strong></p><h4 id="命名路由实现声明式导航-name-params"><a href="#命名路由实现声明式导航-name-params" class="headerlink" title="命名路由实现声明式导航{name:,params:}"></a>命名路由实现声明式导航<code>{name:,params:}</code></h4><blockquote><p>定义路由:<code>取名</code></p></blockquote><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">const routers=createRouter({ history:createWebHistory(), routes:[ { name:'movie', //区一个名字 path:'/childc/:id', component:movie, props:true }, { path:'/childb', component:ChildB }, { path:'/childc', component:ChildC }, { path:'/other', redirect:'/childb' } ]})<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><blockquote><p>组件中使用</p></blockquote><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>template</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>{name:'movie',params:{id:3}}<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>router-link</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>这是一个电影组件-----{{ route.params.id }}<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>el-button</span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>goback<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>后退<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>el-button</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>template</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ol><li><p><code>name,params</code>为声明好的属性,不需要引入route访问,这也是错的</p></li><li><p><strong>将会经过路由名字为<code>'movie'</code>跳转到对应的组件,并接受一个传参<code>id</code></strong></p></li></ol><h4 id="命名路由实现编程式导航push"><a href="#命名路由实现编程式导航push" class="headerlink" title="命名路由实现编程式导航push"></a>命名路由实现编程式导航<code>push</code></h4><pre class="line-numbers language-JavaScript" data-language="JavaScript"><code class="language-JavaScript">const router = useRouter()const route = useRoute()function pushWithQuery(query) { router.push({ name: 'search', query: { ...route.query, ...query, }, })}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>在JavaScript中,三个点<code>...</code>表示展开语法,它可以将一个数组或对象展开成单独的值。在上文提到的代码中,<code>...route.query</code>和<code>...query</code>都是使用了展开语法。</p><p>具体来说,<code>...route.query</code>将当前路由的查询参数转化为一个键值对列表,然后把这些键值对展开到了一个新的对象中。同样的,<code>...query</code>也是将参数对象中所有的键值对展开到了这个新对象中。而这两者用了扩展运算符<code>{...}</code>后,即将这两个展开后的对象进行合并,并且如果存在相同key的时候,后面的会覆盖前面的。最终生成的对象作为 <code>router.push()</code> 方法的 <code>query</code> 参数的值,这个对象中包含了所有的查询参数。</p><p>举例来说,在如下代码中:</p><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">const</span> obj1 <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">x</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">y</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">const</span> obj2 <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token literal-property property">y</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token literal-property property">z</span><span class="token operator">:</span> <span class="token number">4</span> <span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">const</span> combinedObj <span class="token operator">=</span> <span class="token punctuation">{</span> <span class="token operator">...</span>obj1<span class="token punctuation">,</span> <span class="token operator">...</span>obj2<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>combinedObj<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>输出结果应该为:<code>{ x: 1, y: 3, z: 4 }</code>。其中,原本obj1对象中的y键值被obj2的y覆盖掉了。</p><p>这段代码是一个使用Vue 3中的<code>nextTick()</code>函数处理DOM异步更新的示例。</p><p>首先定义了一个异步函数<code>increment()</code>,其中包含一个名为<code>count</code>的响应式变量和<code>document.getElementById('counter')</code>的DOM元素。然后将<code>count</code>的值加1。</p><p>接下来调用了<code>await nextTick()</code>,可以确保在DOM渲染前等待Vue完成其异步更新队列。具体来说,当Vue更新虚拟DOM并准备将更改应用于实际DOM时,会将这些更改排入队列中。<code>nextTick()</code>函数就是用于在队列被清空之前执行回调函数。</p><p>最后,控制台输出了DOM元素的文本内容。由于使用了<code>await nextTick()</code>,此时<code>ele.textContent</code>已经被更新为1,因此控制台会输出1。</p><p>通过使用<code>nextTick()</code>函数,我们可以避免异步更新导致的DOM未能立即更新的问题,并始终获得最新的DOM状态。</p>]]></content>
<categories>
<category> vue </category>
</categories>
<tags>
<tag> vue </tag>
<tag> vue-router </tag>
</tags>
</entry>
<entry>
<title>pm2后台运行 node,js 项目</title>
<link href="/2023/11/20/pm2-hou-tai-yun-xing-node-js-xiang-mu/"/>
<url>/2023/11/20/pm2-hou-tai-yun-xing-node-js-xiang-mu/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>在 CentOS 终端中启动 Node.js 项目时,如果你关闭终端,该项目也会停止运行,这是因为终端关闭会导致该进程的终止。为了解决这个问题,你可以使用一些方法来让项目在后台持续运行,即使终端关闭。以下是几种可行的方法:</p><ol><li><p>使用 nohup 命令:可以使用 nohup 命令在后台运行你的 Node.js 项目。nohup 命令会将程序的输出重定向到一个文件,并且不会受到终端关闭的影响。你可以这样运行你的项目:</p><pre class="line-numbers language-none"><code class="language-none">nohup node your_project.js > output.log &<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这会将项目的输出保存到 <code>output.log</code> 文件中,并在后台运行。你可以通过查看 <code>output.log</code> 文件来获取项目的输出信息。</p></li><li><p>使用进程管理工具:可以使用专门的进程管理工具来管理你的 Node.js 项目,例如 <code>pm2</code>。<code>pm2</code> 可以在后台运行你的项目,并提供日志记录、监控和自动重启等功能。你可以使用以下命令安装 <code>pm2</code>:</p><pre class="line-numbers language-none"><code class="language-none">npm install -g pm2<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>安装完成后,可以使用以下命令启动你的项目:</p><pre class="line-numbers language-none"><code class="language-none">pm2 start your_project.js<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这样,你的项目将在后台一直运行,即使终端关闭。你可以使用 <code>pm2 logs</code> 命令查看项目的日志信息。</p></li><li><p>使用终端多路复用工具:可以使用终端多路复用工具,例如 <code>tmux</code> 或 <code>screen</code>,来创建一个会话,并在该会话中运行你的项目。这样即使终端关闭,会话仍然保持活动状态。你可以按照以下步骤使用 <code>tmux</code>:</p><ul><li>安装 <code>tmux</code>:使用包管理器(如 <code>yum</code>)安装 <code>tmux</code>。</li><li>启动 <code>tmux</code>:运行 <code>tmux</code> 命令启动一个新的会话。</li><li>在 <code>tmux</code> 会话中启动你的项目:进入项目目录,然后运行 <code>node your_project.js</code>。</li><li>分离会话:按下 <code>Ctrl + b</code>,然后按下 <code>d</code> 键,会话将在后台运行。</li><li>关闭终端:关闭终端不会影响 <code>tmux</code> 会话的运行。</li></ul><p>当你需要重新连接到 <code>tmux</code> 会话时,可以使用以下命令:</p><pre class="line-numbers language-none"><code class="language-none">tmux attach<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这将重新连接到之前创建的 <code>tmux</code> 会话,你将能够查看项目的输出。</p></li></ol><p>这些方法中的任何一种都可以让你的 Node.js 项目在后台持续运行,并且不会受到终端关闭的影响。你可以根据个人喜好和需求选择其中之</p>]]></content>
<categories>
<category> Node.js </category>
</categories>
<tags>
<tag> Node.js </tag>
<tag> pm2 </tag>
</tags>
</entry>
<entry>
<title>破解软件列表</title>
<link href="/2023/11/20/po-jie-ruan-jian-lie-biao/"/>
<url>/2023/11/20/po-jie-ruan-jian-lie-biao/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h1 id="资源列表"><a href="#资源列表" class="headerlink" title="资源列表"></a>资源列表</h1><ul><li><a href="https://wgbqr.com/254.html/comment-page-1?scroll=comment-32549">亿图图示</a></li></ul>]]></content>
<tags>
<tag> 破解软件 </tag>
</tags>
</entry>
<entry>
<title>uniapp一键登</title>
<link href="/2023/11/20/uniapp-yi-jian-deng/"/>
<url>/2023/11/20/uniapp-yi-jian-deng/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><h2 id="uniapp一键登录"><a href="#uniapp一键登录" class="headerlink" title="uniapp一键登录"></a><a href="https://blog.csdn.net/qq_41556539/article/details/119911241">uniapp一键登录</a></h2>]]></content>
<categories>
<category> uniapp </category>
</categories>
<tags>
<tag> uniapp </tag>
</tags>
</entry>
<entry>
<title>centos安装 nginx</title>
<link href="/2023/11/20/centos-an-zhuang-nginx/"/>
<url>/2023/11/20/centos-an-zhuang-nginx/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p><a href="https://support.huaweicloud.com/usermanual-ccm/ccm_01_0082.html">配置</a></p><ol><li><p>安装 <code>yum install nginx</code></p></li><li><p><code>vim /etc/nginx/nginx.conf</code></p></li><li><p>检查错误<code> nginx -t</code> or <code>cd cd /usr/local/sbin;nginx -t</code> </p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">nginx: the configuration <span class="token function">file</span> /etc/nginx/nginx.conf syntax is oknginx: configuration <span class="token function">file</span> /etc/nginx/nginx.conf <span class="token builtin class-name">test</span> is successful<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>表示nginx配置正确</p></li><li><p>重启nginx:<code>nginx -s reload</code></p></li></ol>]]></content>
<categories>
<category> nginx </category>
<category> centos </category>
</categories>
<tags>
<tag> centos </tag>
<tag> nginx </tag>
</tags>
</entry>
<entry>
<title>hightlight.js 高亮代码</title>
<link href="/2023/11/20/hightlight-js-gao-liang-dai-ma/"/>
<url>/2023/11/20/hightlight-js-gao-liang-dai-ma/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token doctype"><span class="token punctuation"><!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>head</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>UFT-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>title</span><span class="token punctuation">></span></span>java development <span class="token tag"><span class="token tag"><span class="token punctuation"></</span>title</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>link</span> <span class="token attr-name">rel</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>stylesheet<span class="token punctuation">"</span></span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>F:\highlight\styles\a11y-dark.min.css<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>F:\highlight\highlight.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>style</span><span class="token punctuation">></span></span><span class="token style"><span class="token language-css"> <span class="token selector">pre</span> <span class="token punctuation">{</span> <span class="token property">float</span><span class="token punctuation">:</span> left<span class="token punctuation">;</span> <span class="token property">resize</span><span class="token punctuation">:</span> both<span class="token punctuation">;</span> <span class="token property">overflow</span><span class="token punctuation">:</span> auto<span class="token punctuation">;</span> <span class="token punctuation">}</span> <!DOCTYPE html> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>style</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span> <span class="token special-attr"><span class="token attr-name">style</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span><span class="token value css language-css"><span class="token property">background-color</span><span class="token punctuation">:</span><span class="token function">rgb</span><span class="token punctuation">(</span>241<span class="token punctuation">,</span> 246<span class="token punctuation">,</span> 248<span class="token punctuation">)</span></span><span class="token punctuation">"</span></span></span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>script</span><span class="token punctuation">></span></span><span class="token script"><span class="token language-javascript"> hljs<span class="token punctuation">.</span><span class="token function">initHighlightingOnLoad</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>script</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>html</span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<tags>
<tag> hightlight.js </tag>
</tags>
</entry>
<entry>
<title>Spring处理跨域请求</title>
<link href="/2023/11/19/spring-chu-li-kua-yu-qing-qiu/"/>
<url>/2023/11/19/spring-chu-li-kua-yu-qing-qiu/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>当使用Spring Boot处理跨域请求时,可以通过以下步骤来实现:</p><ol><li><p>在你的Controller类或者全局配置类上添加<code>@CrossOrigin</code>注解来启用跨域请求处理。这个注解可以放在类级别或者方法级别上,具体取决于你的需求。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@RestController</span><span class="token annotation punctuation">@CrossOrigin</span><span class="token punctuation">(</span>origins <span class="token operator">=</span> <span class="token string">"http://example.com"</span><span class="token punctuation">)</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">YourController</span> <span class="token punctuation">{</span> <span class="token comment">// ...</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>在上面的示例中,<code>@CrossOrigin</code>注解设置了<code>origins</code>参数为”<a href="http://example.com" ,表示只允许来自该域名的请求.你可以将`origins`参数设置为`"*"`来允许来自任何域的请求.="" "="">http://example.com",表示只允许来自该域名的请求。你可以将`origins`参数设置为`"*"`来允许来自任何域的请求。</a></p></li><li><p>如果你需要更复杂的跨域配置,可以使用<code>@CrossOrigin</code>注解的其他属性。以下是一些常用的属性:</p><ul><li><p><code>origins</code>:设置允许访问接口的域名。可以使用逗号分隔的字符串来指定多个域名,或者使用<code>"*"</code>表示允许来自任何域的请求。</p></li><li><p><code>methods</code>:设置允许的HTTP方法。可以使用逗号分隔的字符串指定多个方法,例如<code>"GET,POST"</code>。</p></li><li><p><code>allowedHeaders</code>:设置允许的请求头。可以使用逗号分隔的字符串指定多个请求头,例如<code>"Content-Type,Authorization"</code>。</p></li><li><p><code>allowCredentials</code>:设置是否允许发送身份验证信息(如cookies)到服务器。默认情况下,这个属性是关闭的。如果你的应用需要使用身份验证信息,可以将其设置为<code>true</code>。</p></li></ul><p>下面是一个示例,演示如何同时设置多个属性:</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token annotation punctuation">@CrossOrigin</span><span class="token punctuation">(</span>origins <span class="token operator">=</span> <span class="token string">"http://example.com"</span><span class="token punctuation">,</span> methods <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token class-name">RequestMethod</span><span class="token punctuation">.</span><span class="token constant">GET</span><span class="token punctuation">,</span> <span class="token class-name">RequestMethod</span><span class="token punctuation">.</span><span class="token constant">POST</span><span class="token punctuation">}</span><span class="token punctuation">,</span> allowedHeaders <span class="token operator">=</span> <span class="token string">"Content-Type"</span><span class="token punctuation">,</span> allowCredentials <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>通过适当地配置这些属性,你可以根据你的需求进行跨域请求的处理。</p></li></ol><p>需要注意的是,配置跨域请求时,你应该仔细考虑安全性,并确保你的应用程序中的其他安全机制(如身份验证和授权)不受到跨域请求的影响。</p>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
</tags>
</entry>
<entry>
<title>Spring 注解</title>
<link href="/2023/11/19/spring-zhu-jie/"/>
<url>/2023/11/19/spring-zhu-jie/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>Spring Boot是一个用于构建Java应用程序的开源框架,它简化了基于Spring框架的应用程序的开发过程。Spring Boot提供了许多注解,用于配置和管理应用程序的各个方面。以下是一些常用的Spring Boot注解及其作用:</p><ol><li><p><code>@SpringBootApplication</code>:这是一个组合注解,用于标记主应用程序类。它包含了<code>@Configuration</code>、<code>@EnableAutoConfiguration</code>和<code>@ComponentScan</code>等注解,用于开启自动配置和组件扫描。</p></li><li><p><code>@RestController</code>:该注解用于标记一个类,表示它是一个RESTful风格的控制器。它结合了<code>@Controller</code>和<code>@ResponseBody</code>注解。</p></li><li><p><code>@RequestMapping</code>:这个注解用于映射HTTP请求到相应的处理方法。通过指定URL路径和请求方法,可以将请求映射到相应的控制器方法上。</p></li><li><p><code>@GetMapping</code>、<code>@PostMapping</code>、<code>@PutMapping</code>、<code>@DeleteMapping</code>:这些注解分别用于处理HTTP GET、POST、PUT和DELETE请求,并将它们映射到相应的处理方法上。</p></li><li><p><code>@RequestParam</code>:用于将请求参数绑定到方法的参数上。可以指定参数的名称、是否必需以及默认值等。</p></li><li><p><code>@PathVariable</code>:用于将URL路径变量绑定到方法的参数上。可以通过在URL路径上使用占位符来指定变量的名称。</p></li><li><p><code>@RequestBody</code>:用于将请求体的内容绑定到方法的参数上,通常用于处理POST请求中的JSON或XML数据。</p></li><li><p><code>@ResponseBody</code>:用于将方法的返回值直接作为响应体返回给客户端,常用于RESTful风格的控制器方法。</p></li><li><p><code>@Autowired</code>:用于自动装配依赖关系,通过类型匹配将相应的Bean注入到属性、构造函数或方法参数中。</p></li><li><p><code>@Component</code>:用于将类标记为一个可被Spring容器扫描和管理的组件。</p></li><li><p><code>@Configuration</code>:用于标记一个类,表示它是一个配置类,通常与<code>@Bean</code>一起使用。</p></li><li><p><code>@Bean</code>:用于在配置类中声明一个Bean对象,Spring会自动将其注册到应用程序上下文中。</p></li><li><p><code>@Value</code>:用于注入外部配置文件中的值到字段或方法参数上。</p></li><li><p><code>@Conditional</code>:用于根据条件决定是否注册Bean或执行配置。</p></li></ol><p>这只是Spring Boot注解的一小部分,还有很多其他的注解可用于完成更复杂的任务。你可以根据具体的需求和场景去查阅Spring Boot的官方文档,深入了解每个注解的详细用法和配置选项。</p>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
</tags>
</entry>
<entry>
<title>spring技术大纲</title>
<link href="/2023/11/19/spring-ji-zhu-da-gang/"/>
<url>/2023/11/19/spring-ji-zhu-da-gang/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><ol><li><p>IoC(控制反转)和DI(依赖注入)</p><ul><li>理解IoC和DI的概念</li><li>学习如何使用注解实现DI</li><li>了解如何通过注解配置Bean的依赖关系</li></ul></li><li><p>Spring核心注解</p><ul><li><code>@Component</code>及其衍生注解:<code>@Service</code>、<code>@Repository</code>、<code>@Controller</code></li><li><code>@Autowired</code>:自动装配依赖</li><li><code>@Qualifier</code>:指定注入的具体Bean</li><li><code>@Value</code>:注入属性值</li><li><code>@Configuration</code>和<code>@Bean</code>:Java配置和定义Bean</li></ul></li><li><p>AOP(面向切面编程)</p><ul><li>理解AOP的概念和作用</li><li>学习如何使用<code>@Aspect</code>和切面注解进行切面编程</li><li>掌握常用的切点表达式语法</li></ul></li><li><p>Spring MVC(Web开发)</p><ul><li>学习使用<code>@Controller</code>和<code>@RequestMapping</code>处理请求和路由</li><li>理解视图解析器和视图模板</li><li>处理表单数据、请求参数和响应结果</li><li>使用<code>@Valid</code>和数据绑定进行表单验证</li></ul></li><li><p>数据访问和持久化</p><ul><li>使用Spring JDBC进行关系型数据库访问</li><li>集成ORM框架(如Hibernate、MyBatis)进行对象关系映射</li><li>使用Spring Data访问各种数据存储技术</li><li>学习事务管理和声明式事务</li></ul></li><li><p>Spring Boot</p><ul><li>理解Spring Boot的基本原理和优势</li><li>学习如何快速构建和配置Spring Boot应用</li><li>掌握自动配置和起步依赖的使用</li><li>使用Spring Boot Actuator进行应用监控和管理</li></ul></li><li><p>Spring Security</p><ul><li>学习如何使用<code>@EnableWebSecurity</code>和配置类保护Web应用</li><li>实现身份认证和授权控制</li><li>使用注解进行方法级别的安全控制</li><li>定制化和扩展Spring Security的功能</li></ul></li></ol>]]></content>
<categories>
<category> Spring </category>
</categories>
<tags>
<tag> Spring </tag>
</tags>
</entry>
<entry>
<title>servlet解决跨域</title>
<link href="/2023/11/19/servlet-jie-jue-kua-yu/"/>
<url>/2023/11/19/servlet-jie-jue-kua-yu/</url>
<content type="html"><![CDATA[<link rel="stylesheet" class="aplayer-secondary-style-marker" href="/assets/css/APlayer.min.css"><script src="/assets/js/APlayer.min.js" class="aplayer-secondary-script-marker"></script><p>底层使用Java代码解决跨域问题,你可以通过配置<code>CorsFilter</code>或者自定义<code>Filter</code>来实现。</p><p>下面是使用Java代码解决跨域问题的示例:</p><ol><li><p>创建一个名为<code>CorsFilter</code>的Java类,并实现<code>javax.servlet.Filter</code>接口。</p><pre class="line-numbers language-`java" data-language="`java"><code class="language-`java">import javax.servlet.*;import javax.servlet.http.HttpServletResponse;import java.io.IOException;public class CorsFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { // 初始化操作(可选) } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) servletResponse; response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"); response.setHeader("Access-Control-Allow-Headers", "Content-Type,Authorization"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setHeader("Access-Control-Max-Age", "3600"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { // 销毁操作(可选) }}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>在上述示例中,我们在<code>doFilter</code>方法中设置了一些常见的跨域请求头,如<code>Access-Control-Allow-Origin</code>、<code>Access-Control-Allow-Methods</code>、<code>Access-Control-Allow-Headers</code>等。你可以根据需求进行自定义。</p></li><li><p>在你的Spring Boot应用中注册该过滤器。</p><ul><li><p>如果你使用的是基于Servlet的容器(如Tomcat),可以在<code>ServletInitializer</code>类的<code>onStartup</code>方法中进行注册:</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletContextInitializer</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Component</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">DispatcherType</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">FilterRegistration</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span></span><span class="token class-name">ServletContext</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">EnumSet</span></span><span class="token punctuation">;</span><span class="token annotation punctuation">@Component</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ServletInitializer</span> <span class="token keyword">implements</span> <span class="token class-name">ServletContextInitializer</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onStartup</span><span class="token punctuation">(</span><span class="token class-name">ServletContext</span> servletContext<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span> <span class="token punctuation">{</span> <span class="token class-name">FilterRegistration<span class="token punctuation">.</span>Dynamic</span> corsFilter <span class="token operator">=</span> servletContext<span class="token punctuation">.</span><span class="token function">addFilter</span><span class="token punctuation">(</span><span class="token string">"corsFilter"</span><span class="token punctuation">,</span> <span class="token class-name">CorsFilter</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span> corsFilter<span class="token punctuation">.</span><span class="token function">addMappingForUrlPatterns</span><span class="token punctuation">(</span><span class="token class-name">EnumSet</span><span class="token punctuation">.</span><span class="token function">allOf</span><span class="token punctuation">(</span><span class="token class-name">DispatcherType</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token string">"/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre></li><li><p>如果你使用的是基于Spring Web的容器(如Spring Boot内嵌的Tomcat),可以在<code>WebMvcConfigurer</code>的实现类中进行注册:</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>context<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">Configuration</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>config<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">CorsRegistry</span></span><span class="token punctuation">;</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>config<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">WebMvcConfigurer</span></span><span class="token punctuation">;</span><span class="token annotation punctuation">@Configuration</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">WebConfig</span> <span class="token keyword">implements</span> <span class="token class-name">WebMvcConfigurer</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">addCorsMappings</span><span class="token punctuation">(</span><span class="token class-name">CorsRegistry</span> registry<span class="token punctuation">)</span> <span class="token punctuation">{</span> registry<span class="token punctuation">.</span><span class="token function">addMapping</span><span class="token punctuation">(</span><span class="token string">"/**"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">allowedOrigins</span><span class="token punctuation">(</span><span class="token string">"*"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">allowedMethods</span><span class="token punctuation">(</span><span class="token string">"GET"</span><span class="token punctuation">,</span> <span class="token string">"POST"</span><span class="token punctuation">,</span> <span class="token string">"PUT"</span><span class="token punctuation">,</span> <span class="token string">"DELETE"</span><span class="token punctuation">,</span> <span class="token string">"OPTIONS"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">allowedHeaders</span><span class="token punctuation">(</span><span class="token string">"Content-Type"</span><span class="token punctuation">,</span> <span class="token string">"Authorization"</span><span class="token punctuation">)</span> <span class="token punctuation">.</span><span class="token function">allowCredentials</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 function">maxAge</span><span class="token punctuation">(</span><span class="token number">3600</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>在上述示例中,我们通过重写<code>addCorsMappings</code>方法来配置跨域请求。</p></li></ul></li></ol><p>通过以上的方式,你可以在底层使用Java代码解决Spring Boot应用的跨域问题。这样,无论是基于Servlet的容器还是基于Spring Web的容器,都能够正确处理跨域请求。</p>]]></content>