|
-
March 17th, 2011, 11:47 PM
#1
Annoying linked list error
I'm trying to write code that takes can read in ordered triples from a file, store them in a linked list and spit them out again, but I get a strange error.
If I manually code in the triples (1,1,1) (2,2,2) and (4,4,4), and try to display them, I get (1,1,1)(4,4,4)(4,4,4)(4,4,4)(4,4,4) etc.
If I try to read the ordered triples in from a file, I also get a strange problem where the first triple is correct, but only one other, incorrect triple appears.
When I run the debugger, it displays "Segmentation Fault" when I get to the line in "displayTripleList" that is commented as "supererror"
I basically took a linked list that was on my computer and tried to modify it, so there are some vestigial pieces of code, such as anything related to polynomials.
Here's the code
Code:
#include <math.h>
#include <iostream>
#include <fstream>
using namespace std;
class orderedTriple{
private: double xcoord, ycoord, strength;
public:
orderedTriple(){xcoord=ycoord=strength=0;}
orderedTriple(double x, double y, double z){
xcoord=x; ycoord=y; strength=z;}
orderedTriple getForce(double x, double y){
double a,b,c;
a=x-xcoord; b=y-ycoord; c=strength/sqrt(pow(a,2)+pow(b,2));
orderedTriple force(a,b,c);
return force;
};
void setx(double x){xcoord=x;}
void sety(double x){ycoord=x;}
void setz(double x){strength=x;}
double returnx(){return xcoord;}
double returny(){return ycoord;}
double returnz(){return strength;}
};
void display(orderedTriple Lynn){
cout<<"("<<Lynn.returnx()<<","<<Lynn.returny()<<","<<Lynn.returnz()<<")";
}
class string {
private: int n; char letters[25];
public:
string(){n=0; for (int i=0; i<25; i++) letters[i]='0';}
string(int k, char p[]) {n=k; for (int i=0; i<k; i++) letters[i]=p[i];}
void display(){for (int i=0; i<n; i++) cout<<letters[i];}
void appendLetter (char Rachel) {n++; letters[n-1]=Rachel;}
};//this class probably isn't necessary.
struct node{
orderedTriple dunce;
double coff;
double exp;
char p;
node* next;
};
//Here's the linkedList class
class linkedList{
private:
node head;
node* current;
int length;
public:
linkedList();
void addNode(node toAdd);
double evaluate(double a);
int returnLength();
node returnCurrent();
void transverse();
linkedList convertToNumbers(linkedList poly);
char returnCharacter();
void incrementExponents();//useful for creating multi-digit numbers.
void integrate();
void setCurrent(node* a);
void deleteList();
void displayCharacterList();
void displayTripleList();
orderedTriple sumForces(orderedTriple test);
orderedTriple rungeKutta(orderedTriple test);
};
//Function declarations
node makeNode(orderedTriple);
node makeNode(double, double);
node makeCharacterNode(char);
linkedList createList();
/*
int main(){ //?
linkedList two;
node v=makeNode(10,3);
node i=makeNode(4,2);
node c=makeNode(7,1);
node t=makeNode(1,0);
two.addNode(v);
two.addNode(i);
two.addNode(c);
two.addNode(t);
cout<<two.evaluate(2);
system("PAUSE");
return 0;
}
*/
//functions declared here.
linkedList::linkedList(){
current=NULL;
head=makeNode(0,0);
length=0;
}
void linkedList::addNode(node toAdd){
if(length==0){
head = toAdd;
length++;}
else{
transverse();
current->next=&toAdd;
length++;
}
current=&head;
}
double linkedList::evaluate(double a){//This is really dumb.
//cout<<current->coff<<endl;
current=&head;
double sum=0;
while(current->next!=NULL){
sum+=current->coff*pow(a,current->exp);
current=current->next;
}
sum+=current->coff*pow(a,current->exp);
return sum;
}
int linkedList::returnLength(){
return length;
}
node linkedList::returnCurrent(){
return *current;
}
void linkedList::transverse(){
while(current->next!=NULL){
current=current->next;
}}
void linkedList::incrementExponents(){
current=&head;
while(current->next!=NULL){
current->exp++;
current=current->next;
}
current->exp++;
}
linkedList linkedList::convertToNumbers(linkedList poly){
linkedList numbers;
linkedList subnumbers;
double numbscoff;
double numbsexp;
int plusOrMinus=1;
int subcreation=0;
char shownHere;
while (poly.returnCurrent().next!=NULL){
shownHere=poly.returnCharacter();
if ((int)shownHere>47&&(int)shownHere<58){
subcreation=(int)shownHere-48;
subnumbers.addNode(makeNode(subcreation, -1));
subnumbers.incrementExponents();
}
else if(shownHere=='x'||shownHere=='X'){
numbscoff=subnumbers.evaluate(10);
numbscoff*=plusOrMinus;
subnumbers.deleteList();
}
else if(shownHere=='^'){
//does nothing
}
else if(shownHere=='+'){
numbsexp=subnumbers.evaluate(10);
numbers.addNode(makeNode(numbscoff,numbsexp));
subnumbers.deleteList();
plusOrMinus=1;
}
else if(shownHere=='-'){
numbsexp=subnumbers.evaluate(10);
numbers.addNode(makeNode(numbscoff,numbsexp));
subnumbers.deleteList();
plusOrMinus=-1;}
else cout<<"you put something in wrong."<<endl;
poly.setCurrent(poly.returnCurrent().next);
}
return numbers;
}
char linkedList::returnCharacter(){
return current->p;
}
void linkedList::integrate()
{
current=&head;
while(current->next!=NULL){
current->coff=current->coff/current->exp;
current->exp--;
current=current->next;
}
current->coff=current->coff/current->exp;
current->exp--;
}
void linkedList::setCurrent(node* a){
current=a;
}
void linkedList::deleteList(){//Is this doing what I think its doing?
current=&head;
node* temp;
while(current->next!=NULL) {
temp=current->next;
current=temp;
delete current;
}
head.coff=0;
head.exp=0;
}
void linkedList::displayCharacterList(){
current=&head;
while(current->next!=NULL){
cout<<current->p;
current=current->next;
}
cout<<current->p;
}
void linkedList::displayTripleList(){
current=&head;
cout << "Head";
system("PAUSE");
while(current->next != NULL){ //Supererror
display(current->dunce);
cout << "Node";
system("PAUSE");
current=current->next;
}
cout << "Node";
system("PAUSE");
display(current->dunce)
/*orderedTriple linkedList::sumForces(orderedTriple test){
current=&head;
orderedTriple forces;
while(current->next!=NULL){
forces+=addForce(evalForce(current,test),forces);
current=current->next;
}
return forces;
}*/
/*
orderedTriple linkedList::rungeKutta(orderedTriple test){
orderedTriple Rachy;
Rachy=sumForces(test);
Rachy+=sumForces(
*/
//end of linkedList functions
node makeNode(double coefficient, double exponent){
node newNode;
orderedTriple empty;
newNode.coff=coefficient;
newNode.exp=exponent;
newNode.next=NULL;
newNode.dunce=empty;
newNode.p='t'; //We'll let T stand for nothing. It gives me an error to do ''.
return newNode;
}
node makeNode(orderedTriple Rachel) {
node newNode;
newNode.coff=0;
newNode.exp=0;
newNode.p='t';
newNode.dunce = Rachel;
newNode.next = NULL;
return newNode;
}
node makeCharacterNode(char a){ //This function allows me to adapt the linkedList class to the polynomial
orderedTriple empty;
node newNode; // which the user will input.
newNode.coff=0;
newNode.dunce=empty;
newNode.exp=0;
newNode.p=a;
newNode.next=NULL;
return newNode;
}
linkedList createList(){
linkedList toBeReturned;
node b;
char polynomial;
while(polynomial!=';'){
cin>>polynomial;
b=makeCharacterNode(polynomial);
if(polynomial!=';')
toBeReturned.addNode(b);
}
}
//the function below will take in a polynomial and make it nodes and linked lists.
linkedList fetchSpace(){
ifstream coming ("dataset.txt");
linkedList spaceStation;
int n=1; // tells how far past a decimal point you are
bool hitDecimal=false;
orderedTriple toBeAdded;
double upcomingValue=0;
char nextChar;
if(coming.is_open()){
while(!coming.eof()){
coming>>nextChar;
coming>>nextChar; //skips opening parenthesis
if(coming.eof()) break;
upcomingValue=(int)nextChar-48;
do{
coming>>nextChar;
if(nextChar=='.')hitDecimal=true;
if(hitDecimal==false&&nextChar!=','){
upcomingValue*=10;
upcomingValue+=(int)nextChar-48;}
else if (nextChar!='.'&&nextChar!=','){
upcomingValue+=((int)nextChar-48)*pow(10,-n);
n++;
}
}while (nextChar!=',');
n=1;
hitDecimal=false;
toBeAdded.setx(upcomingValue);
upcomingValue=0;
do{
coming>>nextChar;
if(nextChar=='.')hitDecimal=true;
if(hitDecimal==false&&nextChar!=','){
upcomingValue*=10;
upcomingValue+=(int)nextChar-48;}
else if (nextChar!='.'&&nextChar!=','){
upcomingValue+=((int)nextChar-48)*pow(10,-n);
n++;
}
}while (nextChar!=',');
n=1;
hitDecimal=false;
toBeAdded.sety(upcomingValue);
upcomingValue=0;
do {
coming>>nextChar;
if(nextChar=='.')hitDecimal=true;
if(hitDecimal==false&&nextChar!=')'){
upcomingValue*=10;
upcomingValue+=(int)nextChar-48;}
else if (nextChar!='.'&&nextChar!=')'){
upcomingValue+=((int)nextChar-48)*pow(10,-n);
n++;
}
}while (nextChar!=')');
n=1;
hitDecimal=false;
toBeAdded.setz(upcomingValue);
upcomingValue=0;
spaceStation.addNode(makeNode(toBeAdded));
}
}
else cout<<"fileerror";
coming.close();
return spaceStation;
}
orderedTriple evalForce(node Emily, orderedTriple test){ //this orderedTriple will be a vector.
double rSquare=pow(Emily.dunce.returnx()-test.returnx(),2)+pow(Emily.dunce.returny()-test.returny(),2);
//find n such that (ny)^2+(nx)^2=r^2
double why=(test.returny()-Emily.dunce.returny());
double ehx=(test.returnx()-Emily.dunce.returnx());
double n=rSquare/(pow(why,2)+pow(ehx,2));
why*=n;
ehx*=n;
orderedTriple Gigi(ehx, why, 0); // Written with ihats and jhats
return Gigi;
}
orderedTriple addForce(orderedTriple first, orderedTriple second){
first.setx(first.returnx()+second.returnx());
first.sety(first.returny()+second.returny());
return first;
}
int main(){
orderedTriple k;
node n=makeNode(k);
cout<<n.dunce.returnx();
linkedList j;
k.setx(1);
k.sety(1);
k.setz(1);
j.addNode(makeNode(k));
k.setx(2);
k.sety(2);
k.setz(2);
j.addNode(makeNode(k));
k.setx(4);
k.sety(4);
k.setz(4);
j.addNode(makeNode(k));
//linkedList p=fetchSpace();
j.displayTripleList();
system("PAUSE");
return 0;
}
If anyone could tell me how to fix my bug, it would be greatly appreciated.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|