-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
219 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+1.73 KB
(100%)
...xcodeproj/project.xcworkspace/xcuserdata/jalor.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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];//返回答案 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters