| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1791 人关注过本帖
标题:求大神!!表达式顺序求值
只看楼主 加入收藏
MikeBayern
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-10-28
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
求大神!!表达式顺序求值
表达式求值(Expression Evaluation) 编写程序对表达式求值。

Enter an expression: 1+2.53
Value of expression: 10.5


表达式中的操作数是浮点数,运算符是+、-、和/。表达式从左向右求值(所有运算符的优先级都⼀样)。


输出范例:


Enter an expression: 2+3*4-5/2
Value of expression: 7.5
搜索更多相关主题的帖子: 表达式 Value expression 顺序 求值 
2019-10-28 15:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:10 
“1+2.53” 应该是 “1+2.5*3” 吧
“运算符是+、-、和/” 应该是 “运算符是+、-、*和/” 吧
贴完了题目后看都不看就拍拍屁股走人,可以理解,反正大学都是混日子的,等找工作时再痛哭流涕不迟
但,但怎么“2+3*4-5/2”却写对了?!这就不能忍了。

二战时,德军每逢周一、三、五便来布雷,而英军每逢二、四、六就去扫雷。这样持续了很长时间。直到有一天,英军忘了例行扫雷作业。第二天来布雷的德军,竟被自己前天布下的水雷炸沉了。

收到的鲜花
  • bcbbcclbbc2019-10-29 23:34 送鲜花  5朵  
2019-10-28 16:31
纯蓝之刃
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:76
帖 子:566
专家分:3690
注 册:2019-7-29
收藏
得分:10 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

struct type
{
    float num1;
    float num2;
    int sign;
};

int main()
{
    char str[60],tmp;
    struct type sub={0};
    int i=0,flag=0,flag_dot=0;

    printf("请输入表达式:");
    gets(str);
    while(1)
    {
        strncpy(&tmp,str+i,1);
        if(tmp=='\0')break;
        i++;
        switch(tmp)
        {
        case '+':
            {
                if(flag==1)
                {
                    switch(sub.sign)
                    {
                    case 1:{sub.num2=sub.num2+sub.num1;break;}
                    case 2:{sub.num2=sub.num2-sub.num1; break;}
                    case 3:{sub.num2=sub.num2*sub.num1;break;}
                    case 4:{sub.num2=sub.num2/sub.num1;break;}
                    default:{printf("输入错误!\n");exit(1);}
                    }

                }
                else
                {
                    flag=1;
                    sub.num2=sub.num1;
                }
                sub.num1=0;
                sub.sign=1;
                flag_dot=0;
                break;
            }
        case '-':
            {
                if(flag==1)
                {
                    switch(sub.sign)
                    {
                    case 1:{sub.num2=sub.num2+sub.num1;break;}
                    case 2:{sub.num2=sub.num2-sub.num1; break;}
                    case 3:{sub.num2=sub.num2*sub.num1;break;}
                    case 4:{sub.num2=sub.num2/sub.num1;break;}
                    default:{printf("输入错误!\n");exit(1);}
                    }

                }
                else
                {
                    flag=1;
                    sub.num2=sub.num1;
                }
                sub.num1=0;
                sub.sign=2;
                flag_dot=0;
                break;
            }
        case '*':
            {
                if(flag==1)
                {
                    switch(sub.sign)
                    {
                    case 1:{sub.num2=sub.num2+sub.num1;break;}
                    case 2:{sub.num2=sub.num2-sub.num1; break;}
                    case 3:{sub.num2=sub.num2*sub.num1;break;}
                    case 4:{sub.num2=sub.num2/sub.num1;break;}
                    default:{printf("输入错误!\n");exit(1);}
                    }

                }
                else
                {
                    flag=1;
                    sub.num2=sub.num1;
                }
                sub.num1=0;
                sub.sign=3;
                flag_dot=0;
                break;
            }
        case '/':
            {
                if(flag==1)
                {
                    switch(sub.sign)
                    {
                    case 1:{sub.num2=sub.num2+sub.num1;break;}
                    case 2:{sub.num2=sub.num2-sub.num1; break;}
                    case 3:{sub.num2=sub.num2*sub.num1;break;}
                    case 4:{sub.num2=sub.num2/sub.num1;break;}
                    default:{printf("输入错误!\n");exit(1);}
                    }

                }
                else
                {
                    flag=1;
                    sub.num2=sub.num1;
                }
                sub.num1=0;
                sub.sign=4;
                flag_dot=0;
                break;
            }
        case '.':{flag_dot=1;break;}
        default:
            {
                if(tmp>=48&&tmp<=57)
                {
                    if(flag_dot>0)
                    {
                        sub.num1=sub.num1+(float)(tmp-48)/pow(10,flag_dot);
                        //printf("num1=%.2f,tmp=%d,flag_dot=%d\n",sub.num1,tmp,flag_dot);
                        flag_dot++;
                    }
                    else
                        sub.num1=sub.num1*10+tmp-48;
                    break;
                }
                else
                {
                   printf("输入错误!\n");
                   exit(1);
                }
            }
        }
        //printf("num1=%.2f,num2=%.2f,sign=%d\n",sub.num1,sub.num2,sub.sign);
    };

    switch(sub.sign)
    {
    case 1:{sub.num2=sub.num2+sub.num1;break;}
    case 2:{sub.num2=sub.num2-sub.num1;break;}
    case 3:{sub.num2=sub.num2*sub.num1;break;}
    case 4:{sub.num2=sub.num2/sub.num1;break;}
    default:{printf("输入错误!\n");exit(1);}
    }
    printf("表达式的值为%.4f",sub.num2);

    return 0;
}

一沙一世界,一花一天堂。无限掌中置,刹那成永恒。
2019-10-28 20:26
快速回复:求大神!!表达式顺序求值
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019256 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved