Skip to content

Commit

Permalink
feat:运算
Browse files Browse the repository at this point in the history
  • Loading branch information
JalorOo committed Nov 20, 2020
1 parent 9cd7cbb commit d222146
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 45 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions ADT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
33083B5125674533007E7518 /* CALCUL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 33083B4F25674533007E7518 /* CALCUL.cpp */; };
33460298255E0B6800CAE464 /* BIGNUM.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 33460296255E0B6800CAE464 /* BIGNUM.cpp */; };
3346CF8D24FDE480005EADE7 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 3346CF8C24FDE480005EADE7 /* main.cpp */; };
338271BC250663B5003A881A /* SORT.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 338271BA250663B5003A881A /* SORT.cpp */; };
Expand All @@ -25,6 +26,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
33083B4F25674533007E7518 /* CALCUL.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CALCUL.cpp; sourceTree = "<group>"; };
33083B5025674533007E7518 /* CALCUL.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = CALCUL.hpp; sourceTree = "<group>"; };
33460296255E0B6800CAE464 /* BIGNUM.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = BIGNUM.cpp; sourceTree = "<group>"; };
33460297255E0B6800CAE464 /* BIGNUM.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = BIGNUM.hpp; sourceTree = "<group>"; };
3346CF8924FDE480005EADE7 /* ADT */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = ADT; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -74,6 +77,8 @@
338271BB250663B5003A881A /* SORT.hpp */,
33460296255E0B6800CAE464 /* BIGNUM.cpp */,
33460297255E0B6800CAE464 /* BIGNUM.hpp */,
33083B4F25674533007E7518 /* CALCUL.cpp */,
33083B5025674533007E7518 /* CALCUL.hpp */,
);
path = ADT;
sourceTree = "<group>";
Expand Down Expand Up @@ -138,6 +143,7 @@
3346CF8D24FDE480005EADE7 /* main.cpp in Sources */,
338271BC250663B5003A881A /* SORT.cpp in Sources */,
33460298255E0B6800CAE464 /* BIGNUM.cpp in Sources */,
33083B5125674533007E7518 /* CALCUL.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "ADT/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "14"
endingLineNumber = "14"
startingLineNumber = "15"
endingLineNumber = "15"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -356,5 +356,37 @@
landmarkType = "9">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "37C32383-845A-4035-9BD4-1826FB6F57CB"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ADT/CALCUL.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "41"
endingLineNumber = "41"
landmarkName = "Calcul::calcul()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "908C9000-5811-4CBA-B2E5-6A06156C8507"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "ADT/CALCUL.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "40"
endingLineNumber = "40"
landmarkName = "Calcul::calcul()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
105 changes: 105 additions & 0 deletions ADT/CALCUL.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// CALCUL.cpp
// ADT
//
// Created by Jalor on 2020/11/20.
// Copyright © 2020 Jalor. All rights reserved.
//

#include "CALCUL.hpp"

int charPor(char &c){
switch (c) {
case '+':
return 1;

case '-':
return 1;

default:
return 2;
}
}

/*
构造函数
保存为后缀表达式
@param s 字符串
@return 无
*/
Calcul::Calcul(string s){
//保存为后缀表达式
str = s;
}


/*
计算函数
@return OPND[opndPtr] 答案
*/
int Calcul::calcul() {
for(int i = 0;i < str.length();i++){
char cur = str[i];
if (cur - '0' > 9 || cur - '0' < 0) {//当前字符为运算符
if (optrPtr >= 0) {//指针已经初始化
char t = OPTR[optrPtr];//栈顶符号
int cmp = charPor(cur) - charPor(t);//比较优先级
int b;
int a;
int ans;
switch (cmp) {
case 0://相等
b = OPND[opndPtr--];
a = OPND[opndPtr--];
ans = a * b;//默认乘法
if(t == '/'){//除法
ans = a / b;
}else if(t == '+'){
ans = a + b;
}else if(t == '-'){
ans = a - b;
}
optrPtr --;
OPTR[++optrPtr] = cur;
OPND[++opndPtr] = ans;
break;

case 1://大于
OPTR[++optrPtr] = cur;
break;

default://小于
b = OPND[opndPtr--];
a = OPND[opndPtr--];
ans = a * b;//默认乘法
if(t == '/'){//除法
ans = a / b;
}
optrPtr --;
OPTR[++optrPtr] = cur;
OPND[++opndPtr] = ans;
break;
}

} else {//若指针没有初始化,则直接接入
OPTR[++optrPtr] = cur;
}

} else {//当前字符为操作数
OPND[++opndPtr] = (cur - '0');
}
}

while (optrPtr >= 0) {//当运算符栈不为空
int b = OPND[opndPtr--];//弹出操作数栈栈顶
int a = OPND[opndPtr--];//弹出操作数栈栈顶
char f = OPTR[optrPtr--];//弹出运算符栈栈顶
int ans = a + b;//运算
if(f == '-'){
ans = a - b;
}
OPND[++opndPtr] = ans;//操作数栈push答案
}

return OPND[opndPtr];//返回答案
}
28 changes: 28 additions & 0 deletions ADT/CALCUL.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// CALCUL.hpp
// ADT
//
// Created by Jalor on 2020/11/20.
// Copyright © 2020 Jalor. All rights reserved.
//

