Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save majedsiefalnasr/45c57abaeb71cbe53feb28ba1c19ec45 to your computer and use it in GitHub Desktop.
Save majedsiefalnasr/45c57abaeb71cbe53feb28ba1c19ec45 to your computer and use it in GitHub Desktop.
Polynomial expression with Linked list
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
struct node{
int Coefficient_field;
int Exponent_field;
node *next;
};
class Node_d{
public:
node *first, *temp;
void add_last(){
node *tempx = first;
if (tempx != NULL)
{
while (tempx->next != NULL)
tempx = tempx->next;
tempx->next = temp;
}
else
first = temp;
}
Node_d(){
first = NULL;
}
void insert_n(int cf, int ef){
temp = new node;
temp->Coefficient_field = cf;
temp->Exponent_field = ef;
temp->next = NULL;
add_last();
}
int length(){
int count = 0;
temp = first;
while (temp != NULL){
count++;
temp = temp->next;
}
return count;
}
node *search(int ef)
{
node *tempx = first;
while (tempx != NULL)
{
if (ef == tempx->Exponent_field)
return tempx;
else
tempx = tempx->next;
}
return NULL;
}
void display(){
node *tempx;
tempx = first;
string sign;
if (tempx != NULL)
{
while (tempx->next != NULL){
if (tempx == first)
cout << "X^" << tempx->Exponent_field;
else
{
if (tempx->Coefficient_field > 0)
cout << " + " << tempx->Coefficient_field << "X^" << tempx->Exponent_field;
else if (tempx->Coefficient_field < 0)
cout << tempx->Coefficient_field << "X^" << tempx->Exponent_field;
}
tempx = tempx->next;
}
if (tempx->next == NULL)
{
if (tempx == first)
cout << "X^" << tempx->Exponent_field;
else
{
if (tempx->Coefficient_field > 0)
cout << " + " << tempx->Coefficient_field << "X^" << tempx->Exponent_field;
else if (tempx->Coefficient_field < 0)
cout << tempx->Coefficient_field << "X^" << tempx->Exponent_field;
}
}
}
else
cout << "there is no values" << endl;
}
};
// Split Function
vector<string> split(string str, char delimiter) {
vector<string> internal;
stringstream ss(str); // Turn the string into a stream.
string tok;
while (getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
void main(){
string expression1, expression2;
Node_d E1, E2, R;
vector<string> after_split, next_split;
cout << "Enter POLYNOMIAL EXPRESSION n.1 :";
getline(cin, expression1);
after_split = split(expression1, ' ');
for (int i = 0; i < after_split.size(); i++)
{
next_split = split(after_split[i], ',');
E1.insert_n(stoi(next_split[0]), stoi(next_split[1]));
}
cout << "Enter POLYNOMIAL EXPRESSION n.2 :";
getline(cin, expression2);
after_split = split(expression2, ' ');
for (int i = 0; i < after_split.size(); i++)
{
next_split = split(after_split[i], ',');
E2.insert_n(stoi(next_split[0]), stoi(next_split[1]));
}
node *search, *search_result;
Node_d search_in;
if (E1.length() >= E2.length())
{
search = E1.first;
search_in = E2;
}
else
{
search = E2.first;
search_in = E1;
}
while (search != NULL)
{
search_result = search_in.search(search->Exponent_field);
if (search_result != NULL)
R.insert_n(search->Coefficient_field + search_result->Coefficient_field, search->Exponent_field);
else
R.insert_n(search->Coefficient_field , search->Exponent_field);
search = search->next;
}
cout << "Final result :";
R.display();
cin.get();
cin.ignore();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment