由于时间的关系,这个程序还有很多待改进的地方,例如:本程序不支持负数的运算,不能够报错,因此我正在努力的实现上述的功能,还在考虑它的容错功能,例如:计算3+5的时候,我们不小心多输入了几个+号,我们也能够得到我们所期望的数值8,此外也有考虑用二叉树实现此运算的功能等等,总之,诸多不善之处还望大家海涵!
-------偶的QQ:37170732 邮箱:owenstone@sina.com.cn 大家有什么意见不妨和我交流一下。
------------------------------------下面是程序代码部分-----------------------------------------
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define NumberType double
#define NULL 0
#define DataType char
typedef struct Node *PNode;
typedef struct LinkList *PLinkList;
typedef struct LinkStack *PLinkStack;
struct Node
{
DataType info;
PNode link;
};
struct LinkStack
{ PNode base;
PNode top;
};
struct LinkList
{ PNode base;
PNode top;
};
PLinkList Create_LinkListPointer(void)
{PLinkList pllist;
pllist=(PLinkList)malloc(sizeof(struct LinkList));
if(pllist==NULL) pllist=(PLinkList)realloc(sizeof(struct LinkList));
else pllist->top=pllist->base=NULL;
return (pllist);
}
PLinkList Create_LinkList(PLinkList pllist)
{pllist->top=(PNode)malloc(sizeof(struct Node));
if(pllist->top==NULL) pllist->top=(PNode)realloc(sizeof(struct Node));
else {pllist->base=pllist->top;
pllist->base->link=pllist->top->link=NULL;
}
return (pllist);
}
PLinkList Create_LinkListNode(PLinkList pllist)
{pllist->top->link=(PNode)malloc(sizeof(struct Node));
if(pllist->top->link==NULL) pllist->top->link=(PNode)realloc(sizeof(struct Node));
else{ pllist->top=pllist->top->link;
pllist->top->link=NULL;
}
return(pllist);
}
PLinkStack Create_LinkStackPointer(void)
{PLinkStack plstack;
plstack=(PLinkStack)malloc(sizeof(struct LinkStack));
if(plstack==NULL) plstack=(PLinkStack)realloc(sizeof(struct LinkStack));
else plstack->top=plstack->base=NULL;
return (plstack);
}
PLinkStack Create_LinkStack(PLinkStack plstack)
{plstack->top=(PNode)malloc(sizeof(struct Node));
if(plstack->top==NULL) plstack->top=(PNode)realloc(sizeof(struct Node));
else {plstack->base=plstack->top;
plstack->top->link=plstack->base->link=NULL;
}
return(plstack);
}
PLinkStack Create_LinkStackNode(PLinkStack plstack)
{plstack->top->link=(PNode)malloc(sizeof(struct Node));
if(plstack->top->link==NULL) plstack->top->link=(PNode)malloc(sizeof(struct Node));
else{plstack->top=plstack->top->link;
plstack->top->link=NULL;
}
return(plstack);
}
PLinkStack push_stack(PLinkStack plstack,DataType i)
{plstack->top->link=(PNode)malloc(sizeof(struct Node));
if(plstack->top->link==NULL) plstack->top->link=(PNode)realloc(sizeof(struct Node));
else {plstack->top=plstack->top->link;
plstack->top->link=NULL;
plstack->top->info=i;
}
return (plstack);
}
PLinkStack pop_stack_all(PLinkList pllist,PLinkStack plstack)
{PNode p;
while(plstack->top!=plstack->base){
p=plstack->base;
while(p->link!=plstack->top) p=p->link;
Create_LinkListNode(pllist);
pllist->top->info=plstack->top->info;
plstack->top=plstack->top->link;
plstack->top=p;
}
return (plstack);
}
PLinkList print_llist(PLinkList pllist)
{PNode temp;
temp=pllist->base;
while(temp!=pllist->top){
printf("%c",temp->link->info);
temp=temp->link;
}
printf("\n");
return (pllist);
}
int bracket_included(PLinkStack plstack)
{PNode p,q;
q=p=plstack->base;
while(p!=plstack->top)
{if( p->link->info=='(' || p->link->info==')') q=p->link;
p=p->link;
}
if(q->info=='(' || q->info ==')') return (1);
else return (0);
}
int islower_included(PLinkStack plstack)
{PNode p,q;
q=p=plstack->base;
while(p!=plstack->top){
if(p->link->info=='+' || p->link->info=='-' ) q=p->link;
p=p->link;
}
if(q!=plstack->base &&(q->info=='+' || q->info =='-')) return (1);
else return (0);
}
int bracket_included_and_lower(PLinkStack plstack)
{PNode p,q;
p=plstack->base ;
while(p!=plstack->top){
if(p->link->info==')' ) q=p->link;
p=p->link;
}
p=q;
while(p!=plstack->top){
if(p->link->info=='+' || p->link->info=='-') q=p->link;
p=p->link;
}
if(q->info=='+' || q->info=='-') return (1);
else return (0);
}
PLinkList push_LinkList(PLinkList pllist,DataType i)
{
Create_LinkListNode(pllist);
pllist->top->info=i;
return(pllist);
}
PLinkList push_underline(PLinkList pllist)
{Create_LinkListNode(pllist);
pllist->top->info='_';
return (pllist);
}
PLinkStack pop_stack_until_bracket(PLinkList pllist,PLinkStack plstack)
{PNode p,q;
p=plstack->base;
while(p!=plstack->top){
if(p->link->info=='(') q=p->link;
p=p->link;
}
p=q;
while(plstack->top!=p){
q=plstack->base;
while(q->link!=plstack->top) q=q->link;
Create_LinkListNode(pllist);
pllist->top->info=plstack->top->info;
plstack->top=plstack->top->link;
plstack->top=q;
}
return (plstack);
}
PLinkStack pop_until_lower(PLinkList pllist,PLinkStack plstack)
{PNode p,q;
p=plstack->base;
while(p!=plstack->top){
if(p->link->info=='+' || p->link->info=='-') q=p->link;
p=p->link;
}
p=q;
while(plstack->top!=p){
q=plstack->base;
while(q->link!=plstack->top) q=q->link;
Create_LinkListNode(pllist);
pllist->top->info=plstack->top->info;
plstack->top=plstack->top->link;
plstack->top=q;
}
return (plstack);
}
PLinkStack pop_stack_until_bracket_lower(PLinkList pllist,PLinkStack plstack)
{PNode p,q;
p=plstack->base;
while(p->info!='(') p=p->link;
q=p;
while(plstack->top!=q){
p=q;
while(q->link!=plstack->top) q=q->link;
Create_LinkListNode(pllist);
pllist->top->info=plstack->top->info;
plstack->top=plstack->top->link;
plstack->top=q;
}
return (plstack);
}
PLinkStack pop_one(PLinkStack plstack)
{PNode p;
p=plstack->base;
while(p->link!=plstack->top) p=p->link;
plstack->top=plstack->top->link;
plstack->top=p;
return (plstack);
}
PLinkStack pop_all(PLinkList pllist,PLinkStack plstack)
{PNode p;
if(!isOperator(pllist->top->info)&&pllist->top->info!='_') push_underline(pllist);
while(plstack->top!=plstack->base){
p=plstack->base;
while(p->link!=plstack->top) p=p->link;
Create_LinkListNode(pllist);
pllist->top->info=plstack->top->info;
plstack->top=plstack->top->link;
plstack->top=p;
}
return (plstack);
}
int isOperator(DataType i)
{
if(i=='+' || i=='-' || i=='*' || i=='/' || i=='(' || i==')') return (1);
else return (0);
}
int isPoint_included(PNode p)
{
PNode q,temp;
q=p;
while(q->link->info!='_'){
if(q->info=='.') temp=q;
q=q->link;
}
if(temp->info=='.') return (1);
else return (0);
}
int Calculator_underline(PNode p)
{
PNode q;
int i;
i=0;
q=p;
while(!isOperator(q->link->info)){
if(q->link->info=='_') i++;
q=q->link;
}
return (i);
}
NumberType Calculator(PLinkList pllist)
{
PNode p,q,temp,temp1,swap;
int i,j;
NumberType t,t1,t2,tmark1,tmark2,m;
t=t1=t2=0;
q=p=pllist->base;
while(!isOperator(p->link->info)) p=p->link;/* find p */
while(p!=pllist->top){
/**************************************************************/
/**************************************************************/
if(!isOperator(p->info)&&p->link==pllist->top){
if(q==pllist->base){ /* find q */
while(q!=p){
if(q->info=='_') temp=q;
q=q->link;
}
q=temp;
}
temp1=temp=pllist->base;
while(temp!=q){ /* find temp */
if(temp->info=='_') temp1=temp;
temp=temp->link;
}
temp=temp1; temp1=pllist->base;
/*////////// Calculate function /////////////////////*/
swap=q; i=0; /* calculate t1 */
if(isPoint_included(q)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=q;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t1=t1+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(q)){ /*doesn't include point*/
while(swap->link->info!='_') {swap=swap->link; i++;}
swap=q;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
}
swap=temp; t2=0;i=0; /* calculate t2 */
if(isPoint_included(temp)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0||i==-1) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)(swap->link->info-'0');
while(j--!=0) m=m*1/10;
t2=t2+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0||i==-1) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
}
if(p->link->info=='+') t=t1+t2;
else if(p->link->info=='-') t=t2-t1;
else if(p->link->info=='*') t=t1*t2;
else if(p->link->info=='/') t=t2/t1;
/*///// Deal with the left //////////////////*/
q=temp;
p=p->link;
}
/*******************************************************************/
/*******************************************************************/
else if(!isOperator(p->info)&&isOperator(p->link->link->info)){
if(q==pllist->base){ /* find q */
while(q!=p){
if(q->info=='_') temp=q;
q=q->link;
}
q=temp;
}
temp1=temp=pllist->base;
while(temp!=q){ /* find temp */
if(temp->info=='_') temp1=temp;
temp=temp->link;
}
temp=temp1; temp1=pllist->base;
/*////////// Calculate function /////////////////////*/
swap=q; i=0; /* calculate t1 */
if(isPoint_included(q)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=q;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t1=t1+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(q)){ /*doesn't include point*/
while(swap->link->info!='_') {swap=swap->link; i++;}
swap=q;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
}
swap=temp; i=0; /* calculate t2 */
if(isPoint_included(temp)){
/*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t2=t2+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
}
if(p->link->info=='+') t=t1+t2;
else if(p->link->info=='-') t=t1-t2;
else if(p->link->info=='*') t=t1*t2;
else if(p->link->info=='/') t=t2/t1;
/*///// Deal with the left //////////////////*/
q=temp;
p=p->link;
}
/***********************************************************************************/
/***********************************************************************************/
else if(!isOperator(p->info)&&!isOperator(p->link->link->info)&&Calculator_underline(p->link)==1){
if(q==pllist->base){ /* find q */
while(q!=p){
if(q->info=='_') temp=q;
q=q->link;
}
q=temp;
}
temp1=temp=pllist->base;
while(temp!=q){ /* find temp */
if(temp->info=='_') temp1=temp;
temp=temp->link;
}
temp=temp1; temp1=pllist->base;
/*////////// Calculate function /////////////////////*/
swap=q; i=0; /* calculate t1 */
if(isPoint_included(q)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=q;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t1=t1+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(q)){ /*doesn't include point*/
while(swap->link->info!='_') {swap=swap->link; i++;}
swap=q;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
}
swap=temp; i=0; /* calculate t2 */
if(isPoint_included(temp)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t2=t2+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
}
if(p->link->info=='+') t=t1+t2;
else if(p->link->info=='-') t=t1-t2;
else if(p->link->info=='*') t=t1*t2;
else if(p->link->info=='/') t=t2/t1;
/*///// Deal with the left //////////////////*/
q=temp;
temp=p=p->link;
/***note*** Dealt withe the second part*/
while(!isOperator(p->link->info)) p=p->link;
swap=temp; t1=0;i=0; /* calculate t1 */
if(isPoint_included(temp)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t1=t1+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
}
if(p->link->info=='+') t=t+t1;
else if(p->link->info=='-') t=t-t1;
else if(p->link->info=='*') t=t*t1;
else if(p->link->info=='/') t=t/t1;
/*///// Deal with the left //////////////////*/
p=p->link;
}
/************************************************************/
/************************************************************/
else if(isOperator(p->info)&&isOperator(p->link->info)){
temp=temp1=pllist->base; t2=0;i=0; /*find temp */
while(temp!=q){
if(temp->info=='_') temp1=temp;
temp=temp->link;
}
temp=temp1;
swap=temp;
if(isPoint_included(temp)){
while(swap->link->info!='.') {swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t2=t2+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0||i==-1) {t2=t2
+(NumberType)swap->link->info-'0';swap=swap->link;}
}
/*calculate t*/
if(p->link->info=='+') t=t+t2;
else if(p->link->info=='-') t=t-t2;
else if(p->link->info=='*') t=t*t2;
else if(p->link->info=='/') t=t2/t;
/* dealt with the left */
temp=swap=pllist->base;
while(swap!=q){
if(swap->info=='_') temp=swap;
swap=swap->link;
}
q=temp;
p=p->link;
}
/*****************************************************************/
/*****************************************************************/
else if(isOperator(p->info) && !isOperator(p->link->info)&&Calculator_underline(p->link)==1){
temp=p;
while(!isOperator(p->link->info)) p=p->link;
swap=temp;
t1=t2=0;i=0;
if(isPoint_included(temp)){
while(swap->link->info!='.') {swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0||i==-1) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t1=t1+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
}
/*calculate t*/
if(p->link->info=='+') t=t+t1;
else if(p->link->info=='-') t=t-t1;
else if(p->link->info=='*') t=t*t1;
else if(p->link->info=='/') t=t/t1;
/* dealt with the left */
temp=swap=pllist->base;
while(swap!=q){
if(swap->info=='_') temp=swap;
swap=swap->link;
}
q=temp;
p=p->link;
}
/*******************************************************************/
/********************************************************************/
else if(isOperator(p->info)&&!isOperator(p->link->info)&&Calculator_underline(p->link)==2){
PNode ID;
temp1=temp=pllist->base;
while(temp!=q){ /* find temp */
if(temp->info=='_') temp1=temp;
temp=temp->link;
}
ID=temp=temp1;
temp1=q=p;
while(!isOperator(p->link->info)) p=p->link;
while(q->link!=p){
if(q->link->info=='_') temp=q->link;
q=q->link;
}
q=temp;
temp=temp1;
swap=q; t1=0;i=0; /* calculate t1 */
if(isPoint_included(q)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=q;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0 ||i==-1) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t1=t1+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(q)){ /*doesn't include point*/
while(swap->link->info!='_') {swap=swap->link; i++;}
swap=q;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0 || i==-1) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
}
swap=temp; t2=0;i=0; /* calculate t2 */
if(isPoint_included(temp)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0 ||i==-1) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t2=t2+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0||i==-1) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
}
if(p->link->info=='+') tmark1=t1+t2;
else if(p->link->info=='-') tmark1=t1-t2;
else if(p->link->info=='*') tmark1=t1*t2;
else if(p->link->info=='/') tmark1=t2/t1;
p=p->link;
if(p->link->info=='+') t=t+tmark1;
else if(p->link->info=='-') t=t-tmark1;
else if(p->link->info=='*') t=t*tmark1;
else if(p->link->info=='/') t=t/tmark1;
p=p->link;
q=ID;
}
/********************************************************************/
/*********************************************************************/
else if(isOperator(p->info)&&!isOperator(p->link->info)&&Calculator_underline(p->link)==1){
temp1=p;
while(!isOperator(p->link->info)) p=p->link;
swap=temp1; t1=0;i=0; /* calculate t1 */
if(isPoint_included(temp)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t1=t1+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp1;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
}
if(p->link->info=='+') t=t+t1;
else if(p->link->info=='-') t=t-t1;
else if(p->link->info=='*') t=t*t1;
else if(p->link->info=='/') t=t/t1;
p=p->link;
}
/**
****************************************************************/
/********************************************************************/
else if(!isOperator(p->info) && !isOperator(p->link->link->info)&&Calculator_underline(p->link)==2){
PNode ID;
if(q==pllist->base){ /* find q */
while(q!=p){
if(q->info=='_') temp=q;
q=q->link;
}
q=temp;
}
swap=temp=pllist->base; /*find temp */
while(temp->link!=q){
if(temp->link->info=='_') swap=temp;
temp=temp->link;
}
ID=temp=swap;
swap=q;
t1=t2=0;i=0;
if(isPoint_included(q)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}/*calculate t1*/
swap=q;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t1=t1+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(q)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=q;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
}
swap=temp;
if(isPoint_included(temp)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t2=t2+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='_'&&--i!=0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
}
/*calculate t*/
if(p->link->info=='+') tmark1=t1+t2;
else if(p->link->info=='-') tmark1=t1-t2;
else if(p->link->info=='*') tmark1=t1*t2;
else if(p->link->info=='/') tmark1=t2/t1;
temp1=p=p->link;
/*//////////////////////dealt with the second part/////////*/
t1=t2=0;i=0;
while(!isOperator(p->link->info)) p=p->link;
while(q->link!=p){ /*find p*/
if(q->link->info=='_') temp=q->link;
q=q->link;
}
q=temp;
temp=temp1;
if(isPoint_included(q)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=q;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0) {t1=t1+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t1=t1+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(q)){
swap=q;
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=q;
while(swap->link->info!='_'&&--i>0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t1=t1+m;
swap=swap->link;
}
if(i==0||i==-1) {t1=t1+(NumberType)swap->link->info-'0';swap=swap->link;}
}
swap=temp;
if(isPoint_included(temp)){ /*include point */
while(swap->link->info!='.'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='.'&&--i!=0){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0) {t2=t2+(NumberType)swap->link->info-'0'; swap=swap->link->link;}
i=1;
while(swap->link->info!='_'){
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*1/10;
t2=t2+m;
swap=swap->link;
i++;
}
}
else if(!isPoint_included(temp)){
while(swap->link->info!='_'){swap=swap->link;i++;}
swap=temp;
while(swap->link->info!='_'&&--i>0) {
j=i;
m=(NumberType)swap->link->info-'0';
while(j--!=0) m=m*10;
t2=t2+m;
swap=swap->link;
}
if(i==0||i==-1) {t2=t2+(NumberType)swap->link->info-'0';swap=swap->link;}
}
/*calculate tmark2*/
if(p->link->info=='+') tmark2=t1+t2;
else if(p->link->info=='-') tmark2=t1-t2;
else if(p->link->info=='*') tmark2=t1*t2;
else if(p->link->info=='/') tmark2=t2/t1;
p=p->link;
/*calculate t*/
if(p->link->info=='+') t=tmark1+tmark2;
else if(p->link->info=='-') t=tmark1-tmark2;
else if(p->link->info=='*') t=tmark1*tmark2;
else if(p->link->info=='/') t=tmark1/tmark2;
/*dealt with the left*/
p=p->link;
q=ID;
}
}
printf("The result is %f\n",t);
return(t);
}
void main()
{
char s[100];
int i;
PLinkList pllist;
PLinkStack plstack;
pllist=Create_LinkListPointer();
plstack=Create_LinkStackPointer();
Create_LinkList(pllist);
Create_LinkStack(plstack);
printf("Input the operator:\n");
scanf("%s",&s);
for(i=0;i<strlen(s);i++){
if(!isOperator(s[i])) push_LinkList(pllist,s[i]);
else if(isOperator(s[i]) && plstack->top==plstack->base) {
if(!isOperator(pllist->top->info)) push_underline(pllist);
push_stack(plstack,s[i]);
}
else {
if(s[i]=='(') push_stack(plstack,s[i]);
else if(plstack->top->info=='('){
push_underline(pllist);
push_stack(plstack,s[i]);
}
/*//////////////////////////////////////////////////////////////////////////////////*/
else if((s[i]=='+' || s[i]=='-') && (plstack->top->info=='*' || plstack->top->info=='/') && bracket_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_stack_until_bracket(pllist,plstack);
push_stack(plstack,s[i]);
}
else if((s[
i]=='+' || s[i]=='-') && (plstack->top->info=='*' || plstack->top->info=='/') && !bracket_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_all(pllist,plstack);
push_stack(plstack,s[i]);
}
/*////////////////////////////////////////////////////////////////////////////////////////////////////*/
else if((s[i]=='*' || s[i]=='/')&&(plstack->top->info=='+'|| plstack->top->info=='-')){
if(!isOperator(pllist->top->info)) push_underline(pllist);
push_stack(plstack,s[i]);
}
/*////////////////////////////////////////////////////////////////////////////////////////////////////*/
else if((s[i]=='+' && plstack->top->info=='+') || (s[i]=='-' && plstack->top->info=='-')){
if(!isOperator(pllist->top->info)) push_underline(pllist);
push_stack(plstack,s[i]);
}
/*///////////////////////////////////////////////////////////////////////////////////*/
else if(((s[i]=='+' && plstack->top->info=='-') || (s[i]=='-' && plstack->top->info=='+')) && bracket_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_stack_until_bracket(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='+' && plstack->top->info=='-') || (s[i]=='-' && plstack->top->info=='+')) && !bracket_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_all(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='+' && plstack->top->info=='+') || (s[i]=='-' && plstack->top->info=='-')) && bracket_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_stack_until_bracket(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='+' && plstack->top->info=='+') || (s[i]=='-' && plstack->top->info=='-')) && !bracket_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_all(pllist,plstack);
push_stack(plstack,s[i]);
}
/*///////////////////////////////////////////////////////////////////////////////*/
else if(((s[i]=='*'&& plstack->top->info=='/')||(s[i]=='/'&& plstack->top->info=='*'))&&bracket_included(plstack)&&bracket_included_and_lower(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_stack_until_bracket_lower(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='*' && plstack->top->info=='/') || (s[i]=='/' && plstack->top->info=='*')) && bracket_included(plstack) && !bracket_included_and_lower(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_stack_until_bracket(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='*' && plstack->top->info =='/') ||(s[i]=='/' && plstack->top->info=='*')) && !bracket_included(plstack) && islower_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_until_lower(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='*' && plstack->top->info=='/') ||(s[i]=='/' && plstack->top->info=='*')) && !bracket_included(plstack) && !islower_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_all(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='*'&& plstack->top->info=='*')||(s[i]=='/'&& plstack->top->info=='/'))&&bracket_included(plstack)&&bracket_included_and_lower(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_stack_until_bracket_lower(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='*' && plstack->top->info=='*') || (s[i]=='/' && plstack->top->info=='/'))&& bracket_included(plstack) && !bracket_included_and_lower(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_stack_until_bracket(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='*' && plstack->top->info =='*') ||(s[i]=='/' && plstack->top->info=='/'))&& !bracket_included(plstack) && islower_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_until_lower(pllist,plstack);
push_stack(plstack,s[i]);
}
else if(((s[i]=='*' && plstack->top->info=='*') ||(s[i]=='/' && plstack->top->info=='/'))&& !bracket_included(plstack) && !islower_included(plstack)){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_all(pllist,plstack);
push_stack(plstack,s[i]);
}
/*////////////////////////////////////////////////////////////////////////////////////*/
else if(s[i]==')'){
if(!isOperator(pllist->top->info)) push_underline(pllist);
pop_stack_until_bracket(pllist,plstack);
pop_one(plstack);
}
}
}
pop_all(pllist,plstack);
print_llist(pllist);
Calculator(pllist);
getch();
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |