-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResistorList.cpp
More file actions
169 lines (151 loc) · 3.87 KB
/
Copy pathResistorList.cpp
File metadata and controls
169 lines (151 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <string>
#include <iostream>
#include <iomanip>
#include "ResistorList.h"
#include "Resistor.h"
//constructor
ResistorList::ResistorList()
{}
//add resistor to network
void ResistorList::addResistor(string _name, double _resistance, int _node,
int _othernode)
{
//create resistor object
Resistor* newResistor = new Resistor(_name, _resistance, _node, _othernode);
Resistor* temp = rhead;
//if list is empty
if(rhead == NULL){
rhead = newResistor;
}
else{
//loop to find last node in the list
while(temp->getNext() != NULL){
temp = temp->getNext();
}
temp->setNext(newResistor);
}
}
//change the value of an existing resistor
bool ResistorList::changeResistor(string _name, double _resistance)
{
//change resistor object
Resistor* temp = rhead;
if(rhead == NULL){
//error
}
while((temp != NULL) && (temp->getName() != _name )){
temp = temp->getNext();
}
if(temp == NULL){
//return error
}
else{
temp->setVal(_name, _resistance);
return true;
}
return false;
}
//deletes all resistors
bool ResistorList::deleteAllResistors()
{
//delete all resistors
Resistor* curr = rhead;
while(curr != NULL){
rhead = curr->getNext();
delete curr;
curr = rhead;
}
return true;
}
//deletes one resistor
bool ResistorList::deleteOneResistor(string name)
{
//assume correct node was found till now
Resistor* curr = rhead;
Resistor* prev = NULL;
while((curr != NULL) && (curr->getName() != name)){
//find resistor if it exists
prev = curr;
curr = curr->getNext();
//return false if not found
}
if (curr == NULL) {
//resistor list is empty or resistor not found attached to this node
return false;
}
if(prev == NULL){
//match found at first element
//move head pointer to the right
rhead = curr->getNext();
}
else{
prev->setNext(curr->getNext());
}
delete curr;
curr = NULL;
return true;
}
//prints one resistor
bool ResistorList::printOneResistor(string name)
{
Resistor* curr = rhead;
//loops through all resistors in the linked list
for (curr; curr != NULL; curr = curr->getNext()) {
if(curr->getName() == name){
cout << "Print:" << endl;
cout << left;
cout << setw(20) << name << " ";
cout << right;
cout << setw(8) << fixed << setprecision(2) << curr->getResistance()
<< " ";
cout << "Ohms " << curr->getEndPointOne() << " -> "
<< curr->getEndPointTwo() << endl;
return true;
}
}
return false;
}
//get the pointer to resistor
Resistor* ResistorList::getrhead()
{
return rhead;
}
//print the connections
void ResistorList::printConnections()
{
Resistor* curr = rhead;
//prints all resistors at ResistorList
for (curr; curr != NULL; curr = curr->getNext()) {
cout << left;
cout << " " << setw(20) << curr->getName() << " ";
cout << right;
cout << setw(8) << fixed << setprecision(2) << curr->getResistance()
<< " ";
cout << "Ohms " << curr->getEndPointOne() << " -> "
<< curr->getEndPointTwo() << endl;
}
}
//get the number of resistors
int ResistorList::getResNum()
{
Resistor* curr = rhead;
int counter = 0;
for (curr; curr != NULL; curr = curr->getNext()) {
counter++;
}
return counter;
}
//check if resistor exists
bool ResistorList::checkResExists(string name)
{
Resistor* curr = rhead;
if(rhead == NULL)
return false;
else{
for(curr; curr != NULL; curr = curr->getNext()){
if(curr->getName() == name)
return true;
}
return false;
}
}