-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
杨震宇-简单虚拟机
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.16) | ||
project(YangZhenyv) | ||
|
||
set(CMAKE_CXX_STANDARD 14) | ||
|
||
add_executable(YangZhenyv main.cpp DataProcess.cpp DataProcess.h VirtualMachine.cpp VirtualMachine.h) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <climits> | ||
#include <stack> | ||
#include "DataProcess.h" | ||
|
||
// 栈实现四则运算 https://www.cnblogs.com/lcplcpjava/p/6576935.html | ||
|
||
int DataProcess::op_level(char x){ | ||
if(x == '*' || x == '/' || x == '%') return 2; | ||
if(x == '+' || x == '-') return 1; | ||
if(x == '(') return 0; | ||
else return -1; | ||
} | ||
|
||
std::string DataProcess::inf_to_pre(std::string old_expression){ | ||
// 每个数字,字符之间都用' '隔开 | ||
std::stack <char> S1, S2; //运算符栈和答案栈 | ||
std::string ret; | ||
for (int i = (int)old_expression.size()-1 ; i >= 0; i--){ | ||
if (old_expression.at(i) == ' ' || old_expression.at(i) == '\t') | ||
continue; | ||
// 运算式开头的负数处理 | ||
if (i == 0 && old_expression.at(i) == '-'){ | ||
S2.pop(); | ||
S2.push('-'); S2.push(' '); | ||
continue; | ||
} | ||
// 是数字,直接入栈 | ||
if(isdigit(old_expression.at(i))){ | ||
S2.push(old_expression.at(i)); | ||
if (i == 0 || (i > 0 && !isdigit(old_expression.at(i-1)))){ | ||
// 到了数字的开头 | ||
S2.push(' '); | ||
} | ||
} | ||
else if ((old_expression.at(i)) == ')') | ||
S1.push(')'); | ||
// 运算式中间的负数处理 负号前必有'(' | ||
else if (i > 0 && old_expression.at(i-1) == '(' && old_expression.at(i) == '-'){ | ||
S2.pop(); | ||
S2.push('-'); S2.push(' '); | ||
} | ||
// 是')', 弹出S1直到遇到'(' | ||
else if ((old_expression.at(i)) == '('){ | ||
while(S1.top() != ')'){ | ||
S2.push(S1.top()); S2.push(' '); | ||
S1.pop(); | ||
} | ||
S1.pop(); | ||
} | ||
else { | ||
// 若当前比S1栈顶运算符'优先级'低,则将S1栈顶的运算符弹出并压入到S2中 | ||
while (!S1.empty() && op_level(old_expression.at(i)) < op_level(S1.top())){ | ||
S2.push(S1.top()); S2.push(' '); | ||
S1.pop(); | ||
} | ||
S1.push(old_expression.at(i)); | ||
} | ||
} | ||
// 将S1的运算符依次弹出压入S2 | ||
while (!S1.empty()){ | ||
S2.push(S1.top()); S2.push(' '); | ||
S1.pop(); | ||
} | ||
// 将S2的数据弹出赋值给ret变量 | ||
while(!S2.empty()){ | ||
ret += S2.top(); | ||
S2.pop(); | ||
} | ||
return ret; | ||
} | ||
|
||
int DataProcess::str_to_int(const std::string& str){ | ||
int num = 0, flag = 1; | ||
if (str.at(0) == '-') flag = -1; | ||
for(char i : str){ | ||
if(i == ' ' || i == '-') continue; | ||
num = num*10+i-'0'; | ||
} | ||
return num*flag; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef YANGZHENYV_DATAPROCESS_H | ||
#define YANGZHENYV_DATAPROCESS_H | ||
|
||
|
||
#include <string> | ||
|
||
/*该类用于存放一些数据处理的函数,包含将表达式从中缀转换为前缀,将数字从str转换成int*/ | ||
|
||
class DataProcess { | ||
protected: | ||
// 获取运算符优先级函数 | ||
static int op_level(char x); | ||
|
||
public: | ||
// 中缀式表达转换为前缀式表达函数 | ||
static std::string inf_to_pre(std::string old_expression); | ||
// 数字从str转换为int函数 | ||
static int str_to_int(const std::string& str); | ||
}; | ||
|
||
|
||
|
||
|
||
#endif //YANGZHENYV_DATAPROCESS_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
##### 简单的虚拟机 | ||
|
||
###### 使用语言: C++ | ||
|
||
###### 实现功能: | ||
|
||
1. 列式计算并查看所用指令(将表达式从<u>中缀</u>转换成<u>前缀</u> ) | ||
|
||
2. 使用指令对数据栈进行操作。 | ||
|
||
实现的指令有: | ||
|
||
1. EMPTY : 判断RAM栈是否为空,如果空会输出"RAM栈为空"; 非空则输出"RAM栈为非空" | ||
2. SIZE : 输出RAM栈的大小(栈空则输出0) | ||
3. PUSH : 输入"PUSH x"向栈中输入数值 x (分隔符可为空格或制表符) | ||
4. TOP : 输出栈顶值的大小; 如果RAM栈为空,则输出"RAM的大小为0" | ||
5. POP : 输出并弹出RAM栈顶的值; 如果RAM栈为空,则输出"RAM的大小为0" | ||
6. ADD : 输入"ADD"则弹出两个值相加并入栈;如果输入"ADD x y"则将x, y的值相加并入栈 | ||
7. SUB : 输入"SUB"----------相减------------"SUB x y"-----------相减并入栈 | ||
8. MUL : 输入"MUL"----------相乘------------"MUL x y"-----------相乘并入栈 | ||
9. DIV : 输入"DIV"----------相除------------"DIV x y"-----------相除并入栈 | ||
10. MOD : 输入"MOD"----------取余------------"MOD x y"-----------取余并入栈 |