Skip to content

Commit d439fe2

Browse files
author
冷漠
committed
初步完成数学函数类
1 parent df90f3c commit d439fe2

File tree

12 files changed

+258
-255
lines changed

12 files changed

+258
-255
lines changed

SCMath.pro

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ SOURCES += \
99
nodetype.cpp \
1010
funinterface.cpp \
1111
scope.cpp \
12-
output.cpp \
1312
copynode.cpp \
1413
scmath.cpp
1514

@@ -18,7 +17,6 @@ HEADERS += \
1817
nodetype.h \
1918
scope.h \
2019
funinterface.h \
21-
output.h \
2220
excep.h \
2321
marco.h \
2422
scmath.h \

SCMath.pro.user

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.5.0, 2019-03-31T19:59:50. -->
3+
<!-- Written by QtCreator 4.5.0, 2019-04-05T18:20:40. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -292,7 +292,7 @@
292292
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">SCMath.pro</value>
293293
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix">false</value>
294294
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory"></value>
295-
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default"></value>
295+
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default">F:/mycodes/build-SCMath-Desktop_Qt_5_9_4_MinGW_32bit-Debug</value>
296296
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
297297
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
298298
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>

ast.cpp

Lines changed: 100 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ bool ast::isBinOp(const char &c)
7070
return c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c=='=';
7171
}
7272

73+
bool ast::isBinOp(const string &c)
74+
{
75+
return c == "+" || c == "-" || c == "*" || c == "/" || c == "^" || c=="=";
76+
}
77+
7378
bool ast::isLetter(const char &c)
7479
{
7580
return (c >= 'A' && c <='Z') || (c >= 'a' && c <= 'z');
@@ -81,7 +86,7 @@ bool ast::canpush(stack<string> &stackOp, const string& op)
8186
return BinOpPriority[op] > BinOpPriority[stackOp.top()];
8287
}
8388

84-
BasicNode* ast::__ToAST(string &s, Scope* sc)
89+
BasicNode* ast::__toAST(string &s, Scope* sc)
8590
{
8691
s += LowestPriority;
8792
stack<BasicNode*> stackAST;
@@ -129,7 +134,7 @@ BasicNode* ast::__ToAST(string &s, Scope* sc)
129134
j++;
130135
if(j < n && (s[j] == ','|| s[j] == ')') && countleftParenthesis == 1)
131136
{
132-
node->addNode(__ToAST(temp = s.substr(i + 1, j - i - 1), sc));
137+
node->addNode(__toAST(temp = s.substr(i + 1, j - i - 1), sc));
133138
if(s[j]==')') countleftParenthesis--;
134139
i=j;
135140
}
@@ -173,12 +178,12 @@ BasicNode* ast::__ToAST(string &s, Scope* sc)
173178
}
174179
if(s[j] == ')')
175180
{
176-
stackAST.push(__ToAST(temp = s.substr(i + 1, j - i - 1), sc));//为了让参数1变为左值
181+
stackAST.push(__toAST(temp = s.substr(i + 1, j - i - 1), sc));//为了让参数1变为左值
177182
i = j + 1;//+1是要过掉‘)’
178183
}
179184
else//读到了字符串尾还是没有右括号
180185
{
181-
stackAST.push(__ToAST(temp = s.substr(i + 1, j - i - 2), sc));
186+
stackAST.push(__toAST(temp = s.substr(i + 1, j - i - 2), sc));
182187
i = j - 1;//应该忽略掉字符串结尾的$,便于下面判断是否能进stackOp
183188
}
184189
}
@@ -216,7 +221,7 @@ BasicNode* ast::__ToAST(string &s, Scope* sc)
216221
return stackAST.top();
217222
}
218223

