Raised This Month: $51 Target: $400
 12% 

Algorithm


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 12-13-2011 , 12:37   Algorithm
Reply With Quote #1

Hey guys,

I need help with an algorithm im trying to develope, maybe you'll come up with a good one. Try to think and tell me...

how can I analyze a mathematic 'question' that is written in string and make it an answer in integer?

For example, I have:

PHP Code:
public mathExampleQuestion()
{
        new 
question[] = "3 + 3 x 3 / 3 + 3"
        
        
// How do I loop through the string cells and analyze the answer converted to integer? (decimal value)

I'll tell you why its hard? its hard cuz we must follow the math rules, it means '*' and '/' first, only then you can '+' or '-' them.

Got any ideas?

EDIT: By the way, I actually thought of an algorithm but isnt sure if its the best one. Loop through the string cells and solve all the '*' and '/' expressions, then when you get to an answer of the first experssion for example, you copy the answer into a new string, then the next sign must be '+' or '-' cuz its looping till it find '*' or '/' and solves the expressions with it, then stops to solve when it doesnt find it (the '*' or '/'), then after I found the first expression, I add the next expression, of for example '+' or '-'. so for conclusion, solve all the expression with '*' or '/' then post the answers of them into a new string with the signs between, I'll show a code example:

PHP Code:
public example()
{
    new 
question[] = "3 + 3 x 3 / 3 + 3"
    
    
// Loop through the string and solve all the expressions with '*' or '/'
    // after make that a new string with the signs of '+' or '-' between
    // After the operation the new string would look like this:
    
    
new analyzedQuestion[] = "3 + 3 + 3"
    
    
// Then I can easily loop through this string and solve it by checking the sign
    // if its - or +

__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive

Last edited by Diegorkable; 12-13-2011 at 12:49.
Diegorkable is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 12-13-2011 , 19:33   Re: Algorithm
Reply With Quote #2

Solving parts of equation and going back to string makes no sense. You just need to use a stack to temporarily store parts that can not be solved at the moment.

Simple example (no error checking, only positive integers, no brackets):
PHP Code:
public mathExampleQuestion(){
    new 
question[] = "3 + 3 x 3 / 3 + 3"
    
    
new stack[6], top
    
for(new ii<sizeof questioni++){
        switch(
question[i]){
            case 
'0'..'9'stack[top] = stack[top]*10+question[i]-'0'
            
case '+','-','/','*','x':{
                while(
top && (stack[top-1] == '*' || stack[top-1] == 'x' || stack[top-1] == '/' || question[i] == '+' || question[i] == '-')){
                    switch(
stack[top-1]){
                        case 
'+'stack[top-2] += stack[top]
                        case 
'-'stack[top-2] -= stack[top]
                        case 
'/'stack[top-2] /= stack[top]
                        case 
'*','x'stack[top-2] *= stack[top]
                    }
                    
top -= 2                    
                
}                
                
stack[++top] = question[i]            
                
stack[++top] = 0                
            
}
        }
    }
    while(
top>0){
        switch(
stack[top-1]){
            case 
'+'stack[top-2] += stack[top]
            case 
'-'stack[top-2] -= stack[top]
            case 
'/'stack[top-2] /= stack[top]
            case 
'*','x'stack[top-2] *= stack[top]
        }
        
top -= 2
    
}       
    
client_print(0print_chat"%s = %d"questionstack[top])

__________________
Impossible is Nothing
Sylwester is offline
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 12-14-2011 , 14:36   Re: Algorithm
Reply With Quote #3

Damn I like your logic! Took me like 10 minutes to understand it, thought first that the while was in the for so it confused be. Btw a question, I think you dont need to use in the "while (top>0) the case of '*', 'x', '/', cuz in the for all those signs are cancelled and only the '+' or '-' are left, so the array would look like that after analyzing:

stack[] = { 3, '+', 3, '+', 3 }

EDIT: By the way, just so you know, what I suggested is just the same as you. Solving the 'hard to solve' parts, then copying it to a new string. The same I did, I can post the code I created if you want.
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive

Last edited by Diegorkable; 12-14-2011 at 15:14.
Diegorkable is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 12-14-2011 , 19:12   Re: Algorithm
Reply With Quote #4

You didn't understand it. It's not the same as your idea. You said that you want to solve '/' and '*' first, but that's not what I'm doing (see in my algorithm stack will store max 2 operators and 3 values at any time, so something like "3 + 3 + 3 + 3 + 3 x 3 / 3 + 3" wouldn't fit).

My idea goes like this:
When finished reading value from string check last operator (max 2 times at once).
If last operator is * or / then solve it.
If last operator is + or - then solve it if next operator has the same priority.
This way there will be no need to keep more than 5 elements on stack.

"while" loop is needed instead of "if" to avoid problem with example: 3 + 3 * 3 + 3 * 3
On stack: 3 + 3 * 3, next operator is +, so solve 3 * 3, but if there was no loop that also solves 3 + 9 then it would end with stack 3 + 9 + 3 * 3 (index out of bounds) instead of 12 + 3 * 3.
__________________
Impossible is Nothing
Sylwester is offline
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 12-15-2011 , 10:14   Re: Algorithm
Reply With Quote #5

I actually did understand it, and my idea was same to yours cuz, once again, i suggested calculating the problematic places with the * and / and writing it as a new string (in your case its the stack with { value, operator, value ... }).

By the way in that case: 3 + 3 * 3 + 3 * 3

The 'for' will make it: { 3, '+', 9, '+', 3, '*', 3 }, so yeah i understand why its needed. ( the case of '*' or '/' in the while )
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive

Last edited by Diegorkable; 12-15-2011 at 10:14.
Diegorkable is offline
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 12-15-2011 , 15:49   Re: Algorithm
Reply With Quote #6

I tried to let it solve "2 - 6 x 4 x 3" and the answer is -70, and it didnt say it was -70, can you find any bug about that type of question?

EDIT: There was also "0 x 6 + 3", and the answer was 7 for some reason! why is that?
Another EDIT: Another case was "9 x 9 - 1", and it said the answer is 88... :S whats wrong, most of the questions it does good but sometimes it fucks up
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive

Last edited by Diegorkable; 12-15-2011 at 16:35.
Diegorkable is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 12-15-2011 , 16:47   Re: Algorithm
Reply With Quote #7

I get these results:
2 - 6 x 4 x 3 = -70
0 x 6 + 3 = 3
9 x 9 - 1 = 80

You probably edited it and broke something.
__________________
Impossible is Nothing
Sylwester is offline
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 12-15-2011 , 17:39   Re: Algorithm
Reply With Quote #8

Yep that might be it, thanks still.
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive
Diegorkable is offline
Diegorkable
Veteran Member
Join Date: Jun 2011
Old 12-16-2011 , 07:36   Re: Algorithm
Reply With Quote #9

Look there's something wierd...

I gave it a question, "9 x 2 x 4"
I have a global variable called 'Answer' and I store inside the question's Answer.
Also I scripted a command to see the result, and the answer, for some reason, for this question , was 430...

PHP Code:
new AnsweraQuestion[] = "9 x 2 x 4"

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_clcmd("say /checkmath""temp")
    
register_clcmd("say /doquestion""do_question")
}

public 
temp(id)
{
    if ( !
is_user_admin(id) )
        return 
PLUGIN_HANDLED
        
    client_print
(idprint_chat"Answer = %d, question = %s"AnsweraQuestion)
    return 
PLUGIN_HANDLED
}

public 
do_question()
    
Answer analyzeAnswer()

public 
analyzeAnswer(){
    
    new 
stack[6], top
    
for(new ii<sizeof(aQuestion); i++){
        switch(
aQuestion[i]){
            case 
'0'..'9'stack[top] = stack[top]*10+aQuestion[i]-'0'
            
case '+','-','/','*','x':{
                while(
top && (stack[top-1] == '*' || stack[top-1] == 'x' || stack[top-1] == '/' || aQuestion[i] == '+' || aQuestion[i] == '-')){
                    switch(
stack[top-1]){
                        case 
'+'stack[top-2] += stack[top]
                        case 
'-'stack[top-2] -= stack[top]
                        case 
'/'stack[top-2] /= stack[top]
                        case 
'*','x'stack[top-2] *= stack[top]
                    }
                    
top -= 2                    
                
}                
                
stack[++top] = aQuestion[i]            
                
stack[++top] = 0                
            
}
        }
    }
    while(
top>0){
        switch(
stack[top-1]){
            case 
'+'stack[top-2] += stack[top]
            case 
'-'stack[top-2] -= stack[top]
            case 
'/'stack[top-2] /= stack[top]
            case 
'*','x'stack[top-2] *= stack[top]
        }
        
top -= 2
    
}        
    
    return 
stack[top]

Why is that? the code of analyzing is same as yours, why is not working properly? I dont spot anything.
__________________
My Projects:

Auto-Mix (Pug): 100%

Joined the Military (a soldier now) - Inactive
Diegorkable is offline
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 12-16-2011 , 07:53   Re: Algorithm
Reply With Quote #10

Answer = 72, question = 9 x 2 x 4

So you probably calculated answer with different equation, then you changed equation and used command to see result without recalculating it...
__________________
Impossible is Nothing
Sylwester is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:10.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode