<<operator is only printing an empty line - c++

In main the list prints out using the insertion operator, but all that I get is an empty line. Not sure why. Is it that there is nothing being stored in the list even when you use the set_coefficient? Any other critiquing is welcome. Thanks
here's the driver:
#include <iostream>
#include "polynomial.h"
using namespace std;
int main(){
Polynomial* poly = new Polynomial();
poly->set_coefficient(3,2);
poly->set_coefficient(0,2);
poly->set_coefficient(3,1);
cout << "trying to print data" << endl;
cout << *poly << endl;
return 0;
}
Here's the header:
#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_
#include <iostream>
class Polynomial {
public:
struct PolyNode {
int coefficient, degree;
struct PolyNode* next;
PolyNode(int c, int d, PolyNode* n): coefficient(c),degree(d),next(n){}
};
PolyNode* firstTerm;
Polynomial(): firstTerm(0) {}
struct PolyNode* get_first(){
return firstTerm;
}
//makes the term with degree d have a coefficient of c
void set_coefficient(int c, int d);
~Polynomial();
friend std::ostream& operator<<(std::ostream& o, const Polynomial& p);
};
#endif
Here's the implementation:
#include "polynomial.h"
#include <iostream>
#include <ostream>
using namespace std;
void Polynomial::set_coefficient(int c, int d){
PolyNode* start = firstTerm;
if(c != 0 && firstTerm == 0)
firstTerm = new PolyNode(c,d,NULL);
else{
cout << "Entered set_coefficient()" << endl;
while(start->degree != d && start->next != NULL){
cout << "Inside set_coefficient() while loop" << endl;
start = start->next;
}
if(c != 0 && start == 0)
start = new PolyNode(c,d,0);
else if(c!= 0 && start != 0)
start->coefficient = c;
else if(c == 0){
cout << "deleting a term" << endl;
delete start;
}
}
cout << "Leaving set_coefficient()" << endl;
}
ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;
for(unsigned int i = 0; start->next != 0; i++){
o << "Term " << i << "'s coefficient is: " << start->coefficient << " degree is: " << start->degree << endl << flush;
start = start->next;
}
return o;
}

if(c != 0 && start == 0)
start = new PolyNode(c,d,0);
else if(c!= 0 && start != 0)
start->coefficient = c;
else if(c == 0){
cout << "deleting a term" << endl;
delete start;
}
if (c != 0 && start == 0) // this situation may occur ? you have already judged surrounding if
else if (c == 0) {
cout << "deleting a term" << endl;
delete start; // that may be cause linkedList detach.
}

cout << "Entered set_coefficient()" << endl;
while(start->degree != d && start->next != NULL){
cout << "Inside set_coefficient() while loop" << endl;
start = start->next;
}
if(c != 0 && start == 0)
start = new PolyNode(c,d,0);
else if(c!= 0 && start != 0)
start->coefficient = c;
else if(c == 0){
cout << "deleting a term" << endl;
delete start;
}
It appears you want a linked list, but you're neither creating nor removing links (start->next), only allocating memory. Without setting up the links, there is no list.

There are probably multiple bugs here :>
ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;
for(unsigned int i = 0; start->next != 0; i++){
this won't print the last PolyNode...

Related

I m not getting correct output for pre and post order

// in this code I first created nodes stored them in a que and keep on removing them as I entered their left and right children. To make a node have no further children I entered -1 while entering data. Here I am not able to understand what is wrong with my code , I am getting wrong output for preorder and postorder traversals. It would be really great if you guys could help me out.
I made a class que for queue ds and inherited it in tree class in protected mode.
#include <iostream>
#include <math.h>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
class que
{
protected:
int start;
int end;
struct node **arr;
int n;
public:
que(int x)
{
n = x;
arr = new struct node *[n];
start = -1;
end = -1;
}
void isfull()
{
if (end == n)
cout << "Queue is full !!!" << endl;
return;
}
int isempty()
{
if (start == end)
{
start = -1;
end = -1;
cout << "Queue is empty !!!" << endl;
return 1;
}
return 0;
}
void enqu(struct node *x)
{
if (end == n)
{
cout << "called" << endl;
isfull();
return;
}
end++;
arr[end] = x;
}
struct node *dequ(void)
{
struct node *q = 0;
if (start == end)
{
isempty();
return q;
}
start++;
cout << "Element removed is ->" << arr[start] << endl;
return arr[start];
}
};
class tree : protected que
{
public:
struct node *head;
struct node *ptr;
tree(int n) : que(n)
{
head = 0;
ptr = 0;
enter();
}
void create(void)
{
ptr = new struct node;
ptr->left = 0;
ptr->right = 0;
}
void enter(void)
{
struct node *p;
if (head == 0)
{
create();
cout << "Enter root element of tree -> ";
cin >> ptr->data;
head = ptr;
cout << "Enquing ptr - " << ptr << endl;
enqu(ptr);
}
while (!isempty())
{
p = dequ();
cout << "Enter left child ->";
int x;
cin >> x;
if (x != -1)
{
create();
p->left = ptr;
ptr->data = x;
cout << "Enquing ptr - " << ptr << endl;
enqu(ptr);
}
cout << "Enter right child ->";
cin >> x;
if (x != -1)
{
create();
p->right = ptr;
ptr->data = x;
cout << "Enquing ptr - " << ptr << endl;
enqu(ptr);
}
}
}
void inorder(struct node *yes)
{
if (yes != 0)
{
inorder(yes->left);
cout << "--> " << yes->data << endl;
inorder(yes->right);
}
}
void preorder(struct node *yes)
{
if (yes != 0)
{
cout << "--> " << yes->data << endl;
inorder(yes->left);
inorder(yes->right);
}
}
void postorder(struct node *yes)
{
if (yes != 0)
{
inorder(yes->left);
inorder(yes->right);
cout << "--> " << yes->data << endl;
}
}
int count(struct node *yes)
{
static int x = 0, y = 0;
if (yes == 0)
return 0;
x = count(yes->left);
y = count(yes->right);
return x + y + 1;
}
int height(struct node *yes)
{
static int a = 0, b = 0;
if (yes == 0)
return 0;
a = count(yes->left);
b = count(yes->right);
if (a > b)
return a + 1;
else
return b + 1;
}
};
int main()
{
int x;
cout << "Enter height of tree - ";
cin >> x;
int max = 0;
max = pow(2, x + 1) - 1;
tree tr(max);
cout << "Preorder traversal -- " << endl;
tr.preorder(tr.head);
cout << "Inorder traversal -- " << endl;
tr.inorder(tr.head);
cout << "Postorder traversal -- " << endl;
tr.postorder(tr.head);
cout << "\n No. of elements -- " << tr.count(tr.head);
cout << "\n Height of tree --" << tr.height(tr.head);
}
The preorder and postorder functions doesn't call themselves recursively. Instead they call the inorder function, which will lead to all but the root will be printed using inorder.

AVLTree can't find data when searched for

I am working on an assignment for school. I have to create an AVLTree, read data from a text file and then create Professor object with that data. However, when I try and search for a Professor by name, it can't be found. It keeps defaulting to Michael Scott in the program and can't find who I'm looking for. My readFromFile function works perfectly and gets me all the data from the file, so I have the data. At this point it's a matter of issues with the AVL Tree. I'll link everything below.
Driver.cpp:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "AVLTree.h"
using namespace std;
void readFromFile(AVLTree *treeAccess) {
int numOfProfs = 0;
string line;
const char *path="/Users/collinchappell/Desktop/Program4Official/Program4Official/ProfessorData.txt";
fstream myFile (path, ios::in);
if (!myFile) {
cout << "Sorry can't read ProfessorData.txt!" << endl;
}
else {
// Splitting up all the data in the .txt file and the looping through using string stream
// Not the best solution, however nothing else would work for me so this is what I went with.
while (getline(myFile, line)) {
string n;
string nT;
string s;
int b1,b2,b3,b4,b5,b6,b7;
replace(line.begin(), line.end(), '$', ' ');
stringstream ss(line);
for (int k = 0; k < 11; k++) {
ss >> n;
ss >> nT;
n = n + " " + nT;
ss >> s;
ss >> b1;
ss >> b2;
ss >> b3;
ss >> b4;
ss >> b5;
ss >> b6;
ss >> b7;
numOfProfs++;
Professor p(n, s, b1, b2, b3, b4, b5, b6, b7);
cout << p;
treeAccess->insertNode(n, &p);
}
}
}
cout << numOfProfs << " Proffesors have added from the file." << endl << endl;
}
int main() {
AVLTree tree;
readFromFile(&tree);
bool cont = true;
while (cont) {
cout << "Which Computer Science professor do you want details about?" << endl;
tree.displayInOrder();
cout << endl << endl << "Type Professor Name: ";
string typeName;
getline(cin, typeName);
while (tree.searchNode(typeName) == NULL) {
cout << "Couldn't find that professor! Please type another: ";
getline(cin, typeName);
}
cout << tree.searchNode(typeName);
cout << "Would you like to search another? (Y/N): ";
string ans;
cin >> ans;
if (ans == "y" || ans == "Y") {
continue;
}
else if (ans == "n" || ans == "N") {
return 0;
}
}
return 0;
}
AVLTree.h:
#ifndef AVLTree_h
#define AVLTree_h
#include <iostream>
#include "Professor.h"
using namespace std;
class AVLTree {
private:
// TreeNode structure
struct TreeNode {
string name;
Professor *prof;
struct TreeNode* left;
struct TreeNode* right;
};
// Root of tree
TreeNode* root;
// Function declarations
void insert(TreeNode *&nodePtr, TreeNode *&newNode);
void destroySubTree(TreeNode *nodePtr);
void displayInOrder(TreeNode *nodePtr) const;
int height(TreeNode *nodePtr);
int diff(TreeNode *nodePtr);
void balance(TreeNode *&temp);
TreeNode* l_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->right;
parent->right = temp->left;
temp->left = parent;
return temp;
}
TreeNode* r_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->left;
parent->left = temp->right;
temp->right = parent;
return temp;
}
TreeNode* lr_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->left;
parent->left = (l_rotation(temp));
return r_rotation(parent);
}
TreeNode* rl_rotation(TreeNode *parent) {
TreeNode *temp;
temp = parent->right;
parent->right = (r_rotation(temp));
return l_rotation(parent);
}
public:
AVLTree() { // Constructor
root = NULL;
}
~AVLTree() { // Destructor
destroySubTree(root);
}
void insertNode(string n, Professor* professor);
Professor* searchNode(string);
// Call private displayInOrder()
void displayInOrder() const {
displayInOrder(root);
}
};
#endif /* AVLTree_h */
AVLTree.cpp:
#include <iostream>
#include "AVLTree.h"
using namespace std;
// Insert a node into the tree recursively
void AVLTree::insert(TreeNode *&nodePtr, TreeNode *&newNode) {
if (nodePtr == NULL) {
nodePtr = newNode;
}
else if (newNode->name < nodePtr->name) {
insert(nodePtr->left, newNode);
balance(nodePtr);
}
else {
insert(nodePtr->right, newNode);
balance(nodePtr);
}
}
// Destory the sub tree
void AVLTree::destroySubTree(TreeNode *nodePtr) {
if (nodePtr) {
if (nodePtr->left) {
destroySubTree(nodePtr->left);
}
if (nodePtr->right) {
destroySubTree(nodePtr->right);
}
delete nodePtr;
}
}
// Display the tree in order
void AVLTree::displayInOrder(TreeNode *nodePtr) const {
if (nodePtr) {
displayInOrder(nodePtr->left);
cout << nodePtr->name << endl;
displayInOrder(nodePtr->right);
}
}
// Get the tree's height
int AVLTree::height(TreeNode *temp) {
int leftHeight, rightHeight;
if (temp) {
leftHeight = height(temp->left);
rightHeight = height(temp->right);
if (leftHeight > rightHeight) {
return leftHeight + 1;
}
else if (leftHeight < rightHeight) {
return rightHeight + 1;
}
else {
return 0;
}
}
return 0;
}
// Get the difference
int AVLTree::diff(TreeNode *temp) {
int l_height = height(temp->left);
int r_height = height(temp->right);
return l_height - r_height;
}
// Balance the tree
void AVLTree::balance(TreeNode *&temp) {
int bal_factor = diff (temp);
if (bal_factor > 1)
{
if (diff(temp->left) > 0) // 2, 1 RIGHT
{
temp = r_rotation(temp);
cout << "\nRIGHT rotation";
}
else // 2, -1 LEFT-RIGHT
{
temp = lr_rotation(temp);
cout << "\nLEFT-RIGHT rotation";
}
}
else if (bal_factor < -1)
{
if (diff (temp->right) > 0) // -2, 1 RIGHT-LEFT
{
temp = rl_rotation(temp);
cout << "\nRIGHT-LEFT rotation";
}
else // -2, -1 LEFT
{
temp = l_rotation(temp);
cout << "\nLEFT Rotation";
}
}
}
// Insert a node, calls the private insert function
void AVLTree::insertNode(string n, Professor *proffesor) {
TreeNode *newNode = NULL; // Pointer to a new node.
newNode = new TreeNode;
newNode->name = n;
newNode->prof = proffesor;
newNode->left = newNode->right = NULL;
// Insert the node.
insert(root, newNode);
}
Professor* AVLTree::searchNode(string s) {
cout << "Name inputted into search: " << s << endl;
cout << endl << "Actual name: " << root->name << endl;
TreeNode *nodePtr = root;
while (nodePtr != NULL) {
if (nodePtr->name == s) {
return nodePtr->prof;
}
else if (nodePtr->name > s) {
nodePtr = nodePtr->left;
cout << "Left -> " << nodePtr << endl;
}
else {
nodePtr = nodePtr->right;
cout << "Right -> " << nodePtr << endl;
}
}
return NULL;
}
Professor.h:
#ifndef PROFESSOR_H
#define PROFESSOR_H
#include <iostream>
#include <iomanip>
using namespace std;
class Professor
{
private:
string name;
string course;
bool clearGrading;
bool goodFeedback;
bool caring;
bool reachable;
bool toughGrader;
bool lectureHeavy;
bool attendance;
public:
Professor(string n, string course, bool cG, bool gF, bool c, bool r, bool tG, bool lH, bool a) {
this->name = n;
this->course = course;
this->clearGrading = cG;
this->goodFeedback = gF;
this->caring = c;
this->reachable = r;
this->toughGrader = tG;
this->lectureHeavy = lH;
this->attendance = a;
}
friend ostream &operator << (ostream &strm, Professor &p)
{
strm << endl << endl;
strm << setw(30) << "Professor:" << setw(20) << p.name << endl;
strm << setw(30) << "Course:" << setw(20) << p.course << endl;
strm << setw(30) << "Clear Grading Criteria:" << setw(20);
if(p.clearGrading == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Provides Good Feedback:" << setw(20);
if(p.goodFeedback == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Caring:" << setw(20);
if(p.caring == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Reachable Outside of Class:" << setw(20);
if(p.reachable == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Tough Grader:" << setw(20);
if(p.toughGrader == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Lecture Heavy:" << setw(20);
if(p.lectureHeavy == 0) strm << "no"; else strm << "yes";
strm << endl;
strm << setw(30) << "Attendance Mandatory:" << setw(20);
if(p.attendance == 0) strm << "no"; else strm << "yes";
strm << endl;
return strm;
}
};
#endif
ProfessorData.txt:
Michael Scott$CSC1310$0$0$1$1$0$1$1$Jim Halpert$CSC1310$1$1$1$0$0$0$0$Pam Beesly$CSC1300$1$1$1$0$1$0$1$Dwight Schrute$CSC2400$1$1$0$0$1$1$1$Angela Martin$CSC1300$1$0$0$0$1$1$1$Kelly Kapoor$CSC1300$0$0$0$0$0$0$0$Andy Bernard$CSC2400$1$0$1$1$0$1$1$Kevin Malone$CSC1310$0$1$1$1$0$0$0$Meredith Palmer$CSC1310$0$1$1$1$0$0$0$Phyllis Vance$CSC1300$1$1$1$0$1$0$0$Oscar Nunez$2400$1$1$1$1$1$1$1$
Running the program:
11 Proffesors have added from the file.
Which Computer Science professor do you want details about?
Andy Bernard
Angela Martin
Dwight Schrute
Jim Halpert
Kelly Kapoor
Kevin Malone
Meredith Palmer
Michael Scott
Oscar Nunez
Pam Beesly
Phyllis Vance
Type Professor Name: Andy Bernard
Name inputted into search: Andy Bernard
Actual name: Michael Scott
Left -> 0x100704570
Left -> 0x105a045b0
Left -> 0x105b04120
Left -> 0x105b041a0
Name inputted into search: Andy Bernard
Actual name: Michael Scott
Left -> 0x100704570
Left -> 0x105a045b0
Left -> 0x105b04120
Left -> 0x105b041a0
0x7ffeefbfee18Would you like to search another? (Y/N):
As you can see, I've put in some prints to kind of debug what's happening. I believe my objects are not getting inserted into the tree right, or my search function is off. Also, I'm not 100% on the balance function. Any help would be amazing.

Segmentation Fault 11 on return

I realize there are other answers like this one but seg faults are pretty general and none of them fit my situation exactly. I am creating a binary tree and storing data. In the printInOrder function, it recurses all the way to the furthest leftChild and then seg faults when it reaches the return statement. I am not sure why it is doing this. Thanks in advance for the help.
#include <string>
#include <cstdlib>
#include <iostream>
#include "BST.h"
using namespace std;
BST::BST(void) {
root = NULL;
cout << "Default object is being creating." << "\n";
};
/*
BST::BST(string word, int count) {
cout << "Default object is being creating." << "\n";
this->root->word = word;
this->root->count = count;
};
BST::BST(string word) {
cout << "Default object is being creating." << "\n";
this->root->word = word;
this->root->count = 1;
};
*/
bool BST::hasWord(string word) {
return true;
};
void BST::printInOrder() {
pio(this->root);
}
void BST::pio(struct node* node) {
cout << "root: " << root << endl;
cout << "node: " << node << endl;
cout << "leftChild: " << node->leftChild->leftChild << endl;
if (node != NULL){
/* first recur on left child */
pio(node->leftChild);
/* then print the data of node */
cout << "Word: " << node->word << endl;
cout << "Count: " << node->count << endl;
/* now recur on right child */
pio(node->rightChild);
}
if (node == NULL) {
return;
}
}
void BST::insert(string word)
{
if(this->root != NULL)
insert(word, this->root);
else
{
cout << "obj has been inserted " << endl;
this->root = new node;
cout << root << endl;
this->root->word = word;
this->root->count = 1;
this->root->leftChild = NULL;
this->root->rightChild = NULL;
}
}
void BST::insert(string word, struct node *leaf)
{
if(word< leaf->word)
{
if(leaf->leftChild != NULL)
insert(word, leaf->leftChild);
else
{
cout << "obj has been inserted " << endl;
leaf->leftChild = new node;
cout << leaf->leftChild << endl;
leaf->leftChild->word = word;
leaf->leftChild->count = 1;
leaf->leftChild->leftChild = NULL; //Sets the leftChild child of the child node to null
leaf->leftChild->rightChild = NULL; //Sets the rightChild child of the child node to null
}
}
else if(word>leaf->word)
{
if(leaf->rightChild != NULL)
insert(word, leaf->rightChild);
else
{
cout << "obj has been inserted " << endl;
leaf->rightChild = new node;
cout << leaf->rightChild << endl;
leaf->rightChild->word = word;
leaf->rightChild->count = 1;
leaf->rightChild->leftChild = NULL; //Sets the leftChild child of the child node to null
leaf->rightChild->rightChild = NULL; //Sets the rightChild child of the child node to null
}
}
//word is already in the tree so we increment counter
else leaf->count += 1;
}
Main file:
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <dirent.h>
#include <cstring>
#include <functional>
#include <algorithm>
#include <sstream>
#include "BST.h"
using namespace std;
void parseFile(string fullPath, BST bst) {
ifstream infile(fullPath); // Open it up!
std::string line;
char c;
string word = "";
while (std::getline(infile, line))
{
// Iterate through the string one letter at a time.
for (int i = 0; i < line.length(); i++) {
c = line.at(i); // Get a char from string
tolower(c);
// if it's NOT within these bounds, then it's not a character
if (! ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) ) {
//if word is NOT an empty string, insert word into bst
if ( word != "" ) {
bst.insert(word);
//reset word string
word = "";
}
}
else {
word += string(1, c);
}
}
}
}
void getReqDirs(BST bst, const string path, vector<string> files,const bool showHiddenDirs = false){
DIR *dpdf;
struct dirent *epdf;
dpdf = opendir(path.c_str());
int count = 0;
if (dpdf != NULL){
while ((epdf = readdir(dpdf)) != NULL){
if(showHiddenDirs ? (epdf->d_type==DT_DIR && string(epdf->d_name) != ".." && string(epdf->d_name) != "." ) : (epdf->d_type==DT_DIR && strstr(epdf->d_name,"..") == NULL && strstr(epdf->d_name,".") == NULL ) ){
getReqDirs(bst,path+epdf->d_name+"/",files, showHiddenDirs);
}
if(epdf->d_type==DT_REG){
//files.push_back(path+epdf->d_name);
//parseFile(path+epdf->d_name, bst);
cout << path+epdf->d_name << " ";
}
}
}
closedir(dpdf);
}
int main()
{
vector <string> words; // Vector to hold our words we read in.
string str; // Temp string to
cout << "Read from a file!" << endl;
BST bst;
string path = "test/"; //name of directory that holds the data base
vector<string> files; //create vector for file names
/*
//collect all the file names in the data base
getReqDirs(bst,path,files,false);
for (int i = 0; i < files.size(); i++) {
cout << files[i] << " ";
}*/
bst.insert("gary");
bst.insert("Mil");
bst.insert("Taylor");
bst.insert("BrIaN");
bst.insert("fart");
bst.insert("Juan");
bst.insert("James");
bst.insert("Gary");
bst.printInOrder();
//parseFile(path, bst);
return 0;
}
Please let me know if any other info is required.
The following line is the only problematic line of code I found in pio:
cout << "leftChild: " << node->leftChild->leftChild << endl;
It is a problem since node is bound be NULL at some point.
Remove that line.
Also, you don't need the lines
if (node == NULL) {
return;
}
They don't do anything useful and can be removed. Here's an updated version of the function:
void BST::pio(struct node* node) {
cout << "root: " << root << endl;
cout << "node: " << node << endl;
if (node != NULL){
/* first recur on left child */
pio(node->leftChild);
/* then print the data of node */
cout << "Word: " << node->word << endl;
cout << "Count: " << node->count << endl;
/* now recur on right child */
pio(node->rightChild);
}
}

overloaded << insertion operator isn't working correctly

So I can't figure out why my insertion operator isn't working for my list class. I've looked at it for a while and I think the syntax is correct for overloading. Not sure on this one. Any hints as to why it's not working?? Here's the code:
EDIT:Changed some code around to what it currently is right now.
Sorry, the problem specifically now is that I cannot get it to print anything out, it simple prints and empty line.
here's the driver:
#include <iostream>
#include "polynomial.h"
using namespace std;
int main(){
Polynomial* poly = new Polynomial();
poly->set_coefficient(3,2);
poly->set_coefficient(0,2);
poly->set_coefficient(3,1);
cout << "trying to print data" << endl;
cout << *poly << endl;
return 0;
}
Here's the header:
#ifndef _POLYNOMIAL_H_
#define _POLYNOMIAL_H_
#include <iostream>
class Polynomial {
public:
struct PolyNode {
int coefficient, degree;
struct PolyNode* next;
PolyNode(int c, int d, PolyNode* n): coefficient(c),degree(d),next(n){}
};
PolyNode* firstTerm;
Polynomial(): firstTerm(0) {}
struct PolyNode* get_first(){
return firstTerm;
}
//makes the term with degree d have a coefficient of c
void set_coefficient(int c, int d);
~Polynomial();
friend std::ostream& operator<<(std::ostream& o, const Polynomial& p);
};
#endif
Here's the implementation:
#include "polynomial.h"
#include <iostream>
#include <ostream>
using namespace std;
void Polynomial::set_coefficient(int c, int d){
PolyNode* start = firstTerm;
if(c != 0 && firstTerm == 0)
firstTerm = new PolyNode(c,d,NULL);
else{
cout << "Entered set_coefficient()" << endl;
while(start->degree != d && start->next != NULL){
cout << "Inside set_coefficient() while loop" << endl;
start = start->next;
}
if(c != 0 && start == 0)
start = new PolyNode(c,d,0);
else if(c!= 0 && start != 0)
start->coefficient = c;
else if(c == 0){
cout << "deleting a term" << endl;
delete start;
}
}
cout << "Leaving set_coefficient()" << endl;
}
ostream& operator<<(ostream& o,const Polynomial& p){
Polynomial::PolyNode* start = p.firstTerm;
for(unsigned int i = 0; start->next != 0; i++){
o << "Term " << i << "'s coefficient is: " << start->coefficient << " degree is: " << start->degree << endl << flush;
start = start->next;
}
return o;
}
poly is a pointer, to use your custom operator << you need to say
cout << *poly; // output the object pointed-to, not the pointer itself
You haven't overloaded what it means to insert a Polynomial*. Nor should you try.
Besides that, you probably should accept the object by const reference, there's no reason for a stream output operator to go changing the object.

Basic polynomial reading using linked lists

Ok, after failing to read a polynomial, I'm trying first a basic approach to this.
So i have class polinom with function read and print:
#ifndef _polinom_h
#define _polinom_h
#include <iostream>
#include <list>
#include <cstdlib>
#include <conio.h>
using namespace std;
class polinom
{
class term
{
public:
double coef;
int pow;
term(){
coef = 0;
pow = 0;
}
};
list<term> poly;
list<term>::iterator i;
public:
void read(int id)
{
term t;
double coef = 1;
int pow = 0;
int nr_term = 1;
cout << "P" << id << ":\n";
while (coef != 0) {
cout << "Term" << nr_term << ": ";
cout << "coef = ";
cin >> coef;
if (coef == 0) break;
cout << " grade = ";
cin >> pow;
t.coef = coef;
t.pow = pow;
if (t.coef != 0) poly.push_back(t);
nr_term++;
}
}
void print(char var)
{
for (i=poly.begin() ; i != poly.end(); i++ ) { //going through the entire list to retrieve the terms and print them
if (poly.size() < 2) {
if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient
cout << i->coef;
else if (i->pow == 1) {
if (i->coef == 1)
cout << var;
else if (i->coef == -1)
cout << "-" << var;
else
cout << i->coef << var;
}
else
cout << i->coef << var << "^" << i->pow; //otherwise we print both
}
else {
if (i == poly.end()) { // if we reached the last term
if (i->pow == 0) //if the last term's power is 0 we print only it's coefficient
cout << i->coef;
else if (i->pow == 1)
cout << i->coef << var;
else
cout << i->coef << var << "^" << i->pow; //otherwise we print both
}
else {
if (i->coef > 0) {
if (i->pow == 1)//if the coef value is positive
cout << i->coef << var << " + "; //we also add the '+' sign
else
cout << cout << i->coef << var << "^" << i->pow << " + ";
}
else {
if (i->pow == 1)//if the coef value is positive
cout << i->coef << var << " + "; //we also add the '+' sign
else
cout << cout << i->coef << var << "^" << i->pow << " + ";
}
}
}
}
}
};
#endif
Well, it works when reading only one term but when reading more the printed coefficients are some random values and also after the last term it print '+' or '-' when it shouldn't.
So any idea what's wrong?
Thanks!
FINAL UPDATE
Ok, i made it work perfectly by modifying Bill's code so thanks a lot Bill and everyone else who commented or answered!
Here's the final print function:
void print(char var)
{
list<term>::iterator endCheckIter;
for (i=poly.begin() ; i != poly.end(); i++ )
{
//going through the entire list to retrieve the terms and print them
endCheckIter = i;
++endCheckIter;
if (i->pow == 0)
cout << i->coef;
else if (i->pow == 1)
cout << i->coef << var;
else
cout << i->coef << var << "^" << i->pow;
if (endCheckIter != poly.end()) {
if (endCheckIter->coef > 0)
cout << " + ";
else {
cout << " - ";
endCheckIter->coef *= -1;
}
}
}
}
if (i == poly.end()) { // if we reached the last term
This comment shows your error. For any given collection of items, items.end() returns the entry after the last item.
For instance, say I have a 5-item std::vector:
[0] [1] [2] [3] [4]
Then begin() points to:
[0] [1] [2] [3] [4]
/\
And end() points to:
[0] [1] [2] [3] [4] []
/\
Your for loop, it looks like:
for (i=poly.begin() ; i != poly.end(); i++ )
Note that comparing i to poly.end() happens before iter is used. As soon as i == poly.end(), you're done.
Your code inside of if (i == poly.end()) { will never be executed because this can never be true.
You can test for the end using the following:
// get access to the advance function
#include <iterator>
....
std::list<term>::iterator endCheckIter = i;
std::advance(endCheckIter, 1);
if (endCheckIter == poly.end())
{
...
}
But a simpler way might be:
std::list<term>::iterator endCheckIter = i;
++endCheckIter;
if (endCheckIter == poly.end())
{
...
}
Edit:
I'm not sure why you're getting garbage. Add in your missing braces and handle the non-end case, and everything works here:
void print(char var)
{
list<term>::iterator endCheckIter;
for (i=poly.begin() ; i != poly.end(); i++ )
{ // <- MISSING BRACE
//going through the entire list to retrieve the terms and print them
endCheckIter = i;
++endCheckIter;
cout << i->coef << var << "^" << i->pow; // <- MISSING OUTPUT
if (endCheckIter != poly.end()) {
if (i->coef > 0)
cout << " + ";
else
cout << " - ";
}
} // <- MISSING BRACE
}
Okay, now that Vlad has decided how he's going to do it, here's how I'd do it:
#ifndef _polinom_h
#define _polinom_h
#include <iostream>
#include <list>
#include <cstdlib>
#include <cmath>
#include "infix_iterator.h"
using namespace std;
char var;
class polinom {
class term {
double coef;
int power;
ostream &write(ostream &os) const {
// At least to me, the logic is easier to follow if we
// handle one piece at a time.
// It may be longer, but I think it's easier to understand.
// First, if the coefficient is negative, subtract the term instead of adding it.
if (coef < 0)
// backspace over the "+ " and print '- ' in its place.
os << "\b\b- ";
// Then print the absolute value of the coefficient (if needed).
if (fabs(coef) != 1)
os << fabs(coef);
// Then print the var (if needed)
if (power != 0)
os << var;
// then print the power (if needed)
if (abs(power) > 1)
os << "^" << power;
// And we're done.
return os;
}
// support inserting a term into a stream.
friend std::ostream &operator<<(std::ostream &os, term const &t) {
return t.write(os);
}
public:
term(double c=0.0, int p=0) : coef(c), power(p) {}
bool read(std::ostream &os, std::istream &is, int num) {
// This is only slightly modified from the originally posted question
os << "\nTerm " << num << ": coef = ";
is >> coef;
if (coef == 0.0)
return false;
if (coef != 0.0) {
os << " grade = ";
is >> power;
}
return true;
}
bool operator<(term const &other) const {
// order by descending powers.
return other.power < power;
}
};
list<term> poly;
public:
void read(int id) {
term t;
int nr_term = 1;
std::cout << "P: " << id;
// Read and save individual terms:
while (t.read(std::cout, std::cin, nr_term++))
poly.push_back(t);
}
void write(char var) {
// sort the polynomial so the highest powers come first.
poly.sort();
// save the variable name for later use.
::var = var;
// Print out all the terms:
std::copy(poly.begin(), poly.end(), infix_ostream_iterator<term>(std::cout, " + "));
}
};
#endif
Using this is pretty trivial:
#include "polynom.h"
int main() {
polinom p;
p.read(1);
p.write('x');
return 0;
}
void print(char var)
{
for (list<term>::const_iterator i = poly.begin(), e = poly.end(); i != e; ++i) {
if (i != poly.begin() || i->coef < 0) {
cout << (i->coef > 0 ? '+' : '-');
}
if (abs(i->coef) != 1) {
cout << abs(i->coef);
}
if (i->pow == 0) {
if (abs(i->coef) == 1) {
cout << 1;
}
} else {
cout << var;
if (i->pow != 1) {
cout << '^' << i->pow;
}
}
}
}