219-
BasicNode* ast::ToAST(string s, Scope* sc){
224+
BasicNode* ast::toAST(string s, Scope* sc){
220225
if(!isInit){
221226
Init();
222227
isInit = true;
@@ -227,5 +232,94 @@ BasicNode* ast::ToAST(string s, Scope* sc){
227232
c--;
228233
}
229234
}
230-
return __ToAST(s, sc);
235+
return __toAST(s, sc);
236+
}
237+
238+
239+
240+
void ast::__output(BasicNode* now, ostream &os, const string& FatherOP)
241+
{
242+
if(now->getType() == Num)
243+
{
244+
double nownum = ((NumNode*)now)->getNum();
245+
if(nownum < 0)
246+
os << '(' << nownum << ')';
247+
else
248+
os << nownum;
249+
return;
250+
}
251+
252+
if(now->getType() == Fun)
253+
{
254+
FunNode* t = (FunNode*)now;
255+
string op = t->getEntity()->NAME;
256+
if(isBinOp(op))
257+
{
258+
if(ast::BinOpPriority[op] < ast::BinOpPriority[FatherOP])
259+
os << '(';
260+
__output(t->sonNode[0], os, op);
261+
os << op;
262+
if(op == "-" && t->sonNode[1]->getType() == Fun && ast::BinOpPriority[op] == ast::BinOpPriority[((FunNode*)(t->sonNode[1]))->getEntity()->NAME])
263+
os << '(';
264+
__output(t->sonNode[1], os, op);
265+
if(ast::BinOpPriority[op] < ast::BinOpPriority[FatherOP])
266+
os << ')';
267+
if(op == "-" && t->sonNode[1]->getType() == Fun && ast::BinOpPriority[op] == ast::BinOpPriority[((FunNode*)(t->sonNode[1]))->getEntity()->NAME])
268+
os << ')';
269+
return;
270+
}
271+
os << op << '(';
272+
int n = t->getEntity()->getParnum();
273+
for(int i = 0 ; i < n; i++)
274+
{
275+
__output(t->sonNode[i], os);
276+
if(i != n - 1)
277+
os << ",";
278+
}
279+
os << ')';
280+
return;
281+
}
282+
283+
if(now->getType() == Var)
284+
{
285+
os << ((VarNode*)now)->NAME;
286+
}
287+
288+
if(now->getType()==Matrix)
289+
((matrixNode*)now)->output(os);
290+
291+
if(now->getType()==Vector)
292+
((vectorNode*)now)->output(os);
293+
294+
if(now->getType()==Null)
295+
return;
296+
}
297+
298+
void ast::output(BasicNode *now, ostream &os)
299+
{
300+
__output(now, os);;
301+
}
302+
303+
void ast::outputASTstruct(BasicNode* now, int depth)//方便输出AST结构(并没有找到别的好方法)
304+
{
305+
for(int i = 0; i < depth; i++)
306+
cout << ' ';
307+
if(now->getType() == Num)
308+
{
309+
cout << ((NumNode*)now)->getNum() << endl;
310+
return;
311+
}
312+
if(now->getType() == Var)
313+
{
314+
cout << ((VarNode*)now)->NAME << endl;
315+
return;
316+
}
317+
if(now->getType() == Fun)
318+
{
319+
FunNode* temp = (FunNode*)now;
320+
int n = temp->getEntity()->getParnum();
321+
cout << temp->getEntity()->NAME << endl;
322+
for(int i = 0; i < n; i++)
323+
outputASTstruct(temp->sonNode[i], depth+1);;
324+
}
231325
}

ast.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#pragma once
22
#include<stack>
3-
#include<string>
4-
#include<cstdlib>
5-
#include<cmath>
3+
#include <string>
4+
#include <cstdlib>
5+
#include <cmath>
6+
#include <iostream>
67
#include "nodetype.h"
78
#include "scope.h"
9+
#include "matrix.hpp"
810
using namespace std;
911

1012
class record
@@ -23,8 +25,12 @@ namespace ast
2325
static bool canpush(stack<string> &, const string &);
2426
static bool isNum(const char &);
2527
static bool isBinOp(const char &);
28+
static bool isBinOp(const string &);
2629
static bool isLetter(const char &);
27-
static BasicNode* __ToAST(string &, Scope*);
30+
static BasicNode* __toAST(string &, Scope*);
31+
static void __output(BasicNode*, ostream &, const string& = string(ast::LowestPriority, 1));
2832

29-
BasicNode* ToAST(string, Scope* = &record::globalScope);
33+
void output(BasicNode*, ostream & = cout);
34+
void outputASTstruct(BasicNode*, int = 0);
35+
BasicNode* toAST(string, Scope* = &record::globalScope);
3036
}

main.cpp

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,23 @@
11
#include <iostream>
22
#include <string>
33
#include "ast.h"
4-
#include "output.h"
54
#include "scmath.h"
65

76
using namespace std;
7+
using namespace ast;
88

