Welcome Guest ( Log In | Register)



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Php Code?, Mathematical Applications
alex1985
post Feb 27 2008, 10:59 AM
Post #1


Super Member
*********

Group: [HOSTED]
Posts: 387
Joined: 9-February 08
Member No.: 57,615



Hello, everyone. The help is needed again.

How can I make calculator in PHP language? That will act like that a user just type in the fields known values, then click the button, and it's going to be solved automatically. In other words, have can I write a formula in PHP, how to plug it inside that language. For example, the formula to find a peremeter of square is:

P=4a.


So, a user just can write the known value which is peremeter itself and it will find the side of a square; and vice versa.

If you can write many things how to do such formulas, such as complicated ones.
Go to the top of the page
 
+Quote Post
KansukeKojima
post Feb 27 2008, 03:50 PM
Post #2


Privileged Member
*********

Group: [HOSTED]
Posts: 525
Joined: 13-October 06
From: Alberta, Canada
Member No.: 31,584



Here is a basic php math tutorial, that unfortunately I did not write.... and your going to have to play with it alot... but with some work, you should be able to figure it out.....

QUOTE
Equations in PHP can be easily executed. Just like in real life, you can add, subtract, multiply, or divide. How, you have to ensure you have done everything correctly to make a successful equation. For example, 5x-3x would not work. But, trying to make something like 5-3 would work. How exactly? I'll show you.

Code
<?php
$num[1]= 5;
$num[2] = 3;
$total = $num[1] - $num[2];
echo "The number is $total";
?>
Outcome
The number is 3

That is what I would consider a very basic equation. But, you can get much more complicated. How much more? Well, by having a couple sets of brackets and more numbers, you can make yourself look like an PHP equation wiz. Here is one of my text equations I tried while playing around with PHP.

Code
<?php
$num[1] = 5;
$num[2] = 3;
$num[3] = 2;
$num[4] = 2;
$total = (($num[1]+$num[2])/$num[3])*$num[4];
echo "[ ( $num[1] + $num[2] ) / $num[3] ] * $num[4] = $total";
?>
Outcome
[ ( 5 + 3 ) / 2 ] * 2 = 8

Now, you may be asking yourself, "But don't you use an "x" sign to multiply numbers?". Well, no. You see, in PHP programming, if you try to multiply two numbers with an "x", you will just come up with a parse error, simple as that. So, instead, you just have to put in a star. The same goes for dividing. Should you try to divide by using a division sign, you will also come up with an error. Instead, you have to use a slash.

When doing equations in PHP, the programming also uses the acronym of BEDMAS to do the question. In case you don't know, BEDMAS is the form of doing questions in a certain order if multiple operations were required. So it goes, Brackets, Exponents, Division, Multiplacation, Addition, Subtraction. So if you want a certain part to be done first, you must put brackets around it. So let's review the equation I made earlier.

[ ( 5 + 3 ) / 2 ] * 2
[ (8) / 2 ] * 2
[ 4 ] * 2
= 8

Now, wasn't that easy? You just have to know what you're doing if you want to do it effectively. Heck, if you get smart enough with equations in PHP, you might just be able to make it do your math homework for you =) Now would that be nice?


This post has been edited by KansukeKojima: Feb 27 2008, 03:51 PM
Go to the top of the page
 
+Quote Post
tricky77puzzle
post Feb 27 2008, 10:08 PM
Post #3


Super Member
*********

Group: [HOSTED]
Posts: 416
Joined: 26-January 08
Member No.: 56,881



QUOTE(KansukeKojima @ Feb 27 2008, 10:50 AM) *
Code
<?php
$num[1]= 5;
$num[2] = 3;
$total = $num[1] - $num[2];
echo "The number is $total";
?>
Outcome
The number is 3


Shoudn't it be "The number is 2"? Obviously this guy is good at programming, but kinda weird at math...

Basically doing math in PHP is like doing math in C. Except for the fact that you don't have any of the "math" libraries to work with.
Go to the top of the page
 
+Quote Post
galexcd
post Feb 28 2008, 03:53 AM
Post #4


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
*********

Group: [HOSTED]
Posts: 975
Joined: 25-September 05
From: The dungeon deep below the foundation of trap17
Member No.: 12,251



Wow this is quite a project you've got here. If I were actually going to write out the code to do this it would be very long, but I can go over a basic overview with a few examples of how I would make something like this...

Alright, so first of all you want php to remember functions and variables. So let's go with your example of parameter. The easiest way to write a function syntax is P(a)=4a
So let's have your php program recognize that syntax. I would highly advise you to use a preg_match for this unless you want to be parsing strings all day. So just an example of how your php code could recognize a function:

