|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <windows.h>
#include <stdio.h>
#include <string.h>
#define STACKSIZE 1000
typedef struct OPTR{
char stack[STACKSIZE];
int top;
int stacksize;
}OPTR;
int initstack(OPTR *c){
c->top=-1;
c->stacksize=STACKSIZE;
return 1;
}
typedef struct OPND{
double data;
struct OPND *next;
}OPND;
typedef struct linkstack{
OPND *top;
}linkstack;
void Initlinkstack(linkstack *S)
{
S=(linkstack*)malloc(sizeof(linkstack));
S->top=NULL;
}
char pushchar(OPTR *c,char op);
char Gettopcahr(OPTR c,char *op);
char popchar(OPTR *c,char *op);
char pushchar(OPTR *c,char op){
if(c->top=c->stacksize-1)
return 0;
c->stack[++c->top]=op;
return 1;
}
char Gettopchar(OPTR c,char *op){
if(c.top<0)
return 0;
else{
*op=c.stack[c.top];
return 1;
}
}
char popchar(OPTR *c,char *op){
if(c->top<0)
return 0;
*op=c->stack[c->top--];
return 1;
}
double pushnum(linkstack *S,double e);
double Gettopnum(linkstack S,double *e);
double popnum(linkstack *S,double *e);
double pushnum(linkstack *S,double e){
OPND *p;
p=(OPND*)malloc(sizeof(OPND));
if(p==NULL)
return 0;
p->data=e;
p->next=S->top;
S->top=p;
return 1;
}
double Gettopnum(linkstack S,double *e){
if(S.top==NULL)
return 0;
else{
*e=S.top->data;
return 1;
}
}
double popnum(linkstack *S,double *e){
OPND *p;
if(S->top==NULL)
return 0;
*e=S->top->data;
p=S->top;
S->top=S->top->next;
free(p);
return 1;
}
char judgment(char ch);
char judgment(char ch){
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='('||ch==')'||ch=='\n')
return 1;
else
return 0;
}
int Index(char theta);
int Index(char theta)
{
int index = 0;
switch (theta)
{
case '+':
index = 0;
break;
case '-':
index = 1;
break;
case '*':
index = 2;
break;
case '/':
index = 3;
break;
case '(':
index = 4;
break;
case ')':
index = 5;
break;
case '#':
index = 6;
default:break;
}
return index;
}
int getIndex(theta1) {
return 0;
}
char Priority(char theta1, char theta2);
char Priority(char theta1, char theta2)
{
int index1, index2;
char priority[][7] =
{
{ '>','>','<','<','<','>','>' },
{ '>','>','<','<','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '>','>','>','>','<','>','>' },
{ '<','<','<','<','<','=','0' },
{ '>','>','>','>','0','>','>' },
{ '<','<','<','<','<','0','=' },
};
index1 = Index(theta1);
index2 = Index(theta2);
return priority[index1][index2];
}
char precede(char a,char b);
char precede(char a,char b){
Priority(Index(a),Index(b));
return 0;
}
double calculate(double a,char ch,double b);
double calculate(double a,char ch,double b){
switch(ch){
case'+': return a + b; break;
case'-': return a - b; break;
case'/': return a / b; break;
case'*': return a * b; break;
default: return 0;
}
}
double answer();
double answer()
{
char ch;
char theta;double a,b;
int v=0;
OPTR OPTR;
linkstack OPND;
Initlinkstack(&OPND);
pushchar(&OPTR,'#');
printf("请输入表达式并以#结尾:");
scanf_s("%c",&ch);
Gettopchar(OPTR,&theta);
while(ch!='#'&&theta!='#'){
if(judgment(ch)!=1)
{
int temp;
temp=ch-'0';
ch=getchar();
while(judgment(ch)!=1)
{
temp=temp*10+ch-'0';
ch=getchar();
}
pushnum(&OPND,temp);
}
else{
Gettopchar(OPTR,&theta);
switch(precede(theta,ch))
{
case'<':
pushchar(&OPTR,ch);ch=getchar();break;
case'=':
popchar(&OPTR,&ch);ch=getchar();break;
case'>':
popchar(&OPTR,&ch);popnum(&OPND,&a);popnum(&OPND,&b);
pushnum(&OPND,calculate(a,theta,b));break;
}
}
}
while(popchar(&OPTR,&ch)!=0)
{
v=v*10+ch-'0';
}
return v;
}
int main()
{
double i;
i=answer();
printf("%f",i);
return 0;
}
|
|