Ok... so i am making a rather simple calculator, that solves for either derivatives or a polynomial.
Though I have to make my own linkedlist is the challenge.
Ok, I will just copy and paste the LinkedList.cpp file:
#include <stdlib.h>
#include <cstdlib>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include "LinkedList.h"
#include "main.h"
using namespace std;
void LinkedList::add(string imissjava) {
temp = (node*)malloc(sizeof(node));
temp->data = imissjava; // store data(first field)
temp->next=head; // store the address of the pointer head(second field)
head = temp;
}
string LinkedList::get() {
node *temp1 = NULL;
temp1=head;
while( temp1!=NULL )
{
cout<< temp1->data<<" ";// show the data in the linked list
temp1 = temp1->next; // tranfer the address of 'temp->next' to 'temp'
}
}
And here is the header file for it(which I got help on, since I am noob at headerfiles, aka I started c++ like 2 weeks ago, but I know java good)
/*
* File: LinkedList.h
* Author: JigglyWiggly
*
* Created on January 21, 2010, 8:02 PM
*/
#include <stdlib.h>
#include <cstdlib>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include "main.h"
//#include "node.h"
using namespace std;
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
struct node{
string data;
node *next; // Pointer to next node
};
class LinkedList {
public:
//put all the functions and variables here
//void function(int);
void add(string);
string get();
node *start_ptr;
node *head;
node *temp;
};
#endif /* _LINKEDLIST_H */
Ok so, ignore most of the cost I am going to put, except the first 3 lines in the main:
#include <stdlib.h>
#include <cstdlib>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include "LinkedList.h"
#include "main.h"
using namespace std;
//c::getmemorylocationnode(); //empty linked list
double holdingeval = 0;
string userinput = "no";
typedef bool boolean;
boolean istherex = true;
double whatisx = 0;
double d;
double e;
double evaluate(double x, double y, bool c) {
holdingeval += ::pow(x, y);
if (c == true) {
holdingeval = ::pow(x, y) * whatisx;
}
return (holdingeval);
}
void printout() {
}
int main(int argc, char** argv) {
LinkedList* sr = new LinkedList(); //These are the three lines I was talking about up above.
string dis="hi";
sr->add(dis);
string userinputlocal;
cout << "Would you like to solve for the derivative or the polynomial?" << endl;
cin >> userinputlocal;
string poly = "polynomial";
string deriv = "derivative";
if (userinputlocal == poly) {
cout << "What is X" << endl;
cin >> whatisx;
while (userinput == "no") {
cout << "Enter the first integer" << endl;
cin >> d;
cout << "Enter the exponent" << endl;
string istherevar;
cout << "Is there a variable in this part of the equation?" << endl;
cin >> istherevar;
if (istherevar == "yes") {
istherex = true;
} else if (istherevar == "no") {
istherex = false;
} else {
cout << "Type yes or no" << endl;
return 0;
}
holdingeval = holdingeval + ::evaluate(d, e, istherex);
string overyet;
cout << "Done...?" << endl;
cin >> overyet;
if (overyet == "yes") {
cout << holdingeval << endl;
return 0;
}
}
}
if (userinputlocal == deriv) {
while (userinput == "no") {
cout << "Enter the first integer" << endl;
cin >> d;
cout << "Enter the exponent" << endl;
cin >> e;
string vr;
cout << "Is there a variable in this part of the equation?" << endl;
cin >> vr;
if (vr == "yes") {
istherex = true;
} else {
istherex = false;
}
string wasd = "";
cout << "Done?" << endl;
cin >> wasd;
if(wasd=="yes") {
return 0;
}
}
}
return (EXIT_SUCCESS);
}
Running this (I am using netbeans with cygwin)
C:\Program Files (x86)\NetBeans 6.8\dlight2\bin\nativeexecution\dorun.sh: line 3
3: 4636 Segmentation fault (core dumped) sh "${SHFILE}"
Press [Enter] to close the terminal ...
If I take those lines out saying
LinkedList* sr = new LinkedList();
string dis="hi";
sr->add(dis);
It works then, but I need to create a linkedlist.
Any help?
Also here is main.h file
/*
* File: main.h
* Author: JigglyWiggly
*
* Created on January 21, 2010, 8:07 PM
*/
#include <stdlib.h>
#include <cstdlib>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#ifndef _MAIN_H
#define _MAIN_H
double evaluate(double, double, bool);
#endif /* _MAIN_H */