问题描述
- 数据结构题目,四则运算
-
用户从键盘输入一个计算表达式,用栈实现四则运算。表达式中含有数字,加减乘除,小括号。输出表达式的结果。用c语言实现
解决方案
四则运算 (数据结构)
数据结构:四则运算
一些数据结构题目的代码片段
解决方案二:
一年前我自己写的,稍微改改就能用
/***********************************
Coded by LC 2014/11/8 20:00
- This is our last edition,and we *
*changed some details from the book
Later we studied some knowledge of
STL template,and the effect is * *
very good. * * * * * * * * * * * *
**********************************/
#include"iostream"
#include"cstdlib"
#include"cmath"
#include"string"
#include"vector"
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define LENTH " "
template
class stack
{
private:
T base;
T *top;
int stack_size;
public:
stack(void)
{
this->base = new T[STACK_INIT_SIZE];
if (!this->base)
puts("init_stack error_1"),exit(0);
this->top = this->base;
this->stack_size = STACK_INIT_SIZE;
}
~stack(void)
{}
T get_top(void)
{
if (this->top == this->base)
puts("get_top error"), exit(0);
return *(this->top - 1);
}
void push(T e)
{
if (this->top - this->base >= this->stack_size)
{
this->base = (T)realloc(this->base, (this->stack_size + STACKINCREMENT)*sizeof(T));
if(!this->base)
puts("push error"), exit(0);
this->top=this->base+this->stack_size;
this->stack_size+=STACKINCREMENT;
}
*(this->top++)=e;
}
T pop(void)
{
if(this->top==this->base)
puts("pop error"), exit(0);
return *(--(this->top));
}
};
void suffixion(string ,string &);
double calculate(string &);
char judge(char ,char );
bool is_binary_operator(char);
double calculate(string &);
double compute(double ,char ,double );
double compute(double ,char );
int main()
{
string infix;
string suffix=LENTH;
cout<<"Please input your infix expression(End with '='):"<
cin>>infix;
suffixion(infix,suffix);
cout<<"The suffix expression is:"<<suffix<<endl;
cout<<"The result is:"<<calculate(suffix)<<endl;
system("pause");
return 0;
}
void suffixion(string infix,string & suffix)
{
stack char_stack;
char_stack.push('=');
int j=0;
for(int i=0;infix[i]!='=';i++)
{
if(infix[i]>='0'&&infix[i]<='9')
{
suffix[j]=' ';
j++;
while(infix[i]>='0'&&infix[i]<='9'||infix[i]=='.')
{
suffix[j]=infix[i];
j++;
if(infix[i+1]>='0'&&infix[i+1]<='9'||infix[i+1]=='.')
i++;
else
break;
}
suffix[j]=' ';
j++;
}
else if(infix[i]=='(')
{
char_stack.push('(');
}
else if(infix[i]==')')
{
while(char_stack.get_top()!='(')
{
suffix[j]=char_stack.pop();
j++;
}
char_stack.pop();
}
else if(infix[i]>='a'&&infix[i]<='z')
{
int flag;
switch(infix[i])
{
case 'c':case's':
{
//char_stack.push(infix[i]);
flag=2;
}break;
case 'l':
{
if(infix[i+1]=='n')
{
// char_stack.push('n');
i+=1;
flag=0;
}
else if(infix[i+1]=='o')
{
// char_stack.push('l');
flag=2;
}
}break;
default: break;
}
switch(judge(char_stack.get_top(),infix[i]))
{
case '>':
{
while(judge(char_stack.get_top(),infix[i])=='>')
{
suffix[j]=char_stack.pop();
j++;
}
char_stack.push(infix[i]);
}
break;
case '=':
{
char_stack.pop();
}
break;
case '<':
{
char_stack.push(infix[i]);
}
break;
default: break;
}
i+=flag;
}
else if(is_binary_operator(infix[i]))
{
switch(judge(char_stack.get_top(),infix[i]))
{
case '>':
{
while(judge(char_stack.get_top(),infix[i])=='>')
{
suffix[j]=char_stack.pop();
j++;
}
char_stack.push(infix[i]);
}
break;
case '=':
{
char_stack.pop();
}
break;
case '<':
{
//cout<<endl<<"push_isok=1"<<endl;
char_stack.push(infix[i]);
}
break;
default: break;
}
}
}
while(char_stack.get_top()!='=')//last , infix[i]=''
{
suffix[j]=char_stack.pop();
j++;
}
char_stack.pop();
suffix[j]='';
}
double calculate(string & suffix)
{
int i=0;
double a,b,n,k=0.1;
int dot_flag=0,push_flag=0;
stack num_stack;
for(int i=0;suffix[i]!='';i++)
{
n=0;
push_flag=0;
dot_flag=0;
k=0.1;
while((suffix[i]>='0'&&suffix[i]<='9')||suffix[i]=='.')
{
push_flag=1;
if(suffix[i]=='.')
dot_flag=1;
else
{
if(dot_flag==0)
n=n*10+(suffix[i]-'0');
else
{
n=n+(suffix[i]-'0')*k;
k/=10;
}
}
if((suffix[i+1]>='0'&&suffix[i+1]<='9')||suffix[i++]=='.')
i++;
}
if(push_flag==1)
num_stack.push(n);
if(is_binary_operator(suffix[i]))
{
b=num_stack.pop();
a=num_stack.pop();
// cout<
num_stack.push(compute(a,suffix[i],b));
}
else if(suffix[i]>='a'&&suffix[i]<='z')
{
a=num_stack.pop();
// cout<<endl<<"computing:"<<suffix[i]<<a<<endl;
num_stack.push(compute(a,suffix[i]));
}
}
return num_stack.get_top();
}
char judge(char a,char b)
{
int m,n;
if(a=='(' && b==')') return '=';
else
{
switch(a)
{
case '(':case '=':m=0;break;
case '+':case '-':m=1;break;
case '*':case '/':m=2;break;
case '^':case 'c':case 's':case 'l':case 'n':m=3;break;
default : break;
}
switch(b)
{
case '=':case ')':n=0;break;
case '+':case '-':n=1;break;
case '*':case '/':n=2;break;
case '^':case 'c':case 's':case 'l':case 'n':n=3;break;
case '(':n=4;break;
default : break;
}
if(m
return '
else
return '>';
}
}
bool is_binary_operator(char e)
{
return (e=='+'||e=='-'||e=='*'||e=='/'||e=='^');
}
double compute(double a,char operate,double b)
{
switch(operate)
{
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
case '^':return pow(a,b);
default : break;
}
}
double compute(double a,char operate)
{
switch(operate)
{
case 'c':return cos(a);
case 's':return sin(a);
case 'l':return log10(a);
case 'n':return log(a);
default : break;
}
}
解决方案三:
#include
#include
#include
#include
#define STACK_INIT_SIZE 100
typedef char SElemType;
typedef struct
{
SElemType * base;
SElemType * top;
int stacksize;
}SqStack;
typedef struct
{
double * base;
double * top;
int stacksize;
}QSqStack;
int InitStack(SqStack &S)
{
S.base=(SElemType )malloc(STACK_INIT_SIZE * sizeof(SElemType));
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return 1;
}
int QInitStack(QSqStack &Q)
{
Q.base=(double *)malloc(STACK_INIT_SIZE * sizeof(double));
Q.top=Q.base;
Q.stacksize=STACK_INIT_SIZE;
return 1;
}
char icp(char e)
{
if(e=='#') return 0;
if(e=='(') return 6;
if(e==')') return 1;
if(e=='+'||e=='-') return 2;
if(e==''||e=='/') return 4;
}
char isp(char e)
{
if(e=='#') return 0;
if(e=='(') return 1;
if(e==')') return 6;
if(e=='+'||e=='-') return 3;
if(e=='*'||e=='/') return 5;
}
void jisuan(QSqStack &Q,char e)
//将栈丁的两个值计算,并把所得到的值放回栈中
{
double i;
switch(e){
case '+':i=*--Q.top;*(Q.top-1)+=i;break;
case '-':i=*--Q.top;*(Q.top-1)-=i;break;
case '*':i=*--Q.top;*(Q.top-1)*=i;break;
case '/':i=*--Q.top;*(Q.top-1)/=i;break;}
}
void Push1(SqStack &S,char e)
{
S.top++=e;
}
char istake(char e)
{
if(e=='+'||e=='/'||e==''||e=='-'||e=='('||e==')')
return 0;
else
return 1;
}
void Push2(SqStack &S,QSqStack &Q,char a)
//将中缀表达式转换为后缀表达式,并依次将字符和数值放入两个栈中
{
int i=0,len,x=1,k,y;
double e,j,z;
len=strlen(a);
for(;i
{
int flag=1;
if(istake(a[i]))//判断传入值是变量 数值还是字符
{
if(a[i]>='a'&&a[i]<='z')
{
printf("请输入变量值:");
scanf("%lf",&e);
*Q.top++=e;
i++;
}
else{
e=a[i]-'0';
while(a[++i]>='0'&&a[i]<='9')
{
e=e*10+(a[i]-'0');
}
if(a[i]=='.')
{
while(a[++i]>='0'&&a[i]<='9')
{
x=x*10;
j=(double)(a[i]-'0')/x;
e=e+j;
}
}
if(a[i]=='^')
{
z=e;
i++;
y=a[i]-'0';
while(a[++i]>='0'&&a[i]<='9')
{
y=y*10+(a[i]-'0');
}
for(k=1;k
e=e*z;
}
*Q.top++=e;
x=1;
}
}
else
{
if(icp(a[i])>isp((S.top-1)))
{
* S.top++=a[i];
i++;
}
else
{
jisuan(Q,*(S.top-1));
S.top=S.top-1;
while(flag)
{
if(icp(a[i])
{
jisuan(Q,*(S.top-1));
S.top=S.top-1;
flag=1;
}
else if(icp(a[i])==isp(*(S.top-1)))
{
S.top=S.top-1;
flag=0;
}
else {
if(a[i]!=')')
*S.top++=a[i];
flag=0;
}
}
i++;
}
}
}
}
int main()
{
char a[100];
SqStack S;//建立栈存放字符
QSqStack Q;//建立栈存放数值
InitStack(S);
QInitStack(Q);
scanf("%s",a);
Push1(S,'#');
Push2(S,Q,a);//字符串依次进栈
for(;S.top-1>S.base;)
{
jisuan(Q,*(S.top-1));
S.top=S.top-1;
}
printf("%lf",*(Q.top-1));
return 0;
}
已测可用