数据结构c语言实现表达式求值

表达式求值头文件

#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 ;
}


 说明:这个课题全部原创,花费了不少心血,已经上传到资源上。所有的函数在资源中均实现,并且可以包含括号和负号的计算表达式的加减乘除运算。如果感兴趣,可以可以去下载我的资源,共同探讨,顺便给点积分,谢谢合作!

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值