Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
//add 40
//sub -30
//multi 481950000
typedef struct node {
double coefficient, power;
struct node *Previous, *Next;
} *equationList;
typedef equationList node;
typedef equationList resultList;
node MakeEmpty(node list);
void insert(equationList list, node *P, double power, double coefficient);
void DeleteList(equationList list);
node delete(node list, node P);
equationList readFile(char line[50]);
void printToFile(FILE *outFile, resultList results[], int numOfResults);
resultList addition(equationList equation[], int numOfEquations);
resultList multiplication(equationList equation[], int numOfEquations);
resultList subtraction(equationList equation[], int numOfEquations);
void printEquations(equationList list[50], int count);
node find(double X, equationList equation2);
resultList findEquivalent(resultList result);
double valueOfEquation(resultList result, float x);
void printResult(resultList result);
int isFirst(equationList list, node P);
int isLast(equationList list, node P);
int listWithOneNode(node list);
void copy(equationList list, equationList list1, equationList list2, equationList list3){
equationList P = list->Next, tempNode1 = NULL, tempNode2 = NULL, tempNode3 = NULL;
tempNode1 = MakeEmpty(tempNode1);
tempNode2 = MakeEmpty(tempNode2);
tempNode3 = MakeEmpty(tempNode3);
while (P != NULL){
insert(list1, &tempNode1, P->power, P->coefficient);
insert(list2, &tempNode2, P->power, P->coefficient);
insert(list3, &tempNode3, P->power, P->coefficient);
P = P->Next;
}
}
int main() {
FILE *inFile, *outFile;
equationList list[9999], list1[9999] = {0}, list2[9999] = {0} , list3[9999] = {0};
resultList results[50];
char line[50] = {0};
double valueOfResults[50] = {0};
int option = 0, operation = 0, numOfLists = 0, numOfResults = 0, flag1 = 0, flag2 = 0,
flag3 = 0, flag4 = 0, temp = 0;
float x = 0;
inFile = fopen("C:\\Users\\alima\\CLionProjects\\P1_1190502_AliMari_4\\equations.txt", "r");
outFile = fopen("C:\\Users\\alima\\CLionProjects\\P1_1190502_AliMari_4\\results.txt", "w");
if (inFile == NULL) {
printf("The file \"equations.txt\" is not found.\n");
}
while (1) {
printf("----------------------\n");
printf("Menu:\n");
printf("1. Read the file\n");
printf("2. Show equations\n");
printf("3. Select operation\n");
printf("4. Save results\n");
printf("5. Exit\n");
printf("----------------------\n");
scanf("%d", &option);
switch (option) {
case 1:
if (flag1 == 1) {
printf("No need to read the file again.\n");
break;
}
for (int i = 0; !feof(inFile); i++) {
fgets(line, 50, inFile);
if (line[0] == '\n') {
i--;
continue;
}
list1[i] = MakeEmpty(list1[i]);
list2[i] = MakeEmpty(list2[i]);
list3[i] = MakeEmpty(list3[i]);
list[i] = readFile(line);
copy(list[i], list1[i], list2[i], list3[i]);
if (list[i] == NULL) {
i--;
}
numOfLists = i;
}
flag1 = 1;
printf("File read successfully.\n");
fclose(inFile);
break;
case 2:
if (flag1 != 1) {
printf("You must read the file first.\n");
break;
}
printf("\n");
printEquations(list, numOfLists + 1);
printf("\n");
flag3 = 1;
break;
case 3:
if (flag1 != 1) {
printf("You must read the file first.\n");
break;
}
if (flag3 != 1) {
printf("You didn't show the equations, show them firstly then select operations, write (1) to show them\n");
scanf("%d", &temp);
if (temp == 1) {
printEquations(list, numOfLists + 1);
printf("\n");
} else {
printf("Sorry! You can not select any operation, try it after showing the equations\n");
break;
}
}
printf("Select one of the three operations:\n");
printf("\t31. Add equations\n");
printf("\t32. Multiply equations\n");
printf("\t33. Subtract equations\n");
scanf("%d", &operation);
if (operation == 31) {
results[numOfResults] = addition(list1, numOfLists + 1);
} else if (operation == 32) {
results[numOfResults] = multiplication(list2, numOfLists + 1);
} else if (operation == 33) {
results[numOfResults] = subtraction(list3, numOfLists + 1);
} else {
printf("The selected operation is not available.\n");
break;
}
printf("The final equation: ");
printResult(results[numOfResults]);
printf("Do you want to add value to the variable x? (1) Yes, (0) No\n");
scanf("%d", &temp);
if (temp == 1) {
x = 0;
printf("Enter the value of x: ");
scanf("%f", &x);
printf("\n");
valueOfResults[numOfLists] = valueOfEquation(results[numOfResults], x);
printf("The value of the equation is: %.4f\n", valueOfResults[numOfLists]);
}
flag4 = 1;
numOfResults++;
break;
case 4:
if (flag1 != 1) {
printf("You must read the file first.\n");
break;
}
if (flag4 != 1) {
printf("There are no results to be saved.\n");
break;
}
printToFile(outFile, results, numOfResults);
fclose(outFile);
printf("Results are printed to the file successfully!\n");
flag2 = 1;
break;
case 5:
if (flag2 != 1) {
printf("Your results are not saved to the file, are you sure? (1) Yes, (0) No.\n");
scanf("%d", &temp);
if (temp == 1) {
fclose(outFile);
} else {
printf("You are leaving without saving the data, no data will be shown!\n");
}
}
exit(0);
default:
printf("The selected item is not supported in the menu.\n");
}
}
}
node MakeEmpty(node list) {
if (list != NULL)
DeleteList(list);
list = (struct node *) malloc(sizeof(struct node));
list->Previous = NULL;
list->Next = NULL;
if (list == NULL)
printf("Out of memory!\n");
return list;
}
void insert(equationList list, node *P, double power, double coefficient) {
node temp = NULL;
temp = MakeEmpty(temp);
node te = NULL;
if (list->Next == NULL) {
list->Next = temp;
temp->Previous = list;
temp->power = power;
temp->coefficient = coefficient;
*P = temp;
} else {
te = *P;
te->Next = temp;
temp->Previous = te;
temp->power = power;
temp->coefficient = coefficient;
temp->Next = NULL;
*P = temp;
}
}
void DeleteList(equationList list) {
equationList P, temp;
if (list != NULL) {
if (list->Next != NULL && list->Previous == NULL) {
P = list->Next;
P->Previous = NULL;
list->Next = NULL;
list->Previous = NULL;
while (P != NULL) {
temp = P->Next;
P->Previous = NULL;
free(P);
P = temp;
}
}
}
}
int isFirst(equationList list, node P) {
if (list != NULL)
return list == P;
return 0;
}
int isLast(equationList list, node P) {
node tempList = list, lastNode = NULL;
while (tempList != NULL) {
if (tempList->Next == NULL) {
lastNode = tempList;
}
tempList = tempList->Next;
}
return lastNode == P;
}
int listWithOneNode(node list) {
if (list != NULL)
return list->Previous == NULL && list->Next == NULL;
return 0;
}
node delete(node list, node P) {
node previous, next, tempList = NULL;
if (isFirst(list, P) && !listWithOneNode(list)) {
tempList = P->Next;
tempList->Previous = NULL;
free(P);
return tempList;
} else if (isLast(list, P) && list != NULL) {
if (list->Next == NULL) {
list = NULL;
return list;
}
previous = P->Previous;
previous->Next = NULL;
free(P);
return list;
} else if (listWithOneNode(list)) {
tempList = NULL;
return tempList;
} else {
if (list != NULL) {
previous = P->Previous;
next = P->Next;
previous->Next = next;
next->Previous = previous;
free(P);
}
return list;
}
}
void printToFile(FILE *outFile, resultList results[], int numOfResults) {
equationList P;
for (int i = 0; i < numOfResults; i++) {
P = (results[i])->Next;
fprintf(outFile, "Result of operation (%d): %.2fx^%.2f", (i + 1), P->coefficient, P->power);
P = P->Next;
while (P != NULL) {
if (P->coefficient > 0) {
if (P->power == 0) {
fprintf(outFile, "+%.2f", P->coefficient);
} else if (P->power == 1) {
fprintf(outFile, "+%.2fx", P->coefficient);
} else {
fprintf(outFile, "+%.2fx^%.2f", P->coefficient, P->power);
}
} else {
if (P->power == 0) {
fprintf(outFile, "%.2f", P->coefficient);
} else if (P->power == 1) {
fprintf(outFile, "%.2fx", P->coefficient);
} else {
fprintf(outFile, "%f.2x^%.2f", P->coefficient, P->power);
}
}
P = P->Next;
}
fprintf(outFile, "\n");
}
}
void printResult(resultList result) {
if (result != NULL) {
equationList P = result->Next;
printf("%.2fx^%.2f", P->coefficient, P->power);
P = P->Next;
while (P != NULL) {
if (P->coefficient > 1) {
if (P->power == 0) {
printf("+%.2f", P->coefficient);
} else if (P->power == 1) {
printf("+%.2fx", P->coefficient);
} else {
printf("+%.2fx^%.2f", P->coefficient, P->power);
}
} else if (P->coefficient == 1 || P->coefficient == -1) {
if (P->power == 0) {
if (P->coefficient == 1)
printf("+");
if (P->coefficient == -1)
printf("-");
} else if (P->power == 1) {
if (P->coefficient == 1)
printf("+x");
if (P->coefficient == -1)
printf("-x");
} else {
if (P->coefficient == 1)
printf("+x^%.2f", P->power);
if (P->coefficient == -1)
printf("-x^%.2f", P->power);
}
} else {
if (P->power == 0) {
printf("%.2f", P->coefficient);
} else if (P->power == 1) {
printf("%.2fx", P->coefficient);
} else {
printf("%.2fx^%.2f", P->coefficient, P->power);
}
}
P = P->Next;
}
printf("\n");
}
}
void printEquations(equationList list[50], int count) {
equationList P;
for (int i = 0; i < count; i++) {
P = (list[i])->Next;
printf("(%d) %.2fx^%.2f", (i + 1), P->coefficient, P->power);
P = P->Next;
while (P != NULL) {
if (P->coefficient > 0) {
if (P->power == 0) {
printf("+%.2f", P->coefficient);
} else if (P->power == 1) {
printf("+%.2fx", P->coefficient);
} else {
printf("+%.2fx^%.2f", P->coefficient, P->power);
}
} else {
if (P->power == 0) {
printf("%.2f", P->coefficient);
} else if (P->power == 1) {
printf("%.2fx", P->coefficient);
} else {
printf("%.2fx^%.2f", P->coefficient, P->power);
}
}
P = P->Next;
}
printf("\n");
}
}
node find(double X, equationList equation2) {
node P = equation2, temp = P;
while (P != NULL) {
if (P->power == X) {
temp = P;
break;
}
P = P->Next;
temp = NULL;
}
P = temp;
return P;
}
resultList findEquivalent(resultList result) {
if (result != NULL && result->Next != NULL) {
node variable1 = result->Next, P, temp;
while (variable1->Next != NULL) {
P = find(variable1->power, variable1->Next);
while (P != NULL && P->Next != NULL) {
variable1->coefficient += P->coefficient;
temp = P;
P->Previous->Next = P->Next;
temp->Next->Previous = temp->Previous;
free(temp);
P = find(variable1->power, variable1->Next);
}
variable1 = variable1->Next;
}
}
return result;
}
double valueOfEquation(resultList result, float x) {
double value = 0;
node P = result->Next;
while (P != NULL) {
value += P->coefficient * pow(x, P->power);
P = P->Next;
}
return value;
}
equationList readFile(char line[50]) {
equationList list = NULL, tempNode = NULL;
list = MakeEmpty(list);
tempNode = MakeEmpty(tempNode);
char *tokens;
double power = 0, coefficient = 0;
char temp[50] = {0}, tempLine[50] = {0};
//to delete all whitespaces in the equation
for (int i = 0, j = 0; i < strlen(line); i++) {
if (!isspace(line[i])) {
tempLine[j++] = line[i];
continue;
}
}
for (int i = 0, j = 0; i < strlen(tempLine); i++) {
if ((tempLine[i] == '-' || tempLine[i] == '+') && tempLine[i + 1] == 'x' && !isdigit(tempLine[i])) {
temp[j++] = tempLine[i];
temp[j++] = '1';
temp[j++] = tempLine[++i];
continue;
}
if (i == 0 && (tempLine[i] == 'x' || tempLine[i + 1] == 'x') && !isdigit(tempLine[i])) {
if (tempLine[i] == 'x') {
temp[j++] = '1';
temp[j++] = tempLine[i];
continue;
}
if (tempLine[i + 1] == 'x') {
temp[j++] = temp[i];
temp[j++] = '1';
temp[j++] = temp[++i];
continue;
}
}
temp[j++] = tempLine[i];
}
strcpy(tempLine, temp);
for (int i = 0; i < strlen(tempLine); i++) {
if (tempLine[i] == 'x') {
continue;
} else if (isdigit(tempLine[i])) {
continue;
} else if (tempLine[i] == '-' || tempLine[i] == '+') {
continue;
} else if (tempLine[i] == '^' && tempLine[i - 1] == 'x') {
continue;
} else {
printf("Sorry! This equation (%s) is excluded from all operations because it has invalid character.\n",
tempLine);
return NULL;
}
}
//to add whitespace after each variable
for (int i = 0, j = 0; i < strlen(tempLine);) {
if (tempLine[i] == '+' || tempLine[i] == '-') {
temp[j++] = tempLine[i++];
}
while (isdigit(tempLine[i])) {
temp[j++] = tempLine[i++];
}
if (tempLine[i] == 'x') {
if (i == 0 || tempLine[i - 1] == '+' || tempLine[i - 1] == '-') {
temp[j++] = '1';
temp[j++] = tempLine[i++];
} else {
temp[j++] = tempLine[i++];
}
} else {
temp[j++] = 'x';
temp[j++] = '^';
temp[j++] = '0';
temp[j++] = ' ';
continue;
}
if (tempLine[i] == '^') {
temp[j++] = tempLine[i++];
if (tempLine[i] == '-' || tempLine[i] == '+') {
temp[j++] = tempLine[i++];
}
} else {
temp[j++] = '^';
temp[j++] = '1';
temp[j++] = ' ';
continue;
}
while (isdigit(tempLine[i])) {
temp[j++] = tempLine[i++];
}
temp[j++] = ' ';
}
tokens = strtok(temp, "x^ ");
sscanf(tokens, "%lf", &coefficient);
tokens = strtok(NULL, "x^ ");
sscanf(tokens, "%lf", &power);
insert(list, &tempNode, power, coefficient);
//to split each variable and add its coefficient and power to the linked list
while (tokens != NULL) {
tokens = strtok(NULL, "x^ ");
if (tokens == NULL) {
break;
}
sscanf(tokens, " %lf ", &coefficient);
tokens = strtok(NULL, "x^ ");
if (tokens != NULL) {
sscanf(tokens, " %lf ", &power);
} else {
power = 0;
}
insert(list, &tempNode, power, coefficient);
}
return list;
}
resultList addition(equationList equation[], int numOfEquations) {
if (numOfEquations == 1) {
return equation[0];
} else {
node variable1, variable2, x1, x2, tempNode = NULL;
resultList results[9999] = {0};
int numOfResults = 0;
tempNode = MakeEmpty(tempNode);
for (int i = 0; i < numOfEquations; i += 2, numOfResults++) {
if (i == numOfEquations - 1) {
results[numOfResults] = equation[i];
continue;
}
results[numOfResults] = MakeEmpty(results[numOfResults]);
variable1 = equation[i]->Next;
variable2 = equation[i + 1]->Next;
variable1->Previous = NULL, variable2->Previous = NULL;
while (variable1 != NULL || variable2 != NULL) {
if (variable1 != NULL)
x1 = find(variable1->power, variable2);
else
x1 = NULL; //IMPORTANTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
if (variable2 != NULL)
x2 = find(variable2->power, variable1);
else
x2 = NULL;
if (x1 == NULL && x2 == NULL) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, variable1->power, variable1->coefficient);
}
if (variable2 != NULL) {
insert(results[numOfResults], &tempNode, variable2->power, variable2->coefficient);
}
variable1 = delete(variable1, variable1);
variable2 = delete(variable2, variable2);
continue;
}
if (x2 != NULL && x1 == NULL) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, variable1->power, variable1->coefficient);
}
if (variable2 != NULL) {
insert(results[numOfResults], &tempNode, variable2->power,
variable2->coefficient + x2->coefficient);
}
variable1 = delete(variable1, x2);
variable1 = delete(variable1, variable1);
variable2 = delete(variable2, variable2);
continue;
}
if (x2 == NULL && x1 != NULL) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, variable1->power,
variable1->coefficient + x1->coefficient);
}
if (variable2 != NULL) {
insert(results[numOfResults], &tempNode, variable2->power, variable2->coefficient);
}
variable2 = delete(variable2, x1);
variable2 = delete(variable2, variable2);
variable1 = delete(variable1, variable1);
continue;
}
if (x1->power == x2->power) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, x1->power, x1->coefficient + x2->coefficient);
}
variable1 = delete(variable1, x2);
variable2 = delete(variable2, x1);
continue;
}
if (x2 != NULL && x1 != NULL) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, variable1->power,
variable1->coefficient + x1->coefficient);
}
if (variable2 != NULL) {
insert(results[numOfResults], &tempNode, variable2->power,
variable2->coefficient + x2->coefficient);
}
variable1 = delete(variable1, x2);
variable2 = delete(variable2, x1);
variable2 = delete(variable2, variable2);
variable1 = delete(variable1, variable1);
continue;
}
}
}
return addition(results, numOfResults);
}
}
resultList multiplication(equationList equation[], int numOfEquations) {
if (numOfEquations == 1) {
return equation[0];
} else {
resultList results[50] = {0};
int numOfResults = 0;
node equation1 = NULL, equation2 = NULL, P1 = NULL, P2 = NULL, tempNode = NULL;
for (int i = 0; i < numOfEquations; i += 2, numOfResults++) {
if (i == numOfEquations - 1) {
results[numOfResults] = equation[i];
continue;
}
tempNode = MakeEmpty(tempNode);
equation1 = equation[i];
equation2 = equation[i + 1];
results[numOfResults] = MakeEmpty(results[numOfResults]);
P1 = equation1->Next;
P2 = equation2->Next;
while (P1 != NULL) {
while (P2 != NULL) {
insert(results[numOfResults], &tempNode, P1->power + P2->power, P1->coefficient * P2->coefficient);
P2 = P2->Next;
}
P2 = equation[i + 1]->Next;
P1 = P1->Next;
}
results[numOfResults] = findEquivalent(results[numOfResults]);
}
return multiplication(results, numOfResults);
}
}
resultList subtraction(equationList equation[], int numOfEquations) {
if (numOfEquations == 1) {
return equation[0];
} else {
node variable1, variable2, x1, x2, tempNode = NULL;
resultList results[50] = {0};
int numOfResults = 0, flag = 1;
tempNode = MakeEmpty(tempNode);
for (int i = 0; i < numOfEquations; i += 2, numOfResults++) {
if (i == numOfEquations - 1) {
results[numOfResults] = MakeEmpty(results[numOfResults]);
insert(results[numOfResults], &tempNode, (equation[i]->Next)->power, -(equation[i]->Next)->coefficient);
continue;
}
if (i != 0){
flag = -1;
}
results[numOfResults] = MakeEmpty(results[numOfResults]);
variable1 = equation[i]->Next;
variable2 = equation[i + 1]->Next;
variable1->Previous = NULL, variable2->Previous = NULL;
while (variable1 != NULL || variable2 != NULL) {
if (variable1 != NULL)
x1 = find(variable1->power, variable2);
if (variable2 != NULL)
x2 = find(variable2->power, variable1);
else
x2 = NULL;
if (x1 == NULL && x2 == NULL) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, variable1->power, flag * variable1->coefficient);
}
if (variable2 != NULL) {
insert(results[numOfResults], &tempNode, variable2->power, -variable2->coefficient);
}
variable1 = delete(variable1, variable1);
variable2 = delete(variable2, variable2);
continue;
}
if (x2 != NULL && x1 == NULL) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, variable1->power, flag * variable1->coefficient);
}
if (variable2 != NULL) {
insert(results[numOfResults], &tempNode, variable2->power,
(flag * x2->coefficient) - variable2->coefficient);
}
variable1 = delete(variable1, x2);
variable1 = delete(variable1, variable1);
variable2 = delete(variable2, variable2);
continue;
}
if (x2 == NULL && x1 != NULL) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, variable1->power,
(flag * variable1->coefficient) - x1->coefficient);
}
if (variable2 != NULL) {
insert(results[numOfResults], &tempNode, variable2->power, -variable2->coefficient);
}
variable2 = delete(variable2, x1);
variable2 = delete(variable2, variable2);
variable1 = delete(variable1, variable1);
continue;
}
if (x1->power == x2->power) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, x1->power, -x1->coefficient + (flag * x2->coefficient));
}
variable1 = delete(variable1, x2);
variable2 = delete(variable2, x1);
continue;
}
if (x2 != NULL && x1 != NULL) {
if (variable1 != NULL) {
insert(results[numOfResults], &tempNode, variable1->power,
(flag * variable1->coefficient) - x1->coefficient);
}
if (variable2 != NULL) {
insert(results[numOfResults], &tempNode, variable2->power,
-variable2->coefficient + (flag * x2->coefficient));
}
variable1 = delete(variable1, x2);
variable2 = delete(variable2, x1);
variable2 = delete(variable2, variable2);
variable1 = delete(variable1, variable1);
continue;
}
}
}
return addition(results, numOfResults);
}
}