mathph 2020-06-09 12:13 采纳率: 0%
浏览 164

c语言代码问题?,希望大佬解答

#include
#include
#include
#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 return 0;
else{
*op=c.stack[c.top];
return 1;
}
}
char popchar(OPTR *c,char *op){
if(c->top 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;
}
为什么运行后没有输出

  • 写回答

2条回答 默认 最新

  • qiu_shaofeng 2020-06-09 21:52
    关注

    int initstack(OPTR c);
    void Initlinkstack(linkstack S)
    {
    S=(linkstack)malloc(sizeof(linkstack));
    S->top=NULL;
    }
    这两个函数都有问题,混淆了结构体变量名和结构体变量地址
    p=(OPND *)malloc(sizeof(OPND));//要加

    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题