[MFS] Simple Calculator
#1

MFS = Mini FilterScript :P
What can I say ? Simple and clean calculator

You can download a filterscript from here :



or you can see the code here :

Quote:

public OnPlayerCommandText(playerid, cmdtext[])
{
new cmd[128], idx;
cmd = strtok(cmdtext,idx);

if (!strcmp("/calc", cmd, true))
{
new i, num1 = 0, num2 = 0, ch, result;
cmd = strtok_line(cmdtext,idx);
if (!strlen(cmd)) return SendClientMessage(playerid,0xffffffff,"Example for use : /math 5*5");

for (i=0; i<strlen(cmd); i++)
{
if (cmd[i] == ' ') continue;
if (IsDigit(cmd[i]))
{
num1 = num1 * 10 + (cmd[i] - '0');
continue;
}
ch = cmd[i++];
num2 = strval(cmd[i]);
break;
}

result = operation(num1,num2,ch);
format(cmd,sizeof(cmd),"%d %c %d = %d",num1,ch,num2,result);
SendClientMessage(playerid,0xffffffff,cmd);
return 1;
}
return 0;
}

functions I used :

Quote:

stock strtok(const string[], &index,seperator=' ')
{
new length = strlen(string);
new offset = index;
new result[20];
while ((index < length) && (string[index] != seperator) && ((index - offset) < (sizeof(result) - 1)))
{
result[index - offset] = string[index];
index++;
}

result[index - offset] = EOS;
if ((index < length) && (string[index] == seperator))
{
index++;
}
return result;
}

strtok_line(const string[], index)
{
new length = strlen(string);

new offset = index;
new result[128];
while ((index < length) && ((index - offset) < (sizeof(result) - 1)) && (string[index] > '\r'))
{
result[index - offset] = string[index];
index++;
}
result[index - offset] = EOS;
return result;
}

stock IsDigit(ch)
{
if (ch >= 0x30 && ch <= 0x39) return 1;
else return 0;
}

stock operation(x,y,operand)
{
new sum;
switch (operand)
{
case '+' : sum = x+y;
case '-' : sum = x-y;
case '*' : sum = x*y;
case '/' : sum = x/y;
default : sum = 0;
}
return sum;
}

Examples for use :

Quote:

/calc 10*5
/calc 1 + 2
/calc 4-3
/calc 7/ 6

Spaces in the command skipped by the script.
You can simply add more mathematics operation by edit the "operation" function above.
Good Luck
Reply
#2

Quote:
Originally Posted by PhyroIS
MFS = Mini FilterScript :P
What can I say ? Simple and clean calculator

Quote:

public OnPlayerCommandText(playerid, cmdtext[])
{
new cmd[128], idx;
cmd = strtok(cmdtext,idx);

if (!strcmp("/calc", cmd, true))
{
new i, num1 = 0, num2 = 0, ch, result;
cmd = strtok_line(cmdtext,idx);
if (!strlen(cmd)) return SendClientMessage(playerid,0xffffffff,"Example for use : /math 5*5");

for (i=0; i<strlen(cmd); i++)
{
if (cmd[i] == ' ') continue;
if (IsDigit(cmd[i]))
{
num1 = num1 * 10 + (cmd[i] - '0');
continue;
}
ch = cmd[i++];
num2 = strval(cmd[i]);
break;
}

result = operation(num1,num2,ch);
format(cmd,sizeof(cmd),"%d %c %d = %d",num1,ch,num2,result);
SendClientMessage(playerid,0xffffffff,cmd);
return 1;
}
return 0;
}

functions I used :

Quote:

stock strtok(const string[], &index,seperator=' ')
{
new length = strlen(string);
new offset = index;
new result[20];
while ((index < length) && (string[index] != seperator) && ((index - offset) < (sizeof(result) - 1)))
{
result[index - offset] = string[index];
index++;
}

result[index - offset] = EOS;
if ((index < length) && (string[index] == seperator))
{
index++;
}
return result;
}

strtok_line(const string[], index)
{
new length = strlen(string);

new offset = index;
new result[128];
while ((index < length) && ((index - offset) < (sizeof(result) - 1)) && (string[index] > '\r'))
{
result[index - offset] = string[index];
index++;
}
result[index - offset] = EOS;
return result;
}

stock IsDigit(ch)
{
if (ch >= 0x30 && ch <= 0x39) return 1;
else return 0;
}

stock operation(x,y,operand)
{
new sum;
switch (operand)
{
case '+' : sum = x+y;
case '-' : sum = x-y;
case '*' : sum = x*y;
case '/' : sum = x/y;
case '%' : sum = x%y;
default : sum = 0;
}
return sum;
}

Examples for use :

Quote:

/calc 10*5
/calc 1 + 2
/calc 4%3
/calc 7/ 6

Spaces in the command skipped by the script.
You can simply add more mathematics operation by edit the "operation" function above.
Good Luck
Good Job Moti
i saw the this Forum F and next
Reply
#3

Some Scrennshot...
Reply
#4

This is good !
Now you can do your homework and play samp in a same time
just kidding
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)