楼主: wwwyhx
跳转到指定楼层
上一主题 下一主题
收起左侧

Facebook(1) : 简单的计算程序

 
🔗
darksteel 2011-4-21 14:17:02 | 只看该作者
全局:
回复 9# Etrnls
哦,我这GCC4.4还有点问题,不过我大概明白意思了,非常有启发性!
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-4-21 14:26:25 | 只看该作者
全局:
回复 5# vvnesaa


    表示看不懂
回复

使用道具 举报

🔗
diamrem 2011-4-21 17:23:21 | 只看该作者
全局:
本帖最后由 diamrem 于 2011-4-21 17:24 编辑

我不会C++,C也只是懂皮毛
于是拿python写了一个,可以处理小数的,空格应该也考虑了。

话说,这里的code模块是不是不支持Python
#!/usr/bin/python
# encoding:UTF-8


add_sub = ['-','+']
mul_div = ['*','/']


def parse(input, pattern):
    '''
        parse the input string to a raw list
        every elemtns in the list is either an operand or operator
        treat add/substruct and multiply/division differently
        the operand of add/sub could be a expression of mult/div
    '''
    raw_list = []
    digits = ''
    index = 0
    for c in input.strip():
        index += 1
        if c == ' ':
            #check if space
            continue
        elif c not in pattern:
            #if not a operator store it
            digits += c
            if index == len(input):
                raw_list.append(digits)
        elif c in pattern:
            #if find a operator, append the formal found digits
            #to the output list
            raw_list.append(digits)
            raw_list.append(c)
            digits = ''
    return raw_list


def cal_unit(expression):
    oper1, sign, oper2 = expression
    oper1 = check_if_mult_div(oper1)
    oper2 = check_if_mult_div(oper2)
    if sign == '+':
        result = oper1+oper2
    if sign == '-':
        result = oper1-oper2
    if sign == '/':
        result = oper1*1.0/oper2
    if sign == '*':
        result = oper1*oper2
    return str(result)


def check_if_mult_div(operand):
    if operand.find('/') == -1 and operand.find('*') == -1:
        return float(operand.strip())
    else:
        #if the operand contains * or /, calculate it
        return float(cal_all(parse(operand, mul_div)))




def cal_all(list):
    #user recursion to calculate the whole list
    if len(list) == 3:
        return cal_unit(list)
    else:
        return cal_all([cal_unit(list[:3])]+list[3:])


if __name__=='__main__':
    a = raw_input('Enter:')
    print cal_all(parse(a, add_sub))
回复

使用道具 举报

🔗
tiapha 2011-7-5 01:32:12 | 只看该作者
全局:
对4#的代码有疑问,求详解。
double calc_mul()函数中的这句话c=='*'?ret*calc_mul():ret/calc_mul();
请问这样的话10/2*3是不是就成了10/(2*3)?
但是乘除间没有结合率,这样的结果是不是不对了?
回复

使用道具 举报

🔗
 楼主| wwwyhx 2011-7-6 00:03:58 | 只看该作者
全局:
对4#的代码有疑问,求详解。
double calc_mul()函数中的这句话c=='*'?ret*calc_mul():ret/calc_mul();
请问这样的话10/2*3是不是就成了10/(2*3)?
但是乘除间没有结合率,这样的结果是不是不对了?
tiapha 发表于 2011-7-5 01:32



  恩,6楼这个地方是有问题,他可能copy的时候搞错了,你参考3楼的代码
回复

使用道具 举报

🔗
tiapha 2011-7-6 18:22:43 | 只看该作者
全局:
3楼的代码没法处理括号问题~
10/(2*3) 它算不了,所以他弄出6楼的代码了,但是结合率又不满足了
回复

使用道具 举报

🔗
darksteel 2011-7-12 16:39:36 | 只看该作者
全局:
回复 17# tiapha
好久没上了。。你说的问题确实存在,加入新特性的时候确实要非常注意不破坏现有的逻辑。可以考虑这样更改:
  1. double calc_mul(double a, int op)
  2. {
  3.         double ret = 1;
  4.         c=get();
  5.         if(c=='(') {
  6.                 ret = calc_add();
  7.                 c=get();
  8.         } else {
  9.                 cin.putback(c);
  10.                 ret = num();
  11.         }
  12.         ret = (op ? a*ret : a/ret);
  13.         while(c=='*'||c=='/')
  14.                 ret = c=='*'?calc_mul(ret, 1):calc_mul(ret, 0);
  15.         return ret;
  16. }
复制代码

通过加参数解决结合性的问题。严谨点考虑其实加法表达式也应该按左结合处理,不过不影响结果就不改了。
回复

使用道具 举报

🔗
tiapha 2011-7-14 22:32:14 | 只看该作者
全局:
回复 18# darksteel


    想得好巧妙~   多谢!
回复

使用道具 举报

🔗
 楼主| wwwyhx 2012-4-11 05:04:03 | 只看该作者
全局:
代码如下:
支持正负浮点数,没有错误检查。
darksteel 发表于 2011-4-21 13:08



    今天又做了这题, 发现其实个解有问题, 问题就在于他是右结合不是左结合.
比如2*3/4, 如果数据类型是int来算的话答案就是先算3/4 ==0, 然后2*0 == 0
左结合是先算2*3 == 6, 然后6/4 == 1
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表