C语言计算机程序 DEBUG ERROR 求大神解答 5
#include<malloc.h>#include<stdio.h>typedeffloatSElemType;typedefcharOptrType;#defineS...
#include<malloc.h>
#include<stdio.h>
typedef float SElemType;
typedef char OptrType;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -2
#define error 0
#define OK 1
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
typedef struct
{
OptrType *base;
OptrType *top;
int stacksize;
}Sqstack;
float InitStack(SqStack *S)
{
(*S).base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
if(!(*S).base) return (OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return 1;
}
char InitStack(Sqstack *S)
{
(*S).base=(OptrType *)malloc(STACK_INIT_SIZE *sizeof(OptrType));
if(!(*S).base) return (OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
float GetTop(SqStack *S)
{
SElemType x;
if((*S).top==(*S).base)
return error;
x=*((*S).top-1);
return x;
}
char Gettop(Sqstack *S)
{
OptrType x;
if((*S).top==(*S).base)
return error;
x=*((*S).top-1);
return x;
}
float Push(SqStack *S,SElemType e)
{
*((*S).top)++=e;
return OK;
}
float Pop(SqStack *S,SElemType e)
{
e=*--((*S).top);
return OK;
}
char Push(Sqstack *S,OptrType e)
{
*((*S).top)++=e;
return OK;
}
char Pop(Sqstack *S,OptrType e)
{
e=*--((*S).top);
return OK;
}
char Precede(OptrType optr,char c)
{
char a=optr;
char b=c;
if(a=='+'||a=='-')
{
if(b=='+'||b=='-'||b==')'||b=='#')
return '>';
else return '<';
}
if(a=='*'||a=='/')
{
if(b=='(')
return '<';
else return '>';
}
if(a=='(')
{
if(b==')')
return '=';
if(b=='#')
return error;
return '<';
}
if(a==')')
{
if(b=='(')
return error;
else return '>';
}
if(a=='#')
{
if(b=='#')
return '=';
if(b==')')
return error;
return '<';
}
return error;
}
float Operate(SElemType a,OptrType t,SElemType b)
{
float bear;
if(t=='+')
bear=a+b;
if(t=='-')
bear=a-b;
if(t=='*')
bear=a*b;
if(t=='/')
return bear;
}
float EvaluateExpression()
{
SqStack OPND;
Sqstack OPTR;
char c,t,x;
float a,b;
InitStack(&OPTR);
Push(&OPTR,'#');
InitStack(&OPND);
c=getchar();
while(c!='#'||Gettop(&OPTR)!='#')
{
if(c>=48&&c<=57)
{
Push(&OPND,c-'0');
c=getchar();
}
else
switch(Precede(Gettop(&OPTR),c))
{
case'<':
Push(&OPTR,c);printf("aaaaa\n");
c = getchar();
break;
case'=':
Pop(&OPTR,x);
c = getchar();
break;
case'>':
Pop(&OPTR,t);
Pop(&OPND,b);printf("aaaaa");
Pop(&OPND,a);printf("aaaaa");
Push(&OPND,Operate(a,t,b));printf("aaaaa");
break;
}
}
return ("%f",GetTop(&OPND));
}
int main()
{
float f;
printf("请输入计算式\n");
f=EvaluateExpression();
printf("%f",&f);
return 1;
} 展开
#include<stdio.h>
typedef float SElemType;
typedef char OptrType;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -2
#define error 0
#define OK 1
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
typedef struct
{
OptrType *base;
OptrType *top;
int stacksize;
}Sqstack;
float InitStack(SqStack *S)
{
(*S).base=(SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType));
if(!(*S).base) return (OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return 1;
}
char InitStack(Sqstack *S)
{
(*S).base=(OptrType *)malloc(STACK_INIT_SIZE *sizeof(OptrType));
if(!(*S).base) return (OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
float GetTop(SqStack *S)
{
SElemType x;
if((*S).top==(*S).base)
return error;
x=*((*S).top-1);
return x;
}
char Gettop(Sqstack *S)
{
OptrType x;
if((*S).top==(*S).base)
return error;
x=*((*S).top-1);
return x;
}
float Push(SqStack *S,SElemType e)
{
*((*S).top)++=e;
return OK;
}
float Pop(SqStack *S,SElemType e)
{
e=*--((*S).top);
return OK;
}
char Push(Sqstack *S,OptrType e)
{
*((*S).top)++=e;
return OK;
}
char Pop(Sqstack *S,OptrType e)
{
e=*--((*S).top);
return OK;
}
char Precede(OptrType optr,char c)
{
char a=optr;
char b=c;
if(a=='+'||a=='-')
{
if(b=='+'||b=='-'||b==')'||b=='#')
return '>';
else return '<';
}
if(a=='*'||a=='/')
{
if(b=='(')
return '<';
else return '>';
}
if(a=='(')
{
if(b==')')
return '=';
if(b=='#')
return error;
return '<';
}
if(a==')')
{
if(b=='(')
return error;
else return '>';
}
if(a=='#')
{
if(b=='#')
return '=';
if(b==')')
return error;
return '<';
}
return error;
}
float Operate(SElemType a,OptrType t,SElemType b)
{
float bear;
if(t=='+')
bear=a+b;
if(t=='-')
bear=a-b;
if(t=='*')
bear=a*b;
if(t=='/')
return bear;
}
float EvaluateExpression()
{
SqStack OPND;
Sqstack OPTR;
char c,t,x;
float a,b;
InitStack(&OPTR);
Push(&OPTR,'#');
InitStack(&OPND);
c=getchar();
while(c!='#'||Gettop(&OPTR)!='#')
{
if(c>=48&&c<=57)
{
Push(&OPND,c-'0');
c=getchar();
}
else
switch(Precede(Gettop(&OPTR),c))
{
case'<':
Push(&OPTR,c);printf("aaaaa\n");
c = getchar();
break;
case'=':
Pop(&OPTR,x);
c = getchar();
break;
case'>':
Pop(&OPTR,t);
Pop(&OPND,b);printf("aaaaa");
Pop(&OPND,a);printf("aaaaa");
Push(&OPND,Operate(a,t,b));printf("aaaaa");
break;
}
}
return ("%f",GetTop(&OPND));
}
int main()
{
float f;
printf("请输入计算式\n");
f=EvaluateExpression();
printf("%f",&f);
return 1;
} 展开
3个回答
展开全部
- -你调试的时候会告诉你哪出错了,然后根据提示再修改就哦了。这样一大段不好看- -
追问
已经弄好了=。=
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你用的什么开发环境
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
亲,你是在吓人吗?这么长,没勇气看下去
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询