C语言的学习暂时告一段落,在此期间写了两个程序,用来实现计算器功能,附上程序级程序运行结果,供大家参考。
该程序优点在于程序简单,一目了然,把栈的知识合理运用起来,很适合初学者;
程序包括创建栈,入栈、出栈,判断栈满和栈空,销毁栈等;
缺点在于仅能实现int型的加减乘除运算,不能识别括号和小数点的运算,所以实用性不强。
#include<stdio.h>
#include<stdlib.h>
#define max 2014
typedef struct stack
{
int data[max];
int top;
}stack;
stack *createstack(void)//创建栈
{
stack *s;
s=(stack*)malloc(sizeof(stack));
s->top=-1;
return s;
}
int stackempty(stack *s)//判断栈空
{
if(s)
{
if(s->top==-1)
return 1;
else
return 0;
}
return -1;
}
int stackfull(stack *s)//判断栈满
{
if(s)
{
if(s->top==max-1)
return 1;
else
return 0;
}
return -1;
}
void push(stack *s,int x)//入栈
{
if(s)
{
if(stackfull(s)==0)
s->data[++s->top]=x;
}
}
int pop(stack *s)//出栈
{
if(s)
if(stackempty(s)==0)
return s->data[s->top--];
}
void clearstack(