Math a string (?) -
Salim_Karaja - 18.03.2013
I've create math system, and I faced a problem .
The admin creating an exercise .
but its a string, I mean how im extracting the answer from string like this:
remember its a string .
Re: Math a string (?) -
mastermax7777 - 18.03.2013
https://sampwiki.blast.hk/wiki/Strval
Re: Math a string (?) -
Salim_Karaja - 18.03.2013
Quote:
Originally Posted by mastermax7777
|
what about all the math signs, like "+", "-" ..
Re: Math a string (?) -
Rock - 18.03.2013
Strval
EDIT:
Nevermind, too late..
Re: Math a string (?) -
Misiur - 18.03.2013
Well, you should think about that earlier and store result. Otherwise you have to write own parser, or use regex
Re: Math a string (?) -
Konstantinos - 18.03.2013
There is a way.
pawn Код:
// Normal split function, I just added a second delimiter
stock split(const strsrc[], strdest[][], delimiter1, delimiter2)
{
new i, li;
new aNum;
new len;
while(i <= strlen(strsrc))
{
if((strsrc[i] == delimiter1 || strsrc[i] == delimiter2)|| i == strlen(strsrc))
{
len = strmid(strdest[aNum], strsrc, li, i, 128);
strdest[aNum][len] = 0;
li = i+1;
aNum++;
}
i++;
}
return 1;
}
pawn Код:
main( )
{
new
maths_text[ 15 ],
number_coords[ 3 ][ 3 ],
tmpstr[ 32 ]
;
format( maths_text, sizeof( maths_text ), "18-1+8" );
split( maths_text, number_coords, '+', '-' );
format( tmpstr, sizeof( tmpstr ), "maths: a = %s, b = %s, c = %s", number_coords[ 0 ], number_coords[ 1 ], number_coords[ 2 ] );
print( tmpstr );
}
pawn Код:
// Output:
[23:14:36] maths: a = 18, b = 1, c = 8
Re: Math a string (?) -
Salim_Karaja - 18.03.2013
Quote:
Originally Posted by Dwane
There is a way.
pawn Код:
// Normal split function, I just added a second delimiter stock split(const strsrc[], strdest[][], delimiter1, delimiter2) { new i, li; new aNum; new len; while(i <= strlen(strsrc)) { if((strsrc[i] == delimiter1 || strsrc[i] == delimiter2)|| i == strlen(strsrc)) { len = strmid(strdest[aNum], strsrc, li, i, 128); strdest[aNum][len] = 0; li = i+1; aNum++; } i++; } return 1; }
pawn Код:
main( ) { new maths_text[ 15 ], number_coords[ 3 ][ 3 ], tmpstr[ 32 ] ; format( maths_text, sizeof( maths_text ), "18-1+8" ); split( maths_text, number_coords, '+', '-' ); format( tmpstr, sizeof( tmpstr ), "maths: a = %s, b = %s, c = %s", number_coords[ 0 ], number_coords[ 1 ], number_coords[ 2 ] ); print( tmpstr ); }
pawn Код:
// Output: [23:14:36] maths: a = 18, b = 1, c = 8
|
thank you so much !!!!!!!!!!!!!!!!!!!!!!!!
Re: Math a string (?) -
Konstantinos - 18.03.2013
You're welcome! I'd like to inform you though that I tested with a number with 3 digits and it fails, so the numbers should from 0 to 99. If it's equal or higher to 100, it takes the b and it's like
pawn Код:
a = 100
b = 99
result = 10099
I'll try to fix that in case you want to use greater numbers.
Re: Math a string (?) -
Misiur - 18.03.2013
pawn Код:
#define strcpy(%0,%1) strcat(((%0)[0]=EOS,(%0)),%1)
Parser(str[]) {
new result = 0, delimeter, buffer[16], capturing = -1, end = 0;
for(new i, l = strlen(str) + 1; i != l; ++i) {
if('0' <= str[i] <= '9') {
if(-1 == capturing) capturing = i, end = i;
++end;
} else if('+' == str[i] || '-' == str[i] || EOS == str[i]) {
strmid(buffer, str, capturing, end);
if(EOS != str[i]) {
if(delimeter) {
result += (delimeter == '-' ? -1 : 1) * strval(buffer);
} else result = strval(buffer);
delimeter = str[i];
}
capturing = -1;
}
}
return result;
}
main() {
printf("%d", Parser("1823-24+15+1"));
}
Few conditions must be preserved: 1. it must start with positive number 2. no two delimeters in a row (so +-1 will fail). (I wanted to script multiplication and division, but order of operators, etc. etc. and in the end it'd get long).
#e2: Updated. Only limit is positive numbers and 15 digit number max
Re: Math a string (?) -
mastermax7777 - 18.03.2013
or.. a much easiest way is just storing the result right from the beginning... lol