| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 725 人关注过本帖
标题:中缀表达式计算的程序不出结果
只看楼主 加入收藏
pengfei123
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-5-27
收藏
 问题点数:0 回复次数:1 
中缀表达式计算的程序不出结果

#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include<math.h>
#include<string.h>


using namespace std;
typedef struct SNode{
union{
char c;
float fl;
}un;
float separate;//用来区分数字(1)和运算符(0);

struct SNode *next;
}LNode,*Linklist;

typedef struct stack{
Linklist base;
Linklist top;
}Sqstack;
void Inital(Linklist &L)
{L=(Linklist)malloc(sizeof(LNode));
L->next=NULL;

}

void BuildList(Linklist &L)//建中缀链表
{
Inital(L);


Linklist p,k;
Inital(p);
Inital(k);

p=L;


{p->separate=0;
p->un.c='#';
cout<<L->un.c;
}

cout<<"请输入多项式的第一项,若为括号输入0,否则输入数字"<<endl;
float i;
cin>>i;
if(i==0)
{
k->separate=0;
k->un.c='(';
p->next=k;
p=p->next;
p->next=NULL;
}
else
{
k->separate=1;
k->un.fl=i;
p->next=k;
p=p->next;
p->next=NULL;


}
while(1)
{ Linklist n;
Inital(n);


cout<<"请输入多项式的下一项,若为运算符输入0,否则输入数字";
cin>>i;
if(i==0)
{
n->separate=0;
cin>>n->un.c;

p->next=n;
p=p->next;
p->next=NULL;
if(p->un.c=='#')break;
}
else
{
n->separate=1;
n->un.fl=i;
p->next=n;
p=p->next;
p->next=NULL;

}
}

system("pause");
}
void print(Linklist L)//输出链表
{
Linklist x;
Inital(x);
x=L;



while (x)
{
if(x->separate)
cout<<x->un.fl;
else
cout<<x->un.c;

x=x->next;
}

}

void BuildEmpty(Sqstack &S)
{
S.top=S.base=(Linklist)malloc(sizeof(LNode));

S.top=NULL;

}
void Push(Sqstack &S,Linklist t)

{


if(S.top==NULL)
{
S.top=t;

}
else
{t->next=S.top;
S.top=t;

}
}
void Pop(Sqstack &S, Linklist t)
{
t=S.top;
S.top=S.top->next;
free(t);


}
void GetTop(Sqstack s,Linklist t)
{
if(s.top)
if(s.top->separate)
{t->un.fl=s.top->un.fl;
cout<<t->un.fl;}

else { t->un.c=s.top->un.c;
cout<<t->un.c;}

}
char PreCedeJudge(char c1,char c2)//判断运算符 优先级
{
cout<<"调用函数PreCedeJudge";
system("pause");
switch(c1)
{
case'+':
if(c2=='*' || c2=='/' || c2=='(')
return '<';
else return '>';
break;
case'-':
if(c2=='*' || c2=='/' || c2=='(')
return '<';
else return '>';
break;
case'*':
if(c2=='(')
return '<';
else return '>';
break;
case'/':
if(c2=='(')
return '<';
else return '>';
break;
case'(':
if(c2==')')
return '=';
else return '<';
break;
case')':
return '>';
break;
case'#':
if(c2=='#' )
return '=';
else return '<';
break;
}


}
float Caculate(float a,char theta,float b)
{
float c;
cout<<"S输出结果"<<endl;
switch (theta)
{
case '+':
c=a+b;
cout<<c;
return c;
break;
case '-':
c=a-b;
cout<<c;
return c;
break;
case '*':
c=a*b;
cout<<c ;
return c;
break;
case '/':
c=a/b;
cout<<c;
return c;
break;
}
}
int Caculation_process(Linklist L,Sqstack optr,Sqstack opnd)//将链表用栈计算
{
//cout<<"调用函数Caculation_Process()"<<endl;;
Linklist h,x,y,th,z; int c;
Inital(h);
Inital(x);
Inital(y);
Inital(th);
Inital(z);

Push(optr,L);
cout<<"调用函数Caculation_Process()2"<<endl;;

L=L->next;
while(L!=NULL)

{ cout<<"调用函数Caculation_Process()3"<<endl;;
if(L->separate)
{
Push(opnd,L);cout<<"调用函数Caculation_Process() if(L->separate)的时候调用"<<endl;;
/*Linklist t;
t=(Linklist)malloc(sizeof(LNode));
if(opnd.top==NULL)
{
opnd.top=t;
opnd.top->next=NULL;
}
else
{t->next=opnd.top;
opnd.top=t;
opnd.top->next=NULL;
}*/
L=L->next;
}

if(L->separate==0)

{
GetTop(optr,h);
cout<<"调用函数Caculation_Process() if(L->separate)else的时候调用"<<endl;;
switch(PreCedeJudge(h->un.c,L->un.c))
{ case'<':

Push(optr,L);
L=L->next;

break;

case'=':

Pop(optr,h);
L=L->next;
break;
case'>':
Pop(optr,th);
Pop(opnd,y);
Pop(opnd,x);

c=Caculate(x->un.fl, th->un.c,y->un.fl);
z->un.fl=c;
Push(opnd, z);

break;
}//switch
cout<<"while()结束"<<endl;
}//if
cout<<"123";
system("pause");
}//while
cout<<"循环结束"<<endl;


Pop(opnd,h);
return h->un.fl;

}
//{
//return 0;
//}


void Run();
void main()
{
Run();

}
void Run()
{

Linklist sn;
Inital(sn);
//Inital(m);
// Inital(t);
Sqstack s1,s2;
// sn->un.c='k';
// m->separate=0;
// m->un.c='g';
BuildList(sn);
print(sn);


BuildEmpty(s1);
BuildEmpty(s2);
system("pause");
//Push(s,sn);
// Push(s,m);
// GetTop( s, t);
cout<< Caculation_process(sn,s1,s2);



}




搜索更多相关主题的帖子: Linklist include float struct 
2006-05-27 22:50
zinking
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:35
帖 子:916
专家分:0
注 册:2004-12-5
收藏
得分:0 
你问问题还这样!!!!!太过分了,

http://kongfuziandlife. http://codeanddesign.
2006-05-28 12:34
快速回复:中缀表达式计算的程序不出结果
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012087 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved