-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperatorsDefine.h
142 lines (128 loc) · 3.05 KB
/
OperatorsDefine.h
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
#pragma once
#include "Operator.h"
#define ValueType HP_complex
HP_complex _Operator_J("0", "1");
//HP_complex _Operator_ONE("1", "0");
//HP_complex _Operator_ZERO("0", "0");
// 运算符关联函数
ValueType nullOperator(ValueType A, ValueType B) {
return HP_complex();
}
ValueType add(ValueType A, ValueType B) {
return A + B;
}
ValueType sub(ValueType A, ValueType B) {
return A - B;
}
ValueType mul(ValueType A, ValueType B) {
return A * B;
}
ValueType div(ValueType A, ValueType B) {
//HP_complex t = B;
return A / B;
}
ValueType factorial(ValueType A) {
HP_fraction t;
t = A.real().factorial();
HP_complex B(t,HP_fraction("0"));
return B;
}
ValueType _pow(ValueType A, ValueType B) {
// return pow(A, B);
HP_complex ANS;
ANS = A ^ B;
return ANS;
}
ValueType minusSign(ValueType A) {
return HP_complex() - A;
}
ValueType j2imag(ValueType A) {
//HP_complex J("0", "1");
return A * _Operator_J;
}
ValueType is_larger(ValueType A, ValueType B) {
HP_fraction B_f;
B_f = B.real();
if (A.real() > B_f)
return HP_complex("1", "0");
else
return HP_complex("0", "0");
}
ValueType is_notlarger(ValueType A, ValueType B) {
HP_fraction B_f;
B_f = B.real();
if (A.real() > B_f)
return HP_complex("0", "0");
else
return HP_complex("1", "0");
}
ValueType is_smaller(ValueType A, ValueType B) {
HP_fraction B_f;
B_f = B.real();
if (A.real() < B_f)
return HP_complex("1", "0");
else
return HP_complex("0", "0");
}
ValueType is_notsmaller(ValueType A, ValueType B) {
HP_fraction B_f;
B_f = B.real();
if (A.real() < B_f)
return HP_complex("0", "0");
else
return HP_complex("1", "0");
}
ValueType is_equal(ValueType A, ValueType B) {
HP_fraction B_f;
B_f = B.real();
if (A.real() == B_f)
return HP_complex("1", "0");
else
return HP_complex("0", "0");
}
ValueType is_notequal(ValueType A, ValueType B) {
HP_fraction B_f;
B_f = B.real();
if (A.real() == B_f)
return HP_complex("0", "0");
else
return HP_complex("1", "0");
}
ValueType bool_and(ValueType A, ValueType B) {
HP_fraction zero;
zero.in("0");
if (A.real() == zero || B.real() == zero )
return HP_complex("0", "0");
else
return HP_complex("1", "0");
}
ValueType bool_or(ValueType A, ValueType B) {
HP_fraction zero;
zero.in("0");
if (A.real() == zero && B.real() == zero)
return HP_complex("0", "0");
else
return HP_complex("1", "0");
}
// 运算符列表
std::vector<Operator> operatorsInitList = {
Operator("null",0,nullptr,nullptr),
Operator("**",3,nullptr,_pow), // 乘方(借鉴python命名方式)
Operator("!",3,factorial,nullptr),
Operator("+",1,nullptr,add), // 加
Operator("-",1,minusSign,sub), // 减(双目)和负号(特殊单目)
Operator("*",2,nullptr,mul), // 乘
Operator("/",2,nullptr,div), // 除
Operator("j",3,j2imag,nullptr),
//*
Operator(">",0,nullptr,is_larger),
Operator("==",0,nullptr,is_equal),
Operator("<",0,nullptr,is_smaller),
Operator("!=",0,nullptr,is_notequal),
Operator("<",0,nullptr,is_smaller),
Operator(">=",0,nullptr,is_notsmaller),
Operator("<=",0,nullptr,is_notlarger),
Operator("||",0,nullptr,bool_or),
Operator("&&",0,nullptr,bool_and),
//*/
};