4

Python: 3/4/5 や 32432/23423/2354325 や 3425*343/254235 や 43252+34254-2435 などの式を取るプログラム (クラス課題) に取り組んでいます。 +、-、/、* のすべての演算子)。式を解いていきます。

私は評価を使用できません!!

高レベルのコードは使用できません。せいぜい、以下の Web サイトの文字列マニピュレーターを使用して文字列を分割することに制限されています。

http://docs.python.org/2/library/stdtypes.html#typesseq

私の方法は、ユーザーが入力した式を見て、検索関数を使用して演算子を見つけ、これらの演算子とスライス関数 (例: s[0:x]) を使用することです。私が持っているものは以下のとおりですが、残念ながら機能していません: *印刷ステートメントはデバッグ目的でのみ含まれていることに注意してください。編集: プログラムを実行して式を入力すると x が定義されないのはなぜですか?

z= (input("expression:")).strip()

def finding(z):
    if "/" in z:
        x=z.find("/")
        print("hi1")
    elif "*" in z:
        x=z.find("*")
        print("hi2")
    elif "+" in z:
        x=z.find("+")
        print("hi3")
    elif "-" in z:
        x=z.find("-")
        print("hi4")
    else:
        print("error, not math expression")
    return x

def Parsing(z,x):

    x= finding(z)
    qw=z.s[0:x]
    print (qw)
# take the x-value from function finding(z) and use it to split 

finding(z)
Parsing(z,x)
4

3 に答える 3

3

入力をその部分に分割するのに問題がある場合は、ここに役立つものがあります。少なくともそれが何をするのかを理解できるように、できるだけ読みやすくしました。あなたが私に必要な場合、私はそれの任意の部分を説明します:

def parse(text):
    chunks = ['']

    for character in text:
        if character.isdigit():
            if chunks[-1].isdigit():   # If the last chunk is already a number
                chunks[-1] += character  # Add onto that number
            else:
                chunks.append(character) # Start a new number chunk
        elif character in '+-/*':
            chunks.append(character)  # This doesn't account for `1 ++ 2`.

    return chunks[1:]

使用例:

>>> parse('123 + 123')
['123', '+', '123']
>>> parse('123 + 123 / 123 + 123')
['123', '+', '123', '/', '123', '+', '123']

残りはあなたにお任せします。の使用が許可されていない場合は.isdigit()、それを低レベルのPythonコードに置き換える必要があります。

于 2012-10-30T05:43:38.410 に答える
2

これを行う最も簡単な方法は、Shunting-yard アルゴリズムを実装して方程式を後置表記に変換し、それを左から右に実行することだと思います。

しかし、これはクラスの課題なので、実際の実装は自分で行う必要があります。必要以上のものを既に提供しています。

于 2012-10-30T05:09:29.103 に答える
0

プログラムを実行して式を入力すると、xが定義されないのはなぜですか?

x範囲内ではなく、メソッドで定義するだけで、他の場所からアクセスしようとします。

z= (input("expression:")).strip()

def finding(z):
    # ... removed your code ...
    # in this method, you define x, which is local
    # to the method, nothing outside this method has
    # access to x
    return x

def Parsing(z,x):

    x= finding(z) # this is a different x that is assigned the 
                  # return value from the 'finding' method.
    qw=z.s[0:x] # I'm curious as to what is going on here.
    print (qw)
# take the x-value from function finding(z) and use it to split 

finding(z) # here, z is the value from the top of your code
Parsing(z,x) # here, x is not defined, which is where you get your error.

Parsingはすでにの値を取得するために呼び出しているのでfinding、値をxに渡す必要はありません。また、値をどこにも保存しないため、外部Parsingに呼び出す必要もありません。finding(z)Parsing

def Parsing(z):

    x= finding(z) # this is a different x that is assigned the 
                  # return value from the 'finding' method.
    qw=z.s[0:x] # I'm curious as to what is going on here.
    print (qw)
# take the x-value from function finding(z) and use it to split 

# finding(z)  -- not needed 
Parsing(z)
于 2012-10-30T05:44:05.460 に答える