• 大小: 5KB
    文件类型: .cpp
    金币: 1
    下载: 0 次
    发布日期: 2021-05-19
  • 语言: C/C++
  • 标签: 等式求值  

资源简介

大一学习了用C语言实现的表达式求值,就是用C语言写的一个计算器,包括+-*/()等运算,也许你会觉得很简单,但是我花了很长时间才弄明白,其中用到了栈、队列等算法。

资源截图

代码片段和文件信息

#include
#include
#define max 100
#define maxop 6
typedef struct node
{
int date[max];
int top;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ;
}sqstack;

sqstack K;
sqstack *L*s;

sqstack * inistack(sqstack * s)
{
s=(sqstack *)malloc(sizeof(K));
s->top=-1;
return s;
}

int stackempty(sqstack * s)
{
return(s->top==-1);
}

sqstack * push(sqstack *Lint i)
{
if(s->top==max-1)
{
printf(“栈满!“);
return 0;
}
s->top++;
s->date[s->top]=i;
return(L);
}

int pop(sqstack *s)
{
int e;
if(s->top==-1)
{
printf(“栈空!“);
return 0;
}
e=s->date[s->top];
s->top--;
return(e) ;
}

 int gettop(sqstack *s)
{
int e;
if(s->top==-1)
{
printf(“栈空!“);
return 0;
}
e=s->date[s->top];
return(e) ;
}

struct youxianji
{
char ch;
int pri;
}lpri[7]={{‘=‘0}{‘(‘1}{‘*‘5}{‘/‘5}{‘+‘3}{‘-‘3}{‘)‘6}}
 rpri[7]={{‘=‘0}{‘(‘6}{‘*‘4}{‘/‘4}{‘+‘2}{‘-‘2}{‘)‘1}};
 
 int leftpri(char op)
{
int i;
for(i=0;i if(lpri[i].ch==op)
return(lpri[i].pri);
}

 int rightpri(char op)
{
int i;
for(i=0;i if(rpri[i].ch==op)
return(rpri[i].pri);
}
int inop(char ch)
{
if(ch==‘(‘||ch==‘)‘||ch==‘+‘||ch==‘-‘||ch==‘*‘||ch==‘/‘)
return(1);
else
return(0);
}

int outop(char exc[])
{
    char *exp=exc;
    while(* exp!=‘\0‘)
{
    if(*exp==‘(‘||*exp==‘)‘||*exp==‘+‘||*exp==‘-‘||*exp==‘*‘||*exp==‘/‘||(*exp>=‘0‘&& *exp<=‘9‘))
exp++;
else 
break;
};
if(*exp==‘\0‘)
return(1);
else
{
printf(“输入表达式错误!请重新输入表达式:\n“);
 return(0);
    }
}
int precede(char op1char op2)
{
if(leftpri(op

评论

共有 条评论

相关资源