c++实现了一个计算器,中缀转后缀,但如果计算到1/33时就会出错,它会先计算33
.h文件
#ifndef CALCULATOR_H
#define CALCULATOR_H
enum MAXSIZE
{
STACK_SIZE = 30,//初始容量
STACK_ADD = 10,//每次增加10个
BUFF = 10,
MAX_LEN = 100//设定最长为100
};
template<typename Type_init>
class Calculator
{
public:
struct sqStack
{
Type_init* base;//指向栈顶
Type_init* top;
int stackSize;//当前栈的最大容量
};
Calculator();
~Calculator();
void Push(Type_init e);
bool Pop(Type_init& e);
void clearStack();
int StackLen();
double Calculation(char Postfix[]);//后缀表达式的计算
bool Change(char Infix[], char Postfix[]);//变为后缀表达式
private:
sqStack s;
};
#endif // CALCULATOR_H
cpp文件:
#include<iostream>
#include "CALCULATOR.h"
#include <cstring>
#include <stdio.h>
using namespace std;
template<typename Type_init>
Calculator<Type_init>::Calculator()
{
s.base = new Type_init[STACK_SIZE];
if (s.base == NULL)
exit(0);
s.top = s.base;
s.stackSize = STACK_SIZE;
}
//销毁
template<typename Type_init>
Calculator<Type_init>::~Calculator()
{
delete[]s.base;
}
template<typename Type_init>
void Calculator<Type_init>::Push(Type_init e)
{
if (s.top - s.base >= s.stackSize)
{
s.base = (Type_init*)realloc(s.base, (s.stackSize + STACK_ADD) * sizeof(Type_init));
//
if (s.base == NULL)
exit(0);
s.top = s.base + s.stackSize;//重新设置栈顶
s.stackSize = s.stackSize + STACK_ADD;//当前栈的最大容量
}
*(s.top) = e;
s.top++;
}
template<typename Type_init>
bool Calculator<Type_init>::Pop(Type_init& e)
{
if (s.top == s.base)
return false;//空栈
e = *(--(s.top));
return true;
}
//清空栈
template<typename Type_init>
void Calculator<Type_init>::clearStack()
{
s.top = s.base;
}
//计算栈的当前容量
template<typename Type_init>
int Calculator<Type_init>::StackLen()
{
return s.top - s.base;
}
template<typename Type_init>
double Calculator<Type_init>::Calculation(char Postfix[]) //表达式计算
{
int i = 0, j;
char c;
char str[BUFF];
double a = 0, b = 0;
for (j = 0; Postfix[j] != '\0'; j++)
{
while ((Postfix[j] >= 48) && (Postfix[j] <= 57) || Postfix[j] == '.') //输入的是数字
{
str[i] = Postfix[j];
i++;
str[i] = '\0';
if (i >= 10)
{
printf("出错,输入的数据长度过大!\n");
return -1;
}
j++;
if ((Postfix[j] == ' '))
{
a = atof(str); //参数 str 所指向的字符串转换为一个浮点数
Push(a);//入栈
i = 0;
}
}
switch (Postfix[j])
{
case '+':
Pop(a);
if (!Pop(b))//防止这是符号位
{
Push(a);
break;
}
printf("%f+%f=%f\n", b, a, b + a);
//Pop(b);
Push(b + a);
break;
case '-':
Pop(a);
if (!Pop(b))//
{
Push(-a);
break;
}
printf("%f-%f=%f\n",b,a,b-a);
Push(b - a);
break;
case '*':
Pop(a);
Pop(b);
printf("%f*%f=%f\n",b,a,b*a);
Push(b * a);
break;
case '/':
Pop(a);
if (a == 0)
{
printf("除数不能为零 !\n");
return -1;
}
Pop(b);
printf("%f/%f=%f\n",b,a,b/a);
Push(b / a);
break;
default:
break;
}
}
Pop(a);
return a;
}
template<typename Type_init>
bool Calculator<Type_init>::Change(char Infix[], char Postfix[])
{
Calculator<char> s;
int i = 0, j = 0;
char e;
printf("中缀表达式为:");
while (Infix[j] != '\0')
{
while (Infix[j] >= '0' && Infix[j] <= '9')
{
printf("%c", Infix[j]);
Postfix[i++] = Infix[j++];
if (Infix[j] < '0' || Infix[j]>'9')
{
Postfix[i++] = ' ';
printf(" ");
}
}
switch (Infix[j])
{
case ')':
s.Pop(e);
while ('(' != e)
{
printf("%c ", e);
Postfix[i++] = e;
Postfix[i++] = ' ';
s.Pop(e);
}
break;
case '+':
case '-':
if (0 == s.StackLen())
s.Push(Infix[j]);
else
{
do
{
s.Pop(e);
if ('(' == e)
{
s.Push(e);
}
else
{
printf("%c ", e);
Postfix[i++] = e;
Postfix[i++] = ' ';
}
} while (s.StackLen() && '(' != e);
s.Push(Infix[j]);
}
break;
case '*':
case '/':
case '(':
s.Push(Infix[j]);
break;
case '\0':
break;
default:
printf("\n输入格式错误!\n");
return -1;
}
if ('\0' == Infix[j])
break;
j++;
}
while (s.StackLen())
{
s.Pop(e);
printf("%c ", e);
Postfix[i++] = e;
Postfix[i++] = ' ';
}
Postfix[i] = '\0';
printf("\n");
return true;
}
int main()
{
Calculator<double> cal;
char Infix[MAX_LEN], Postfix[MAX_LEN];
gets_s(Infix);
double sum;
cal.Change(Infix, Postfix);
sum = cal.Calculation(Postfix);
printf("最终计算结果为:%f\n\n", sum);
return 0;
}
最后结果