![]() |
| ||
| CIN>>EQUATION (how to then mult by it later) 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. |
| ||
| Re: CIN>>EQUATION (how to then mult by it later) Depends, is the function hard coded or can the user enter any function at the command line? |
| ||
| Re: CIN>>EQUATION (how to then mult by it later) Do you want to multiply numbers or do you want to replace the x in your inputfunction with a value? |
| ||
| Re: CIN>>EQUATION (how to then mult by it later) exactly, what is your problem? |
| ||
| Re: CIN>>EQUATION (how to then mult by it later) 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. |
| ||
| Re: CIN>>EQUATION (how to then mult by it later) Quote:
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. |
| ||
| Re: CIN>>EQUATION (how to then mult by it later) 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: #include <string> 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 |
| ||
| Re: CIN>>EQUATION (how to then mult by it later) 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 |
| ||
| Re: CIN>>EQUATION (how to then mult by it later) 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. #include <string.h> 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. |
| All times are GMT -4. The time now is 2:23 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC