-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator_AdvancedFunctionality.m
More file actions
2077 lines (1936 loc) · 75.2 KB
/
Calculator_AdvancedFunctionality.m
File metadata and controls
2077 lines (1936 loc) · 75.2 KB
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
%{
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Copyright (C) [2023] [HPC2H2]
%}
function Calculator
%% 全局变量
global GUI
% 计算器待运算的字符串
global strPendingEvaluation
% 上一次运算的结果
global previousCalculationResult
% 模式转换三状态
% displayPrecision 输出结果的时候用
% complexFormat 在输出值为复数的时候用
% angularUnit 极直互换的时候用
global displayPrecision complexFormat angularUnit
% 控制工程存储的用户输入
global upperG lowerG upperH lowerH parameterPID
% 画图类型选择
global plotLineOrFunctionChosed
%画折线图 数据与x的个数
global xDataLine yDataLine xCountLine
% 画函数图 表达式与坐标轴范围
global funcGraphExpression xLimFunc yLimFunc
% 积分
global intUpperLimit intLowerLimit intKindChosed
% 求导
global derivativedExpression
% 定位多状态过程运行节点
% 模式转换进行到哪步了 max:(3)
global modeChoosingState
% 控制工程进行到哪一步了 max:(7)
global controlSystemsimulatingState
% 画图到哪一步了 max:(4/3)
global plottingState
% 求根到哪一步了 max:(2)
global findRootState
% 积分和求导进行到哪一步了 max:(4/3,4)
global intState diffState
% 极坐标和直角坐标的互换进行到哪一步了 max:(2)
global polState recState
% 初始化全局变量
strPendingEvaluation = '0'; previousCalculationResult = '0';
modeChoosingState = 0;
displayPrecision = 1; complexFormat = 1; angularUnit = 1;
controlSystemsimulatingState = 0;
upperG = 1;lowerG = [1 10];upperH = 1;lowerH = 1;
parameterPID = [1 0 0];
plottingState = 0;
plotLineOrFunctionChosed = 2;% 画折线图
xDataLine = []; yDataLine = []; xCountLine = 0;
xLimFunc = []; yLimFunc = [];
findRootState = 0;
intState = 0;
intUpperLimit = inf; intLowerLimit = -inf;
intKindChosed = 1;% 定积分
diffState = 0;
derivativedExpression = '';
polState = 0; recState = 0;
%% 计算器figure界面参数
fhHeight = 1100;
fhWidth = 600;
fhLeft = 1200;
fhBottom = 90;
GUI.fh = figure('units','pixels',...
'position',[fhLeft fhBottom fhWidth fhHeight],...
'menubar','none',...
'name','Calculator','NumberTitle','off');
% 计算器布局: 按钮9行5列,显示屏占3行5列的距离
% 大多数(最小)按钮大小 宽*高 = 108*88
% 各相对距离
btnWidth = 0.18; % 按钮宽度
btnHeight = 0.08; % 按钮高度
btnHorDis = (1 - 5*btnWidth)/6; % 两个按钮之间的水平距离
btnVerDis = (1 - 12*btnHeight)/11; % 两个按钮之间的竖直距离
btnInitLeft = btnHorDis;% 按钮离左边框的基准距离
btnInitBottom = btnVerDis;% 按钮离底边框的基准距离
%% 按钮布局
% Position 按钮位置 [left bottom width height]
% 数字按键 digit组
GUI.button0 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom btnWidth btnHeight], ...
'Style','pushbutton',...
'String','0', ...
'FontUnits','normalized', ...% 字体大小随窗口变化
'FontSize',0.5,...
'callback',{@processDigit,'0'},...
'UserData', 'digit');
GUI.button1 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ (btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','1', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processDigit,'1'},...
'UserData', 'digit');
GUI.button2 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + (btnWidth + btnHorDis) btnInitBottom ...
+ (btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','2', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processDigit,'2'},...
'UserData', 'digit');
GUI.button3 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 2*(btnWidth + btnHorDis) btnInitBottom ...
+ (btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','3', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processDigit,'3'},...
'UserData', 'digit');
GUI.button4 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ 2*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','4', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processDigit,'4'},...
'UserData', 'digit');
GUI.button5 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + (btnWidth + btnHorDis) btnInitBottom ...
+ 2*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','5', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processDigit,'5'},...
'UserData', 'digit');
GUI.button6 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 2*(btnWidth + btnHorDis) btnInitBottom ...
+ 2*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','6', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processDigit,'6'},...
'UserData', 'digit');
GUI.button7 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ 3*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','7', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processDigit,'7'},...
'UserData', 'digit');
GUI.button8 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + (btnWidth + btnHorDis) btnInitBottom ...
+ 3*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','8', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processDigit,'8'},...
'UserData', 'digit');
GUI.button9 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 2*(btnWidth + btnHorDis) btnInitBottom ...
+ 3*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','9', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processDigit,'9'},...
'UserData', 'digit');
% 小数点、倒数运算、等于号、加减乘除按键
% Equal 在求导与积分、画图、模式转换、求根、控制工程复用为"下一步"
% 除了选择模式情况之外都能用,属于 basic1 组
% 除了选择模式和输入数据的情况之外都能用,属于 basic2 组
% Equal(下一步)在任何情况下都能用,属于 essential 组
GUI.buttonDot = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + (btnWidth + btnHorDis) btnInitBottom ...
btnWidth btnHeight], ...
'Style','pushbutton',...
'String','.', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@executeBasicOperation,'.'},...
'UserData', 'basic1');
GUI.buttonReciprocal = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 2*(btnWidth + btnHorDis) btnInitBottom ...
btnWidth btnHeight], ...
'Style','pushbutton',...
'String','1/x', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@performAdvancedCalculation,'Recip'},...
'UserData', 'basic2');
GUI.buttonDiv = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 3*(btnWidth + btnHorDis) btnInitBottom ...
btnWidth btnHeight], ...
'Style','pushbutton',...
'String','÷', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@executeBasicOperation,'/'},...
'UserData', 'basic2');
GUI.buttonEqual = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 4*(btnWidth + btnHorDis) btnInitBottom ...
btnWidth btnHeight], ...
'Style','pushbutton',...
'String','=', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@evalPendingStr},...
'UserData', 'essential');
GUI.buttonSub = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 3*(btnWidth + btnHorDis) btnInitBottom ...
+ (btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','-', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@executeBasicOperation,'-'},...
'UserData', 'basic2');
GUI.buttonMult= uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 4*(btnWidth + btnHorDis) btnInitBottom ...
+ (btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','*', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@executeBasicOperation,'*'},...
'UserData', 'basic2');
GUI.buttonAdd = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 3*(btnWidth + btnHorDis) btnInitBottom ...
+ 2*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','+', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@executeBasicOperation,'+'},...
'UserData', 'basic2');
% Ans、回退、清空按键
% 求导与积分、画图、模式转换、求根、控制工程复用为"退出"
% Ans(退出)在任何情况下都能用,属于 essential 组
% 回退和清空除了选择模式的情况之外一般都能用,属于 basic1 组
GUI.buttonAns = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 4*(btnWidth + btnHorDis) btnInitBottom ...
+ 2*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','Ans', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@recallPreviousCalculationResult},...
'UserData', 'essential');
GUI.buttonBackspace = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 3*(btnWidth + btnHorDis) btnInitBottom ...
+ 3*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','回退', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@performBackspace},...
'UserData', 'basic1');
GUI.buttonEmpty = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 4*(btnWidth + btnHorDis) btnInitBottom ...
+ 3*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','清空', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@performEmpty},...
'UserData', 'basic1');
% 第五行: 画图、求根、控制工程、逗号按键
% 画图、求根、控制工程 属于 function 组
% 逗号 只能在输入数据的时候使用,分为 other 组
GUI.buttonPlot = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ 4*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','画图', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@startPlotGraph},...
'UserData', 'function');
GUI.buttonFindRoot = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + (btnWidth + btnHorDis) btnInitBottom ...
+ 4*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','求根', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@startFindRoot},...
'UserData', 'function');
GUI.buttonControlSystem = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 2*(btnWidth + btnHorDis) btnInitBottom ...
+ 4*(btnHeight + btnVerDis) 2*btnWidth + btnHorDis btnHeight], ...
'Style','pushbutton',...
'String','控制工程', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@simulateControlSystem},...
'UserData', 'function');
GUI.buttonComma= uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 4*(btnWidth + btnHorDis) btnInitBottom ...
+ 4*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String',',', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processComma},...
'UserData', 'other');
% 第六行: 求导、i、Pol(将直角坐标转为极坐标)、Rec(将极坐标转为直角坐标)、x
% 求导、POL、REC 属于 function 组
% i 不能用于极直坐标转换、画图、模式转换、控制工程,分为 other 组
% x 只能用于输入表达式的情况,即画函数图、积分、求导,分为 other 组
GUI.buttonDerive = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ 5*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','d□/dx', ...
'FontUnits','normalized', ...
'FontSize',0.4,...
'callback',{@startCalculateDifferentiation},...
'UserData', 'function');
GUI.buttoni = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + (btnWidth + btnHorDis) btnInitBottom ...
+ 5*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','i', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processImaginaryi},...
'UserData', 'other');
GUI.buttonPol = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 2*(btnWidth + btnHorDis) btnInitBottom ...
+ 5*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','Pol', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@startConvertCoordinates,'cart2pol'},...
'UserData', 'function');
GUI.buttonRec = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 3*(btnWidth + btnHorDis) btnInitBottom ...
+ 5*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','Rec', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@startConvertCoordinates,'pol2cart'},...
'UserData', 'function');
GUI.buttonx = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 4*(btnWidth + btnHorDis) btnInitBottom ...
+ 5*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','x', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processUnknownx},...
'UserData', 'other');
% 第七行: 左括号、右括号、sin-1、cos-1、tan-1
% 左括号、右括号 sin-1、cos-1、tan-1 属于 basic2 组
GUI.buttonLeftBracket = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ 6*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','(', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processBracket,'('},...
'UserData', 'basic2');
GUI.buttonRightBracket = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + (btnWidth + btnHorDis) btnInitBottom ...
+ 6*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String',')', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@processBracket,')'},...
'UserData', 'basic2');
GUI.buttonarcsin= uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 2*(btnWidth + btnHorDis) btnInitBottom ...
+ 6*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','arcsin', ...
'FontUnits','normalized', ...
'FontSize',0.4,...
'callback',{@performAdvancedCalculation,'asin'},...
'UserData', 'basic2');
GUI.buttonarccos= uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 3*(btnWidth + btnHorDis) btnInitBottom ...
+ 6*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','arccos', ...
'FontUnits','normalized', ...
'FontSize',0.4,...
'callback',{@performAdvancedCalculation,'acos'},...
'UserData', 'basic2');
GUI.buttonarctan= uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 4*(btnWidth + btnHorDis) btnInitBottom ...
+ 6*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','arctan', ...
'FontUnits','normalized', ...
'FontSize',0.4,...
'callback',{@performAdvancedCalculation,'atan'},...
'UserData', 'basic2');
% 第八行: 模式转换、sin、cos、tan
% 模式转换 属于 function 组
% sin、cos、tan 属于 basic2 组
GUI.buttonMode = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ 7*(btnHeight + btnVerDis) 2*btnWidth + btnHorDis btnHeight], ...
'Style','pushbutton',...
'String','模式转换', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@chooseCalculatorMode},...
'UserData', 'function');
GUI.buttonsin = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 2*(btnWidth + btnHorDis) btnInitBottom ...
+ 7*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','sin', ...
'FontUnits','normalized', ...
'FontSize',0.4,...
'callback',{@performAdvancedCalculation,'sin'},...
'UserData', 'basic2');
GUI.buttoncos = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 3*(btnWidth + btnHorDis) btnInitBottom ...
+ 7*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','cos', ...
'FontUnits','normalized', ...
'FontSize',0.4,...
'callback',{@performAdvancedCalculation,'cos'},...
'UserData', 'basic2');
GUI.buttontan= uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 4*(btnWidth + btnHorDis) btnInitBottom ...
+ 7*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton',...
'String','tan', ...
'FontUnits','normalized', ...
'FontSize',0.4,...
'callback',{@performAdvancedCalculation,'tan'},...
'UserData', 'basic2');
% 第九行: 积分、e^x、a^x、log10x、lnx
% 积分 属于 function 组
% e^x、a^x、log10x、lnx 属于 basic2 组
GUI.buttonIntegral = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ 8*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@startCalculateIntegration},...
'UserData', 'function');
imgIntegralRaw = imread('积分.jpg');
imgIntegral = imresize(imgIntegralRaw,[88,108]);
set(GUI.buttonIntegral,'CData',imgIntegral);
GUI.buttonExp = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + (btnWidth + btnHorDis) btnInitBottom ...
+ 8*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton', ...
'String','e^□', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@performAdvancedCalculation,'exp'},...
'UserData', 'basic2');
GUI.buttonPow = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 2*(btnWidth + btnHorDis) btnInitBottom ...
+ 8*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton', ...
'String','a^□', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@performAdvancedCalculation,'^'},...
'UserData', 'basic2');
GUI.buttonlog10 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 3*(btnWidth + btnHorDis) btnInitBottom ...
+ 8*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton', ...
'String','log10□', ...
'FontUnits','normalized', ...
'FontSize',0.35,...
'callback',{@performAdvancedCalculation,'log10'},...
'UserData', 'basic2');
GUI.buttonln = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft + 4*(btnWidth + btnHorDis) btnInitBottom ...
+ 8*(btnHeight + btnVerDis) btnWidth btnHeight], ...
'Style','pushbutton', ...
'String','ln□', ...
'FontUnits','normalized', ...
'FontSize',0.5,...
'callback',{@performAdvancedCalculation,'log'},...
'UserData', 'basic2');
% 显示屏两行文本
% 从上往下第一行
GUI.textDisplay1 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ 10.5*btnHeight + 10*btnVerDis 5*btnWidth + 4*btnHorDis 1.5*btnHeight], ...
'Style','text', ...
'String','0', ...
'FontUnits','normalized', ...
'FontSize',0.4,...
'HorizontalAlignment','right'); % HorizontalAlignment 文本对齐方式
% 从上往下第二行
GUI.textDisplay2 = uicontrol('Parent',GUI.fh, ...
'Units','normalized',...
'Position',[btnInitLeft btnInitBottom ...
+ 9*(btnHeight + btnVerDis) 5*btnWidth + 4*btnHorDis 1.5*btnHeight], ...
'Style','text', ...
'String','0', ...
'FontUnits','normalized', ...
'FontSize',0.4,...
'HorizontalAlignment','right'); % HorizontalAlignment 文本对齐方式
initOrReset;
end
%% 回调函数
% 处理数字
function processDigit(~,~,strNum)
global GUI strPendingEvaluation modeChoosingState
global displayPrecision complexFormat angularUnit
global intState intKindChosed
global plottingState plotLineOrFunctionChosed
global controlSystemsimulatingState
% 模式转换 1~3
switch modeChoosingState
% 设置显示精度
case 1
switch strNum
case '1'
displayPrecision = 1;
case '2'
displayPrecision = 2;
case '3'
displayPrecision = 3;
case '4'
displayPrecision = 4;
otherwise
return;
end
set(GUI.textDisplay1,'String','复数表示');
set(GUI.textDisplay2,'String',{'1.a+bi ','2. A∠φ'},...
'HorizontalAlignment','right');
modeChoosingState = modeChoosingState + 1;
return;
% 设置复数表示
case 2
switch strNum
case '1'
complexFormat = 1; % a+bi
case '2'
complexFormat = 2; % A∠φ
otherwise
return;
end
set(GUI.textDisplay1,'String','角度单位');
set(GUI.textDisplay2,'String',{'1.角度制','2. 弧度制'},...
'HorizontalAlignment','right');
modeChoosingState = modeChoosingState + 1;
return;
case 3
switch strNum
case '1'
angularUnit = 1; % 角度制
case '2'
angularUnit = 2; % 弧度制
otherwise
return;
end
initOrReset; % 最后一个状态
modeChoosingState = 0;
return;
end
% 积分 1
if intState == 1
switch strNum
case '1'
intKindChosed = 1;
initBtnForData;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String',...
'输入积分上下限(下限,上限)(默认为:-inf,+inf): ');
set(GUI.textDisplay2,'String', strPendingEvaluation);
case '2'
intKindChosed = 2;
initBtnForExpression;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String','输入表达式: ');
set(GUI.textDisplay2,'String', strPendingEvaluation);
otherwise
return;
end
intState = intState + 1;
return;
end
% 画图 1
if plottingState == 1
switch strNum
case '1'
plotLineOrFunctionChosed = 1;
initBtnForData;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String','输入横坐标轴的范围(用逗号分隔):');
set(GUI.textDisplay2,'String', strPendingEvaluation);
case '2'
plotLineOrFunctionChosed = 2;
initBtnForData;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String', ...
'输入x(用逗号分隔):');
set(GUI.textDisplay2,'String', strPendingEvaluation);
otherwise
return;
end
plottingState = plottingState + 1;
return;
end
% 控制工程 6 7
switch controlSystemsimulatingState
case 6
controlSystemAnalysis(strNum)
return;
case 7 % 修改系统(回退步骤)
switch strNum
case '1' % 回退到设置pid参数
controlSystemsimulatingState = 5;
initBtnForData;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String',...
'请输入pid参数(kp,ki,kd): ');
set(GUI.textDisplay2,'String',strPendingEvaluation);
case '2' % 回退到设置反馈通路传递函数分子
controlSystemsimulatingState = 3;
initBtnForData;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String',{'反馈通路传递函数(默认为1): ',...
'分子系数(逗号分隔降幂排列): '},'FontSize',0.3);
set(GUI.textDisplay2,'String',strPendingEvaluation);
case '3' % 回退到设置前向通路传递函数分子
controlSystemsimulatingState = 1;
initBtnForData;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String',{'前向通路传递函数: ',...
'分子系数(逗号分隔降幂排列): '},'FontSize',0.3);
set(GUI.textDisplay2,'String',strPendingEvaluation);
otherwise
return;
end
set(GUI.buttonEqual,'String','下一步','FontSize',0.4);
return;
end
% 一般情况
if strPendingEvaluation == '0'
strPendingEvaluation = strNum;
else
if ~ismember(strPendingEvaluation(end),['i','x',')'])
strPendingEvaluation = [strPendingEvaluation strNum];
end
end
set(GUI.textDisplay2,'String',strPendingEvaluation);
end
% 处理加减乘除和小数点
function executeBasicOperation(~,~,operator)
global GUI strPendingEvaluation
if ~ismember(strPendingEvaluation(end),['.','(','+','-','*','/'])
% 负号
if operator == '-' && strcmp(strPendingEvaluation,'0')
strPendingEvaluation = '-';
else
strPendingEvaluation = [strPendingEvaluation operator];
end
end
set(GUI.textDisplay2,'String',strPendingEvaluation);
end
% 处理'i'
% 不能用于极直坐标转换、画图、模式转换、控制工程
function processImaginaryi(~,~)
global GUI strPendingEvaluation
if strcmp(strPendingEvaluation,'0')
strPendingEvaluation = 'i';
elseif ~ismember(strPendingEvaluation(end),['.','i','x',')'])
strPendingEvaluation = [strPendingEvaluation 'i'];
end
set(GUI.textDisplay2,'String',strPendingEvaluation);
end
% 处理'括号'
function processBracket(~,~,bracket)
global GUI strPendingEvaluation
switch bracket
case '('
if strcmp(strPendingEvaluation,'0')
strPendingEvaluation = '(';
elseif ~ismember(strPendingEvaluation(end),['.','i','x',')','0',...
'1','2','3','4','5','6','7','8','9'])
strPendingEvaluation = [strPendingEvaluation '('];
end
case ')'
if strcmp(strPendingEvaluation,'0')
strPendingEvaluation = '(';
elseif ~ismember(strPendingEvaluation(end),['.','(','+','-','*',...
'/'])
strPendingEvaluation = [strPendingEvaluation ')'];
end
end
set(GUI.textDisplay2,'String',strPendingEvaluation);
end
% 处理等于号
% 极直互换、画图、模式转换、求根、控制工程、求导与积分中复用为'下一步'
function evalPendingStr(~,~)
global GUI strPendingEvaluation previousCalculationResult
global displayPrecision complexFormat angularUnit
global modeChoosingState polState recState findRootState
global intState intKindChosed
global intUpperLimit intLowerLimit
global diffState derivativedExpression
global plottingState plotLineOrFunctionChosed
global xDataLine yDataLine xCountLine
global funcGraphExpression xLimFunc yLimFunc
global controlSystemsimulatingState
global upperG lowerG upperH lowerH parameterPID
global controlSystemOptionChosed
strDisplay = get(GUI.buttonEqual, 'String');
% 处理多状态过程
if ~strcmp(strDisplay, '退出') % 可以是'下一步'或'选择界面'
% 模式转换
switch modeChoosingState
case 1
set(GUI.textDisplay1,'String','复数表示');
set(GUI.textDisplay2,'String',{'1.a+bi ','2. A∠φ'},...
'HorizontalAlignment','right');
modeChoosingState = modeChoosingState + 1;
return;
case 2
set(GUI.textDisplay1,'String','角度单位');
set(GUI.textDisplay2,'String',{'1.角度制','2. 弧度制'},...
'HorizontalAlignment','right');
modeChoosingState = modeChoosingState + 1;
return;
case 3
modeChoosingState = 0;
initOrReset;
return;
end
% 处理极直互换
% 直角坐标转为极坐标
if polState >= 1
if polState == 1
[isMatch, numberCount] = isCommaSeparatedNumbers(...
strPendingEvaluation);
if isMatch && numberCount == 2
polState = polState + 1;
initBtnForDisplay;
set(GUI.textDisplay1,'String','结果');
set(GUI.textDisplay2,'String',...
convertCoordinates(strPendingEvaluation));
else
polState = 0;
initBtnForDisplay;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String','错误: 该语句不完整(或误用减号)。');
set(GUI.textDisplay2,'String', strPendingEvaluation);
end
clear isMatch numberCount
return;
else
polState = 0;
initOrReset;
return;
end
end
% 极坐标转为直角坐标
if recState >= 1
if recState == 1
[isMatch, numberCount] = isCommaSeparatedNumbers(...
strPendingEvaluation);
if isMatch && numberCount == 2
recState = recState + 1;
initBtnForDisplay;
set(GUI.textDisplay1,'String','结果');
set(GUI.textDisplay2,'String',...
convertCoordinates(strPendingEvaluation));
else
recState = 0;
initBtnForDisplay;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String','错误: 该语句不完整(或误用减号)。');
set(GUI.textDisplay2,'String', strPendingEvaluation);
end
clear isMatch numberCount;
return;
else
recState = 0;
initOrReset;
return;
end
end
% 求根
if findRootState
try
[numericRoot, exactRoot] = findRoots(strPendingEvaluation);
catch
findRootState = 0;
initBtnForDisplay;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String','错误: 表达式不完整。');
set(GUI.textDisplay2,'String', strPendingEvaluation);
return;
end
if ~isnan(exactRoot)
previousCalculationResult = allNum2Char(exactRoot);
set(GUI.textDisplay1,'String','精确解: ');
if displayPrecision ~= 4
set(GUI.textDisplay2,'String',...
previousCalculationResult);
end
elseif ~isnan(numericRoot)
previousCalculationResult = allNum2Char(numericRoot);
set(GUI.textDisplay1,'String','解析解: ');
if displayPrecision ~= 4
set(GUI.textDisplay2,'String',...
previousCalculationResult);
end
else
initBtnForDisplay;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String','错误: 无法求解。');
set(GUI.textDisplay2,'String', strPendingEvaluation);
end
findRootState = 0;
clear exactRoot numericRoot;
return;
end
% 定积分 第一状态默认选项
if intState == 1 && intKindChosed == 1 % 默认选项
initBtnForData;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String',...
'输入积分上下限(下限,上限)(默认为:-inf,+inf): ');
set(GUI.textDisplay2,'String', strPendingEvaluation);
intState = intState + 1;
return;
end
% 定积分 第二状态
if intState == 2 && intKindChosed == 1
[isMatch, numberCount] = isCommaSeparatedNumbers(...
strPendingEvaluation);
if isMatch && numberCount == 2
% 储存积分上下限
intLimit = sscanf(strPendingEvaluation,'%f,%f');
[intLowerLimit, intUpperLimit] = deal(min(intLimit),...
max(intLimit));
intState = intState + 1;
initBtnForExpression;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String','输入表达式: ');
set(GUI.textDisplay2,'String',strPendingEvaluation);
clear intLimit;
else
if strcmp(strPendingEvaluation,'0') % 取默认值 [-inf, inf]
intLowerLimit = -inf;
intUpperLimit = inf;
intState = intState + 1;
initBtnForExpression;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String','输入表达式: ');
set(GUI.textDisplay2,'String',strPendingEvaluation);
else % 输入有误
intState = 0;
initBtnForDisplay;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String','错误: 该语句不完整(或误用减号)。');
set(GUI.textDisplay2,'String', strPendingEvaluation);
end
end
clear isMatch numberCount;
return;
end
% 定积分 第三状态
if intState == 3 && intKindChosed == 1
integralValue = calculateDefiniteIntegral(intLowerLimit,...
intUpperLimit,strPendingEvaluation);
if ismember('错误',integralValue) % integralValue 为报错信息
intState = 0;
initBtnForDisplay;
strPendingEvaluation = '0';
set(GUI.textDisplay1,'String', integralValue);
set(GUI.textDisplay2,'String', strPendingEvaluation);
else % integralValue 为积分结果
intState = 0;
previousCalculationResult = num2str(integralValue);
initBtnForDisplay;
set(GUI.textDisplay1,'String',{['积分上限: ',...
num2str(intLowerLimit)],['积分下限: ',...
num2str(intUpperLimit)]});