栈实现计算器
来源:互联网 发布:免费炒股软件手机版 编辑:程序博客网 时间:2024/06/07 13:14
v栈的应用举例 n栈在表达式计算过程中的应用 :建立操作数栈和运算符栈。运算符有优先级。规则: n自左至右扫描表达式,凡是遇到操作数一律进操作数栈。n当遇到运算符时,如果它的优先级比运算符栈栈顶元素的优先级高就进栈。反之,取出栈顶运算符和操作数栈栈顶的连续两个操作数进行运算,并将结果存入操作数栈,然后继续比较该运算符与栈顶运算符的优先级。n左括号一律进运算符栈,右括号一律不进运算符栈,取出运算符栈顶运算符和操作数栈顶的两个操作数进行运算,并将结果压入操作数栈,直到取出左括号为止。
v栈的应用举例
n栈在表达式计算过程中的应用 :建立操作数栈和运算符栈。运算符有优先级。规则:
n自左至右扫描表达式,凡是遇到操作数一律进操作数栈。
n当遇到运算符时,如果它的优先级比运算符栈栈顶元素的优先级高就进栈。反之,取出栈顶运算符和操作数栈栈顶的连续两个操作数进行运算,并将结果存入操作数栈,然后继续比较该运算符与栈顶运算符的优先级。
n左括号一律进运算符栈,右括号一律不进运算符栈,取出运算符栈顶运算符和操作数栈顶的两个操作数进行运算,并将结果压入操作数栈,直到取出左括号为止。
error.h
#ifndef __ERROR_H_
#define __ERROR_H_
#define ERROR -1
#define FULL -2
#define EMPTY -3
#define Malloc -4
int error;
void myERROR(char *str);
char * mystrerror(int num);
#endif
error.c
#include"error.h"
#include<stdio.h>
void myERROR(char *str)
{
/*char * msg=mystrerror(error);
printf("%s:%s",str,msg);*/
switch(error)
{
case ERROR:
printf("%s:输入参数错误\n",str);
break;
case FULL:
printf("%s:已满栈\n",str);
break;
case EMPTY:
printf("%s:已空栈\n",str);
break;
}
}
char * mystrerror(int num)
{
switch(error)
{
case ERROR:
return "%s:输入参数错误\n";
case FULL:
return "%s:已满栈\n";
case EMPTY:
return "%s:已空栈\n";
case Malloc:
return "%s:空间分配失败\n";
}
}
#include<stdio.h>
void myERROR(char *str)
{
/*char * msg=mystrerror(error);
printf("%s:%s",str,msg);*/
switch(error)
{
case ERROR:
printf("%s:输入参数错误\n",str);
break;
case FULL:
printf("%s:已满栈\n",str);
break;
case EMPTY:
printf("%s:已空栈\n",str);
break;
}
}
char * mystrerror(int num)
{
switch(error)
{
case ERROR:
return "%s:输入参数错误\n";
case FULL:
return "%s:已满栈\n";
case EMPTY:
return "%s:已空栈\n";
case Malloc:
return "%s:空间分配失败\n";
}
}
Sqstack.h
#ifndef __SQSTACK_H_
#define __SQSTACK_H_
#define SIZE 100
#define TRUE 1
#define FALSE 0
#include"error.h"
typedef int stackdata;
typedef struct stack
{
stackdata data[SIZE];
int top;
}STACK;
typedef struct stack1
{
char data[SIZE];
int top;
}S;
int Initstack(STACK *s);//zhi kong
int Initstack1(S *s);
//shi fou kong
int stackempty(STACK *s);
int stackempty1(S *s);
//shi fou man
int stackfull(STACK *s);
int stackfull1(S *s);
//jin zhan
int push(STACK *s,stackdata x);
int push1 (S*s,char x);
//chu zhan
int pop(STACK *s,stackdata *x);
int pop1(S *s,char *x);
//qu zhan ding
int gettop(STACK *s,stackdata *x);
int gettop1(S *s,char *x);
#endif
#define __SQSTACK_H_
#define SIZE 100
#define TRUE 1
#define FALSE 0
#include"error.h"
typedef int stackdata;
typedef struct stack
{
stackdata data[SIZE];
int top;
}STACK;
typedef struct stack1
{
char data[SIZE];
int top;
}S;
int Initstack(STACK *s);//zhi kong
int Initstack1(S *s);
//shi fou kong
int stackempty(STACK *s);
int stackempty1(S *s);
//shi fou man
int stackfull(STACK *s);
int stackfull1(S *s);
//jin zhan
int push(STACK *s,stackdata x);
int push1 (S*s,char x);
//chu zhan
int pop(STACK *s,stackdata *x);
int pop1(S *s,char *x);
//qu zhan ding
int gettop(STACK *s,stackdata *x);
int gettop1(S *s,char *x);
#endif
Sqstack.c
#include"Sqstack.h"
#include<stdio.h>
//#include"error.h"
int Initstack(STACK *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
s->top=-1;
}
int Initstack1(S *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
s->top=-1;
}
int stackempty(STACK *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
return s->top==-1;
}
int stackempty1(S *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
return s->top==-1;
}
int stackfull(STACK *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
return s->top==SIZE-1;
}
int stackfull1(S *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
return s->top==SIZE-1;
}
int push(STACK *s,stackdata x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackfull(s))
{
error=FULL;
return FALSE;
}
s->data[++s->top]=x;
return TRUE;
}
int push1 (S*s,char x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackfull1(s))
{
error=FULL;
return FALSE;
}
s->data[++s->top]=x;
return TRUE;
}
int pop(STACK *s,stackdata *x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackempty(s))
{
error=EMPTY;
return FALSE;
}
*x=s->data[s->top--];
return TRUE;
}
int pop1(S *s,char *x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackempty1(s))
{
error=EMPTY;
return FALSE;
}
*x=s->data[s->top--];
return TRUE;
}
int gettop(STACK *s,stackdata *x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackfull(s))
{
error=FULL;
return FALSE;
}
if(stackempty(s))
{
error=EMPTY;
return FALSE;
}
*x=s->data[s->top];
return TRUE;
}
int gettop1(S *s,char *x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackfull1(s))
{
error=FULL;
return FALSE;
}
if(stackempty1(s))
{
error=EMPTY;
return FALSE;
}
*x=s->data[s->top];
return TRUE;
}
#include<stdio.h>
//#include"error.h"
int Initstack(STACK *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
s->top=-1;
}
int Initstack1(S *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
s->top=-1;
}
int stackempty(STACK *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
return s->top==-1;
}
int stackempty1(S *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
return s->top==-1;
}
int stackfull(STACK *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
return s->top==SIZE-1;
}
int stackfull1(S *s)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
return s->top==SIZE-1;
}
int push(STACK *s,stackdata x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackfull(s))
{
error=FULL;
return FALSE;
}
s->data[++s->top]=x;
return TRUE;
}
int push1 (S*s,char x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackfull1(s))
{
error=FULL;
return FALSE;
}
s->data[++s->top]=x;
return TRUE;
}
int pop(STACK *s,stackdata *x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackempty(s))
{
error=EMPTY;
return FALSE;
}
*x=s->data[s->top--];
return TRUE;
}
int pop1(S *s,char *x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackempty1(s))
{
error=EMPTY;
return FALSE;
}
*x=s->data[s->top--];
return TRUE;
}
int gettop(STACK *s,stackdata *x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackfull(s))
{
error=FULL;
return FALSE;
}
if(stackempty(s))
{
error=EMPTY;
return FALSE;
}
*x=s->data[s->top];
return TRUE;
}
int gettop1(S *s,char *x)
{
if(s==NULL)
{
error=ERROR;
return FALSE;
}
if(stackfull1(s))
{
error=FULL;
return FALSE;
}
if(stackempty1(s))
{
error=EMPTY;
return FALSE;
}
*x=s->data[s->top];
return TRUE;
}
main.c
#include<stdio.h>
#include<string.h>
#include"Sqstack.h"
int main()
{
STACK num;
S ch;
int x,s;
char chh;
Initstack(&num);
Initstack1(&ch);
char str[100];
gets(str);
s=strlen(str);
int i=0,j,k,q;
if(stackempty(&num))
{
printf("数字空栈\n");
}
if(stackempty1(&ch))
{
printf("符号空栈\n");
}
for(i=0;i<s;)
{
k=1;
j=0;
if(isdigit(str[i]))
{
while(isdigit(str[i]))
{
j=j*k+str[i++]-48;
k=10;
}
push(&num,j);
if(str[i]==0)
break;
}
else if(str[i]=='(')
{
push1(&ch,str[i]);
printf("ch\n");
i++;
}
else if(str[i]=='+'||str[i]=='-')
{
if(ch.top==-1||ch.data[ch.top]=='(')
push1(&ch,str[i++]);
else
{
pop(&num,&j);
pop(&num,&k);
pop1(&ch,&chh);
if(chh=='+')
push(&num,j+k);
else if(chh=='-')
push(&num,k-j);
else if(chh=='*')
push(&num,k*j);
else
push(&num,k/j);
push1(&ch,str[i]);
i++;
}
}
else if(str[i]==')')
{
while(ch.data[ch.top]!='(')
{
pop(&num,&j);
pop(&num,&k);
pop1(&ch,&chh);
if(chh=='+')
push(&num,j+k);
else if(chh=='-')
push(&num,k-j);
else if(chh=='*')
push(&num,k*j);
else
push(&num,k/j);
}
pop1(&ch,&chh);
i++;
}
else if(str[i]=='*'||str[i]=='/')
{
if(ch.top==-1||ch.data[ch.top]=='('
||ch.data[ch.top]=='+'
||ch.data[ch.top]=='-')
push1(&ch,str[i++]);
else
{
pop(&num,&j);
pop(&num,&k);
pop1(&ch,&chh);
if(chh=='+')
push(&num,j+k);
else if(chh=='-')
push(&num,k-j);
else if(chh=='*')
push(&num,k*j);
else
push(&num,k/j);
push1(&ch,str[i++]);
}
}
}
printf("\n");
printf("numtop=%d\n",num.top);
/*while(num.top!=-1)
{
pop(&num,&x);
printf("%5d",x);
}*/
printf("\n");
printf("chtop=%d\n",ch.top);
/*while(ch.top!=-1)
{
pop1(&ch,&chh);
printf("%c ",chh);
}*/
printf("\n");
while(ch.top!=-1)
{
pop(&num,&j);
pop(&num,&k);
pop1(&ch,&chh);
if(chh=='+')
push(&num,j+k);
else if(chh=='-')
push(&num,k-j);
else if(chh=='*')
push(&num,k*j);
else
push(&num,k/j);
}
pop(&num,&j);
printf("sum=%d\n",j);
return 0;
}
#include<string.h>
#include"Sqstack.h"
int main()
{
STACK num;
S ch;
int x,s;
char chh;
Initstack(&num);
Initstack1(&ch);
char str[100];
gets(str);
s=strlen(str);
int i=0,j,k,q;
if(stackempty(&num))
{
printf("数字空栈\n");
}
if(stackempty1(&ch))
{
printf("符号空栈\n");
}
for(i=0;i<s;)
{
k=1;
j=0;
if(isdigit(str[i]))
{
while(isdigit(str[i]))
{
j=j*k+str[i++]-48;
k=10;
}
push(&num,j);
if(str[i]==0)
break;
}
else if(str[i]=='(')
{
push1(&ch,str[i]);
printf("ch\n");
i++;
}
else if(str[i]=='+'||str[i]=='-')
{
if(ch.top==-1||ch.data[ch.top]=='(')
push1(&ch,str[i++]);
else
{
pop(&num,&j);
pop(&num,&k);
pop1(&ch,&chh);
if(chh=='+')
push(&num,j+k);
else if(chh=='-')
push(&num,k-j);
else if(chh=='*')
push(&num,k*j);
else
push(&num,k/j);
push1(&ch,str[i]);
i++;
}
}
else if(str[i]==')')
{
while(ch.data[ch.top]!='(')
{
pop(&num,&j);
pop(&num,&k);
pop1(&ch,&chh);
if(chh=='+')
push(&num,j+k);
else if(chh=='-')
push(&num,k-j);
else if(chh=='*')
push(&num,k*j);
else
push(&num,k/j);
}
pop1(&ch,&chh);
i++;
}
else if(str[i]=='*'||str[i]=='/')
{
if(ch.top==-1||ch.data[ch.top]=='('
||ch.data[ch.top]=='+'
||ch.data[ch.top]=='-')
push1(&ch,str[i++]);
else
{
pop(&num,&j);
pop(&num,&k);
pop1(&ch,&chh);
if(chh=='+')
push(&num,j+k);
else if(chh=='-')
push(&num,k-j);
else if(chh=='*')
push(&num,k*j);
else
push(&num,k/j);
push1(&ch,str[i++]);
}
}
}
printf("\n");
printf("numtop=%d\n",num.top);
/*while(num.top!=-1)
{
pop(&num,&x);
printf("%5d",x);
}*/
printf("\n");
printf("chtop=%d\n",ch.top);
/*while(ch.top!=-1)
{
pop1(&ch,&chh);
printf("%c ",chh);
}*/
printf("\n");
while(ch.top!=-1)
{
pop(&num,&j);
pop(&num,&k);
pop1(&ch,&chh);
if(chh=='+')
push(&num,j+k);
else if(chh=='-')
push(&num,k-j);
else if(chh=='*')
push(&num,k*j);
else
push(&num,k/j);
}
pop(&num,&j);
printf("sum=%d\n",j);
return 0;
}
阅读全文
0 0
- 【栈】实现高级计算器
- 栈实现计算器
- 用栈实现计算器
- 用栈实现计算器
- 计算器(栈实现)
- 栈实现计算器
- 栈实现计算器
- 双栈实现计算器
- 栈实现计算器
- 栈实现的简单计算器
- 栈实现计算器简单功能
- Java实现栈之计算器
- 巧妙利用栈实现计算器
- 用栈实现科学计算器
- 栈 实现四则运算的计算器
- hdu1237简单计算器-栈实现
- 栈实现简单的计算器
- 用栈实现的计算器
- 9S12汇编指令【HCS12】
- iOS开发
- Spring boot 热加载
- 【iOS】Runtime解读
- 韩信点兵
- 栈实现计算器
- 现代处理器与代码性能优化
- 在CodeWarrior中指定堆栈大小及堆栈指针
- 知乎上看到的一些鸡汤语录
- TP 多环境配置
- thinkphp前端套数据,遇到的坑--数组
- AppcompatActivity拦截View创建方法
- io流最详解
- 从多层感知器到卷积网络(一)