I am trying to evaluate a polynomial that is stored in a linked list. I ask the user to input a string, the string gets converted to a linked list polynomial, then when I try to evaluate it the program gets a run time error and closes. The weird thing is "checkpoint 1" shows, but "checkpoint 2" does not. Here is the piece(s) of code I am working with, and the evaluate function. My linked list is fine, and there is nothing wrong with it. I have tried
Here is the way it looks when ran. The "3 3 2" means coefficient is 3, it went to the 3rd if statement in TranslateStrToLinkedList(), and has a power of 2. This was for my assurance it was working right.Code:#include <iostream> #include <fstream> #include <string> #include <ctype.h> #include "class.h" double EvaluatePoly(POLY input, int num); //pre-condtion: the input string must have all coefficients //including 1. does not need to have all powers with 0 coefficients //DOES NEED powers of zero if only a coefficient! void TranslateStrToLinkedList(char* str, POLY &input); int main() { char* input; int from, to; int answer = 0; POLY poly; cout<<"Enter a polynomial like t^3+2t^1+9t^0: "; cin>> input; TranslateStrToLinkedList(input, poly); poly.print(); cout<<endl<<"Enter domain for t: "; cin>> from >> to; while(from <= to){ cout<<"checkpoint 1\n"; answer = answer + EvaluatePoly(poly,from); cout<< answer <<endl; from++; } } //evaluates a linked list with polynomials double EvaluatePoly(POLY input, int num) { cout<<"checkpoint 2"; //NOT SHOWN!!! >.< double ans = 0; //prevents problems if input is empty int hi_degree = 0; //prevents problems if input is empty cout<<"checkpoint 3"<<endl; hi_degree = input.degree(); while(input.isLast(hi_degree) != 0){ ans = ans + input.coefficient(hi_degree)*(num^hi_degree); cout<<ans<<endl; hi_degree--; } return ans; } void TranslateStrToLinkedList(char* str, POLY &input) { char ch, *chstr; double coef = 0; int pow = 0, n = 0; while( n < strlen(str)-1){ ch = str[n]; if(isdigit(ch) || ch == '-' || ch == '+'){ if(ch == '-'){ ch = str[n+1]; chstr = &ch; coef = -1*atof(chstr); chstr = &str[n+4]; pow = atoi(chstr); cout<<endl<<coef<<" 1 "<<pow<<endl; n++; n++; n++; n++; n++; //move 5 places input.append(coef,pow); } else if(ch == '+'){ ch = str[n+1]; chstr = &ch; coef = atof(chstr); chstr = &str[n+4]; pow = atoi(chstr); cout<<endl<<coef<<" 2 "<<pow<<endl; n++; n++; n++; n++; n++; //move 5 places input.append(coef,pow); } else if(isdigit(ch)){ chstr = &ch; coef = atof(chstr); chstr = &str[n+3]; pow = atoi(chstr); cout<<endl<<coef<<" 3 "<<pow<<endl; n++; n++; n++; n++; //move 4 places input.append(coef,pow); } } } return; }
Code:Enter a polynomial like t^3+2t^1+9t^0: 3x^2 3 3 2 3x^2 Enter domain for t: 2 4 checkpoint 1



LinkBack URL
About LinkBacks