#ifndef CALCUL_hpp
#define CALCUL_hpp
#include <iostream>
using namespace std;

const int MAXN = 5;

class Calcul{
private:
string str;
int OPND[MAXN];//操作数
char OPTR[MAXN];//运算符
int opndPtr = -1;//操作数指针
int optrPtr = -1;//运算符指针
public:
Calcul(string s);
int calcul();//计算
};

#endif /* CALCUL_hpp */
89 changes: 46 additions & 43 deletions ADT/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,53 @@
#include "SEQLIST.hpp"
#include "LINKLIST.h"
#include "BIGNUM.hpp"
#include "CALCUL.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
cout<<"Input func(e.g: 123+456 ):"<<endl;
BigNum num1;
BigNum num2;
string str;
cin>>str;

int idx;
idx = str.find('+');
if(idx > -1){
string s1 = str.substr(0,idx);
string s2 = str.substr(idx+1);
num1 = BigNum(s1);
num2 = BigNum(s2);
cout<<"="<<num1+num2<<endl;
return 0;
}
idx = str.find('-');
if(idx > -1){
string s1 = str.substr(0,idx);
string s2 = str.substr(idx+1);
num1 = BigNum(s1);
num2 = BigNum(s2);
cout<<"="<<num1-num2<<endl;
return 0;
}
idx = str.find('*');
if(idx > -1){
string s1 = str.substr(0,idx);
string s2 = str.substr(idx+1);
num1 = BigNum(s1);
num2 = BigNum(s2);
cout<<"="<<num1*num2<<endl;
return 0;
}
idx = str.find('/');
if(idx > -1){
string s1 = str.substr(0,idx);
string s2 = str.substr(idx+1);
num1 = BigNum(s1);
num2 = BigNum(s2);
cout<<"="<<num1/num2<<endl;
return 0;
}
// cout<<"Input func(e.g: 123+456 ):"<<endl;
// BigNum num1;
// BigNum num2;
// string str;
// cin>>str;
//
// int idx;
// idx = str.find('+');
// if(idx > -1){
// string s1 = str.substr(0,idx);
// string s2 = str.substr(idx+1);
// num1 = BigNum(s1);
// num2 = BigNum(s2);
// cout<<"="<<num1+num2<<endl;
// return 0;
// }
// idx = str.find('-');
// if(idx > -1){
// string s1 = str.substr(0,idx);
// string s2 = str.substr(idx+1);
// num1 = BigNum(s1);
// num2 = BigNum(s2);
// cout<<"="<<num1-num2<<endl;
// return 0;
// }
// idx = str.find('*');
// if(idx > -1){
// string s1 = str.substr(0,idx);
// string s2 = str.substr(idx+1);
// num1 = BigNum(s1);
// num2 = BigNum(s2);
// cout<<"="<<num1*num2<<endl;
// return 0;
// }
// idx = str.find('/');
// if(idx > -1){
// string s1 = str.substr(0,idx);
// string s2 = str.substr(idx+1);
// num1 = BigNum(s1);
// num2 = BigNum(s2);
// cout<<"="<<num1/num2<<endl;
// return 0;
// }
Calcul c = Calcul("9*9*7-1");
cout<<c.calcul()<<endl;
return 0;
}

0 comments on commit d222146

Please sign in to comment.