目前正在学习数据结构,根据书本上的思路自己捣鼓了一个表达式计算的小程序,目前支持负数、小数运算和报错。
下面贴上代码,代码写的很烂(耦合度比较高,比较繁琐),大家如果有什么更好的建议可以提一下!
PS:第一次写博客,诚惶诚恐
#include
#include
#include //cin>>string
#include //strcpy_s()
#include //pow()
using namespace std;
char ops[] = { '+','-','/','*','(',')','#','.','0','1','2','3','4' ,'5','6','7','8','9' };//符号集
int icp[] = { 2,2,4,4,7,1,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };//进栈优先级
int isp[] = { 3,3,5,5,1,7,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };//栈内优先级
class Calculator {
public:
Calculator();
~Calculator();
void Run();
private:
char* cOrin;//中缀表达式数组
char** cResu;//后缀表达式
void Initialize();//初始化
bool ErrCheck(string expr);//检错
int icpF(char c);//栈外优先级搜索
int ispF(char c);//栈内优先级搜索
void InToPost();//中缀转后缀
double getOperand(int pos);//获取数字
double doOperation(char Operator, double op1, double op2);//计算
};
Calculator::Calculator() {
cOrin = NULL;
cResu = NULL;
}
Calculator::~Calculator() {
delete[] cOrin;
int size = sizeof(*this->cResu) / sizeof(char*);
for (int i = 0; i < size; i++) {
delete[] this->cResu[i];
}
delete[] this->cResu;
}
void Calculator::Initialize() {
string cinOrder = "";//中缀表达式
int size = 0;//表达式长度
while (true) {
cout << "请输入表达式:";
cin >> cinOrder;
size = cinOrder.size();
if (this->ErrCheck(cinOrder) == true) {
goto loop;
}
else {
break;
}
loop: {
cinOrder = "";
size=0;
continue;
}
}
this->cOrin = new char[size + 2];
this->cResu = new char*[size + 1];
strcpy_s(this->cOrin, size+1, cinOrder.c_str());
this->cOrin[size] = '#';//标识表达式结束
this->cOrin[size + 1] = '\0';//标识数组结束
}
bool Calculator::ErrCheck(string expr){
int rBrace = 0;//右括号计数
int lBrace = 0;//左括号计数
bool isInvaild = false;//非法输入
bool isDefalut = false;//括号不匹配
for (int count = 0; count < expr.size(); count++) {
if (expr[count] == '(') {
lBrace++;
}
else if (expr[count] == ')') {
rBrace++;
}
if (this->icpF(expr[count]) == -2) {
isInvaild = true;
}
}
if (lBrace != rBrace) {
isDefalut = true;
}
if (isInvaild == true) {
cout << "表达式含有非法字符,请检查!\n";
}
if (isDefalut == true) {
cout << "表达式左右括号不匹配,请检查!\n";
}
if (isInvaild == true || isDefalut == true) {
return true;
}
else {
return false;
}
}
void Calculator::Run() {
int count = 0;
int search;
double op1, op2, number;
this->Initialize();
this->InToPost();
stack<double> s;
while (this->cResu[count][0] != '#') {
search = this->icpF(this->cResu[count][0]);
if (search == -1) {
number = this->getOperand(count);
s.push(number);
}
else {
op2 = s.top();//后进先出
s.pop();
op1 = s.top();
s.pop();
number=this->doOperation(this->cResu[count][0], op1, op2);
s.push(number);
}
count++;
}
number = s.top();
s.pop();
cout << "结果为:" << number << endl;
}
double Calculator::doOperation(char Operator, double op1, double op2) {
double number = 0;
switch (Operator) {
case '+':
number = op1 + op2;
break;
case '-':
number = op1 - op2;
break;
case '*':
number = op1*op2;
break;
case '/':
number = op1 / op2;
break;
}
return number;
}
double Calculator::getOperand(int pos) {
double total = 0.0;//实际数字
bool isDec = false;//判断是否小数
int intCount = 0;//整数计数
int count = 0;//数组遍历计数
int numBit = 0;//每位上的数字
int i = 0;//小数次方计数
while (this->cResu[pos][count] != '+'
&& this->cResu[pos][count] != '-') {
if (this->cResu[pos][count] == '.') {
isDec = true;
break;
}
else {
intCount++;
count++;
}
}
while (this->cResu[pos][count] != '+'
&& this->cResu[pos][count] != '-') {//当遇到小数时,将计数移到符号位上
count++;
}
if (this->cResu[pos][count] == '+') {//正数
count = 0;
if (isDec == true) {//是小数
while (this->cResu[pos][count] != '.') {
numBit = this->cResu[pos][count++] - '0';
total += numBit*pow(10, --intCount);
}
count++;//跳过小数点
while (this->cResu[pos][count] != '+') {
numBit = this->cResu[pos][count++] - '0';
total += numBit*pow(10, --i);
}
}
else {//不是小数
while (this->cResu[pos][count] != '+') {
numBit = this->cResu[pos][count++] - '0';
total += numBit*pow(10, --intCount);
}
}
}
else {//负数
count = 0;
if (isDec == true) {//是小数
while (this->cResu[pos][count] != '.') {
numBit = this->cResu[pos][count++] - '0';
total += numBit*pow(10, --intCount);
}
count++;//跳过小数点
while (this->cResu[pos][count] != '-') {
numBit = this->cResu[pos][count++] - '0';
total += numBit*pow(10, --i);
}
total = -total;
}
else {//不是小数
while (this->cResu[pos][count] != '-') {
numBit = this->cResu[pos][count++] - '0';
total += numBit*pow(10, --intCount);
}
total = -total;
}
}
return total;
}
int Calculator::icpF(char c) {
int j = -2;
for (int i = 0; i < 18; i++) {
if (ops[i] == c) {
j = icp[i];
}
}
return j;
}
int Calculator::ispF(char c) {
int j = -2;
for (int i = 0; i < 18; i++) {
if (ops[i] == c) {
j = isp[i];
}
}
return j;
}
void Calculator::InToPost() {
bool isDec = false;//判断是否为小数
bool isNeg = false;//判断是否为负数
int cInCount = 0;//中缀表达式计数
int cPostCount = 0;//后缀表达式计数
int nCount = 0;//数字位数计数
char c = '\0';
int i;//优先级
int temp;//重置中缀表达式计数
stack<char> s;
s.push('#');//确保扫描到的第一个运算符进栈
while (true) {
c = this->cOrin[cInCount];//扫描中缀表达式
i = this->icpF(c);
if (i == -1) {//没扫到运算符
temp = cInCount;
while ((i = this->icpF(this->cOrin[cInCount])) == -1) {//确定数字位数
if (this->cOrin[cInCount] == '.') {//遇到小数点
isDec = true;
cInCount++;
}
else {
nCount++;
cInCount++;
}
}
if (isNeg == false) {//如果不是负数
if (isDec == true) {
this->cResu[cPostCount] = new char[nCount + 3];//存放小数点、正号和'\0'
for (int j = 0; j < nCount + 1; j++, temp++) {//输出数字
this->cResu[cPostCount][j] = this->cOrin[temp];
}
this->cResu[cPostCount][nCount + 1] = '+';
this->cResu[cPostCount][nCount + 2] = '\0';
isDec = false;//重置小数布尔值
}
else {
this->cResu[cPostCount] = new char[nCount + 2];//存放正号和'\0'
for (int j = 0; j < nCount; j++, temp++) {//输出数字
this->cResu[cPostCount][j] = this->cOrin[temp];
}
this->cResu[cPostCount][nCount] = '+';
this->cResu[cPostCount][nCount + 1] = '\0';
}
}
else {//如果是负数
if (isDec == true) {
this->cResu[cPostCount] = new char[nCount + 3];//存放小数点、负号和'\0'
for (int j = 0; j < nCount + 1; j++, temp++) {//输出数字
this->cResu[cPostCount][j] = this->cOrin[temp];
}
this->cResu[cPostCount][nCount + 1] = '-';
this->cResu[cPostCount][nCount + 2] = '\0';
cPostCount++;
isDec = false;//重置小数布尔值
}
else {
this->cResu[cPostCount] = new char[nCount + 2];//存放负号和'\0'
for (int j = 0; j < nCount; j++, temp++) {//输出数字
this->cResu[cPostCount][j] = this->cOrin[temp];
}
this->cResu[cPostCount][nCount] = '-';
this->cResu[cPostCount][nCount + 1] = '\0';
}
isNeg = false;//重置负数布尔值
}
cPostCount++;
nCount = 0;
}
else {//扫描到运算符
if (c == ')') {
while (s.top() != '(') {//连续输出直到遇到'('
this->cResu[cPostCount] = new char[1];
this->cResu[cPostCount][0] = s.top();
s.pop();
cPostCount++;
}
s.pop();//'('直接出栈不输出
cInCount++;
}
else if (this->cOrin[cInCount] == '-'
&& cInCount == 0) {//表达式例子:-4+5#
isNeg = true;
cInCount++;//跳过负号
continue;
}
else if (this->cOrin[cInCount] == '-'
&& cInCount > 0
&& this->cOrin[cInCount - 1] != ')'
&& this->icpF(this->cOrin[cInCount - 1]) != -1) {//表达式例子:((-4+5)+1)*3#
isNeg = true;
cInCount++;//跳过负号
continue;
}
else if (i <= ispF(s.top())) {//进栈优先级小于栈内优先级,一直输出
while (i <= this->ispF(s.top())) {
this->cResu[cPostCount] = new char[1];
this->cResu[cPostCount][0] = s.top();
s.pop();
if (s.empty() == true) {
goto exit;//当遇到中缀表达式的#时,一直输出到栈空然后退出函数
}
cPostCount++;
}
}
else {//进栈优先级大于栈内优先级,入栈
s.push(c);
cInCount++;
}
}
}
exit:
return;
}
int main() {
Calculator c1;
c1.Run();
system("pause");
return 0;
}