CIN>>EQUATION (how to then mult by it later)

Reply

Join Date: Sep 2008
Posts: 12
Reputation: swbuko is an unknown quantity at this point 
Solved Threads: 0
swbuko swbuko is offline Offline
Newbie Poster

CIN>>EQUATION (how to then mult by it later)

 
0
  #1
Nov 5th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: CIN>>EQUATION (how to then mult by it later)

 
0
  #2
Nov 5th, 2008
Depends, is the function hard coded or can the user enter any function at the command line?
Last edited by iamthwee; Nov 5th, 2008 at 5:06 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,839
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 266
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: CIN>>EQUATION (how to then mult by it later)

 
0
  #3
Nov 5th, 2008
Do you want to multiply numbers or do you want to replace the x in your inputfunction with a value?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

Re: CIN>>EQUATION (how to then mult by it later)

 
0
  #4
Nov 5th, 2008
exactly, what is your problem?
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 12
Reputation: swbuko is an unknown quantity at this point 
Solved Threads: 0
swbuko swbuko is offline Offline
Newbie Poster

Re: CIN>>EQUATION (how to then mult by it later)

 
0
  #5
Nov 5th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,757
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: CIN>>EQUATION (how to then mult by it later)

 
0
  #6
Nov 5th, 2008
Originally Posted by swbuko View Post
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.
You probably need to post some code and provide an example of a run of the program, even though you haven't written the program yet. You need to decide what kind of functions are legal (polynomials, exponents, logarithms, etc.). Have the user enter the function, read it in as a string, then parse the string in such a way that you can tell what kind of function it is (e.g. polynomial, whether it's a legal function that you can handle, then parse the string further to extract the relevant numbers (i.e. coefficients and exponents in a polynomial). But I think you'll have to read the function in as a string from the user.

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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: CIN>>EQUATION (how to then mult by it later)

 
0
  #7
Nov 5th, 2008
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:

  1. #include <string>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. string sFormula = "";
  7. strin sBuffer = "";
  8. bool numStream(false);
  9. int iVal[255] = {0};
  10. int iCount(0);
  11. cout << "Enter formula: ";
  12. cin >> sFormula
  13. for (int i(0); i < strlen(sFormula); ++i)
  14. {
  15. if (isdigit(sFormula[i]))
  16. {
  17. numStream = true;
  18. sBuffer += sFormula[i];
  19. }
  20. if (numStream && !isdigit(sFormula[i])
  21. {
  22. numStream = false;
  23. iVal[iCount++] = atoi(sBuffer);
  24. }
  25. }
  26. return 0;
  27. }

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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,839
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 266
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: CIN>>EQUATION (how to then mult by it later)

 
0
  #8
Nov 5th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: CIN>>EQUATION (how to then mult by it later)

 
0
  #9
Nov 5th, 2008
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.

  1. #include <string.h>
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. string sFormula = "";
  7. string sBuffer;
  8. char szFormula[255];
  9. bool numStream(false);
  10. int iVal[2] = {0};
  11. int iReturnVal(0);
  12. int iCount(0);
  13. char cLastOperator(0);
  14. cout << "Enter formula: ";
  15. cin.getline(szFormula, 54);
  16. for (unsigned int i(0); i < strlen(szFormula); ++i)
  17. {
  18. if (isdigit(szFormula[i]))
  19. {
  20. numStream = true;
  21. sBuffer += szFormula[i];
  22. }
  23. if (numStream && (!isdigit(szFormula[i]) || i == strlen(szFormula) - 1))
  24. {
  25. numStream = false;
  26. iVal[iCount++] = atoi(sBuffer.c_str());
  27. if (cLastOperator == '+')
  28. {
  29. iReturnVal += iVal[0] + iVal[1];
  30. iVal[0] = 0;
  31. iVal[1] = 0;
  32. iCount = 0;
  33. cLastOperator = 0;
  34. }
  35. else if (cLastOperator == '-')
  36. {
  37. iReturnVal -= iVal[0] + iVal[1];
  38. iVal[0] = 0;
  39. iVal[1] = 0;
  40. iCount = 0;
  41. cLastOperator = 0;
  42. }
  43. sBuffer = "";
  44. }
  45. if (!numStream && (szFormula[i] == '+' || szFormula[i] == '-' || szFormula[i] == '*'))
  46. {
  47. cLastOperator = szFormula[i];
  48. }
  49. }
  50. cout << szFormula << " = " << iReturnVal << endl;
  51. cin.get();
  52. return 0;
  53. }

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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC