2016-04-09 9 views
-2

Ich möchte eine Befehlszeilenanwendung in C/Objective C implementieren, die als ein Rechner von mehr als zwei Zahlen dienen wird.Befehlszeilenrechner in Objective C

Z. B ./calc 5 + 4 * 6 = 29

Ich brauche nur eine Idee oder eine einfachen Algorithmus zu starten. Ich werde jede Hilfe zu diesem Thema schätzen.

Antwort

2

Der Algorithmus, den Sie möchten, ist die Infixnotation zum Postfix-Notationskonverter. Sie können mehr Informationen darüber hier finden.

http://scriptasylum.com/tutorials/infix_postfix/algorithms/infix-postfix/index.htm.

EDIT: Ich bin mir nicht sicher, ob dies helfen wird, aber hier ist eine Implementierung in Java. Ich bin nicht vertraut mit Objective-C

// converts a infix string to postfix string 
private void convertInfixToPostfix(){ 
    // create an empty operand stack 
    operatorStack = new Stack<>(); 
    Operator operator = null; 
    Operand operand = null; 
    for(int i = 0; i < expressionTokens.size(); i++){ 
     String token = expressionTokens.get(i); 
     Element element = new Element(token); 

     if(element.isOperand(token)){ // check if element is operand 
      // add the element to the postfix string 
      operand = new Operand(element.getStringValue()); 
      postFixString.add(operand); 
     } 
     else if(operatorStack.isEmpty()){ 
      // push the token to the operator stack, its an operator 
      operator = new Operator(element.getStringValue()); 
      operatorStack.push(operator); 
     } 
     else { 
      operator = new Operator(element.getStringValue()); 
      while(!operatorStack.isEmpty() && 
        (operatorStack.peek().getPrecedence() 
        <= operator.getPrecedence()))  
       postFixString.add(operatorStack.pop()); 

      operatorStack.push(operator); 
     } 
    } 

    // add the rest of the operator stack to the postfix string 
    while(!operatorStack.isEmpty()){ 
     Operator remainingOperator = operatorStack.pop(); 
     postFixString.add(remainingOperator); 
    } 
} 
+0

Danke. Ich habe die Idee :) –