Chinaunix首页 | 论坛 | 博客
  • 博客访问: 60114
  • 博文数量: 10
  • 博客积分: 207
  • 博客等级: 入伍新兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-15 14:29
文章分类

全部博文(10)

文章存档

2013年(3)

2012年(7)

分类: LINUX

2012-07-27 20:12:24

系统环境ubuntu10.10  运行于VMware Workstation 8.0虚拟机上
gcc 4.4.5编译

#include
#include
#include
#define N 100

/*************************************************************************/
typedef int type_stack;
typedef struct struct_node
{
type_stack data[N];
int top;
}orderstack,*stack_link;

stack_link creat_stack();
int push_stack(stack_link ,type_stack);
type_stack pop_stack(stack_link);
type_stack get_top(stack_link top);
int empty_stack(stack_link top);
int full_stack(stack_link top);
/***************************************************************************/
int mycompare(char ch1,char ch2);//=2 push_stack ,= 1 pop and do 
int col(int num1,char n,int num2);
int main()
{
char ch=0;
int sum=0,ret=0;
type_stack tmp,ret1,ret2;

stack_link fore=creat_stack();
stack_link mid=creat_stack();
printf("'('=%d , ')'=%d \n",'(',')');
puts("input next:\nexample: 12*(2+12/(3-1)+23) =");
puts("example: 2*13+2*(3*(4+2)/(2+1)+12) =");
while('=' != ch)
{
ch=getchar();
if(ch<=32)
continue ;
while(ch>='0'&&ch<='9')
{
if(ch>='0'&&ch<='9')
sum =sum*10 + ch-'0';
ch=getchar();
}
if(sum!=0)
push_stack(fore,sum);
sum=0;
if('+'==ch||'-'==ch||'*'==ch||'/'==ch)
{
if(empty_stack(mid)==1)
push_stack(mid,ch);
else
{
ret=mycompare(get_top(mid),ch);
if(1==ret)
{
// puts("<=");
tmp=pop_stack(mid);
ret2=pop_stack(fore);
ret1=pop_stack(fore);
tmp=col(ret1,tmp,ret2);
push_stack(fore,tmp);
push_stack(mid,ch);
}
if(2==ret)
{
// puts(">");
push_stack(mid,ch);
}
}
}
if('('==ch)
push_stack(mid,ch);
if(ch==')')
while(1)
{
tmp=pop_stack(mid);
if(tmp=='(')break;
ret2=pop_stack(fore);
ret1=pop_stack(fore);
tmp=col(ret1,tmp,ret2);
push_stack(fore,tmp);
}
}
while(empty_stack(mid)!=1)
{
tmp=pop_stack(mid);
ret2=pop_stack(fore);
ret1=pop_stack(fore);
tmp=col(ret1,tmp,ret2);
push_stack(fore,tmp);
}
printf("ret=%d\n",pop_stack(fore));
free(fore);
free(mid);
return 0;
}
/********************************************************************/
stack_link creat_stack()
{
stack_link top1=(stack_link)malloc(sizeof(orderstack));
memset(top1,0,sizeof(orderstack));
top1->top=-1;
return top1;
}

int push_stack(stack_link top1,type_stack x)
{
if(1==full_stack(top1))
{
puts("the stack is full");
return 0;
}
top1->data[++top1->top]=x;
return 1;
}
type_stack pop_stack(stack_link top)
{
if(1==empty_stack(top))
{
puts("the stack is empty");
return 0;
}
return top->data[top->top--];
}
type_stack get_top(stack_link top)
{
if(1==empty_stack(top))
{
puts("the stack is empty");
return 0;
}
return top->data[top->top];
}
int empty_stack(stack_link top)
{
if(-1==top->top)
return 1;
else
return 0;
}
int full_stack(stack_link top)
{
if(top->top>=N-1)
return 1;
else
return 0;
}
int col(int num1,char n,int num2)
{
switch(n)
{
case '+':
return num1+num2;
break;
case '-':
return num1-num2;
break;
case '*':
return num1*num2;
break;
case '/':
return num1/num2;
break;
default:
return 0;
break;

}
}

int mycompare(char ch1,char ch2)
{
if('('==ch1)return 2;
if('+'==ch1&&'+'==ch2)
return 1;
if('-'==ch1&&'-'==ch2)
return 1;
if('*'==ch1&&'*'==ch2)
return 1;
if('/'==ch1&&'/'==ch2)
return 1;
if('*'==ch1&&'+'==ch2)
return 1;
if('*'==ch1&&'-'==ch2)
return 1;
if('*'==ch1&&'/'==ch2)
return 1;
if('/'==ch1&&'+'==ch2)
return 1;
if('/'==ch1&&'-'==ch2)
return 1;
if('/'==ch1&&'*'==ch2)
return 1;
if('+'==ch1&&'-'==ch2)
return 1;
if('+'==ch1&&'*'==ch2)
return 2;
if('+'==ch1&&'/'==ch2)
return 2;
if('-'==ch1&&'+'==ch2)
return 1;
if('-'==ch1&&'*'==ch2)
return 2;
if('-'==ch1&&'/'==ch2)
return 2;
}
在终端显示结果如下:
测试过没整理,主函数里面塞得东东有点多,请多多指点
阅读(1845) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~