- Read in a file of complex numbers (126import.txt) and store them in an array of complex objects. (Please include complex.h, complex.cpp and main.cpp)
- Create a menu to allow console add / delete / list and save functions.
Add: Allows user to append a new complex number into an array of complex numbers
Delete: Removes one complex number from an array of complex numbers either by selecting an index or by searching for a specific complex number
List: Prints out an array of complex numbers in ascending order according to magnitude onto the console
Save: Saves the array of complex numbers into a file
The magnitude of a complex number a+bi is sqrt(a*a+b*b).
Save function stores the results to file 126complex.txt
--------------------------------------------------------------------------------------
File: 126import.txt
1+1i
3.3+3.4i
4.4-4.5i
-5.5-5.6i
-6
7i
-8i
2+i
#include
#include #include using namespace std; /* * Node Declaration */ struct node { int info; struct node *next; struct node *prev; }*start, *last; int counter = 0; /* * Class Declaration */ class double_clist { public: node *create_node(int); void insert_begin(); void insert_last(); void insert_pos(); void delete_pos(); void search(); void update(); void display(); void reverse(); void sort(); double_clist() { start = NULL; last = NULL; } }; /* * Main: Contains Menu */ int main() { int choice; double_clist cdl; while (1) { cout<<\"\ -------------------------------\"<>choice; switch(choice) { case 1: cdl.insert_begin(); break; case 2: cdl.insert_last(); break; case 3: cdl.insert_pos(); break; case 4: cdl.delete_pos(); break; case 5: cdl.update(); break; case 6: cdl.search(); break; case 7: cdl.sort(); break; case 8: cdl.display(); break; case 9: cdl.reverse(); break; case 10: exit(1); default: cout<<\"Wrong choice\"<info = value; temp->next = NULL; temp->prev = NULL; return temp; } /* *INSERTS ELEMENT AT BEGINNING */ void double_clist::insert_begin() { int value; cout<>value; struct node *temp; temp = create_node(value); if (start == last && start == NULL) { cout<<\"Element inserted in empty list\"<next = last->next = NULL; start->prev = last->prev = NULL; } else { temp->next = start; start->prev = temp; start = temp; start->prev = last; last->next = start; cout<<\"Element inserted\"<>value; struct node *temp; temp = create_node(value); if (start == last && start == NULL) { cout<<\"Element inserted in empty list\"<next = last->next = NULL; start->prev = last->prev = NULL; } else { last->next = temp; temp->prev = last; last = temp; start->prev = last; last->next = start; } } /* *INSERTS ELEMENT AT POSITION */ void double_clist::insert_pos() { int value, pos, i; cout<>value; cout<>pos; struct node *temp, *s, *ptr; temp = create_node(value); if (start == last && start == NULL) { if (pos == 1) { start = last = temp; start->next = last->next = NULL; start->prev = last->prev = NULL; } else { cout<<\"Position out of range\"<next; if (i == pos - 1) { ptr->next = temp; temp->prev = ptr; temp->next = s; s->prev = temp; cout<<\"Element inserted\"<>pos; if (counter < pos) { cout<<\"Position out of range\"<next = s->next; s->next->prev = last; start = s->next; free(s); cout<<\"Element Deleted\"<next; ptr = s->prev; } ptr->next = s->next; s->next->prev = ptr; if (pos == counter) { last = ptr; } counter--; free(s); cout<<\"Element Deleted\"<>pos; cout<<\"Enter the new value: \"; cin>>value; struct node *s; if (counter < pos) { cout<<\"Position out of range\"<info = value; cout<<\"Node Updated\"<next; } s->info = value; cout<<\"Node Updated\"<>value; s = start; for (i = 0;i < counter;i++) { pos++; if (s->info == value) { cout<<\"Element \"<next; } if (!flag) cout<<\"Element not found in the list\"<next; while (temp != start) { if (s->info > temp->info) { value = s->info; s->info = temp->info; temp->info = value; } temp = temp->next; } s = s->next; } } /* * Display Elements of the List */ void double_clist::display() { int i; struct node *s; if (start == last && start == NULL) { cout<<\"The List is empty, nothing to display\"<info<<\"<->\"; s = s->next; } cout<info<next; p1->next = NULL; p1->prev = p2; while (p2 != start) { p2->prev = p2->next; p2->next = p1; p1 = p2; p2 = p2->prev; } last = start; start = p1; cout<<\"List Reversed\"<