How do you make C++ solve equations?

Jan 6, 2011 at 9:01pm
Hey, I'm new too C++ so I have a pretty simple question I guess.

How do you make the program find x in the following...

5 = 2 + x

So yeah that's it just wondering what I have to do to solve for x.
Jan 6, 2011 at 9:16pm
you write x = 5-2; !
really, if you want to make your program solve equations, you'll have to write some code. (and it won't be too easy)
Last edited on Jan 6, 2011 at 9:17pm
Jan 6, 2011 at 9:23pm
is it preset integer variables ? like var1 = 5; if so..
1
2
3
4
5
var1 = 5;
var2 = 2;
varX = x;
varX = var1 - var2;
std::cout << var1 << "=" << var2 << "+" << varX  << std::endl;


See what's going on? Code it so that X or the value subtracted from the other integers...i'll let you play with the idea : ) -> Just know this is 1 idea,..not "the" idea.
Jan 6, 2011 at 9:24pm
How hard is it?
Jan 6, 2011 at 9:58pm
Firstly, what result do you expect?
If you want to be able to write 5 = 2+x; in your code, it's not possible.
If you want to print "enter an equation:", then when user enters "5=2+x" print "x = 3 !!!", it is possible. How hard it is depends on the complexity of equations. For linear equations it wouldn't be hard at all.
The hardest part would be parsing the string.
If you really want to do it, for practice write a calculator which could calculate "2+3-4" or something. I'd then explain the rest (if needed).
Jan 6, 2011 at 10:48pm
Ok so you can't code it to find the value of a variable in a linear equation?
Jan 6, 2011 at 11:02pm
yes
1
2
3
4
5
int main(){
   int x;
   5 = 2+x;
   return x;
}

can never work.
Jan 7, 2011 at 2:48am
Hi hamsterman why don't you show ProAlbino your small little equation solving functions? You even have source code where he can learn!
Jan 7, 2011 at 4:49am
try this. Hope this help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;
int main()
{

  /*5 = 2 + x
   find the value of x
  */

  cout << "the value of x in (5 = 2 + x) is " << 5 - 2;

  return 0;
}


http://codewall.blogspot.com
Jan 7, 2011 at 2:04pm
@sohguanh
apparently that's not what he wants. also, I have a feeling a link wouldn't be helpful at all..

by the way, I was bored so...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>

struct VAR{
	float i;
};

struct LINE{//k*x+a
	float a, k;
	VAR* x;

	LINE(){}
	LINE(int a) : a(a), k(0), x(0) {}
	LINE(VAR& v) : a(0), k(1), x(&v) {}
};

LINE operator + (LINE A, LINE B){//assumes that A.x == 0 or B.x == 0 or A.x == B.x
	LINE R;
	R.a = A.a + B.a;
	R.k = A.k + B.k;
	if(A.x) R.x = A.x;
	else R.x = B.x;
	return R;
}

LINE operator - (LINE A, LINE B){//same as +
	LINE R;
	R.a = A.a - B.a;
	R.k = A.k - B.k;
	if(A.x) R.x = A.x;
	else R.x = B.x;
	return R;
}

LINE operator * (LINE A, LINE B){//assumes that A.x == 0 or B.x == 0
	LINE R;
	R.a = A.a * B.a;
	R.k = A.k * B.a + B.k * A.a;
	if(A.x) R.x = A.x;
	else R.x = B.x;
	return R;
}

LINE operator / (LINE A, LINE B){//assumes that B.x == 0
	LINE R;
	R.a = A.a / B.a;
	R.k = A.k / B.a;
	R.x = A.x;
	return R;
}

void operator == (LINE A, LINE B){
	LINE C = A - B;
	C.x->i = -C.a/C.k;
}

int main(){
	VAR x;
	5 == (2 + (x-7)*10)/2;

	std::cout << "x = " << x.i;
	std::cin.get();

	return 0;
}

:)
Last edited on Jan 7, 2011 at 2:04pm
Topic archived. No new replies allowed.