Say I had:
all stored in a string...would it then be possible me to calculate that sum in any way?Code:(6/2*(3+4))/6
Say I had:
all stored in a string...would it then be possible me to calculate that sum in any way?Code:(6/2*(3+4))/6
Do a board search for "reverse polish"
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Or a Google. There's also this nifty site called "Wikipedia", rings a bell?
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
Yep...I looked at the stuff on Wikipedia asked my Dad as he is a fair good Maths person and I understand how you order it...but how do you get a program to output say 3+4-
There is no standard way. You can write our own math expression interpreter that works recursively on parentheses. You can also make compile your math expression into reverse polish notation format which allows you to deal with the expression easier since parentheses are unnecessary when using it. Executing a compiled expression is also faster than interpreting it which is nice when you want to execute it fast in rapid succession, like when you want to graph a function say "x^2 + 4*x + 12".
You can use the shunting yard algorithm to convert from infix to RPN.
http://en.wikipedia.org/wiki/Reverse...infix_notation
No I mean how can I get the result of the sum I can think of ways in which I could convert to RPN but not to do the sum.
After converting it to RPN calculating the sum will be easy. You'll have a stack that contains both numbers and operators like this:
6 2 / 3 4 + * 6 /
Then you traverse the stack. If a number is found add that number to new stack which we can call the number stack. If an operator is found pop as many numbers needed for the operator, perform the operation and push the result back to the number stack. In this case all the operators are binary operators which means they take two operands so you pop two numbers from the stack.
In steps,
Stack: 6 2 / 3 4 + * 6 /
Num stack:
Stack: 2 / 3 4 + * 6 /
Num stack: 6
Stack: / 3 4 + * 6 /
Num stack: 2 6
Stack: 3 4 + * 6 /
Num stack: 3
Stack: 4 + * 6 /
Num stack: 3 3
Stack: + * 6 /
Num stack: 4 3 3
Stack: * 6 /
Num stack: 7 3
Stack: 6 /
Num stack: 21
Stack: /
Num stack: 21 6
Stack:
Num stack: 3.5
The result is now at the top of the number stack.
I don't really get you there...I will have all of my stuff stored in a string.
If you converted it to RPN you wouldn't be having it in a string. Well you could but I don't see why.
If you don't really feel up to doing any RPN conversion you could just interpret the expression.
Well what I have is theuser inputting their equation into a string and then it is converting it to RPN.
Show some code? I don't know how you're storing the converted RPN expression.
Well I may be completely wrong but I haven't quite finished the code
That is what I have done...I may be wrong in what I have done and I have yet to add parenthesisCode:#include <iostream> #include <string> #include <cstdlib> using namespace std; int main() { string sum = ""; string output= ""; string stack = ""; cout << "Please type in your sum: "; getline(cin, sum); for (int i = 0; i < sum.length(); i++) { char x = sum.at(i); switch (x) { case '1': output += x; break; case '2': output += x; break; case '3': output += x; break; case '4': output += x; break; case '5': output += x; break; case '6': output += x; break; case '7': output += x; break; case '8': output += x; break; case '9': output += x; break; case '0': output += x; break; case '+': stack += x; break; case '-': stack += x; break; case '*': stack += x; break; case '/': stack += x; break; default : cout << "Error with input"; cin.ignore(); cin.get(); return 0; } } output += stack; cout << output << endl; system("PAUSE"); return 0; }
It seems to delete the wrong part of the string when I try that...why?Code:case ')': int z; z = sum.find_last_of('(', 0); int n; n = ++z; output += sum.substr(n); sum.erase(z); break;
How can I get it to delete the bit on the end that I need to get rid of.
Ok. If I was going to do this, I would do it this way:
This works, but it doesn't have any hierarchy of operators, if you know what I mean. It just works from the left hand side to the right hand side.Code:#include <iostream> #include <string> #include <cstdlib> using namespace std; int main() { string sum = ""; cout<< "Please type in your sum: "; cin >> sum; int x = 0; double output = 0; for (int i = 0; i < sum.length(); i++) { x = sum.at(i); switch ( x ) { case '1': output += ( x - '0'); break; case '2': output += ( x - '0'); break; case '3': output += ( x - '0'); break; case '4': output += ( x - '0'); break; case '5': output += ( x - '0'); break; case '6': output += ( x - '0'); break; case '7': output += ( x - '0'); break; case '8': output += ( x - '0'); break; case '9': output += ( x - '0'); break; case '0': output += ( x - '0'); break; case '+': output += ( sum[i+1] - '0' ); i++; break; case '*': output *= ( sum[i+1] - '0' ); i++; break; default : cout << "Error with input"; cin.ignore(); cin.get(); return 0; } } cout << output << endl; return 0; }
This is essentially your code, but the changes I made were that the output is an integer (it'd probably be better to have a float or something), and in all the operators, because the input is a string, I subtracted '0' from it ... ie:
Output: 1Code:'1' = 31 (from memory, so it's prob wrong), in ASCII, '1' - '0' = 1 in ascii. int Var = '1' - '0'; cout<< Var;
get me?
Last edited by twomers; 05-15-2006 at 01:59 AM.