V
V
Vanya Ivanov2014-03-18 20:23:42
Objective-C
Vanya Ivanov, 2014-03-18 20:23:42

How to make a string parser?

Square root calculator.
The problem is that I can't do "square root" because you have to enter x first and then y. And if I just describe it as x = sqrt(x), then you still need to enter both x and y. I can't figure out what's wrong.

- (IBAction)operation:(id)sender {
    
     if (yFlag && !enterFlag) {
        
        switch (operation) {
                
            case plusOperation:
                x = y + x;
                break;
                
            case minusOperation:
                x = y - x;
                break;
                
            case multiplyOperation:
                x = y * x;
                break;
                
            case divisionOperation:
                x = y / x;
                break;
         
            case degreeOperation:
                x = pow(y, x);
                break;
                
    // вот тут проблема
                
            case radicalOperation:
                x = sqrt(x);
                break;
                
            default:
                break;
        }
    }
    
    
    y = x;
    
    enterFlag = YES;
    yFlag = YES;
    operation = [sender tag];
    
    [self calcScreen];
}

I was advised to make a string parser, but I don't know how to do it since I'm just learning. Help to implement, please. I want that when you enter a number, then when you click on the "square root", the result is displayed immediately, and does not require you to enter both x and y.
Thanks a lot for your help! I would be grateful if someone could help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2014-03-18
Protko @Fesor

In the context of a calculator, you need to parse the string into reverse polish .
Examples:
www.interface.ru/home.asp?artId=1492
ru.wikiversity.org/wiki/%D0%9E%D0%B1%D1%80%D0%B0%D...
habrahabr.ru/post/ 50158

M
MagoVinch, 2014-03-18
@MagoVinch

Try this itw66.ru/blog/obj_c/539.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question