You are on page 1of 7

// Solution of System of D.

E by Elimination Using Data Structure LINKED LIST


// Programmer Athar Aiman Khan Roll no [17271519-050] _ Section A CS 17

#include "pch.h"
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

struct operator_D {
bool exist;
int pow;
};

struct var {
char ch;
int pow;
operator_D D;
};

struct term {
int coeff;
var v;

term() {
coeff = v.pow = v.D.pow = 1;
v.ch = '\0';
v.D.exist = false;
}

term& operator= (const term& t) {


if (this != &t) {
coeff = t.coeff;
v.pow = t.v.pow;
v.D.pow = t.v.D.pow;
v.ch = t.v.ch;
v.D.exist = t.v.D.exist;
}
return *this;
}

};

template <typename T>


struct node {
T data;
node* next;

node() {
next = NULL;
}

node(node& n) {
data = n.data;
}
node& operator= (const node& n) {
data = n.data;
}

};
template <typename U>
class LinkList {
private:
U* head;
U* tail;
U* temp;
public:
LinkList() {
head = NULL;
tail = NULL;
temp = NULL;
}
bool is_empty() {
if (head == NULL)
return true;
return false;
}

void insert(U d) {
if (is_empty()) {
head = new U;
head->data = d.data;
head->next = NULL;
tail = head;
}
else {
temp = new U;
temp->data = d.data;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
}

void del() {
if (!is_empty()) {
temp = head;
U* ptr;
while (temp != NULL) {
ptr = temp;
temp = temp->next;
delete ptr;
}
}
}

void display() {
if (is_empty()) {
cout << "The List is Empty" << endl;
return;
}
temp = head;
while (temp->next != NULL) {
cout << temp->data;
temp = temp->next;
}
cout << temp->data << endl;
}

term getterm(char ind_var) {


if (!is_empty()) {
temp = head;
while (temp != NULL) {
if (temp->data.v.ch == ind_var)
return temp->data;
temp = temp->next;
}
}
}

void MultiplyCoeff(int c) {
if (!is_empty()) {
temp = head;
while (temp != NULL) {
temp->data.coeff = temp->data.coeff * c;
temp = temp->next;
}
}
}
void MultiplyD() {
if (!is_empty()) {
temp = head;
while (temp != NULL) {
temp->data.v.D.pow = temp->data.v.D.pow + 1;
temp = temp->next;
}
}
}
void negate() {
if (!is_empty()) {
temp = head;
while (temp != NULL) {
temp->data.coeff = temp->data.coeff * -1;
temp = temp->next;
}
}
}
};

bool operator == (const term &t1, const term &t2) {


if (t1.coeff != t2.coeff || t1.v.ch != t2.v.ch || t1.v.pow != t2.v.pow ||
t1.v.D.pow != t2.v.D.pow || t1.v.D.exist != t2.v.D.exist)
return false;
return true;
}

bool operator != (const term &t1, const term &t2) {


if (t1.coeff != t2.coeff || t1.v.ch != t2.v.ch || t1.v.pow != t2.v.pow ||
t1.v.D.pow != t2.v.D.pow || t1.v.D.exist != t2.v.D.exist)
return true;
return false;
}

ostream& operator<< (ostream& print, const term& t) {


if (t.coeff != 1)
print << t.coeff;
if (t.v.D.exist == true) {
print << "D";
if (t.v.D.pow != 1)
print << t.v.D.pow;
}
print << t.v.ch;
if (t.v.pow != 1)
print << t.v.pow;
return print;
}

int main()
{

cout << "...................Solution of System of D.E by


Elimination..................." << endl << endl;
char ind_var,check,dep_var[2];

//INPUT INDEPENDENT VARIABLE


do {
cout << endl << "Enter Independent variable : ";
ind_var = _getche();
_getche();
cout << endl << "Do you want to change independent variable (" << ind_var
<< ") ...(y/n)";
check = _getch();
_getch();
} while (check != 'n' && check != 'N');

//INPUT DEPENDENT VARIABLES


do {
cout << endl << endl << "Enter 2 dependent variables";
cout << endl << "Enter 1st dependent variable : ";
dep_var[0] = _getche();
_getche();
cout << endl;
cout << "Enter 2nd dependent variable : ";
dep_var[1] = _getche();
_getche();
cout << endl << "Do you want to change dependent variables (" << dep_var[0]
<< "," << dep_var[1] << ") ...(y/n)";
check = _getch();
_getch();
} while (check != 'n' && check != 'N');
string eq1, eq2;
LinkList <node<term>> exp1;
LinkList <node<term>> exp2;
node <term> nod1, nod2;
term temp;
int i;
bool is_positive = true;
//INPUT 1ST DIFFERENTIAL EQUATION USING LINKED LIST

cout << endl << "Example: Dx-3y=0 (NO SPACES! NO Brackets!)" << endl;
cout << "Now Enter First Linear Homogeneous Differential Equation : ";
getline(cin, eq1);
i = 0;
check = eq1[i];
while (check != 0) {
if (check == '=') {
nod1.data = temp;
exp1.insert(nod1);
break;
}
else if (isdigit(check)) {
temp.coeff = check - '0';
if (!is_positive) temp.coeff = 0 - temp.coeff;
check = eq1[++i];
continue;
}
else if (check == 'D') {
temp.v.D.exist = true;
check = eq1[++i];
if (isdigit(check)) {
temp.v.D.pow = check - '0';
check = eq1[++i];
continue;
}
continue;
}
else if (check == dep_var[0] || check == dep_var[1]) {
temp.v.ch = check;
check = eq1[++i];
if (isdigit(check)) {
temp.v.pow = check - '0';
check = eq1[++i];
continue;
}
continue;
}
else if (check == '+' || check == '-'){
if (i == 0) {
if (check == '+') is_positive = true; else is_positive =
false;
check = eq1[++i];
continue;
}
nod1.data = temp;
exp1.insert(nod1);
temp = term();
is_positive = true;
check = eq1[++i];
continue;
}
else {
exp1.del();
cout << ".................INVALID Equation..................." <<
endl;
system("pause");
exit(0);
}
}

//INPUT SECOND DIFFERENTIAL EQUATION USING LINKED LIST


cout << endl << "Example: Dx-3y=0 (NO SPACES! NO Brackets!)" << endl;
cout << "Now Enter Second Linear Homogeneous Differential Equation : ";
getline(cin, eq2);
i = 0;
check = eq2[i];
temp.v.D.exist = false;
while (check != 0) {
if (check == '=') {
nod2.data = temp;
exp2.insert(nod2);
break;
}
else if (isdigit(check)) {
temp.coeff = check - '0';
check = eq2[++i];
continue;
}
else if (check == 'D') {
temp.v.D.exist = true;
check = eq2[++i];
if (isdigit(check)) {
temp.v.D.pow = check - '0';
check = eq2[++i];
continue;
}
continue;
}
else if (check == dep_var[0] || check == dep_var[1]) {
temp.v.ch = check;
check = eq2[++i];
if (isdigit(check)) {
temp.v.pow = check - '0';
check = eq2[++i];
continue;
}
continue;
}
else if (check == '+' || check == '-') {
if (i == 0) {
if (check == '+') is_positive = true; else is_positive =
false;
check = eq2[++i];
continue;
}
nod2.data = temp;
exp2.insert(nod2);
temp = term();
is_positive = true;
check = eq2[++i];
continue;
}
else {
exp2.del();
cout << ".................INVALID Equation..................." <<
endl;
system("pause");
exit(0);
}
}

temp = exp1.getterm(dep_var[0]);
term temp2 = exp2.getterm(dep_var[0]);

while (temp.coeff != temp2.coeff) {


if (temp.coeff == 1 && temp2.coeff != 1) {
exp1.MultiplyCoeff(temp2.coeff);
}
else if (temp.coeff != 1 && temp2.coeff == 1) {
exp2.MultiplyCoeff(temp.coeff);
}
else {
exp1.MultiplyCoeff(temp2.coeff);
exp2.MultiplyCoeff(temp.coeff);
}
}

if (temp.coeff < 0 && temp2.coeff < 0) {


exp1.negate();
exp2.negate();
}

while (temp.v.D.pow != temp2.v.D.pow) {


if (temp.v.D.pow > temp2.v.D.pow)
exp2.MultiplyD();
else
exp1.MultiplyD();
}

You might also like