Complex Numbers
You,•web development
I/O Style -
- For the input the user is required to input a string either ADD,SUB,DOT,COS at first.
- Then the user is directed to the function created by this program and is required to give the complex number terms.
- The program will ask for the input again and again unless the user decides to input '-1' and then the user will exit out the program.
Functions Used -
printComplex -
- It takes the linkedlist head as the input which is typedefed as Complex in my code, and prints the linkedlist upto 2 decimal precision.
add -
- It takes two linkedlist as inputs and we make 2 seperate nodes as the head of the both linked list and add them simultaneously and import the data we get into a different linkedlist and we return that linkedlist later.
sub -
- It takes two linkedlist as inputs and we make 2 seperate nodes as the head of the both linked list and subtract them simultaneously and import the data we get into a different linkedlist and we return that linkedlist later.
dot -
- Takes 2 linkedlist as inputs and we have declared a float variable and it uspdates for each term.
magnitude -
- Takes one LL as input and add the square of each term and return the final square root using math.h.
cosineSimilarirty
- Takes 2 LL as inputs and finds their magnitude using the magnitude function and returns the output as expected.
inputfunctions
-
User is taken here from the initial input and will be asked to repetetively input as per their desired function.
Code -
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> typedef struct node { float term; struct node* next; } Node; typedef struct { Node* head; int n; } Complex; void printComplex(Complex c) { Node* current = c.head; while (current != NULL) { printf("%.2f ", current->term); current = current->next; } printf("\n"); } Complex add(Complex c1, Complex c2) { Complex ans; ans.n = c1.n; Node* current1 = c1.head; Node* current2 = c2.head; Node* currentAns = NULL; Node* prevAns = NULL; while (current1 != NULL && current2 != NULL) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = current1->term + current2->term; newNode->next = NULL; if (currentAns == NULL) { ans.head = newNode; currentAns = newNode; } else { currentAns->next = newNode; currentAns = newNode; } if (prevAns != NULL) { prevAns->next = currentAns; } prevAns = currentAns; current1 = current1->next; current2 = current2->next; } return ans; } Complex sub(Complex c1, Complex c2) { Complex ans; ans.n = c1.n; Node* current1 = c1.head; Node* current2 = c2.head; Node* currentAns = NULL; Node* prevAns = NULL; while (current1 != NULL && current2 != NULL) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = current1->term - current2->term; newNode->next = NULL; if (currentAns == NULL) { ans.head = newNode; currentAns = newNode; } else { currentAns->next = newNode; currentAns = newNode; } if (prevAns != NULL) { prevAns->next = currentAns; } prevAns = currentAns; current1 = current1->next; current2 = current2->next; } return ans; } float dot(Complex c1, Complex c2) { float ans = 0; Node* current1 = c1.head; Node* current2 = c2.head; while (current1 != NULL && current2 != NULL) { ans += current1->term * current2->term; current1 = current1->next; current2 = current2->next; } return ans; } float magnitude(Complex c) { float ans = 0; Node* current = c.head; while (current != NULL) { ans += current->term * current->term; current = current->next; } return sqrt(ans); } float cosineSimilarity(Complex c1, Complex c2) { float dotProduct = dot(c1, c2); float magc1 = magnitude(c1); float magc2 = magnitude(c2); return dotProduct / (magc1 * magc2); } void inputadd(){ Complex c1, c2, ans; int n; while(1){ scanf("%d", &n); if(n!=-1){ c1.n = n; c1.head = NULL; for (int i = 0; i < n; i++) { float term; scanf("%f", &term); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = term; newNode->next = NULL; if (c1.head == NULL) { c1.head = newNode; } else { Node* current = c1.head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } c2.n = n; c2.head = NULL; for (int i = 0; i < n; i++) { float term; scanf("%f", &term); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = term; newNode->next = NULL; if (c2.head == NULL) { c2.head = newNode; } else { Node* current = c2.head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } ans = add(c1, c2); printComplex(ans); } else{ return ; } } } void inputsub(){ Complex c1, c2, ans; int n; while(1){ scanf("%d", &n); if(n!=-1){ c1.n = n; c1.head = NULL; for (int i = 0; i < n; i++) { float term; scanf("%f", &term); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = term; newNode->next = NULL; if (c1.head == NULL) { c1.head = newNode; } else { Node* current = c1.head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } c2.n = n; c2.head = NULL; for (int i = 0; i < n; i++) { float term; scanf("%f", &term); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = term; newNode->next = NULL; if (c2.head == NULL) { c2.head = newNode; } else { Node* current = c2.head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } ans = sub(c1, c2); printComplex(ans); } else{ return ; } } } void inputdot(){ Complex c1, c2; int n; while(1){ scanf("%d", &n); if(n!=-1){ c1.n = n; c1.head = NULL; for (int i = 0; i < n; i++) { float term; scanf("%f", &term); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = term; newNode->next = NULL; if (c1.head == NULL) { c1.head = newNode; } else { Node* current = c1.head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } c2.n = n; c2.head = NULL; for (int i = 0; i < n; i++) { float term; scanf("%f", &term); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = term; newNode->next = NULL; if (c2.head == NULL) { c2.head = newNode; } else { Node* current = c2.head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } float ans = dot(c1, c2); printf("%.2f\n", ans); } else{ return ; } } } void inputcos(){ Complex c1, c2; int n; while(1){ scanf("%d", &n); if(n!=-1){ c1.n = n; c1.head = NULL; for (int i = 0; i < n; i++) { float term; scanf("%f", &term); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = term; newNode->next = NULL; if (c1.head == NULL) { c1.head = newNode; } else { Node* current = c1.head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } c2.n = n; c2.head = NULL; for (int i = 0; i < n; i++) { float term; scanf("%f", &term); Node* newNode = (Node*)malloc(sizeof(Node)); newNode->term = term; newNode->next = NULL; if (c2.head == NULL) { c2.head = newNode; } else { Node* current = c2.head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } float ans = cosineSimilarity(c1, c2); printf("%.2f\n", ans); } else{ return ; } } } int main() { char choice[10]; scanf("%s", choice); char add[10]="ADD"; char sub[10]="SUB"; char dot[10]="DOT"; char cos[10]="COS"; if(strcmp(choice,add)==0){ inputadd(); } else if(strcmp(choice,sub)==0){ inputsub(); } else if(strcmp(choice,dot)==0){ inputdot(); } else if(strcmp(choice,cos)==0){ inputcos(); } return 0; }