表达式求值头文件
#ifndef HEADEXP_H_INCLUDED
#define HEADEXP_H_INCLUDED#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20
int GetNumFromStr( char str[] , int pos , float* num ) ;
int GetOperateFromStr( char str[] , int pos , char* opert ) ;
int IsOperate( char c ) ;
char PriorityOfOperation( char c1 , char c2 ) ;
float Calculate( float a , char theta , float b ) ;
float EvaluateExpression( char str[] ) ;
int InputExpression( char str[] ) ;
#endif // HEADEXP_H_INCLUDED
栈的头文件
#ifndef HEAD_H_INCLUDED
#define HEAD_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 10
#define STACK_INCR_SIZE 3
typedef char ElemType ;
typedef struct
{
ElemType* sbase ;
ElemType* stop ;
int stacksize ;
} STACK ;
int InitStack( STACK* s ) ;
int DestroyStack( STACK* s ) ;
int IsEmpty( STACK* s ) ;
int ClearStack(STACK* s ) ;
int GetTop( STACK* s , ElemType* e ) ;
int GetLength( STACK* s ) ;
int Push( STACK* s , ElemType e ) ;
int Pop( STACK* s , ElemType* e ) ;
int TraverseStack( STACK* s ) ;
#endif // HEAD_H_INCLUDED
主函数
#include "head.h"
#include "headexp.h"
int main()
{
//char str[] = "-7+(-8*2*9)+28/4-7+1+3*10-7*8 =" ;
//char str[] = "8*(5+9)=" ;
float result = 0.0 ;
char str[200] = "";
/*float num ;
char opr ;
int position = 0 ;
while( str[position] != '\0' )
{
if( IsOperate( str[position] ) )
{
position = GetOperateFromStr( str , position , &opr ) ;
printf( "%c\n" , opr ) ;
}
else
{
position = GetNumFromStr( str , position , &num ) ;
printf("%.2f\n" , num ) ;
}
}*/
printf( "Please input the expression , end the expression with \" = \" , end the program with \"quit\" \n" ) ;
while( 1 )
{
scanf( "%s" , str ) ;
if( !strcmp( str , "quit" ) )
{
break ;
}
result = EvaluateExpression( str ) ;
printf( "%s %.2f\n" , str , result ) ;
}
return 0 ;
}
说明:这个课题全部原创,花费了不少心血,已经上传到资源上。所有的函数在资源中均实现,并且可以包含括号和负号的计算表达式的加减乘除运算。如果感兴趣,可以可以去下载我的资源,共同探讨,顺便给点积分,谢谢合作!