99
int main()
1010
{
11-
12-
string TestBuildTree = "1-(2+3)-(4-5)";
13-
BasicNode* ansTestBuildTree = ast::ToAST(TestBuildTree);
14-
BasicNode* ansAfterEval;
15-
output::outputASTstruct(ansTestBuildTree);
16-
output::outputAST(ansTestBuildTree);
17-
cout << endl;
18-
output::outputAST(ansAfterEval = ansTestBuildTree ->eval());
19-
cout << endl;
20-
delete ansAfterEval;
21-
output::outputAST(ansTestBuildTree);
22-
cout << endl << endl;
23-
delete ansTestBuildTree;
24-
25-
string TestSimplificate ="(1+2)*(3+4)+(a+b)/(c+d)";
26-
BasicNode* ansTestSimplificate = ast::ToAST(TestSimplificate);
27-
output::outputAST(ansTestSimplificate);
11+
string testString = "a+b-c*d/e+f^g-2^a";
12+
MathFunc test(testString);
13+
cout << test.diff("a") << endl;
14+
cout << test.diff("c") << endl;
15+
cout << test.diff("e") << endl;
16+
cout << test.diff("f") << endl;
17+
cout << test.diff("g") << endl;
2818
cout << endl;
29-
Simplificate(ansTestSimplificate);
30-
output::outputAST(ansTestSimplificate);
31-
cout << endl << endl;
32-
delete ansTestSimplificate;
3319

34-
string TestDerivation = "a+b-c*d/e+f^g-2^a";
35-
BasicNode* ansTestDerivation = ast::ToAST(TestDerivation);
36-
BasicNode* ansAfterDerivation;
37-
output::outputAST(ansTestDerivation);
38-
cout << endl;
39-
ansAfterDerivation = Derivation(ansTestDerivation, "a");
40-
Simplificate(ansAfterDerivation);
41-
output::outputAST(ansAfterDerivation);
42-
cout << endl;
43-
delete ansAfterDerivation;
44-
ansAfterDerivation = Derivation(ansTestDerivation, "c");
45-
Simplificate(ansAfterDerivation);
46-
output::outputAST(ansAfterDerivation);
47-
cout << endl;
48-
delete ansAfterDerivation;
49-
ansAfterDerivation = Derivation(ansTestDerivation, "e");
50-
Simplificate(ansAfterDerivation);
51-
output::outputAST(ansAfterDerivation);
52-
cout << endl;
53-
delete ansAfterDerivation;
54-
ansAfterDerivation = Derivation(ansTestDerivation, "f");
55-
Simplificate(ansAfterDerivation);
56-
output::outputAST(ansAfterDerivation);
57-
cout << endl;
58-
delete ansAfterDerivation;
59-
ansAfterDerivation = Derivation(ansTestDerivation, "g");
60-
Simplificate(ansAfterDerivation);
61-
output::outputAST(ansAfterDerivation);
62-
cout << endl << endl;
63-
delete ansAfterDerivation;
64-
delete ansTestDerivation;
65-
66-
TestDerivation = "sin(x)*cos(x)";
67-
ansTestDerivation = ast::ToAST(TestDerivation);
68-
output::outputAST(ansTestDerivation);
69-
cout << endl;
70-
ansAfterDerivation = Derivation(ansTestDerivation, "x");
71-
Simplificate(ansAfterDerivation);
72-
output::outputAST(ansAfterDerivation);
73-
cout << endl << endl;
74-
75-
TestDerivation = "ln(cos(x))";
76-
ansTestDerivation = ast::ToAST(TestDerivation);
77-
output::outputAST(ansTestDerivation);
78-
cout << endl;
79-
ansAfterDerivation = Derivation(ansTestDerivation, "x");
80-
Simplificate(ansAfterDerivation);
81-
output::outputAST(ansAfterDerivation);
82-
cout << endl << endl;
20+
test.setVal("a", 1);
21+
cout << test.eval() << endl;
8322
return 0;
8423
}

matrix.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "nodetype.h"
3+
#include<iostream>
34
#include <math.h>
45
using namespace std;
56

@@ -67,13 +68,13 @@ class vectorNode : public BasicNode
6768
}
6869
}
6970

70-
void output()
71+
void output(ostream &os)
7172
{
7273
for (unsigned int i = 0; i < l; i++)
7374
{
74-
printf("%g ", v[i]);
75+
os << v[i];
7576
}
76-
printf("\n");
77+
os << endl;
7778
}
7879

7980
~vectorNode() { delete[]v; }
@@ -307,15 +308,15 @@ class matrixNode : public BasicNode
307308
this->setRVector(vr2,r1);
308309
}
309310

310-
void output()
311+
void output(ostream &os = cout)
311312
{
312313
for (unsigned int i = 0; i < r; i++)
313314
{
314315
for (unsigned int j = 0; j < c; j++)
315316
{
316-
printf("%g ", m[i][j]);
317+
os << m[i][j];
317318
}
318-
printf("\n\t");
319+
os << "\n\t";
319320
}
320321
}
321322

0 commit comments

Comments
 (0)