-
Notifications
You must be signed in to change notification settings - Fork 0
/
SC(有注释).c
1631 lines (1500 loc) · 69.6 KB
/
SC(有注释).c
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
#include <stdio.h>
#include <math.h>
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
#include <ctype.h>
#include <bios.h>
#define SCREENWIDTH 120
#define BUTTONY 52
#define BUTTONX 58
#define GAP 4
#define BLANK 10
#define MSIZEX 10
#define MSIZEY 15
extern int shift=0;
void far *imagep;
void far *imagep1;
extern char input[151]="", output[51]="", Ans[51]="";
extern char buttonleft[36][7]={"Bin", "Oct", "Dec", "Hex", "Rad", "Deg",
"PI", "e", "1/x", "x^2", "x^y", "sqrt",
"sin", "cos", "tan", "log", "ln", "sqry",
"Mod", " ", "abs", "n!", "Ans", "Shift",
"&", "|", "!", "XOR", "<<", ">>",
"A", "B", "C", "D", "E", "F"};
extern char buttonright[24][7]={"", "", "Clear", "",
"(", ")", "%", "/",
"7", "8", "9", "*",
"4", "5", "6", "-",
"1", "2", "3", "+",
"00", "0", ".", "="};
extern char extra[5][7]={"asin", "acos", "atan","10^x","exp"};
void InitGraph(void);
void welcome(void);
void UI(void);
void InitMouse(void);
void MousePosition(int *x, int *y);
int LeftPress(void);
int RightPress(void);
int fbuttoni(int x,int y);
int fbuttonj(int x,int y);
int fbuttonLR(int x,int y);
void buttondown(int i, int j,int LR);
void buttonup(int i, int j,int LR);
void show(int i, int j, int LR);
void outequation(int i, int j, int LR);
void backspace(void);
void clearscreen(void);
void instring(int i, int j, int LR);
void outstring(void);
int judge(char *input);
#define MAX_TOKEN_LEN 100 /*最大长度*/
#define EXPR_INCREMENT 20 /*防止容量不够时的容量增量*/
typedef struct
{
double opnd; /*储存操作数*/
char optr[11]; /*储存运算符*/
int flag; /*区别单双目运算*/
} StackItem; /*定义一个栈项*/
typedef struct SNode
{
StackItem date;
struct SNode * next;
} SNode, *Stack;/*定义一个栈*/
struct
{
char str[MAX_TOKEN_LEN];
int type;
} token;/*存储操作数或者运算符*/
struct
{
char *str;
int cur;
} expr;/*存储表达式*/
Stack OPND, OPTR; /*定义全局变量,两个栈,分别存储操作数与运算符*/
int expr_size;
/**********************************************************
* 函数原型: void InitStack(Stack *S)
* 传入参数: 指向栈的指针
* 返回值: 无
* 函数功能: 建立一个栈
**********************************************************/
void InitStack(Stack *S)
{
*S = (Stack)malloc(sizeof(SNode));
if(!(*S))
{
printf("动态申请内存失败!\n");
exit(0);
}
(*S)->next = NULL;
}
/**********************************************************
* 函数原型: void DestroyStack(Stack *S)
* 传入参数: 指向栈的指针
* 返回值: 无
* 函数功能: 销毁栈
**********************************************************/
void DestroyStack(Stack *S)
{
SNode *p;
while(p = *S)
{
*S = p->next;
free(p);
}
}
/**********************************************************
* 函数原型: void Push(Stack S, StackItem e)
* 传入参数: 栈S,栈项e
* 返回值: 无
* 函数功能: 压栈操作
**********************************************************/
void Push(Stack S, StackItem e)
{
SNode *p;
p = (SNode *)malloc(sizeof(SNode));
if(! p)
{
printf("动态申请内存失败!\n");
exit(0);
}
strcpy(p->date.optr,e.optr);
p->date.opnd = e.opnd;
p->date.flag = e.flag;
p->next = S->next;
S->next = p;
}
/**********************************************************
* 函数原型: void Pop(Stack S,StackItem *e)
* 传入参数: 栈S,栈项e
* 返回值: 无
* 函数功能: 弹出栈项操作
**********************************************************/
void Pop(Stack S,StackItem *e)
{
SNode *p;
p = S->next;
if(! p)
{
printf("栈为空,不能出栈!\n");
exit(0);
}
S->next = p->next;
strcpy(e->optr,p->date.optr);
e->opnd = p->date.opnd;
e->flag = p->date.flag;
free(p);
}
/***********************************************************************************************************************************/
/**********************************************************
* 函数原型: void get_expr(char *s)
* 传入参数: 字符串s
* 返回值: 无
* 函数功能: 从字符串中提取表达式
**********************************************************/
void get_expr(char *s)
{
char *p;
int size;
int i=0;
expr.cur = 0;
expr_size = 100;
expr.str = (char*)malloc(expr_size * sizeof(char));
if(! expr.str)
{
printf("内存分配失败!\n");
exit(0);
}
size = 0;
p = expr.str;
while((*p = s[i]) != '=')
{
if(*p != ' ')
{
p++;
size++;
if(size == expr_size-1)
{
expr_size += EXPR_INCREMENT;
expr.str = (char*)realloc(expr.str,expr_size * sizeof(char));
if(! expr.str)
{
printf("内存分配失败!\n");
exit(0);
}
p = &expr.str[size];
} /*size满了的时候要重新给它增加量*/
}/*if*/
i++;
} /*while*/
*p++ = '#';
*p = '\0';
}
/**********************************************************
* 函数原型: int IsOpnd(char ch)
* 传入参数: 字符ch
* 返回值: 1或0
* 函数功能: 判断字符ch是否为操作数
**********************************************************/
int IsOpnd(char ch)
{
if((ch >= '0') && (ch <= '9') || (ch == '.'))
{
return 1;
}
if((ch == '-') || (ch == '+') || (ch == '~'))
{
if((expr.cur == 0) || (expr.str[expr.cur-1] == '('))
{
return 1;
}
}/*如果-,+,~前面无数字,或者是(的话,则为正负号或取反符号,也列入操作数*/
return 0;
}
/**********************************************************
* 函数原型: void gettoken()
* 传入参数: 无
* 返回值: 无
* 函数功能: 获取一个操作数或运算符
**********************************************************/
void gettoken()
{
char *p = token.str;
*p = expr.str[expr.cur];
if(IsOpnd(*p))
{
while(IsOpnd(*++p = expr.str[++expr.cur]));
*p = '\0';
token.type = 0;
return;
} /*token.type,如果是操作数则为0,是运算符为1*/
if((*p >= 'a') && (*p <= 'z'))
{
while((expr.str[expr.cur+1] >= 'a') && (expr.str[expr.cur+1] <= 'z'))
{
*++p = expr.str[++expr.cur];
}
}
++expr.cur;
*++p = '\0';
if(!strcmp(token.str,"e"))
{
sprintf(token.str,"%.16g",exp(1));
token.type = 0;
return;
} /*token.type,如果是操作数则为0,是运算符为1*/
if(!strcmp(token.str,"pi"))
{
sprintf(token.str,"%.16g",exp(1));
strcpy(token.str,"3.1415926535897932");
token.type = 0;
return;
} /*token.type,如果是操作数则为0,是运算符为1*/
token.type = 1;
}
/**********************************************************
* 函数原型: char Precede(StackItem *optr1, StackItem *optr2)
* 传入参数: 运算符1,运算符2
* 返回值: '>'或'<'
* 函数功能: 判断运算符优先级
**********************************************************/
char Precede(StackItem *optr1, StackItem *optr2)
{
char *str1,*str2;
str1 = optr1->optr;
str2 = optr2->optr;
if(!strcmp(str1,"ln") || !strcmp(str1,"lg") || !strcmp(str1,"sin") || !strcmp(str1,"cos") || !strcmp(str1,"tan") ||
!strcmp(str1, "asin") || !strcmp(str1, "acos") || !strcmp(str1, "atan") || !strcmp(str1, "sqrt") || !strcmp(str1, "abs"))
{
optr1->flag = 1;
return (!strcmp(str2,"(") || !strcmp(str2,"^") || !strcmp(str2,"!")) ? '<' : '>';
} /*设置单目运算符优先级*/
if(!strcmp(str1,"!"))
{
optr1->flag = 1;
return '>';
} /*设置单目运算符优先级*/
optr1->flag = 2;
switch(str1[0])
{
case '|': return (!strcmp(str2,"|") ||
!strcmp(str2,")") || !strcmp(str2,"#")) ? '>' : '<';
case 'x': return (!strcmp(str2,"|") ||
!strcmp(str2,"x") ||
!strcmp(str2,")") || !strcmp(str2,"#")) ? '>' : '<';
case '&': return (!strcmp(str2,"|") ||
!strcmp(str2,"x") ||
!strcmp(str2,"&") ||
!strcmp(str2,")") || !strcmp(str2,"#")) ? '>' : '<';
case '<': case '>': return (!strcmp(str2,"|") ||
!strcmp(str2,"x") ||
!strcmp(str2,"&") ||
!strcmp(str2,"<") || !strcmp(str2,">") ||
!strcmp(str2,")") || !strcmp(str2,"#")) ? '>' : '<';
case '+': case '-': return (!strcmp(str2,"|") ||
!strcmp(str2,"x") ||
!strcmp(str2,"&") ||
!strcmp(str2,"<") || !strcmp(str2,">") ||
!strcmp(str2,"+") || !strcmp(str2,"-") ||
!strcmp(str2,")") || !strcmp(str2,"#")) ? '>' : '<';
case '*': case '/': case 'm': return (!strcmp(str2,"|") ||
!strcmp(str2,"x") ||
!strcmp(str2,"^") ||
!strcmp(str2,"<") || !strcmp(str2,">") ||
!strcmp(str2,"+") || !strcmp(str2,"-") ||
!strcmp(str2,"*") || !strcmp(str2,"/") || !strcmp(str2,"m") ||
!strcmp(str2,")") || !strcmp(str2,"#")) ? '>' : '<';
case '(': return (!strcmp(str2,")")) ? '=' : '<';
case ')': return '>';
case '^': return (!strcmp(str2,"(")|| !strcmp(str2,"!") || !strcmp(str2,"^")) ? '<' : '>';
case '#': return (!strcmp(str2,"#")) ? '=' : '<';
}/*设置双目运算符优先级*/
}
/**********************************************************
* 函数原型: long fact(long n)
* 传入参数: n
* 返回值: 阶乘结果
* 函数功能: 算出阶乘
**********************************************************/
long fact(long n)
{
return (n <= 1) ? 1 : n * fact(n-1);
}/*阶乘运算*/
/**********************************************************
* 函数原型: StackItem Operate(StackItem opnd1, StackItem optr, StackItem opnd2)
* 传入参数: 操作数1,运算符,操作数2
* 返回值: 栈项temp
* 函数功能: 运算操作,最后返回运算后的结果栈项
**********************************************************/
StackItem Operate(StackItem opnd1, StackItem optr, StackItem opnd2)
{
StackItem temp;
if(optr.flag == 1)
{
if(!strcmp(optr.optr,"!")) temp.opnd = fact((long)opnd2.opnd);
else if(!strcmp(optr.optr,"lg")) temp.opnd = log10(opnd2.opnd);
else if(!strcmp(optr.optr,"ln")) temp.opnd = log(opnd2.opnd);
else if(!strcmp(optr.optr,"sin")) temp.opnd = sin(opnd2.opnd);
else if(!strcmp(optr.optr,"cos")) temp.opnd = cos(opnd2.opnd);
else if(!strcmp(optr.optr,"tan")) temp.opnd = tan(opnd2.opnd);
else if(!strcmp(optr.optr,"asin")) temp.opnd = asin(opnd2.opnd);
else if(!strcmp(optr.optr,"acos")) temp.opnd = acos(opnd2.opnd);
else if(!strcmp(optr.optr,"atan")) temp.opnd = atan(opnd2.opnd);
else if(!strcmp(optr.optr,"sqrt")) temp.opnd = sqrt(opnd2.opnd);
else if(!strcmp(optr.optr,"abs")) temp.opnd = fabs(opnd2.opnd);
return temp;
} /*单目运算符运算*/
switch(optr.optr[0])
{
case '|': temp.opnd = (double)((int)opnd1.opnd | (int)opnd2.opnd); break;
case 'x': temp.opnd = (double)((int)opnd1.opnd ^ (int)opnd2.opnd); break;
case '&': temp.opnd = (double)((int)opnd1.opnd & (int)opnd2.opnd); break;
case '<': temp.opnd = (double)((int)opnd1.opnd << (int)opnd2.opnd); break;
case '>': temp.opnd = (double)((int)opnd1.opnd >> (int)opnd2.opnd); break;
case '+': temp.opnd = opnd1.opnd + opnd2.opnd; break;
case '-': temp.opnd = opnd1.opnd - opnd2.opnd; break;
case '*': temp.opnd = opnd1.opnd * opnd2.opnd; break;
case '/': temp.opnd = opnd1.opnd / opnd2.opnd; break;
case 'm': temp.opnd = (double)((int)opnd1.opnd % (int)opnd2.opnd); break;
case '^': temp.opnd = pow(opnd1.opnd,opnd2.opnd);
} /*双目运算符运算*/
return temp;
}
/**********************************************************
* 函数原型: double compute(char *s)
* 传入参数: 字符串s
* 返回值: 字符串计算结果
* 函数功能: 计算用户输入的算式
**********************************************************/
double compute(char *s)
{
StackItem optr,opnd1,opnd2;
double result;
get_expr(s);
InitStack(&OPTR);
InitStack(&OPND);
strcpy(optr.optr,"#");
Push(OPTR, optr);
gettoken();
while(strcmp(token.str,"#") || strcmp(OPTR->next->date.optr,"#"))
{
if(token.type) /*如果token是运算符的话执行*/
{
strcpy(optr.optr,token.str);
switch(Precede(&(OPTR->next->date),&optr))
{
case '<':
strcpy(optr.optr,token.str);
Push(OPTR,optr);
gettoken();
break;
case '=':
Pop(OPTR,&optr);
gettoken();
break;
case '>': /*如果发现前面的运算符优先级比较高则先取出栈项进行计算*/
Pop(OPTR,&optr);
Pop(OPND,&opnd2);
if(optr.flag == 2)
{
Pop(OPND,&opnd1);
}
Push(OPND,Operate(opnd1,optr,opnd2));
}
}
else /*如果token是操作数的话执行*/
{
if(token.str[0] == '~')/*如果需要取反先操作使之变为取反后的数*/
{
strcpy(token.str,token.str+1);
opnd1.opnd = (double)(~(int)atof(token.str));
}
else
opnd1.opnd = atof(token.str);
Push(OPND,opnd1);
gettoken(); /*获取下个符号*/
}
}
result = OPND->next->date.opnd;
free(expr.str);
DestroyStack(&OPTR);
DestroyStack(&OPND);/*销毁栈和运算式*/
return result;/*返回计算后的结果*/
}
/**************************************************************************************************************************************/
/**********************************************************
* 函数原型: void InitMouse()
* 传入参数: 无
* 返回值: 无
* 函数功能: 鼠标初始化
鼠标初始化失败,程序终止退出
**********************************************************/
void InitMouse()
{
int *x, *y;
union REGS r;
r.x.ax = 0; /*鼠标零号功能:鼠标复位*/
int86(0x33, &r, &r); /*调用0x33号中断*/
if(r.x.ax != 0xFFFF)
{
printf("Mouse error: Not installed\n");
exit(1);
}
r.x.ax = 0x0004;
r.x.cx = 0;
r.x.dx = 0;
int86(0x33, &r, &r);
r.x.ax = 2;
int86(0x33, &r, &r);
r.x.ax = 0x0003;
int86(0x33, &r, &r);
*x = r.x.cx;
*y = r.x.dx;
}
/**********************************************************
* 函数原型: void MousePosition(int *x, int *y)
* 传入参数: x,y是指针变量,指向鼠标坐标的(x,y)的地址
* 返回值: 无
* 函数功能: 获取鼠标坐标(x,y)
无返回值,鼠标位置坐标由指针带回
**********************************************************/
void MousePosition(int *x, int *y)
{
union REGS r;
r.x.ax = 3; /*鼠标器3号功能:取按钮状态和鼠标位置*/
int86(0x33, &r, &r);
*x = r.x.cx; /*鼠标x坐标存于指针x所指的单元*/
*y = r.x.dx; /*鼠标y坐标存于指针y所指的单元*/
}
/**********************************************************
* 函数原型: int LeftPress()
* 传入参数: 无
* 返回值:1-鼠标左键单击;0-左键未单击
* 函数功能: 检测鼠标左键是否单击
**********************************************************/
int LeftPress()
{
union REGS r;
r.x.ax= 3; /*鼠标器3号功能:取按钮状态和鼠标位置*/
int86(0x33, &r, &r);
return r.x.bx&1; /*单击了左键函数值为1*/
}
/**********************************************************
* 函数原型: int RightPress()
* 传入参数: 无
* 返回值:1-鼠标右键单击;0-右键未单击
* 函数功能: 检测鼠标右键是否单击
**********************************************************/
int RightPress()
{
union REGS r;
r.x.ax= 3; /*鼠标器3号功能:取按钮状态和鼠标位置*/
int86(0x33, &r, &r);
delay(30000);
return r.x.bx&2; /*单击了右键函数值为1*/
}
/**********************************************************
* 函数原型: int fbuttoni(int x,int y)
* 传入参数: x-鼠标位置的x坐标
y-鼠标位置的y坐标
* 返回值:鼠标停留按钮的列数
* 函数功能: 从鼠标停留位置坐标计算鼠标停留按钮的列数
**********************************************************/
int fbuttoni(int x,int y){
int i,j;
int buttoni,buttonj,buttonLR,buttonflag;
for(i=0;i<6;i++){
for(j=0;j<6;j++){
if((x>(10+i*(BUTTONX+GAP)))
&&(x<(10+(i+1)*(BUTTONX+GAP)))
&&(y>(140+j*(BUTTONY+GAP)))
&&(y<(140+(j+1)*(BUTTONY+GAP)))){
buttoni = i;
buttonj = j;
buttonflag = 1;
buttonLR = 0;
}
}
}
for(i = 0;i<4;i++){
for(j = 0;j<6;j++){
if((x>(i*(BUTTONX+GAP))+386)&&(x<((i+1)*(BUTTONX+GAP)+386-GAP))&&
(y>(j*(BUTTONY+GAP)+140))&&(y<((j+1)*(BUTTONY+GAP)+140-GAP))){
buttoni = i;
buttonj = j;
buttonflag = 1;
buttonLR = 1;
}
}
}
if(buttonflag==1)return buttoni;
}
/**********************************************************
* 函数原型: int fbuttonj(int x,int y)
* 传入参数: x-鼠标位置的x坐标
y-鼠标位置的y坐标
* 返回值:鼠标停留按钮的行数
* 函数功能: 从鼠标停留位置坐标计算鼠标停留按钮的行数
**********************************************************/
int fbuttonj(int x,int y){
int i,j;
int buttoni,buttonj,buttonLR,buttonflag;
for(i=0;i<6;i++){
for(j=0;j<6;j++){
if((x>(10+i*(BUTTONX+GAP)))
&&(x<(10+(i+1)*(BUTTONX+GAP)))
&&(y>(140+j*(BUTTONY+GAP)))
&&(y<(140+(j+1)*(BUTTONY+GAP)))){
buttoni = i;
buttonj = j;
buttonflag = 1;
buttonLR = 0;
}
}
}
for(i = 0;i<4;i++){
for(j = 0;j<6;j++){
if((x>(i*(BUTTONX+GAP))+386)&&(x<((i+1)*(BUTTONX+GAP)+386-GAP))&&
(y>(j*(BUTTONY+GAP)+140))&&(y<((j+1)*(BUTTONY+GAP)+140-GAP))){
buttoni = i;
buttonj = j;
buttonflag = 1;
buttonLR = 1;
}
}
}
if(buttonflag==1)return buttonj;
}
/**********************************************************
* 函数原型: int fbuttonLR(int x,int y)
* 传入参数: x-鼠标位置的x坐标
y-鼠标位置的y坐标
* 返回值:LR-表示按钮的左右位置,0为左侧,1为右侧
* 函数功能: 从鼠标停留位置坐标计算鼠标停留按钮的左右位置
**********************************************************/
int fbuttonLR(int x,int y){
int i,j;
int buttoni,buttonj,buttonLR,buttonflag;
for(i=0;i<6;i++){
for(j=0;j<6;j++){
if((x>(10+i*(BUTTONX+GAP)))
&&(x<(10+(i+1)*(BUTTONX+GAP)))
&&(y>(140+j*(BUTTONY+GAP)))
&&(y<(140+(j+1)*(BUTTONY+GAP)))){
buttoni = i;
buttonj = j;
buttonflag = 1;
buttonLR = 0;
}
}
}
for(i = 0;i<4;i++){
for(j = 0;j<6;j++){
if((x>(i*(BUTTONX+GAP))+386)&&(x<((i+1)*(BUTTONX+GAP)+386-GAP))&&
(y>(j*(BUTTONY+GAP)+140))&&(y<((j+1)*(BUTTONY+GAP)+140-GAP))){
buttoni = i;
buttonj = j;
buttonflag = 1;
buttonLR = 1;
}
}
}
if(buttonflag==1)return buttonLR;
}
/**************************************************************************************************************************************/
/**********************************************************
* 函数原型: void InitGraph()
* 传入参数: 无
* 返回值: 无
* 函数功能: 图形方式初始化
**********************************************************/
void InitGraph()
{
int driver = VGA, mode = VGAHI;
initgraph(&driver, &mode, "");
}
/**********************************************************
* 函数原型: void welcome()
* 传入参数: 无
* 返回值: 无
* 函数功能: 绘制欢迎界面,显示作者信息、简单说明
**********************************************************/
void welcome()
{
int p,q;
int pp=0,qq=0;
int a=320, b=233;
unsigned long gsize;
setfillstyle(SOLID_FILL, 15); /*绘制光标图案*/
bar(a, b, a+MSIZEX, b+MSIZEY);
gsize= imagesize(a, b, a+MSIZEX, b+MSIZEY); /*保存光标图案*/
imagep=malloc(gsize);
imagep1=malloc(gsize);
getimage(a, b, a+MSIZEX, b+MSIZEY, imagep);
setfillstyle(SOLID_FILL, 0); /*抹去光标移动痕迹*/
bar(a, b, a+MSIZEX, b+MSIZEY);
setbkcolor(0); /*绘制欢迎界面,显示作者信息、说明信息*/
setcolor(4);
settextstyle(0, 0, 4);
outtextxy(200, 30, "Welcome!");
setcolor(15);
settextstyle(1, 0, 3);
outtextxy(110, 130, "It is A Scientific Calculator Made by:");
outtextxy(125, 170, "Benjamin He, Sam Li, Leslie Rong");
outtextxy(203, 210, "@ZheJiang University.");
setcolor(5);
outtextxy(70, 285, "Left Click to Enter the Scientific Calculator.");
outtextxy(80, 320, "Please Input a Right Formula When Using");
outtextxy(115, 355, "Press \"ESC\" to Close the Calculator.");
while(1) /*处理光标移动、点击*/
{
MousePosition(&p,&q); /*显示光标实时位置*/
getimage(p,q,p+MSIZEX,q+MSIZEY,imagep1);
putimage(p,q,imagep,COPY_PUT);
delay(270);
pp=p; qq=q;
putimage(pp, qq, imagep1, COPY_PUT);
if(LeftPress()==1)
{
break;
}
}
}
/**********************************************************
* 函数原型: void UI()
* 传入参数: 无
* 返回值: 无
* 函数功能: 绘制基础用户界面
**********************************************************/
void UI()
{
int i, j;
int fontd= 0;
setbkcolor(0);
setfillstyle(SOLID_FILL, 0); /*填涂背景*/
bar(0, 0, 640, 640);
setfillstyle(SOLID_FILL, 17); /*绘制显示屏上方的蓝线*/
bar(0+BLANK, 0+BLANK, 640-BLANK, 2+BLANK);
setfillstyle(SOLID_FILL, 8); /*绘制显示屏*/
bar(0+BLANK,2+BLANK, 640-BLANK, SCREENWIDTH+BLANK);
j=0; /*绘制第一排亮蓝色按钮*/
setfillstyle(SOLID_FILL, 9);
for(i=0; i<4; i++)
{
if(i==2)
{
setfillstyle(SOLID_FILL, 3);
bar(i*(BUTTONX+GAP)+BLANK, SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK,
(i+1)*(BUTTONX+GAP)+BLANK-GAP, SCREENWIDTH+(j+1)*(BUTTONY+GAP)+GAP+2+BLANK);
setfillstyle(SOLID_FILL, 9);
}
else bar(i*(BUTTONX+GAP)+BLANK, SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK,
(i+1)*(BUTTONX+GAP)+BLANK-GAP, SCREENWIDTH+(j+1)*(BUTTONY+GAP)+GAP+2+BLANK);
}
setfillstyle(SOLID_FILL, 2); /*绘制第一排绿色按钮*/
for(i=4; i<6; i++)
{
if(i==4)
{
setfillstyle(SOLID_FILL, 10);
bar(i*(BUTTONX+GAP)+BLANK, SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK,
(i+1)*(BUTTONX+GAP)+BLANK-GAP, SCREENWIDTH+(j+1)*(BUTTONY+GAP)+GAP+2+BLANK);
setfillstyle(SOLID_FILL, 2);
}
else bar(i*(BUTTONX+GAP)+BLANK, SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK,
(i+1)*(BUTTONX+GAP)+BLANK-GAP, SCREENWIDTH+(j+1)*(BUTTONY+GAP)+GAP+2+BLANK);
}
j=0; /*绘制第一排蓝色按钮*/
setfillstyle(SOLID_FILL, 1);
for(i=0; i<2; i++)
{
bar((6+i)*(BUTTONX+GAP)+GAP+BLANK, SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK,
(7+i)*(BUTTONX+GAP)+BLANK, SCREENWIDTH+(j+1)*(BUTTONY+GAP)+GAP+2+BLANK);
}
setfillstyle(SOLID_FILL, 4); /*绘制第一排红色按钮*/
for(i=2; i<4; i++)
{
bar((6+i)*(BUTTONX+GAP)+GAP+BLANK, SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK,
(7+i)*(BUTTONX+GAP)+BLANK, SCREENWIDTH+(j+1)*(BUTTONY+GAP)+GAP+2+BLANK);
}
setfillstyle(SOLID_FILL, 8); /*绘制二到六排前六列按钮*/
for(i=0; i<6; i++)
{
for(j=1; j<6; j++)
{
bar(i*(BUTTONX+GAP)+BLANK, SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK,
(i+1)*(BUTTONX+GAP)+BLANK-GAP, SCREENWIDTH+(j+1)*(BUTTONY+GAP)+GAP+2+BLANK);
}
}
for(i=0; i<4; i++) /*绘制二到六排后四列按钮*/
{
for(j=1; j<6; j++)
{
bar((6+i)*(BUTTONX+GAP)+GAP+BLANK, SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK,
(7+i)*(BUTTONX+GAP)+BLANK, SCREENWIDTH+(j+1)*(BUTTONY+GAP)+GAP+2+BLANK);
}
}
settextstyle(2, 0, 7); /*绘制按钮文字标识*/
setcolor(15);
for(i=0; i<6; i++) /*绘制左侧按钮文字标识*/
{
for(j=0; j<6; j++)
{
if(strlen(buttonleft[j*6+i])==0) continue; /*设置调整字符位置的参数*/
else if(strlen(buttonleft[j*6+i])==1) fontd= 1;
else if(strlen(buttonleft[j*6+i])==2) fontd= 2;
else if(strlen(buttonleft[j*6+i])==3) fontd= 3;
else if(strlen(buttonleft[j*6+i])==4) fontd= 4;
else if(strlen(buttonleft[j*6+i])==5) fontd= 5;
if(i==1&&j==4) /*调整"|"字符至按钮中间*/
outtextxy(i*(BUTTONX+GAP)+BLANK+BUTTONX/2-fontd*5+6,
SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10-2, buttonleft[j*6+i]);
else if(i==2&&j==4) /*调整"!"字符至按钮中间*/
outtextxy(i*(BUTTONX+GAP)+BLANK+BUTTONX/2-fontd*5+6,
SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10, buttonleft[j*6+i]);
else if(i==5&&j==4) /*调整">>"字符至按钮中间*/
outtextxy(i*(BUTTONX+GAP)+BLANK+BUTTONX/2-fontd*5+3,
SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10, buttonleft[j*6+i]);
else outtextxy(i*(BUTTONX+GAP)+BLANK+BUTTONX/2-fontd*5, /*绘制剩余按钮*/
SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10, buttonleft[j*6+i]);
}/*for(j=0*/
}/*for(i=0*/
line(1*(BUTTONX+GAP)+BLANK+BUTTONX/2-9, SCREENWIDTH+3*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6, /*绘制取整按钮*/
1*(BUTTONX+GAP)+BLANK+BUTTONX/2-9, SCREENWIDTH+3*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6+12);
line(1*(BUTTONX+GAP)+BLANK+BUTTONX/2-9, SCREENWIDTH+3*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6+12,
1*(BUTTONX+GAP)+BLANK+BUTTONX/2-9+4, SCREENWIDTH+3*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6+12);
line(1*(BUTTONX+GAP)+BLANK+BUTTONX/2+9, SCREENWIDTH+3*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6,
1*(BUTTONX+GAP)+BLANK+BUTTONX/2+9, SCREENWIDTH+3*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6+12);
line(1*(BUTTONX+GAP)+BLANK+BUTTONX/2+9, SCREENWIDTH+3*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6+12,
1*(BUTTONX+GAP)+BLANK+BUTTONX/2+9-4, SCREENWIDTH+3*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6+12);
for(i=0; i<4; i++) /*绘制右侧按钮文字标识*/
{
for(j=0; j<6; j++)
{
if(strlen(buttonright[j*4+i])==0) continue;
else if(strlen(buttonright[j*4+i])==1) fontd= 1;
else if(strlen(buttonright[j*4+i])==2) fontd= 2;
else if(strlen(buttonright[j*4+i])==3) fontd= 3;
else if(strlen(buttonright[j*4+i])==4) fontd= 4;
else if(strlen(buttonright[j*4+i])==5) fontd= 5;
outtextxy((6+i)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-fontd*5,
SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10, buttonright[j*4+i]);
}
}
line((6+0)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6, /*绘制左移按钮*/
(6+0)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+6);
line((6+0)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2,
(6+0)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+6);
line((6+0)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2,
(6+0)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6);
line((6+1)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6, /*绘制右移按钮*/
(6+1)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+6);
line((6+1)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2,
(6+1)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+6);
line((6+1)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2,
(6+1)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-8, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-6);
line((6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+17, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10, /*绘制退格按钮*/
(6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+17, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+10);
line((6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-5-15, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2,
(6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+5-15, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+10);
line((6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-5-15, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2,
(6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+5-15, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10);
line((6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+5-15, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+10,
(6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+17, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+10);
line((6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+5-15, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10,
(6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+17, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10);
line((6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-5+2, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-5,
(6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+5+2, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+6);
line((6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2+5+2, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-5,
(6+3)*(BUTTONX+GAP)+GAP+BLANK+BUTTONX/2-5+2, SCREENWIDTH+0*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2+5);
}
/**************************************************************************************************************************************/
int conp=1,conq=4; /*状态按钮(进制、角/弧度制)参数*/
/**********************************************************
* 函数原型: void buttondown(int i, int j,int LR)
* 传入参数: i-该按钮所在列数
j-该按钮所在行数
LR-该按钮左右位置(左侧为0、右侧为1)
* 返回值: 无
* 函数功能: 实现按钮被按下时的图形效果
**********************************************************/
void buttondown(int i, int j,int LR){
int fontd= 0;
int x,y,color;
int flagi,flagj;
int o;
if (LR==0) /*判断按钮左右位置,得出左上角坐标信息*/
{
x = i*(BUTTONX+GAP)+BLANK;
y = j*(BUTTONY+GAP)+140;
}
else if (LR==1)
{
x = i*(BUTTONX+GAP)+386;
y = j*(BUTTONY+GAP)+140;
}
color = 15; /*非状态类按钮处理,绘制按下效果*/
setfillstyle(SOLID_FILL, color);
if((LR==0&&j!=0)||(LR==1))
{
if(((LR==0)&&(i==5)&&(j==3))) /*shift按钮处理,参数值变化*/
{
if(shift==0)
{
shift=1;
}
else if(shift==1)
{
shift=0;
}
}
bar(x,y,x+BUTTONX,y+BUTTONY);
}
else if((LR==0)&&(i<=3)&&(j==0)) /*状态类按钮处理,绘制按下效果*/
{
conp=i;
bar(x,y,x+BUTTONX,y+BUTTONY);
}
else if((LR==0)&&(i>3)&&(i<6)&&(j==0))
{
conq=i;
bar(x,y,x+BUTTONX,y+BUTTONY);
}
flagi=i; /*存储按钮行、列数信息*/
flagj=j;
settextstyle(2, 0, 7); /*绘制按下时按钮文字标识*/
if(!((LR==0)&&(i==5)&&(j==3)))
{
if(shift==0) /*shift钮未被按下的情况*/
{
for(i=0; i<6; i++) /*绘制左侧按钮文字标识*/
{
for(j=0; j<6; j++)
{
if(LR==0&&i==flagi&&j==flagj) setcolor(0); /*绘制按下的钮时调整文字颜色*/
if(strlen(buttonleft[j*6+i])==0) continue;
else if(strlen(buttonleft[j*6+i])==1) fontd= 1;
else if(strlen(buttonleft[j*6+i])==2) fontd= 2;
else if(strlen(buttonleft[j*6+i])==3) fontd= 3;
else if(strlen(buttonleft[j*6+i])==4) fontd= 4;
else if(strlen(buttonleft[j*6+i])==5) fontd= 5;
if(i==1&&j==4) /*调整"|"字符至按钮中间*/
outtextxy(i*(BUTTONX+GAP)+BLANK+BUTTONX/2-fontd*5+6,
SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10-2, buttonleft[j*6+i]);
else if(i==2&&j==4) /*调整"!"字符至按钮中间*/
outtextxy(i*(BUTTONX+GAP)+BLANK+BUTTONX/2-fontd*5+6,
SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10, buttonleft[j*6+i]);
else if(i==5&&j==4) /*调整">>"字符至按钮中间*/
outtextxy(i*(BUTTONX+GAP)+BLANK+BUTTONX/2-fontd*5+3,
SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10, buttonleft[j*6+i]);
else outtextxy(i*(BUTTONX+GAP)+BLANK+BUTTONX/2-fontd*5, /*绘制剩余按钮*/
SCREENWIDTH+j*(BUTTONY+GAP)+2*GAP+2+BLANK+BUTTONY/2-10, buttonleft[j*6+i]);
setcolor(15);
}/*for(j=0*/
}/*for(i=0*/
}/*if(shift==0*/
if(shift==1) /*shift钮已被按下的情况*/
{
for(i=0; i<6; i++)
{
for(j=0; j<6; j++)
{
if(LR==0&&i==flagi&&j==flagj)setcolor(0); /*绘制按下的钮时调整文字颜色*/
if(!((j==2)&&(i<=4)))
{
if(strlen(buttonleft[j*6+i])==0) continue; /*设置调整字符位置的参数*/
else if(strlen(buttonleft[j*6+i])==1) fontd= 1;
else if(strlen(buttonleft[j*6+i])==2) fontd= 2;
else if(strlen(buttonleft[j*6+i])==3) fontd= 3;
else if(strlen(buttonleft[j*6+i])==4) fontd= 4;
else if(strlen(buttonleft[j*6+i])==5) fontd= 5;
}
else if(((j==2)&&(i<=4)))
{
if(strlen(extra[i]==3))fontd = 3;
else if(strlen(extra[i]==4))fontd = 4;
}