| | |
CIN>>EQUATION (how to then mult by it later)
![]() |
•
•
Join Date: Sep 2008
Posts: 12
Reputation:
Solved Threads: 0
I'm currently writing a program that takes in a function such as ( 7x+19 ) and I need to evaluate the function that user inputs at range of numbers. I've got the loop figured out to loop through my desired numbers, but I don't know what to call the function when the user cin>>function. Should it be a char, string, int, double? Because later I'll need to call function and mult. it by lets say 3, or 5, or 12, etc? So what should I do to accomplish what I need to get done, I'm kind of stuck on this one. I'm kind of a beginner.
•
•
Join Date: Jan 2008
Posts: 3,757
Reputation:
Solved Threads: 491
•
•
•
•
yea, I need to exchange x for the the number. such as 3, 4, 5 etc. Ill run a while loop to get my x,y coord.
The big thing is to decide what input your program needs to be able to handle, what assumptions it can make as far as good input, then go from there, in my opinion.
Last edited by VernonDozier; Nov 5th, 2008 at 3:00 pm.
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
Sounds like your trying to parse a function. You'll have to enter the function as a string (either char * or string class), then use a loop to figuire out its contents. Depending on how robust/useful you want it to be, this could be a bit challenging for a beginner. Here's a start:
This will parse the formula and extract all the numbers into an array. This isn't really useful for solving anything, but it's a start. I would sugguest creating a class for holding/handling formulas and their operations. As you can see, it's a bit more complex an issue than it seems
C++ Syntax (Toggle Plain Text)
#include <string> #include <iostream> using namespace std; int main() { string sFormula = ""; strin sBuffer = ""; bool numStream(false); int iVal[255] = {0}; int iCount(0); cout << "Enter formula: "; cin >> sFormula for (int i(0); i < strlen(sFormula); ++i) { if (isdigit(sFormula[i])) { numStream = true; sBuffer += sFormula[i]; } if (numStream && !isdigit(sFormula[i]) { numStream = false; iVal[iCount++] = atoi(sBuffer); } } return 0; }
This will parse the formula and extract all the numbers into an array. This isn't really useful for solving anything, but it's a start. I would sugguest creating a class for holding/handling formulas and their operations. As you can see, it's a bit more complex an issue than it seems
Last edited by skatamatic; Nov 5th, 2008 at 3:15 pm.
As VernonDozier said you have to read your input as a string. But then you have to parse your inputstring and that's a whole world on it's own! But if you're adventurous enough to try it, don't hesitate! Start with http://en.wikipedia.org/wiki/Parsing
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
Here's an actual working math parser. It only works with addition and subtraction though. You've got me interested now, so I'm gonna write a (much more useful) class for this problem.
It works like this:
Input a stream of numbers and operands (- or +), all seperated by a space. Ie 2 + 6 + 8 - 11
And it will output the result. It won't throw any exceptions, so if you see eratic results you probably entered the equation wrong.
C++ Syntax (Toggle Plain Text)
#include <string.h> #include <iostream> using namespace std; int main() { string sFormula = ""; string sBuffer; char szFormula[255]; bool numStream(false); int iVal[2] = {0}; int iReturnVal(0); int iCount(0); char cLastOperator(0); cout << "Enter formula: "; cin.getline(szFormula, 54); for (unsigned int i(0); i < strlen(szFormula); ++i) { if (isdigit(szFormula[i])) { numStream = true; sBuffer += szFormula[i]; } if (numStream && (!isdigit(szFormula[i]) || i == strlen(szFormula) - 1)) { numStream = false; iVal[iCount++] = atoi(sBuffer.c_str()); if (cLastOperator == '+') { iReturnVal += iVal[0] + iVal[1]; iVal[0] = 0; iVal[1] = 0; iCount = 0; cLastOperator = 0; } else if (cLastOperator == '-') { iReturnVal -= iVal[0] + iVal[1]; iVal[0] = 0; iVal[1] = 0; iCount = 0; cLastOperator = 0; } sBuffer = ""; } if (!numStream && (szFormula[i] == '+' || szFormula[i] == '-' || szFormula[i] == '*')) { cLastOperator = szFormula[i]; } } cout << szFormula << " = " << iReturnVal << endl; cin.get(); return 0; }
It works like this:
Input a stream of numbers and operands (- or +), all seperated by a space. Ie 2 + 6 + 8 - 11
And it will output the result. It won't throw any exceptions, so if you see eratic results you probably entered the equation wrong.
Last edited by skatamatic; Nov 5th, 2008 at 4:05 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: A little problem
- Next Thread: Repaint window help?
| Thread Tools | Search this Thread |
anyfile api array based binary bitmap c++ c++borland c/c++ char class classes code coding compile console conversion count csimilaritybetweentext delete deploy desktop developer development directshow dll download draw dynamic dynamiccharacterarray email encryption error file forms fstream function functions game gdi+ givemetehcodez gnu graph gui homeworkhelp homeworkhelper http iamthwee ibm ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node output packing parameter pointer problem program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video visualc++ visualizationtoolkit win32 windows winsock wordfrequency wxwidgets