CODE
$input=$_GET['input'];
if(preg_match('/[A-Z]\([a-z]\)=/',$input)){
//$input is a function!
}else{
//$input is not a function, or may be formatted incorrectly
}


Alright, so you wanted your users to set many different functions. We'll put this code in a loop in a bit but to prepare for that let's make each function have its own variable so we can store the data from the function. I will do this with a preg_split. The variable will also start with "func_" so that we don't confuse our function variables from regular variables we are using.

CODE
$input=$_GET['input'];
if(preg_match('/[A-Z]\([a-z]\)=/',$input)){
//$input is a function!
$split=preg_split('/\([a-z]\)=/',$input);
${"func_".strtolower($split[0])}=$split[1];
}


And let's not forget about that parameter, it will be important later so go ahead and start a new group of variables starting with "param_" to store these in.

CODE
$input=$_GET['input'];
if(preg_match('/[A-Z]\([a-z]\)=/',$input)){
//$input is a function!
$split=preg_split('/\([a-z]\)=/',$input);
${"func_".strtolower($split[0])}=$split[1];
${"param_".strtolower($split[0])}=$input[2];
echo $param_f;
}


Now you would do the same thing for variables just take out the parameter part. Alright so now that you've got function and variable definition set, lets put them to use. There would be two parts to this, one would be getting the answer to a function when calling it with a number as a parameter, and the other would be calling it with a variable as a parameter.
Now lets make a check to see if somebody called the function with a number:
CODE
if(preg_match('/[A-Z]\([0-9]{?}\)=/',$input)){


and if so evaluate the function with all instances of that variable swapped out.
WARNING: DO NOT FORGET TO PARSE OUT THE SEMICOLON OR YOUR USERS COULD DO MAJOR DAMAGE TO YOUR WEBSITE!
CODE
f(preg_match('/[A-Z]\([0-9]{?}\)=/',$input)){
return eval(str_replace(array(";",${"param_".strtolower($input[0])}),array("",substr($input,2,strpos(")")-2))),${"func_".strtolower($input[0])}));
}


and for evaluating the function if somebody calls it with a variable would be the same except instead of substr($input,2,strpos(")")-2) put in the variable name of the variable they inserted. So probably something like this:

CODE
${"vars_".strtolower($input[2])}


Alright, so now what I would do is put this all into a function and make all of the variables and functions global. Then for every line of math that this php page takes in, run the function for it.

So now your page should successfully be able to do this:
QUOTE
INPUT:
F(x)=cos(x)
P(a)=4*a
P(4)
b=17
P(b)
d=6.28318
F(d)

OUTPUT:
null
null
16
null
68
null
1



Again, none of this code was tested by me yet so if you have any errors just post em up here.

P.S. I would suggest writing my own evaluation function rather that using eval, for security reasons and so if people enter variables into the function that aren't passed in the parameters they don't get an error.

This post has been edited by alex7h3pr0gr4m3r: Feb 28 2008, 07:04 PM
Go to the top of the page
 
+Quote Post
alex1985
post Mar 2 2008, 05:12 PM
Post #5


Super Member
*********

Group: [HOSTED]
Posts: 387
Joined: 9-February 08
Member No.: 57,615



What's about formulas in finance?
Go to the top of the page
 
+Quote Post
galexcd
post Mar 12 2008, 04:57 PM
Post #6


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
*********

Group: [HOSTED]
Posts: 975
Joined: 25-September 05
From: The dungeon deep below the foundation of trap17
Member No.: 12,251



QUOTE(alex1985 @ Mar 2 2008, 10:12 AM) *
What's about formulas in finance?


What formulas are those? Sorry I'm not that familiar with finance.
Go to the top of the page
 
+Quote Post
alex1985
post Mar 12 2008, 05:51 PM
Post #7


Super Member
*********

Group: [HOSTED]
Posts: 387
Joined: 9-February 08
Member No.: 57,615



Like the one, where you find the future value, let' s say of your investment:

FV=PV(1+I)^N, where:

FV-->Future Value
PV-->Present Value
I-->Interest Rate
^-->powered, N times in our case
N-->number of years.
Go to the top of the page
 
+Quote Post
galexcd
post Mar 12 2008, 06:15 PM
Post #8


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
*********

Group: [HOSTED]
Posts: 975
Joined: 25-September 05
From: The dungeon deep below the foundation of trap17
Member No.: 12,251



Well yes, if you used this code then you could make that into a function, but you would need to modify the code I gave you to take multiple paramaters for functions like that, not just single ones.