Hi. I'm a novice to programming with 2 years of daydreaming through my programming classes and am now stuck with a unresponsive online teacher with horrible assignment instructions. The assignment is due today so I'm not asking how to do it. I've just been screwing around with example problems I've found and manipulated them to suit my purpose. Commented out sections of code are just some of my thinking/monkeying around. Excuse them. The code below has none of my classes included. Its purely trying to work out the basics of the assignments objectives.
My questions:
-How to convert a string to a double? //(strtod? atof?
-How could you take a string that the user enters and identify the numbers in it?
// say the users string was "2*x*x-13*x+4" : how to identify that its '13' and //not '1' and '3'?
-How to identify that the numbers as negative and positive from the string?
-How to offset find to that it continues to find the rest of the 'x' in the string?
________________________________________________________
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
|
#include <stdio.h>
#include <string.h>
#include <cstdlib> //need this for exit function
#include <iostream> //need this for cout
#include <cmath>
#include <stdlib.h> /* strtod */
using namespace std;
int main ()
{
char str[256];
cout << "enter numbers and symbols: \n";
cin >> str;
char * pch;
char * pchb;
const int SIZE =100;
char keys[] = "1234567890";
//char keystwo[] = "x*+-";
//double array[SIZE];
//for(int count=0; count<=9;count++)//change 9 to biggest degree
//{
pchb = strpbrk (str,keys);
while(pchb !=NULL)
{
//array[count] = strtod (pchb,NULL);
//char coef[count]= *pchb;//collectthe coefficents
printf ("%c " , *pchb);
pchb = strpbrk (pchb+1,keys);
}
//}
// for(int i=0; i<=;i++)//change 9 to biggest degree
//{
// printf ("double : %c",array[i]);
//}
printf ("\nSplitting string \"%s\" into tokens:\n",str);
pch = strtok (str," +*x");
while (pch != NULL)
{
//char coeftwo[count] = pch;
printf ("%s\n",pch);
pch = strtok (NULL, " +*x");
}
system("pause");
return 0;
}
|
_____________________________________________
So far the I've gotten the above code to print out this:(EXAMPLE)
enter numbers and symbols:
2*x*x*x*x-13*x*x*x*x+0*x*x-7
2 1 3 0 7
slitting string "2*x*x*x*x-13*x*x*x*x+0*x*x-7" into tokens:
2
-13
0
7
Press any key to coutinue...
_ |
_______________________________________________
Bonus Info:
The assignment is called Polynomial Class and after going to my programming teacher in high school we think we figured out what the assignment was asking.
INTERPRETATION OF ASSIGNMENT:
Using dynamic arrays, implement a polynomial class with infix operators +, -, *.
It has the user enter a string a polynomial in the form of having x*x to signify the degree of the polynomial. (EX: x^2 == x*x, x^4==x*x*x*x, etc)
The student is to provide these member functions:
-default constructor,
-copy constructor,
-operator=
-destructor
-parameterized constructor to create an arbitrary polynomial
-operator+
-operator-
-operator*
-assign and inspect function (or functions) for
-coefficients, indexed by exponent
-function to evaluate polynomial as a value of type double
The student is to decide on whether these are to be member, friend, or neither (standalone).
The program should print statements like this...
(EX:
2x^3 -3x + 4 (or, written out as C++ code) 2*x*x*x –3*x + 4
the coefficients for terms as listed:
"degree 3 term has coefficient 2,
degree 2 term has coefficient 0,
degree 1 term has coefficient -3, and
degree 0 term has coefficient 4. " )