Linux系统下用C语言实现浮点数四则运算表达式的求值

该博客介绍了如何在Linux环境下使用C语言实现浮点数四则运算表达式的求值。程序支持加、减、乘、除运算,接受任意长度的表达式,操作数可以是小数或指数形式,并通过命令行输入。博客提供了源代码文件EvaluateExpression.c和头文件EvaluateExpression.h,实现了浮点数计算的栈结构。
摘要由CSDN通过智能技术生成

Linux系统下用C语言实现浮点数四则运算表达式的求值

 

实现功能:

1. 支持任意长度四则运算表达式,允许的操作符是:"+","-","*","/","(",")",以“#”或“=”结束表达式的输入
2. 操作数是浮点数,可以小数形式或指数形式表示,如123.456和1.23456E2两种表示形式
3. C语言实现,在linux平台上运行,也可以在Cygwin上运行
4. 用命令行方式输入操作符和操作数

5.共两个文件:头文件和源代码文件

 

文件1: EvaluateExpression.c

-----------------------FILE: EvaluateExpression.c BEGIN--------------------------------------------------

/**
* File Name:           EvaluateExpression.c
* Description:         This file implements the function of the basic arithmetic calculations.
*                           All valid/acceptable operations are only addition,subtraction,multiplication,division.
*                           The valid operators are defined as {'+', '-', '*', '/', '(', ')', '#'}.
*                           The '#' sign is defined as the ending symbol of experession input.
*                           And it surpports the float oprand type only now.
*
* Author:                Sean.Wang
* Contact Mail:        Sean.Gn@gmail.com
* Completed Date:  July 05, 2009
**/

#include "EvaluateExpression.h"

int main ( void )
{
        float result = 0.0F;

        printf ( "/nPlease enter an expression to evaluate,and ends with the pound sign:/n" );
        result = ( float ) FloatEvaluateExpression();
        printf ( "Its result:%f/n", result );
        return 0;
}


/**
* Function Name:  FloatEvaluateExpression(void)
* Description: Implementing the float operations of addition,subtraction,multiplication,division.
* Arguments: void
* Returns: float
**/
float FloatEvaluateExpression ( void )
{
        STACK *sOPTR = ( STACK * ) malloc ( sizeof ( STACK ) ); /* sOPTR:Operator Stack*/
        STACK *sOPND = ( STACK * ) malloc ( sizeof ( STACK ) ); /* sOPND:Operand Stack*/
        char c = ' ';
        char lastchar = ' ';
        float a = 0.0F;
        float b = 0.0F;
        char theta = ' ';
        float result = 0.0F;
        float valf = 0.0F; /*the float value of the digital string input from STDIN*/
        char strf[MAXLENGTH] = {'/0'}; /*the valid float string input from STDIN*/
        int idx = 0;
        bool pointAvail = TRUE; /*Decimal Point available*/
        bool expAvail = TRUE; /*Exponent available*/

        InitStack ( sOPTR );
        Push ( sOPTR, '#' );
        lastchar = '#';
        InitStack ( sOPND );
        c = getchar();

        while ( c != '#' || ( ( char ) GetTop ( sOPTR ) ) != '#' )
        {
                while ( isspace ( c ) )
                        c = getchar();

                if ( c == '=' )
                        c = '#';

                idx = 0;
                if ( ( c == '-' || c == '+' ) && ( IsOprator ( lastchar ) ) )
                {
                        strf[idx++] = c;
                        c = getchar();
                        if ( ! ( isdigit ( c ) || c == '.' ) )
                        {
                                printf ( "EXIT_FAILURE: format is invalid or undefined,!/n" );
                                exit ( EXIT_FAILURE );
                        }
                }
                strf[idx] = '/0';

                if ( isdigit ( c ) || c == '.' )
                {
                        lastchar = '0';
                        pointAvail = TRUE; /*Decimal Point available*/
                        expAvail = TRUE; /*Exponent available*/

                        do
                        {
                                if ( c == '.' )
                                {
             

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值