后缀表达式求值中的若干问题。
既用char类型的,又要用float类型的Push(S, x);该怎么编写程序。
还有就是后面的问题应该怎么样去解决。
/*****************************************/
/**** stack.h ****/
/*****************************************/
# include <stdlib.h>
# ifndef NULL
# define NULL ((void*)0);
# endif
typedef char datatype;
typedef struct node{
datatype data;
struct node* next;
} stacknode,* slink;
void Clearstack(slink& top)
{
top=NULL;
}
int Emptystack(slink top)
{
if(top == NULL)
{
return (1);
}
else
{
return (0);
}
}
//..........
datatype Gettop(slink top)
{
return top-> data;
}
//ERROR
void Push(slink& top,datatype x)
{
slink p;
p=(slink)malloc(sizeof(stacknode));
p-> data=x;
p-> next=top;
top=p;
}
datatype Pop(slink& top)
{
datatype x;
slink p;
if(Emptystack(top))
{
return NULL;
}
else
{
x=top-> data;
p=top;
top=top-> next;
free(p);
return (x);
}
}
/*****************************************************/
/**** 表达式求值.cpp ****/
/*****************************************************/
# include "stack.h "
# include <iostream.h>
# include <string.h>
# include <ctype.h>
# include <stdlib.h>
# include <stdio.h>
# define TRUE 1;
# define FALSE 0;
# define OK 1;
# define ERROR 0;
typedef int Status;
//比较调用函数
Status Precede(char x,char y)
//x> =y;则此函数返回TRUE;否则返回FALSE;
{
if(x== '+ '||x== '- '&&y== '* '||y== '/ ')
{
return FALSE;
}
else
{
return TRUE;
}
}
void mid_post(char E[], char B[])
{
int i=0,j=0;
char x;
slink top;
Push(top, '# ');
do
{
x= E[i++];
if(x== '# ')
{
while(!Emptystack(top))
{
B[j++]=Pop(top);
}
}
else
{
if(isdigit(x))
{
B[j++]=x;
}
else
if(x== ') ')
{
while(Gettop(top)!= '( ')
{
B[j++]=Pop(top);
}
Pop(top);
}
else
{
while(Precede(Gettop(top),x))
{
B[j++]=Pop(top);
}
Push(top,x);
}
}
}while(x!= '# ');
}
//后缀表达式求值的算法
void postcount(char B[])
{
int i=0;
float x, y, z, a, b;
slink top;
Clearstack(top);
while(B[i]!= '# ')
{
if(isdigit(B[i]))
{
x=B[i]- '0 ';
}
Push(top,x);
}
else
{
b= Pop(top); a=Pop(top);
switch(B[i])
{
case '+ ':
z=a+b; Push(top,z);break;
case '- ':
z=a-b; Push(top,z);break;
case '* ':
z=a*b; Push(top,z);break;
case '/ ':
z=a/b; Push(top,z);break;
default:
cout < < "ERROR! " < <endl;
break;
}
i++;
}
if(!Emptystack(top))
{
printf( "result=%f ",Gettop(top));
}
}
void main()
{
char a[]= "5+(4-2)*3# ";
char b[20];
mid_post( a, b);
postcount(b);
}
--------------------Configuration: 表达式求值 - Win32 Debug--------------------
Compiling...
表达式求值.cpp
C:\Documents and Settings\HWX\桌面\表达式求值.cpp(87) : warning C4244: '= ' : conversion from 'int ' to 'float ', possible loss of data
C:\Documents and Settings\HWX\桌面\表达式求值.cpp(89) : warning C4244: 'argument ' : conversion from 'float ' to 'char ', possible loss of data
C:\Documents and Settings\HWX\桌面\表达式求值.cpp(91) : error C2181: illegal else without matching if
C:\Documents and Settings\HWX\桌面\表达式求值.cpp(97) : warning C4244: 'argument ' : conversion from 'float ' to 'char ', possible loss of data
C:\Documents and Settings\HWX\桌面\表达式求值.cpp(99) : warning C4244: 'argument ' : conversion from 'float ' to 'char ', possible loss of data
C:\Documents and Settings\HWX\桌面\表达式求值.cpp(101) : warning C4244: 'argument ' : conversion from 'float ' to 'char ', possible loss of data
C:\Documents and Settings\HWX\桌面\表达式求值.cpp(103) : warning C4244: 'argument ' : conversion from 'float ' to 'char ', possible loss of data
Error executing cl.exe.
表达式求值.exe - 1 error(s), 6 warning(s)
*************************************************************************
*************************************************************************
请给出出现的问题情况,并给出解决问题的办法,谢谢。
[解决办法]
error C2181: illegal else without matching if
...
错误说明了啊
自己改撒