C++ :: Adding And Multiplying Polynomials - Linked List And Operation Overloading
Mar 3, 2013
I am having a bit of difficulty with implementing an object oriented program that uses both linked lists and operator overloading. The program calls for adding and multiplying polynomials together, with each single polynomial being represented as a node of a linked list (which is further a data member of an object of a class I have defined to implement this program). For example:
polynomial A will be: 3x^4 // 1 node of a linked list
polynomial B will be: 5x^2 // 1 node of a linked list
polynomial C will be blank for the time being. // empty list
Now, I need to use operator overloading so that this following line of code can be implemented:
C = A + B;
C should now be: 3x^4 + 5x^2.
The checklist of which parts of my code that work:
constructor works;
copy constructor works;
destructor works;
operator= works;
print function needs work but i can worry about that later;
operator* work on later
Here is my code:
#include <iostream>
using namespace std;
struct termNode {
int exp; // exponent
int coef; // coefficient
termNode * next;
[Code] ....
For the time being I need to add multiple nodes together (with the result being in descending order). So for example:
polyType a(2,3), b(4,5), c(6,7), d;
d = a + b + c;
d.print(); // should print out 7x^6 + 5x^4 + 3x^2, but it will only print out: 3x^2 + 7x^6
View 5 Replies
ADVERTISEMENT
Apr 5, 2014
namespace main_savitch_5 {
class polynode {
public:
// CONSTRUCTOR: Creates a node containing a specified initial coefficient (init_coef), initial exponent (init_exponent), and initial links forward and backward (init_fore and init_back).
polynode(
double init_coef = 0.0,
unsigned int init_exponent = 0,
polynode* init_fore = nullptr,
[Code] ....
View 1 Replies
View Related
Jan 13, 2015
This program should add two polynomials, but for some reason, it just asks for polynomial degree and it's coefficients, then, it just stops
Code:
#include <stdio.h>
#define MAX 100
typedef struct polinom {
int n, cl[MAX], st[MAX];
[Code] ....
View 4 Replies
View Related
Dec 31, 2014
Code:
// Write a function called insertEntry() to insert a new entry into a linked list.
Have the procedure take as arguments a pointer to the list entry to be inserted (of type struct entry as defined in this chapter), and a pointer to an element in the list after which the new entry is to be inserted.
// The function dveloped in exercise 2 only inserts an element after an existing element in the list, thereby prenting you from inserting a new entry at the front of the list.
(Hint: Think about setting up a special structure to point to the beginning of the list.)
#include <stdio.h
struct entry1 {
int value;
struct entry1 *next;
};
[code]...
This is a working version of the exercise, but I don't think I'm doing what's asked. I was able to add an element to the beginning of the list using an if statement, not creating a special structure that points to the beginning of the list. How would I go about creating a special structure that points to the beginning of the list to add a new element at the beginning of the list?
View 8 Replies
View Related
Sep 11, 2014
I have code the following but result is not ok.
int main()
{
int i,j,m;
m = 0;
[Code].....
View 3 Replies
View Related
Jul 23, 2014
I wrote function to add to elements in the list, but when I call printList function it returns me empty list ! I'm new with linked list in C
Output:
Empty list
List is empty add element at the begining
New node with packet num 245
List is not empty add element at the end
New node with packet num 486
Linked list: Empty
Main:
Code:
int main(){
struct node * start ;
start = NULL;
int i;
/*Check if list is empty*/
if(start == NULL){
printf("Empty list
[Code]...
View 2 Replies
View Related
Feb 17, 2013
I'm trying to make a function that lets me pass an index and a string and will insert the string at that index in the linkedlist..
Code:
typedef struct node {
char* value;
struct node* next;
}
[code].....
View 4 Replies
View Related
Aug 18, 2013
I've been struggling with linked list for a few days. I've tried performing a few operations. Here is my code. the add_begin() function doesn't seem to work.
#include<iostream>
using namespace std;
struct node {
int data;
node *next;
[Code] .....
View 8 Replies
View Related
Oct 17, 2014
Ok so I am having difficulty adding nodes to my linked list....
how to add a third node while keeping track of the address...Thats where I get lost..I don't know how to keep track of the addresses of the next node..
#include <iostream>
using namespace std;
typedef struct Node{
[Code]......
View 1 Replies
View Related
Mar 6, 2015
I am having trouble adding a second node to a doubly linked list. I define my list as follows:
Code:
typedef struct Node {
char command[256];
int length;
struct Node *prev;
struct Node *next;
} userInput;
}
[code]...
Now, when I iterate back through my list (I want to start at the tail and work my way towards the head), I can only ever get the 1st node to print, then the 2nd node is garbage, which means, to me, that I've linked something wrong.
View 3 Replies
View Related
Dec 2, 2014
Should my if statement be
if (tail== NULL) or if(tail->next == tail) ?
View 4 Replies
View Related
Sep 22, 2014
I am currently trying to add to a linked list in sorted order but I have reached an impasse. I can get it to add in sorted order if it belongs in the beginning or second in the list. If i were to type in 9 then 4 i would get 49, but if i type in 5 it changes it to 559. I'm just at a loss and need some sort of direction.
#include "singly_linked_list.h"
#include <iostream>
using namespace std;
void add_node(node*& head_ptr, const int& payload){
if (head_ptr == nullptr) {
node* my_node = new node();
my_node->data = payload;
[Code]...
View 2 Replies
View Related
Oct 31, 2013
I have a .cpp file which contains 5 smaller defined classes. In my missile class I have a default constructor and a constructor that I invoke
class Missile{
private:
bool isHuman;
[Code]...
My issue is when creating and adding the pointer object; it doesn't seem to create a new instance of the class-the Missile objects all share the same xPos value which is always the first xPos when the "fire" command is given. The "Missile *missile = new Missile(xPos, yPos, true);" line does not seem to create a new instance of the object with different variables but instead creates a new object with the same variables. Is it necessary for me to always make a separate .cpp and .h file for any class I want to create multiple instances of or can I keep the smaller classes in the same file and still create a new separate instance of the class?
View 3 Replies
View Related
Oct 9, 2014
can we pass two linked lists as argument in a member function to add two linked lists? and how to access them both in the function??
View 1 Replies
View Related
Aug 1, 2013
#include<iostream.h>
#include<conio.h>
class sum {
int a,b,ans;
public:
void geta() {
cin>>a;
[Code] ....
View 1 Replies
View Related
Mar 1, 2013
I have tried to understand the concept of linked lists and I have read the assigned chapter 2 times. My teacher is a little laid back when it comes to teaching! This is only a portion of my program. This function is supposed to add 2 binary numbers 11101+1101 and store the result in the temp list. The answer I get is 10000.I don't think that it is adding the carry.
binary operator+( binary num1, binary num2) {
binary temp;
int carry, sum;
[Code]..
View 19 Replies
View Related
Apr 29, 2013
I'm trying to write a function that takes two linked lists and creates a third one with only the common elements.
It assumes the first list (the caller) has no dups, but it doesn't seem to be working. The program doesn't crash, it just hangs when it is supposed to display L3 (the third list)..everything else runs and is displayed fine.
template <typename T>
LList <T> LList <T>:: common (LList <T> &B)//common fct
{
Node <T> *hunter1 = Head;
[Code]......
View 10 Replies
View Related
Nov 19, 2014
I'm trying to use the given Iterators to overload my = operator to print my list to screen. I keep getting Link2019 error though and have narrowed the problem down to my print operator. I'm wondering if it has anything to do with the fact that my operator is in private part of class? I'm new to this concept.
Code:
#include "List.h"
// methods for Node all
//ME
Node::Node( const string &s, Node * p, Node * z) : word( s ), next( p ), prev(z)//constructor {
word = s; // init. word data with a copy of s
next = p; // next pointer points to p
[Code] .....
View 1 Replies
View Related
Jun 29, 2013
I have a linked list comprised of chars like so...
Code:
node1 - "p"
node2 - "o"
node3 - "p"
I need a function that will take in three perameters...node *replaceChar(node *head, char key, char *str)Stipulations of this function. head is the head of the list, 'key' and 'str' are guaranteed to contain alphanumeric characters only (A-Z, a-z, and 0-9). str can range from 1 to 1023 characters (inclusively). So if I call this function with these perameters..
Code:
node *head == /*the head of the list to be examined*/
char key == "p"char *str == "dog"The new list will look like this...
node1 - 'd'
node2 - 'o'
node3 - 'g'
node4 - 'o'
node5 - 'd'
node6 - 'o'
node7 - 'g'
All instances of 'p' were replaced with 'dog' I have a toString function which takes in a string and converts it to a linked list and returns the head. So assume that you can call the function on str = "dog" so...
Code:
toString(str) == /*this will return the head to the list made from the str*/
If it's unclear what my question is...I am stumped on how to write the replaceChar function the one that takes in three perameters..
View 3 Replies
View Related
Oct 25, 2014
The goal of this is to create a list of polynomials as a <Pol> vector, where Pol is the class I developed. It "reads" the .txt, gets the strings, transforms the polynomials in the strings into class Pol objects, and it places them in the polynomial vector.I remove the strings from the text where the polynomials are, and I convert those polynomials(in string) to actual polynomials ( "Convert" in MPol.C ,bellow) , but in line 169 from Mpol.C , when I try to put "temp" ( the highest degree of the polynomial), i get a "memory corruption" error or similar. However if instead of "temp", I place "temp+1" I no longer get any errors, but i get an extra coefficient.
Compilation: g++ -o teste2.exe Pol.C MPol.C teste2.C
Main is "teste2.c"
Pol.H
#ifndef __POL__
#define __POL__
#include <string>
#include <vector>
#include <iostream>
[code]....
View 2 Replies
View Related
Apr 3, 2013
I am currently working on a program and Originally I was to create a bash program to extra information from youtube and create a table of users, views, titles, duration, etc. (I sent each to a txt file)..Then I was to create a C program to use the extracted information. In this case Users and read in the conents of user.txt and construct a linked list to hold the information. Then print from the Linked list.I managed to do that,
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define LINE_LENGTH 100
void build_a_lst(), print_a_line(), print_lst();
void insert_at_end();
}
[code]...
What I have to do now is start reading in The other files and construct a table. And radix sort it by views. But to get to the radix sort part I get stuck on reading in all the files correctly and having the linked list hold them. I keep getting seg faults when I change my code.
View 3 Replies
View Related
Dec 11, 2014
I am writing a program to list the 8 planets, then you select which planet you want. it gives you the mass of the planet, the radius, then you use the mass and radius of the planet to find the surface area (sphere formula) and the density of the planet. Along with those options, you have to add and delete a planet from the list and then sort them alphabetically. All i am having trouble with is the adding and deleting part. The code the adding and deleting from the list would go in cases 9 and 10.
heres my code as of this point.
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
void print(const std::string& item) {
[Code] ....
View 5 Replies
View Related
Feb 25, 2014
So I have this code:
temp =count + " x " + name;
if (count > 0) { Stats.Inventory.Add(temp); }
Where Inventory is declared as
public static List<String> Inventory, Sellables;
and temp and count are declared locally as
double count = 0;
string name = string.Empty,temp = string.Empty;
An it throws an error
>Craft.exe!Craft.Stats.InventoryUpdate(string NewInventory) Line 166 + 0x73 bytesC#
My main questions is why? This is my first time using lists
View 14 Replies
View Related
Dec 11, 2014
i am writing a program to list the 8 planets, then you select which planet you want. it gives you the mass of the planet, the radius, then you use the mass and radius of the planet to find the surface area(sphere formula) and the density of the planet. along with those options, you have to add and delete a planet from the list and then sort them alphabetically. All i am having trouble with is the adding and deleting part. the code the adding and deleting from the list would go in cases 9 and 10.
heres my code as of this point.
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
void print(const std::string& item) {
[Code] ....
View 3 Replies
View Related
Jun 1, 2014
I've been trying to write my homework assignment (a list of countries, their codes and their capitals) and I've done most of it but I'm stuck at this: I have to open a file, read it and if there are data, add them to the list. So far I've created an element of the structure, queue list, printed the list on the screen and freed the memory. I thought that for reading the file and adding the data I could first open the file (of course) with fopen and after that use a for loop (i=0;i=!EOF;i++) to scan the whole file and fscanf(fp,"%s",result->country),etc in the loop to add the data to the structure of the element and finally insert that element to the queue list. However, when I try to do these operations, I only get to writing the name of the file and the program crashes.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct List {
char country[20];
char code[5];
char capital[20];
struct List*next;
[Code] .....
View 1 Replies
View Related
Jul 28, 2014
I tried to add a .a file to the list of link libraries, but when I build, I'm confronted with the following;
ld.exe||<the path>: Permission denied
I'm an admin on this machine - What?
View 14 Replies
View Related