使用栈实现计算器,最主要的就是进行运算符的优先级判断,然后进行对应的计算,然后将其压入栈和从栈中取出来。
其中要对输入的表达式进行判断,如果表达式不符合的话就直接不用计算。
下面直接上代码
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
const int MaxSize = 20;
const char A[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','0'},
{'>','>','>','>','0','>','>'},
{'<','<','<','<','<','0','='}};
const char B[7]={'+','-','*','/','(',')','='};
typedef struct stackI{
float List[MaxSize];
int top;
int base;
}StackI;
typedef struct stackC{
char List[MaxSize];
int top;
int base;
}StackC;
int search(char c)
{ int i=0;
while(c!=B[i]) {i++;}
return(i);
}
char Precede(char c1,char c2)
{
int i,j;
i=search(c1);
j=search(c2);
return(A[i][j]);
}
//初始化
void initStacki(StackI *st){
st->top = st->base=0;
}
void initStackc(StackC *st){
st->top = st->base=0;
st->List[st->top] = '=';
st->top++;
}
//判断栈是否为空
int stackempty(StackC sc){
if(sc.top==sc.base){
return 1;
}
else{
return 0;
}
}
//得到栈顶的数据
char getTopC(StackC st){
if (st.top==st.base)
return('!');
else
return(st.List[st.top-1]);
}
int getTopI(StackI st){
if (st.top==st.base)
return('!');
else
return(st.List[st.top-1]);
}
//往栈里面增加数据
void pushC(StackC *st,char c){
st->List[st->top] = c;
st->top++;
}
void PushI(StackI *st,float n){
st->List[st->top] = n;
st->top++;
}
//数据出栈
char popC(StackC *st){
if (st->top==st->base)
return('!');
else { st->top--;
return(st->List[st->top]);
}
}
float popI(StackI *st){
if (st->top==st->base)
return('!');
else { st->top--;
return(st->List[st->top]);
}
}
//判断输入的是数字还是运算符
bool isDigti(char a){
if('0'<= a && a<='9'){
return 1;
} else{
return 0;
}
}
bool isC(char d){
if(('a'<=d && d<='z')||('A'<=d && d<='Z')){
return 1;
}else{
return 0;
}
}
//计算
float Operate(float i, char theta, float j)
{ float result;
switch(theta)
{ case '+': result = i + j; break;
case '-': result = i - j; break;
case '*': result = i * j; break;
case '/': result = i / j; break;
}
return result;
}
int main()
{
int i=0,len=0,j,nofpoint,g=0;//len表示输入式子的长度。 g表示读入的字符是否是字母变量、数字以及运算符。
float a,b,result;//a、b用来存储操作数栈中弹出的操作数,便于代入函数中进行计算。
StackC sC;
StackI sI;
char lines[MaxSize];
char operates;
char temps[20];
printf("输入想要计算的表达式\n");
scanf("%s",&lines);
while(lines[len++]!='\0');
len--;
//printf("%d\n",len);
//300*50+(20+50)*3.14
initStackc(&sC);
initStacki(&sI);
while(len>0){
g=0;
if('0'<=lines[i] && lines[i]<='9')//若读入的字符为数字,则继续判断下一个字符,直到下一个字符不是数字或者不是小数点,即可保证该操作数是完整的小数,然后将该数入操作数栈。
{
j=0; g=1;
nofpoint = 0;//记录所存的数中小数点的个数
while(('0'<=lines[i] && lines[i]<='9') || lines[i]=='.')
{
if(lines[i]=='.'){
nofpoint++;
}
temps[j]=lines[i];
i++;
j++;
if(i>=len) break;
}
if( nofpoint>1 || (i<len&&(lines[i]!='-' && lines[i]!='+' && lines[i]!='*' && lines[i]!='/' && lines[i]!=')')) )
{ printf("表达式有错\n");
printf("%d\n",nofpoint);
printf("有错");
return 0; }//表达式中不止一个小数点还有数字后面跟的不是运算符则表达式错误
temps[j]='\0';
b = atof(temps);
// printf("%f\n",b);
PushI(&sI,b);
if(i>=len)
break;
}else{
if(lines[i]=='-' || lines[i]=='+' || lines[i]=='*' || lines[i]=='/'|| lines[i]=='(' || lines[i]==')' ) //若读入的字符为运算符的情况
{
g=1;
if(lines[i]=='(' && i==len)
{
printf("表达式有错\n");
return 0;
}// “(”放表达式最后面,错误
if(lines[i]=='-' || lines[i]=='+' || lines[i]=='*' || lines[i]=='/')
{
if(i==len) { printf("表达式有错\n"); return 0; }//“+、-、*、/、^”放在表达式最后面,错误
if( (!isDigti(lines[i+1])) && (!isC(lines[i+1])) && lines[i+1]!='(')//“+、-、*、/、^”后面跟的不是数字或者变量,错误
{
printf("表达式有错\n");
return 0;
}
}
if(lines[i]=='-' && (i==0 || lines[i-1]=='(' ))//运算符是负号
{
PushI(&sI,0);
pushC(&sC,lines[i]);
i++;
}else{
if(Precede(getTopC(sC),lines[i])=='<'){
// printf("压入运算符\n");
pushC(&sC,lines[i]);
// printf("push %c\n",lines[i]);
i++;
}
if(Precede(getTopC(sC),lines[i])=='='){
//printf(" 脱括号%c\n",lines[i]);
i++;
sC.top--;
}
else{
if(Precede(getTopC(sC),lines[i])=='>'){
a = popI(&sI);
b = popI(&sI);
//printf("开始计算\n");
operates = popC(&sC);
result = Operate(a,operates,b);
//printf("result is %f\n",result);
PushI(&sI,result);
}
}
if(i>=len)
break;
}
}
else
{
if(isC(lines[i]))//读入的字符是字母变量的情况
{
g=1;
printf("请输入变量");
while( isC(lines[i])) { printf("%c",lines[i]); i++; }
printf("的值\n");
scanf("%f",&b);
PushI(&sI,b);
if(i>=len) break;
if(lines[i]!='-' && lines[i]!='+' && lines[i]!='*' && lines[i]!='/' && lines[i]!=')')//变量后面跟的不是“+、-、*、/、^、)”,则为错误
{ printf("表达式有错\n"); return 0; }
}
}
}
if(g==0)
{ printf("表达式有错\n"); return 0; }//g=0表示该字符是不符合要求的字符
}
while(getTopC(sC)!='='){
a= popI(&sI);
b=popI(&sI);
operates = popC(&sC);
//printf("%c\n",operates);
if(operates=='(' || operates==')') //括号多余的情况
{ printf("表达式有错\n");
return 0; }
result = Operate(a,operates,b);
//printf("result is%f\n",result);
PushI(&sI,result);
}
printf("%.2f\n",result);
return 0;
}
里面有注释,如果有错误的地方欢迎指出,有不清楚的欢迎留言交流