设为首页 加入收藏

TOP

科学计算器源码--c实现
2014-11-23 21:42:00 来源: 作者: 【 】 浏览:21
Tags:科学 计算器 源码 --c 实现

#include
#include
#include
#define MAXSIZE 4000;
typedef struct
{
char data[10];
int top;//头地址

int base;//基地址

int length;//长度

}Stack;

void init(Stack *st)//初始化栈

{
st->base=0;
st->top=0;
st->length=0;
}

int isEmpty(Stack *st)
{
int n=0,top,base;
top =st->top;
base =st->base;
if(top==base)
{
return 1;
}
return n;
}

int isFull(Stack *st)
{
int n=0,top,base;
top =st->top;
if(top>=4000)
{
return 1;
}
return n;
}

char getTop(Stack *st)// 返回top值,不改变栈的结构

{
char n;
if(isEmpty(st))
{
printf("栈为空 ");
return 0;
}
int positon= st->top-1;
n= st->data[positon];//取出数据;

return n;
}

char pop(Stack *st)// 出栈,返回

{
char n;
if(isEmpty(st))
{
printf("栈为空 ");
return 0;
}
int positon= st->top-1;
n= st->data[positon];//取出数据;


st->top--;
st->length--;
st->data[positon]=;//消除数据

return n;
}

void push(char n,Stack *st)//入栈

{
int positon ;
if(isFull(st))
{
printf("栈满 ");

}
else
{
positon= st->top;//获取位置


st->data[positon]=n;//存入数据


st->top++;//改变位置

}

}

void show(Stack *m1)//输出栈中的数据

{
int top,base;
top=m1->top;
base=m1->base;
while(top>base)
{
printf("%c,",m1->data[--top]);
}
printf(" ");
}

int isOperate(char temp)//是否是操作符

{
if(temp==+||temp==-||temp==*||temp==/||temp==(||temp==)||temp==#)
{
return 1;
}
return 0;
}

int isValue(char temp)//是否是数值

{
if(temp>=0&&temp<=9)//

{
return 1;
}
else
{
return 0;
}
}

int isAvail(char temp)//是否有效字符

{
if(isOperate(temp)||isValue(temp))//如果temp既不是操作符和数值的话,则它是非法的

{
return 1;
}
return 0;
}

int detect(char temp)//搜索矩阵位置

{
int i=0;
char oper[7]={+,-,*,/,(,),#};
for(i=0;i<7;i++)
{
if(temp==oper[i])
{
return i;
}
}
}

char Priority(char temp,char optr)//判断优先级

{
/**//*
+ - * / ( ) #
1 2 3 4 5 6 7
+ 1 < < < < > > >
- 2 < < < < > > >
* 3 > > < < > > >
/ 4 > > < < > > >
( 5 > > > > > = 0
) 6 < < < < = 0 >
# 7 < < < < > 0 =
*/
int row ,col;
char priority[7][7]={/**//* + - * / ( ) # */
{<,<,<,<,>,>,>},

{<,<,<,<,>,>,>},

{>,>,<,<,>,>,>},

{>,>,<,<,>,>,>},

{>,>,>,>,>,=,>},

{<,<,<,<,=,0,>},

{<,<,<,<,>,<,=},
};


row = detect(temp);//找出对应的矩阵下标;

col = detect(optr);
// printf("%d,%d",row,col);


//优先级存储在一个7x7的矩阵中,对应关系上图;


return priority[row][col];

}
char eva luate(int a,int b,char oper)
{
switch(oper)
{
case +: return a+b+0;
case -: return a-b+0;
case *: return a*b+0;
case /: return a/b+0

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇写给新手 选择结构程序设计总结 下一篇C语言中最大公约数求法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: