简单的四则运算(请看看是哪里错了,哦是找不出来了)

xviubu 2012-06-05 10:11:03
/*
* main.c
*
* Created on: 2012-6-2
* Author: xviubu
*/
#include<stdio.h>
#include<stdlib.h>
#include"main.h"
#include"oper_stack.h"
#include"oper.h"

void menu()
{
printf(" -------MENU------- \n" );
printf("1:Init Stack \n");
printf("2:Push\n");
printf("3:Pop \n");
printf("4:Empty \n");
printf("5:Length\n");
printf("6:Display STack Elem \n");
printf("7:Precede \n");
printf("8:Get Top\n");
printf("9:Operate\n");
printf("10:Evaluate expression \n");
printf("11:InOptr\n");
printf("Please input a choice");

}
int main()
{
Stack S;
char expression[MAXSIZE];
int choice;
int a,b;
char x,y;
char op;
SElemType e;
while(1)
{
menu();
scanf("%d",&choice);
switch(choice)
{
case 1:
if(InitStack(&S))
printf("Init OK\n");
else
printf("Init ERROR\n");
break;
case 2:
printf("Input elem:");
getchar();
scanf("%c",&e);
if(Push(&S,e))
printf("Push OK\n");
else
printf("Push error \n");
break;
case 3:
if(Pop(&S,&e))
printf("popok \n");
else
printf("pop error\n");
break;
case 4:
if(StackEmpty(&S))
printf("Stack is Empty\n");
else
printf("Stack is not empty \n");
break;
case 5:
printf("Stack length is :%d",StackLength(&S));
break;
case 6:
printf("Stack elem is :");
DispStack(&S);
break;
case 7:
printf("Input compare char:");
getchar();
scanf("%c",&x);
scanf("%c",&y);
printf("%c\n",Precede(x,y));
break;
case 8:
printf("Top elem is :%c\n",GetTop(&S));
break;
case 9:
printf("Input expression:");
getchar();
scanf("%d",&a);
scanf("%c",&op);
scanf("%d",&b);
if(Operate(a,op,b))
printf("%d\n",Operate(a,op,b));
else
printf("ERROR \n");
break;
case 10:
printf("Input expression :");
getchar();
gets(expression);
if(EvaluateExpression(expression))
printf("%d\n",EvaluateExpression(expression));
else
printf("ERROR \n");
break;
case 11:
printf("Input a char:");
getchar();
scanf("%c",&op);
if(InOptr(op))
printf("It is a optr \n");
else
printf("It is not a optr \n");
break;
default:
printf("Input Error \n");
break;
}
}
return 0;
}


/*
* main.h
*
* Created on: 2012-5-31
* Author: xviubu
*/

#ifndef MAIN_H_
#define MAIN_H_

#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef char SElemType;
typedef int Status;
typedef struct
{
SElemType data[MAXSIZE];
int top;
}Stack;
typedef int SElemType1;
typedef struct
{
SElemType1 data[MAXSIZE];
int top;
}Stack1;
#endif /* MAIN_H_ */


/*
* oper_stack.c
*
* Created on: 2012-6-5
* Author: xviubu
*/



#include<stdio.h>
#include<stdlib.h>
#include"main.h"

Status InitStack(Stack1 *S)
{
S->top=-1;
return OK;

}
Status StackEmpty(Stack1 *S)
{
if(S->top==-1)
return OK;
else
return ERROR;
}
int StackLength(Stack1 *S)
{
return S->top+1;
}
Status GetTop(Stack *S)
{
if(S->top==-1)
return ERROR;
else
return S->data[S->top];
}
Status Push(Stack1 *S,SElemType1 e)
{
if(S->top==MAXSIZE-1)
return ERROR;
S->top++;
S->data[S->top]=e;
return OK;
}
Status Pop(Stack1 *S,SElemType *e)
{
if(S->top==-1)
return ERROR;
*e=S->data[S->top];
S->top--;
return OK;
}
void DispStack(Stack1 *S)
{
int i;
for(i=0;i<=S->top;i++)
{
printf("%c",S->data[i]);
}
}

/*
* oper_stack.h
*
* Created on: 2012-6-2
* Author: xviubu
*/

#ifndef OPER_STACK_H_
#define OPER_STACK_H_

Status InitStack(Stack *);
Status StackEmpty(Stack *);
int StackLength(Stack *);
Status GetTop(Stack *);
Status Push(Stack *,SElemType);
Status Pop(Stack *,SElemType *);
void DispStack(Stack *);
#endif /* OPER_STACK_H_ */


/*
* oper_stack1.c
*
* Created on: 2012-6-5
* Author: xviubu
*/


/*
* oper_stack.c
*
* Created on: 2012-6-2
* Author: xviubu
*/
#include<stdio.h>
#include<stdlib.h>
#include"main.h"

Status InitStack1(Stack1 *S)
{
S->top=-1;
return OK;

}
Status StackEmpty1(Stack1 *S)
{
if(S->top==-1)
return OK;
else
return ERROR;
}
int StackLength1(Stack1 *S)
{
return S->top+1;
}
Status GetTop1(Stack *S)
{
if(S->top==-1)
return ERROR;
else
return S->data[S->top];
}
Status Push1(Stack1 *S,SElemType1 e)
{
if(S->top==MAXSIZE-1)
return ERROR;
S->top++;
S->data[S->top]=e;
return OK;
}
Status Pop1(Stack1 *S,SElemType1 *e)
{
if(S->top==-1)
return ERROR;
*e=S->data[S->top];
S->top--;
return OK;
}
void DispStack1(Stack1 *S)
{
int i;
for(i=0;i<=S->top;i++)
{
printf("%c",S->data[i]);
}
}

/*
* oper_stack1.h
*
* Created on: 2012-6-5
* Author: xviubu
*/

#ifndef OPER_STACK1_H_
#define OPER_STACK1_H_
Status InitStack1(Stack1 *);
Status StackEmpty1(Stack1 *);
int StackLength1(Stack1 *);
Status GetTop1(Stack1 *);
Status Push1(Stack1 *,SElemType1);
Status Pop1(Stack1 *,SElemType1 *);
void DispStack1(Stack1 *);


#endif /* OPER_STACK1_H_ */


/*
* oper.c
*
* Created on: 2012-6-2
* Author: xviubu
*/

#include<stdio.h>
#include<stdlib.h>
#include"main.h"
#include"oper_stack.h"
#include"oper_stack1.h"
/*算术优先权的比较*/
Status Precede(char ch1,char ch2)//ch1栈顶运算符,ch2读入运算符
{
switch(ch1)
{
case '+':
case '-':
switch(ch2)
{
case '+':
case '-':
case ')':
case '#':
return '>';
break;
case '*':
case '/':
case '(':
return '<';
break;
}
break;
case '*':
case '/':
switch(ch2)
{
case '+':
case '-':
case '*':
case '/':
case ')':
case '#':
return '>';
break;
case '(':
return '<';
break;
}
break;
case '(':
switch(ch2)
{
case '+':
case '-':
case '*':
case '/':
case '(':
return '<';
break;
case ')':
return '=';
break;
case '#':
break;
}
break;
case ')':
switch(ch2)
{
case '+':
case '-':
case '*':
case '/':
case ')':
case '#':
return '>';
break;
case '(':
break;
}
break;
case '#':
switch(ch2)
{
case '+':
case '-':
case '*':
case '/':
case '(':
return '<';
break;
case ')':
break;
case '#':
return '=';
break;
}
break;
}
return OK;
}
int Operate(int opnd1,char optr,int opnd2 )
{
switch(optr)
{
case '+':
return opnd1+opnd2;
break;
case '-':
return opnd1-opnd2;
break;
case '*':
return opnd1*opnd2;
break;
case '/':
return opnd1/opnd2;
break;
default:
printf("OPtr error,retry \n");
return ERROR;
}
}
Status InOptr(char ch)
{
switch(ch)
{
case '#':
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
return OK;
break;
default:
return ERROR;
}
}
int EvaluateExpression(char *expression)
{
Stack OPTR;
Stack1 OPND;
char x,op;
int a,b;

InitStack(&OPTR);
Push(&OPTR,'#');

InitStack1(&OPND);
while(*expression!='#' || GetTop(&OPTR)!='#')
{

if(!InOptr(*expression))
{
Push1(&OPND,*expression-'0');
expression++;
}
else
{
switch(Precede(GetTop(&OPTR),*expression))
{
case '<':
Push(&OPTR,*expression);
expression++;
break;
case '=':
Pop(&OPTR,&x);
expression++;
break;
case '>':
Pop(&OPTR,&op);
Pop1(&OPND,&b);
Pop1(&OPND,&a);
Push1(&OPND,Operate(a,op,b));
break;
}

}
}
return GetTop1(&OPND);
}




/*
* oper.h
*
* Created on: 2012-6-2
* Author: xviubu
*/

#ifndef OPER_H_
#define OPER_H_


char Precede(char ,char );//比较两个运算符的优先级,并返回优先级高的算术符
int Operate(int ,char ,int );//计算表达式
Status InOptr(char );//判断输入的是不是运算符
int EvaluateExpression(char *);//算法
#endif /* OPER_H_ */




































...全文
129 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lvleshuzhi 2012-06-06
  • 打赏
  • 举报
回复
欧,眼睛都花了。
lanmeng521131485 2012-06-06
  • 打赏
  • 举报
回复
加以楼主发代码用插入代码啊!这样好乱啊!

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